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

+7 -20
View File
@@ -4,7 +4,7 @@
-- with yuv420p frames (C420 / C420jpeg / C420mpeg2 / C420paldv as they are; C422 / C444 chroma averaged down; Cmono
-- with neutral chroma; 9..16-bit samples (C420p10, ...) scaled to 8 bits). Y4m.demux(r) gives the Demuxer.
-- Write: yuv420p 8-bit frames ("C420jpeg", as ffmpeg writes for yuv420p) from any video or animated image; rgba8
-- frames are converted (BT.601 limited range). Not supported: interlaced field handling (the I tag is kept in meta),
-- frames are converted (BT.601 limited range); uneven frame times (a GIF's) become a constant rate with repeated frames. Not supported: interlaced field handling (the I tag is kept in meta),
-- alpha (C444alpha drops it).
-- ------------------------------------------------------------------------------------------------ requires
@@ -31,7 +31,9 @@ export type OpenOptions = {
maxPixels: number?, -- default 2^26
}
export type EncodeOptions = {
frameRate: { number }?, -- { num, den } for the F tag (default: from the source's frame rate)
frameRate: { number }?, -- { num, den } for the F tag, one frame per source frame (default: the constant rate of the
-- shortest frame, longer frames repeated)
maxFps: number?, -- the highest rate uneven frame times may need (default 100)
}
-- ------------------------------------------------------------------------------------------------ constants
@@ -173,20 +175,6 @@ function Y4m.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
end
-- ------------------------------------------------------------------------------------------------ encode
local function rational(fps: number): (number, number)
if fps <= 0 or fps ~= fps 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
-- encode(video, opts?) -> buffer | nil, Err
function Y4m.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local streams, err = Source.streams(value, { audio = false }, { video = { copy = nil, encode = "rawvideo" } })
@@ -194,12 +182,11 @@ function Y4m.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
return nil, err
end
local s = streams[1]
local fn, fd
local fn, fd, packets = Source.constantRate(s, opts and opts.maxFps)
if opts and type(opts.frameRate) == "table" then
fn, fd = opts.frameRate[1], opts.frameRate[2]
else
fn, fd = rational(s.frameRate or 25)
fn, fd, packets = opts.frameRate[1], opts.frameRate[2], s.packets
end
s.packets = packets
local w = Bytes.writer(64 + #s.packets * (buffer.len(s.packets[1].data) + 6))
w:string(string.format("YUV4MPEG2 W%d H%d F%d:%d Ip A1:1 C420jpeg XYSCSS=420JPEG\n", s.width or 0, s.height or 0, fn, fd))
for _, p in s.packets do