-
Notifications
You must be signed in to change notification settings - Fork 1
/
Video.lua
57 lines (48 loc) · 1.2 KB
/
Video.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
local ok, video = pcall(require, "video") -- c
-- local ok, video = pcall(require, "video.video") -- ffi
if not ok then
video = {}
function video.open(p, s) end
end
local class = require("class")
---@class video.Video
---@operator call: video.Video
local Video = class()
---@param fileData love.FileData
---@return nil?
---@return string?
function Video:new(fileData)
local v = video.open(fileData:getPointer(), fileData:getSize())
if not v then
return nil, "can't open video"
end
self.video = v
self.fileData = fileData
self.imageData = love.image.newImageData(v:getDimensions())
self.image = love.graphics.newImage(self.imageData)
end
function Video:release()
self.video:close()
self.imageData:release()
self.image:release()
end
function Video:rewind()
local v = self.video
v:seek(0)
v:read(self.imageData:getPointer())
end
---@param time number
function Video:play(time)
local v = self.video
local read_count = 10
while time >= v:tell() and time < v:getDuration() do
v:read(self.imageData:getPointer())
read_count = read_count - 1
if read_count == 0 then
break
end
end
---@diagnostic disable-next-line: missing-parameter
self.image:replacePixels(self.imageData)
end
return Video