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:
4DBug committed 2026-10-31 14:00:36 -05:00
1 parent 10bd6b021f
commit d73b93c740
8 files changed
+207 -6

No files matched your search

+80
View File
@@ -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