-- video writer tests: files our muxers wrote (tools/fixtures/_video_encode.luau: WebM remux, MJPEG Matroska / AVI -- copied and re-encoded from VP8 / MPEG-1, an animated GIF with uneven delays to AVI / Matroska / Y4M, WebM to Y4M) -- were probed by ffprobe and decoded by ffmpeg at fixture time; here our demuxers must see the same packets, our -- decoders the same frames (CRC for VP8 / raw, PSNR for MJPEG) and audio; the writers are rerun and must give the same -- bytes. Fixtures: tools/fixtures/videoenc.py. local T = require("@tests/T") local C = require("@tests/video/_common") local F = require("@formats") local Fx = require("@tests/gen/videoenc_cases") local Src = require("@tests/gen/videoenc_src") local t = T.suite("video encode") local byName = {} for _, c in Fx do byName[c.name] = c end for _, c in Fx do local label = c.name if c.err then t:note("%s: not written at fixture time: %s", label, c.err) else local v, err = C.open(c.data) t:check(v ~= nil, label .. ": opens: " .. tostring(err)) if v then t:eq(#v.demuxer.tracks, #c.streams, label .. ": track count (ffprobe)") -- (ffmpeg's AVI demuxer cuts PCM chunks into 1024-sample packets: AVI audio is compared decoded) local avi = string.sub(label, -4) == ".avi" C.packets(t, v.demuxer, c, label, { skipStream = function(_: any, tr: any): boolean return avi and tr ~= nil and tr.kind == "audio" end }) C.frames(t, v, c, label, 40) local at = v.demuxer:track("audio") if at then C.audio(t, v, c, label, string.sub(at.codec, 1, 4) == "pcm_", 30) end end end end -- the GIF (100 / 50 / 200 / 100 ms) at a constant 20 fps: 2 + 1 + 4 + 2 frames do local c = byName["gif.avi"] if c and c.frames then t:eq(#c.frames, 9, "gif.avi: 9 frames at 20 fps (ffmpeg)") local v = C.open(c.data) t:eq(v.frameRate, 20, "gif.avi: 20 fps") end local y = byName["gif.y4m"] if y and y.frames then t:eq(#y.frames, 9, "gif.y4m: 9 frames (ffmpeg)") end -- F.video.open on the GIF itself: a frames-backed video local g = F.video.open(buffer.fromstring(Src.gif)) t:check(g ~= nil and g.codec == "frames" and g.frames == 4, "F.video.open(gif): 4 frames") if g then local f1 = g:nextFrame() local f2 = g:nextFrame() t:check(f1 and f1.image.format == "rgba8" and f1.duration == 0.1 and f2 and f2.pts == 0.1 and f2.duration == 0.05, "gif frames: rgba8 with the GIF's times") g:seek(0.2) local f3 = g:nextFrame() t:check(f3 ~= nil and f3.index == 3, "gif seek: frame 3 at 0.15 s holds 0.2 s") end end -- animated WebP through F.video.open: frames-backed, the canvases equal Pillow's (libwebp's WebPAnimDecoder) do local Crc32 = require("@formats/core/Crc32") local Anims = require("@tests/gen/webp_anim") for _, a in Anims do local v = F.video.open(buffer.fromstring(a.data)) t:check(v ~= nil and v.codec == "frames" and v.frames == #a.frames, a.name .. ": opens as a " .. #a.frames .. "-frame video") if v then local bad, n, t0 = 0, 0, 0 while true do local f = v:nextFrame() if not f then break end n += 1 local px = f.image.pixels if a.rgba == false then px = buffer.create(buffer.len(f.image.pixels)) buffer.copy(px, 0, f.image.pixels) for i = 3, buffer.len(px) - 1, 4 do buffer.writeu8(px, i, 255) end end if Crc32.of(px) ~= a.frames[n].crc or math.abs(f.pts - t0) > 1e-6 then bad += 1 end t0 += a.frames[n].ms / 1000 end t:check(n == #a.frames and bad == 0, a.name .. ": video frames equal Pillow's canvases and times (" .. bad .. " differ)") end end end -- the writers are deterministic: the same bytes again do local Mkv = require("@tests/gen/mkv_cases") local remux = byName["remux.webm"] for _, m in Mkv do if m.name == "vp8_opus.webm" and remux and remux.data then local out = F.convert(buffer.fromstring(m.data), "webm") t:check(out ~= nil and buffer.tostring(out) == remux.data, "remux.webm: written again byte for byte") end end local gifAvi = byName["gif.avi"] if gifAvi and gifAvi.data then local out = F.convert(buffer.fromstring(Src.gif), "avi", { quality = 95 }) 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()