diff --git a/docs/guide/video.md b/docs/guide/video.md index 8da3c70..cbabe27 100644 --- a/docs/guide/video.md +++ b/docs/guide/video.md @@ -95,6 +95,7 @@ assert(r and g and b) | `width`, `height`, `frameRate` | video tracks | | `sampleRate`, `channels`, `bitsPerSample` | audio tracks | | `config` | codec configuration bytes (avcC, CodecPrivate, Vorbis headers, ...) | +| `codecDelay`, `playDuration` | audio: seconds of encoder priming to drop, and seconds to play after it (an MP4 edit list; Matroska `CodecDelay`) | | `duration`, `language`, `name`, `default` | when the container has them | A Packet is `{ track, pts, dts?, duration?, keyframe, data }`: `data` is a buffer of the compressed bytes. @@ -142,7 +143,8 @@ v:reset() assert(v:nextFrame().pts == 0) ``` -The audio track is an ordinary audio Decoder: read it, or turn it into an Audio. +The audio track is an ordinary audio Decoder: read it, or turn it into an Audio. Encoder priming and an MP4 edit +list's end padding are cut as ffmpeg cuts them, so a 1 s AAC track decodes to 1 s of samples. ```luau local v = assert(F.video.open(webmBytes)) @@ -151,6 +153,8 @@ assert(a.sampleRate == 44100 and a.channels == 1) local sound = assert(F.ops.audio.toAudio(a)) local ogg = assert(F.save(sound, nil, { format = "vorbis" })) -- the soundtrack on its own assert(F.detect(ogg) == "vorbis") +local aac = assert(F.ops.audio.toAudio(assert(F.video.open(mp4Bytes)).audio)) +assert(aac.frames == 44100) -- 1 s: the AAC priming and padding cut by the edit list ``` Animated images open as videos (`codec = "frames"`, rgba8 frames, no demuxer). `F.video.fromImage(img)` does the same diff --git a/src/Formats/Types.luau b/src/Formats/Types.luau index 63e3d79..9ec4443 100644 --- a/src/Formats/Types.luau +++ b/src/Formats/Types.luau @@ -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, } diff --git a/src/Formats/containers/AudioWrap.luau b/src/Formats/containers/AudioWrap.luau index 0646754..ac51aee 100644 --- a/src/Formats/containers/AudioWrap.luau +++ b/src/Formats/containers/AudioWrap.luau @@ -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 diff --git a/src/Formats/init.luau b/src/Formats/init.luau index 9ac5ccb..dba2a8a 100644 --- a/src/Formats/init.luau +++ b/src/Formats/init.luau @@ -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 diff --git a/src/Formats/video/mp4/init.luau b/src/Formats/video/mp4/init.luau index 0dcf3e8..48b57c7 100644 --- a/src/Formats/video/mp4/init.luau +++ b/src/Formats/video/mp4/init.luau @@ -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, diff --git a/tests/video/_common.luau b/tests/video/_common.luau index b4d9673..63988d0 100644 --- a/tests/video/_common.luau +++ b/tests/video/_common.luau @@ -152,6 +152,21 @@ function C.audio(t: any, v: any, c: any, label: string, exact: boolean?, minSnr: end local n = got * a.channels t:check(n == want or n * 4 == c.pcmBytes, label .. ": " .. n .. " samples read of " .. want) + -- the whole stream: as long as ffmpeg's decode (priming and end padding trimmed the same way: MP4 edit lists). + -- Not yet for Matroska / WebM, where ffmpeg also cuts the end padding of the last packets (Vorbis, MP2: a few + -- hundred frames) and we do not. + if c.pcmBytes and v.format ~= "mkv" and v.format ~= "webm" then + local total = got + while true do + local _, k = a:read(4096) + if not k or k <= 0 then + break + end + total += k + end + t:eq(total * a.channels * 4, c.pcmBytes, label .. ": decoded length = ffmpeg's (" .. total .. " frames)") + a:seek(0) + end n = math.min(n, want) if exact then local same = true diff --git a/tests/video/mp4.luau b/tests/video/mp4.luau index 9ff4d7a..fa314a5 100644 --- a/tests/video/mp4.luau +++ b/tests/video/mp4.luau @@ -62,6 +62,16 @@ for _, job in { { "mjpeg_aac.mp4", "mp4" }, { "mjpeg_pcm.mov", "mov" }, { "h264_ end end t:check(same, "write " .. job[1] .. ": same packets and times") + -- copied AAC keeps its edit list: priming and end padding cut as in the source (ffmpeg's length) + local at = a:track("audio") + if at and at.codec == "aac" then + local bt = b:track("audio") + t:check(bt and bt.playDuration and math.abs(bt.playDuration - at.playDuration) < 1e-3 and math.abs((bt.codecDelay or 0) - (at.codecDelay or 0)) < 1e-6, "write " .. job[1] .. ": edit list kept") + if v2.audio and src.pcmBytes then + local all = assert(F.ops.audio.toAudio(v2.audio)) + t:eq(all.frames * all.channels * 4, src.pcmBytes, "write " .. job[1] .. ": AAC length = ffmpeg's decode of the source") + end + end end end end