ws-apifix: MP4 AAC length matches ffmpeg: the edit list's length (Track.playDuration) cuts the end padding as well as the priming (codecDelay); a 1 s clip decodes to 44100 frames, not 45056. AudioWrap.trim(dec, skip, length?); the MP4 writer keeps the edit length of copied audio. Video tests compare the whole decoded audio length with ffmpeg (not yet for Matroska)

This commit is contained in:
4DBug committed 2026-10-31 14:13:08 -05:00
1 parent ac47545aa3
commit 6274eedd12
7 files changed
+66 -9

No files matched your search

+21 -6
View File
@@ -325,16 +325,23 @@ local function adts(track: Types.Track, packets: { Types.Packet }): (buffer?, Er
end
-- ------------------------------------------------------------------------------------------------ trim
-- trim(decoder, frames) -> an audio Decoder that drops the first `frames` sample frames (a container's codec delay:
-- Matroska CodecDelay for MP2 / MP3 / AAC, which ffmpeg drops too; Opus handles its pre-skip itself)
function AudioWrap.trim(dec: any, skip: number): any
if skip <= 0 then
-- trim(decoder, frames, length?) -> an audio Decoder that drops the first `frames` sample frames (a container's
-- codec delay: Matroska CodecDelay, an MP4 edit's media time for MP2 / MP3 / AAC, which ffmpeg drops too; Opus
-- handles its pre-skip itself) and, with length, ends after that many frames (an MP4 edit's length: end padding)
function AudioWrap.trim(dec: any, skip: number, length: number?): any
if skip <= 0 and length == nil then
return dec
end
skip = math.max(0, skip)
local rate = dec.sampleRate
local w: any = setmetatable({}, { __index = dec })
w.frames = math.max(0, (dec.frames or 0) - skip)
w.duration = math.max(0, (dec.duration or 0) - skip / rate)
if length then
w.frames = if (dec.frames or 0) > 0 then math.min(w.frames, length) else length
end
w.duration = w.frames / rate
local cap = length or math.huge
local delivered = 0
local function drop()
local left = skip
while left > 0 do
@@ -351,15 +358,23 @@ function AudioWrap.trim(dec: any, skip: number): any
started = true
drop()
end
return dec:read(n, out)
local want = math.min(n, cap - delivered)
if want <= 0 then
return out or buffer.create(0), 0
end
local b, got = dec:read(want, out)
delivered += got
return b, got
end
function w.seek(_: any, seconds: number)
started = true
if seconds <= 0 then
dec:seek(0)
drop()
delivered = 0
else
dec:seek(seconds + skip / rate)
delivered = math.floor(seconds * rate + 0.5)
end
end
function w.position(_: any): number