-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVideo.lua
More file actions
64 lines (52 loc) · 1.22 KB
/
Video.lua
File metadata and controls
64 lines (52 loc) · 1.22 KB
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
58
59
60
61
62
63
64
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()
self:seek(0)
end
---@param time number
function Video:seek(time)
self.video:seek(time)
self:play(time)
end
---@param time number
function Video:play(time)
local v = self.video
local read = false
while time > v:tell() and time < v:getDuration() do
if not v:read(self.imageData:getPointer()) then
break
end
read = true
end
if read then
---@diagnostic disable-next-line: missing-parameter
self.image:replacePixels(self.imageData)
end
end
return Video