ws-apifix: videos save as animated images (F.save(video, gif | apng | webp), F.convert(avi, "gif"): frames / fps / maxFrames (300) / maxPixels (2^25)); VideoFrame.index restarts at the seek position; F.detect names WebM files "webm"
api: encoderFor finds image formats for a VideoDecoder, videoToImage() collects the frames; F.detect asks a format's optional variant(r) for a more precise name (video/mkv: Mkv.variant, from Parse.docType). codecs/video/Video: index after seek = round(pts * frameRate) + 1.
This commit is contained in:
1 parent
10bd6b021f
commit
d73b93c740
8 files changed
+207
-6
No files matched your search
@@ -466,6 +466,11 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
|
||||
if img then
|
||||
local t = (pts or 0) / scale - start
|
||||
if t + 1e-9 >= skipUntil then
|
||||
if index < 0 then
|
||||
-- the first frame after a seek: its position in the stream (constant-rate: round(pts * fps);
|
||||
-- without a frame rate, counted from the seek)
|
||||
index = if v.frameRate > 0 then math.max(0, math.floor(t * v.frameRate + 0.5)) else 0
|
||||
end
|
||||
index += 1
|
||||
local d = defaultDur
|
||||
if lastPts and t > (lastPts :: number) and defaultDur == 0 then
|
||||
@@ -502,6 +507,7 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
|
||||
flushed = false
|
||||
skipUntil = seconds
|
||||
lastPts = nil
|
||||
index = -1 -- recomputed from the first frame's time
|
||||
if self.audio then
|
||||
pcall(self.audio.seek, self.audio, seconds)
|
||||
end
|
||||
|
||||
@@ -412,6 +412,68 @@ end
|
||||
|
||||
local encodeTextures: (scene: any) -> Err.Err?
|
||||
|
||||
-- videoToImage(v, opts) -> an animated rgba8 Image of the video's frames (for the GIF / APNG / WebP writers) | nil,
|
||||
-- Err. opts.frames: how many frames to keep (default all, up to the cap); opts.fps: the output frame rate (frames are
|
||||
-- picked at 1 / fps steps, each held as long as it covers; default the source's own timing); opts.maxFrames: the cap
|
||||
-- (default 300); opts.maxPixels: a cap on frames * width * height (default 2^25). Reads the decoder from where it is.
|
||||
local function videoToImage(v: any, o: Options): (any, Err.Err?)
|
||||
if type(v.nextFrame) ~= "function" or type(v.toRGBA) ~= "function" then
|
||||
return nil, Err.new(Err.ARG, "save: not a VideoDecoder")
|
||||
end
|
||||
local cap = o.maxFrames or 300
|
||||
local want = o.frames
|
||||
local fps = o.fps
|
||||
if type(cap) ~= "number" or cap < 1 or (want ~= nil and (type(want) ~= "number" or want < 1)) or (fps ~= nil and (type(fps) ~= "number" or fps <= 0 or fps > 1000)) then
|
||||
return nil, Err.new(Err.ARG, "save: frames / maxFrames must be >= 1 and fps 0..1000")
|
||||
end
|
||||
local limit = math.floor(if want then math.min(want, cap) else cap)
|
||||
local w, h = math.max(1, tonumber(v.width) or 1), math.max(1, tonumber(v.height) or 1)
|
||||
limit = math.max(1, math.min(limit, math.floor((o.maxPixels or 2 ^ 25) / (w * h))))
|
||||
local defaultDur = if type(v.frameRate) == "number" and v.frameRate > 0 then 1 / v.frameRate else 0.1
|
||||
local images, delays = {}, {}
|
||||
local t0: number? = nil
|
||||
local taken = 0 -- output slots used (fps mode)
|
||||
while #images < limit do
|
||||
local ok, f, ferr = pcall(v.nextFrame, v)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, "video decoder threw: " .. tostring(f))
|
||||
end
|
||||
if f == nil then
|
||||
if ferr then
|
||||
return nil, ferr
|
||||
end
|
||||
break
|
||||
end
|
||||
local dur = if type(f.duration) == "number" and f.duration > 0 then f.duration else defaultDur
|
||||
local delay = dur
|
||||
if fps then
|
||||
local start: number = t0 or f.pts
|
||||
t0 = start
|
||||
local slots = math.ceil((f.pts - start + dur) * fps - 1e-6) - taken
|
||||
delay = slots / fps
|
||||
taken += math.max(0, slots)
|
||||
end
|
||||
if delay > 0 then
|
||||
local rok, img = pcall(v.toRGBA, v, f)
|
||||
if not rok or type(img) ~= "table" then
|
||||
return nil, Err.new(Err.CORRUPT, "frame " .. tostring(f.index) .. " did not convert to rgba8")
|
||||
end
|
||||
table.insert(images, img)
|
||||
table.insert(delays, delay)
|
||||
end
|
||||
end
|
||||
if #images == 0 then
|
||||
return nil, Err.new(Err.ARG, "save: the video has no frames")
|
||||
end
|
||||
local img, ierr = ImageOps.fromFrames(images, delays)
|
||||
if not img then
|
||||
return nil, ierr
|
||||
end
|
||||
img.loops = if type(v.loops) == "number" then v.loops else 0
|
||||
img.meta.source = { format = v.format }
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- renderMusic(value, opts) -> the audio Decoder of a Module (codecs/audio/Tracker) or a Midi (music/midi render) |
|
||||
-- nil, Err. opts are the render options: sampleRate, soundfont, gain, tail, interpolation, stereoSeparation, ...
|
||||
local function renderMusic(value: any, o: Options): (any, Err.Err?)
|
||||
@@ -452,6 +514,9 @@ local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entr
|
||||
if not entry and category == "image" and type(value) == "table" and value.kind == "image" then
|
||||
entry = find(key, "video") -- an (animated) image written as a video: F.convert(gif, "avi")
|
||||
end
|
||||
if not entry and category == "video" and type(value) == "table" and value.kind == "video" then
|
||||
entry = find(key, "image") -- a video written as an animated image (F.save): F.convert(avi, "gif")
|
||||
end
|
||||
if not entry and category == "music" and type(value) == "table" and (value.kind == "music" or value.kind == "midi") then
|
||||
entry = find(key, "audio") -- a Module / Midi saved as audio is rendered first (F.save): F.convert(mod, "wav")
|
||||
end
|
||||
@@ -516,6 +581,13 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
|
||||
end
|
||||
value = v
|
||||
end
|
||||
if entry.category == "image" and type(value) == "table" and value.kind == "video" then
|
||||
local img, ierr = videoToImage(value, o)
|
||||
if not img then
|
||||
return nil, ierr
|
||||
end
|
||||
value = img
|
||||
end
|
||||
if entry.category == "audio" and type(value) == "table" and (value.kind == "music" or value.kind == "midi") then
|
||||
local dec, rerr = renderMusic(value, o) -- the render options (sampleRate, soundfont, loop, ...) come from opts
|
||||
if not dec then
|
||||
@@ -588,6 +660,14 @@ function F.detect(src: any, opts: Options?): (string?, Err.Err?)
|
||||
if not entry then
|
||||
return nil, err
|
||||
end
|
||||
-- a format may name the file's flavour more precisely (video/mkv: "webm" for DocType webm)
|
||||
local m = module(entry)
|
||||
if m and type(m.variant) == "function" then
|
||||
local ok, v = pcall(m.variant, Bytes.reader(buf))
|
||||
if ok and type(v) == "string" then
|
||||
return v, nil
|
||||
end
|
||||
end
|
||||
return entry.name, nil
|
||||
end
|
||||
|
||||
|
||||
@@ -553,7 +553,9 @@ end
|
||||
export type Options = { maxPackets: number? }
|
||||
|
||||
-- parse(buf, base, limit, opts) -> a Demux | nil, Err
|
||||
function Parse.parse(buf: buffer, base: number, limit: number, opts: Options?): (any, Err.Err?)
|
||||
-- docType(buf, base, limit) -> the EBML header's DocType ("matroska" when it has none), the header's size and data
|
||||
-- offset | nil, Err
|
||||
function Parse.docType(buf: buffer, base: number, limit: number): (string?, any, number?)
|
||||
local id, size, data = Ebml.header(buf, base, limit)
|
||||
if id ~= ID.EBML or not size or not data then
|
||||
return nil, Err.new(Err.CORRUPT, "not an EBML file", base)
|
||||
@@ -567,6 +569,14 @@ function Parse.parse(buf: buffer, base: number, limit: number, opts: Options?):
|
||||
docType = Ebml.string(buf, o, n)
|
||||
end
|
||||
end
|
||||
return docType, size, data
|
||||
end
|
||||
|
||||
function Parse.parse(buf: buffer, base: number, limit: number, opts: Options?): (any, Err.Err?)
|
||||
local docType, size, data = Parse.docType(buf, base, limit)
|
||||
if not docType then
|
||||
return nil, size
|
||||
end
|
||||
if docType ~= "matroska" and docType ~= "webm" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "EBML document type " .. docType)
|
||||
end
|
||||
|
||||
@@ -67,6 +67,15 @@ function Mkv.sniff(r: Bytes.Reader): number
|
||||
return 0.5
|
||||
end
|
||||
|
||||
-- variant(r) -> "webm" for a WebM file (DocType webm), else nil: the name F.detect reports (never throws)
|
||||
function Mkv.variant(r: Bytes.Reader): string?
|
||||
local ok, docType = pcall(Parse.docType, r.buf, r.pos, r.limit)
|
||||
if ok and docType == "webm" then
|
||||
return "webm"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- encode(video, opts?) -> buffer | nil, Err: a VideoDecoder (F.load / F.video.open of any video or animated image)
|
||||
-- or a Demuxer
|
||||
|
||||
Reference in new issue
Block a user