ws-video: writers checked by ffprobe / ffmpeg (tools/fixtures/videoenc.py + _video_encode.luau, tests/video/encode.luau); constant-rate output for AVI / Y4M shared in Source.constantRate (uneven GIF delays repeat frames)

This commit is contained in:
4DBug committed 2026-10-31 09:59:16 -05:00
1 parent bd06545e2e
commit 8d2deb13ab
6 files changed
+248 -59

No files matched your search

+45
View File
@@ -232,6 +232,51 @@ function Source.streams(value: any, opts: any?, want: Want): ({ Stream }?, Err.E
return out, nil
end
-- rational(fps) -> num, den: integer rates as n / 1, NTSC rates as n * 1000 / 1001, else to a millisecond
function Source.rational(fps: number): (number, number)
if fps ~= fps or fps <= 0 then
return 25, 1
end
if math.abs(fps - math.floor(fps + 0.5)) < 1e-6 then
return math.floor(fps + 0.5), 1
end
local ntsc = fps * 1001 / 1000
if math.abs(ntsc - math.floor(ntsc + 0.5)) < 1e-4 then
return math.floor(ntsc + 0.5) * 1000, 1001
end
return math.floor(fps * 1000 + 0.5), 1000
end
-- constantRate(stream, maxFps?) -> num, den, { OutPacket }: a video stream at a constant frame rate for the
-- containers that have one (AVI, Y4M): the rate of the shortest frame (at most maxFps, default 100), each packet
-- repeated to fill its duration (a GIF's 100 / 50 / 200 ms frames become 2, 1 and 4 frames at 20 fps)
function Source.constantRate(s: Stream, maxFps: number?): (number, number, { OutPacket })
local fd = math.huge
for _, p in s.packets do
if p.duration and p.duration > 0 and p.duration < fd then
fd = p.duration
end
end
if fd == math.huge then
fd = if s.frameRate and s.frameRate > 0 then 1 / s.frameRate else 0.04
end
fd = math.max(fd, 1 / (maxFps or 100))
local num, den = Source.rational(1 / fd)
local step = den / num
local out: { OutPacket } = {}
local slots = 0
local t0 = if s.packets[1] then s.packets[1].pts else 0
for _, p in s.packets do
local stop = p.pts - t0 + (if p.duration and p.duration > 0 then p.duration else step)
local n = math.max(1, math.floor(stop / step + 0.5) - slots)
for _ = 1, n do
table.insert(out, { pts = t0 + slots * step, duration = step, keyframe = p.keyframe, data = p.data })
slots += 1
end
end
return num, den, out
end
-- interleave(streams) -> { { stream index, packet } } merged by decode time (dts, else pts); each stream keeps its
-- own order
function Source.interleave(streams: { Stream }): { { any } }