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

+6
View File
@@ -180,6 +180,12 @@ function C.seek(t: any, v: any, c: any, label: string, at: number)
v:seek(at)
local f = v:nextFrame()
t:check(f ~= nil and f.pts >= at - 1e-6 and f.pts < at + 0.2, label .. ": seek to " .. at .. " lands at " .. tostring(f and f.pts))
if f and v.frameRate > 0 then
-- frame.index restarts at the seek position (not counted on from before the seek)
t:eq(f.index, math.floor(f.pts * v.frameRate + 0.5) + 1, label .. ": frame index after seeking")
local g = v:nextFrame()
t:check(g == nil or g.index == f.index + 1, label .. ": index counts on after the seek")
end
if f and C.EXACT[v.codec] and c.frames then
local idx = math.floor(f.pts * v.frameRate + 0.5) + 1
t:eq(Crc32.of(f.image.pixels), c.frames[idx], label .. ": the frame after seeking equals ffmpeg's frame " .. idx)
+57
View File
@@ -113,4 +113,61 @@ do
t:check(out ~= nil and buffer.tostring(out) == gifAvi.data, "gif.avi: F.convert(gif, 'avi') again byte for byte")
end
end
-- frame.index after seek on a demuxed video (MJPEG AVI at 10 fps): the frame's own position, then counting on
local function gradient(n: number): { any }
local imgs = {}
for i = 1, n do
local im = assert(F.ops.image.new(16, 8))
F.ops.image.fill(im, i * 12, 0, 255 - i * 12)
imgs[i] = im
end
return imgs
end
local avi = assert(F.save(assert(F.video.fromFrames(gradient(20), 0.1)), nil, { format = "avi", quality = 90 }))
do
local v = assert(F.video.open(avi))
for i = 1, 3 do
t:eq((v:nextFrame() :: any).index, i, "index before seeking " .. i)
end
v:seek(1.0)
local f = v:nextFrame() :: any
t:check(f.index == 11 and math.abs(f.pts - 1.0) < 1e-6, "seek(1.0): frame 11")
t:eq((v:nextFrame() :: any).index, 12, "then 12")
v:seek(0.25)
t:eq((v:nextFrame() :: any).index, 4, "seek back to 0.25: frame 4 (0.3 s)")
v:reset()
t:eq((v:nextFrame() :: any).index, 1, "reset: frame 1")
end
-- a video saved as an animated image (GIF / APNG / WebP): every frame with its timing, or frames / fps / maxFrames
do
local gif = assert(F.convert(avi, "gif"))
t:eq(F.detect(gif), "gif", "convert(avi, gif)")
local g = assert(F.image.load(gif))
t:check(#g.frames == 20 and math.abs(g.frames[1].delay - 0.1) < 1e-9 and g.width == 16 and g.height == 8, "gif: 20 frames of 0.1 s")
local g2 = assert(F.image.load(assert(F.convert(avi, "gif", { fps = 5 }))))
t:check(#g2.frames == 10 and math.abs(g2.frames[1].delay - 0.2) < 1e-9, "fps = 5: 10 frames of 0.2 s")
local g3 = assert(F.image.load(assert(F.convert(avi, "gif", { fps = 20 }))))
local total = 0
for _, fr in g3.frames do
total += fr.delay
end
t:check(#g3.frames == 20 and math.abs(total - 2) < 0.02, "fps above the source: frames held, total 2 s")
t:eq(#(assert(F.image.load(assert(F.convert(avi, "gif", { frames = 4 })))) :: any).frames, 4, "frames = 4")
t:eq(#(assert(F.image.load(assert(F.convert(avi, "gif", { maxFrames = 7 })))) :: any).frames, 7, "maxFrames = 7")
t:eq(#(assert(F.image.load(assert(F.convert(avi, "gif", { maxPixels = 16 * 8 * 5 })))) :: any).frames, 5, "maxPixels caps the frames")
local apng = assert(F.save(assert(F.video.open(avi)), nil, { format = "apng" }))
local a = assert(F.image.load(apng))
t:check(F.detect(apng) == "png" and #a.frames == 20, "save(video, apng)")
local webp = assert(F.save(assert(F.video.open(avi)), nil, { format = "webp" }))
t:check(F.detect(webp) == "webp" and #(assert(F.image.load(webp)) :: any).frames == 20, "save(video, webp)")
-- the frames are the decoded pictures: the GIF's first frame is the MJPEG frame (lossy, palette)
local v = assert(F.video.open(avi))
local first = v:toRGBA(v:nextFrame())
local r1 = buffer.readu8(first.pixels, 0)
t:check(math.abs(buffer.readu8(g.pixels, 0) - r1) <= 8, "gif frame 1 matches the decoded frame")
local _, e = F.save(assert(F.video.open(avi)), nil, { format = "gif", fps = -1 })
t:check(e and e.code == F.Err.ARG, "bad fps")
end
t:done()
+10
View File
@@ -88,6 +88,16 @@ do
t:eq(len, 160, "lacing: EBML-laced frame size")
end
-- F.detect names WebM files (DocType webm) "webm", Matroska files "mkv"; both open through the mkv format
do
t:eq(F.detect(byName["vp8_opus.webm"].data), "webm", "detect: webm")
t:eq(F.detect(byName["mjpeg_pcm.mkv"].data), "mkv", "detect: mkv")
t:eq(Mkv.variant(Bytes.reader(buffer.fromstring(byName["vp8_opus.webm"].data))), "webm", "variant: webm")
t:eq(Mkv.variant(Bytes.reader(buffer.fromstring(byName["mjpeg_pcm.mkv"].data))), nil, "variant: matroska")
t:eq(Mkv.variant(Bytes.reader(buffer.fromstring("\26\69\223\163"))), nil, "variant: truncated")
t:eq(F.format("webm").name, "mkv", "format(webm) is mkv")
end
-- 4. our muxer: remux WebM (VP8 + Opus copied) and read it back packet for packet
do
local src = byName["vp8_opus.webm"]