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)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
51630729f6
commit
a7d44451c4
7 files changed
+66
-9
No files matched your search
@@ -190,6 +190,9 @@ export type Track = {
|
||||
sampleRate: number?,
|
||||
channels: number?,
|
||||
bitsPerSample: number?,
|
||||
codecDelay: number?, -- seconds of decoder output to drop at the start (encoder priming: Matroska CodecDelay, the
|
||||
-- first edit's media time of an MP4 audio track)
|
||||
playDuration: number?, -- seconds to play after the codec delay (an MP4 edit list's length: the end padding cut)
|
||||
config: buffer?, -- codec configuration (CodecPrivate, avcC, OpusHead, Vorbis headers, AVI strf extra, ...)
|
||||
[string]: any,
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -287,8 +287,13 @@ local function openAudio(demuxer: any, track: any): (any, Err.Err?)
|
||||
local dec, oerr = F.open(buf, { format = fmt, category = "audio" })
|
||||
-- Matroska CodecDelay: ffmpeg drops it for MPEG audio / AAC; Opus has its own pre-skip and Vorbis's delay is
|
||||
-- already the decoder's own (its first packet returns nothing)
|
||||
if dec and fmt ~= "opus" and fmt ~= "vorbis" and type(track.codecDelay) == "number" and track.codecDelay > 0 then
|
||||
dec = AudioWrap.trim(dec, math.floor(track.codecDelay * dec.sampleRate + 0.5))
|
||||
-- MP4 edit lists add the play length (the encoder's end padding is cut, as ffmpeg does)
|
||||
if dec and fmt ~= "opus" and fmt ~= "vorbis" then
|
||||
local skip = if type(track.codecDelay) == "number" and track.codecDelay > 0 then math.floor(track.codecDelay * dec.sampleRate + 0.5) else 0
|
||||
local length = if type(track.playDuration) == "number" and track.playDuration > 0 then math.floor(track.playDuration * dec.sampleRate + 0.5) else nil
|
||||
if skip > 0 or length then
|
||||
dec = AudioWrap.trim(dec, skip, length)
|
||||
end
|
||||
end
|
||||
return dec, oerr
|
||||
end
|
||||
|
||||
@@ -154,6 +154,9 @@ function Mp4.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
|
||||
if it.skip and it.skip > 0 then
|
||||
t.codecDelay = it.skip / it.timescale -- AAC priming: dropped like ffmpeg
|
||||
end
|
||||
if it.playDuration and it.playDuration > 0 and it.timescale > 0 then
|
||||
t.playDuration = it.playDuration / it.timescale -- the edit's length: the end padding is not played
|
||||
end
|
||||
end
|
||||
table.insert(tracks, t)
|
||||
table.insert(src, it)
|
||||
@@ -272,6 +275,8 @@ function Mp4.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
for _, d in durations do
|
||||
playDuration += d
|
||||
end
|
||||
elseif s.kind == "audio" and s.track and s.copied and type(s.track.playDuration) == "number" then
|
||||
playDuration = math.floor(s.track.playDuration * scale + 0.5) -- keep the source's end trim
|
||||
end
|
||||
local t: any = {
|
||||
kind = s.kind,
|
||||
|
||||
Reference in new issue
Block a user