Skip to content

Commit 113a498

Browse files
committed
fix(render): trim AAC packet padding exactly
1 parent 8c50770 commit 113a498

5 files changed

Lines changed: 44 additions & 24 deletions

File tree

‎packages/producer/src/services/distributed/assemble.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export async function assemble(
291291
// ── 3. Audio: pad-or-trim then mux ────────────────────────────────────
292292
let audioForMux: string | null = null;
293293
if (audioPath !== null && existsSync(audioPath)) {
294-
const paddedAudioPath = join(workDir, "audio-padded.aac");
294+
const paddedAudioPath = join(workDir, "audio-padded.m4a");
295295
const padTrimResult = await padOrTrimAudioToVideoFrameCount({
296296
videoPath: postConcatPath,
297297
audioPath,

‎packages/producer/src/services/render/audioPadTrim.test.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,20 @@ describe("buildPadTrimAudioArgs", () => {
8181
expect(args[args.indexOf("-t") + 1]).toBe("1.000000");
8282
});
8383

84-
it("emits -t when audio is longer than target", () => {
85-
const { args, operation } = buildPadTrimAudioArgs("/tmp/in.aac", "/tmp/out.aac", 6.123, 5.0);
84+
it("filter-trims and re-encodes AAC packet padding beyond the target", () => {
85+
const { args, operation } = buildPadTrimAudioArgs(
86+
"/tmp/in.aac",
87+
"/tmp/out.m4a",
88+
15.018667,
89+
15.0,
90+
);
8691
expect(operation).toBe("trim");
87-
const tIdx = args.indexOf("-t");
88-
expect(tIdx).toBeGreaterThan(-1);
89-
expect(args[tIdx + 1]).toBe("5.000000");
90-
// Trim preserves AAC stream copy.
92+
const filterIdx = args.indexOf("-af");
93+
expect(args[filterIdx + 1]).toBe("atrim=duration=15.000000,asetpts=PTS-STARTPTS");
9194
const codecIdx = args.indexOf("-c:a");
92-
expect(args[codecIdx + 1]).toBe("copy");
95+
expect(args[codecIdx + 1]).toBe("aac");
96+
expect(args[args.indexOf("-b:a") + 1]).toBe("192k");
97+
expect(args.at(-1)).toBe("/tmp/out.m4a");
9398
});
9499

95100
it("emits a plain copy when source duration matches target within ~1ms", () => {
@@ -243,8 +248,8 @@ describe("padOrTrimAudioToVideoFrameCount", () => {
243248
expect(result.operation).toBe("trim");
244249
expect(result.targetDurationSeconds).toBe(4);
245250
expect(captured.args).toHaveLength(1);
246-
const tIdx = captured.args[0]!.indexOf("-t");
247-
expect(captured.args[0]![tIdx + 1]).toBe("4.000000");
251+
const filterIdx = captured.args[0]!.indexOf("-af");
252+
expect(captured.args[0]![filterIdx + 1]).toBe("atrim=duration=4.000000,asetpts=PTS-STARTPTS");
248253
});
249254

250255
it("emits a copy when audio duration already equals frameCount/fps", async () => {

‎packages/producer/src/services/render/audioPadTrim.ts‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* "audio cuts off early" or "video shows a frozen final frame" bugs.
1515
*
1616
* The fix: post-pad/trim audio to *exactly* `frameCount / fps` seconds at
17-
* assemble time. Pad by concat-copying a generated silence tail, trim with
18-
* `-t`, and avoid re-encoding the already mixed source AAC in either case.
17+
* assemble time. Pad by concat-copying a generated silence tail. For trim,
18+
* decode and filter to the exact target before re-encoding into an M4A
19+
* container; packet-copying AAC can only cut on packet boundaries.
1920
*/
2021

2122
import { spawn } from "node:child_process";
@@ -121,8 +122,8 @@ export interface PadTrimAudioPlan {
121122
* tail, then concat-copy the source AAC plus that tail. This avoids
122123
* re-encoding the already mixed `audio.aac`; the pad branch remains the
123124
* inverse of trim instead of becoming a second full-source AAC encode.
124-
* - `sourceDuration > targetDuration` → trim with `-t target`. `-c:a copy`
125-
* is preserved when the input is already AAC.
125+
* - `sourceDuration > targetDuration` → filter to the exact target and
126+
* re-encode AAC so packet padding cannot outlast the video.
126127
* - `|Δ| < AUDIO_DURATION_TOLERANCE_SECONDS` → no-op `copy`, but we still
127128
* run ffmpeg with `-c:a copy` to materialize the output path.
128129
*/
@@ -187,13 +188,27 @@ export function buildPadTrimAudioPlan(
187188
cleanupPaths: [silencePath, concatListPath],
188189
};
189190
}
190-
// Trim. `-t` truncates AAC without re-encoding because AAC frames are
191-
// independently decodable; ffmpeg snaps the cut point to the nearest
192-
// packet boundary, fine for the ±1ms tolerance we care about here.
191+
// Packet-copy trimming snaps to AAC frame boundaries (typically 1024
192+
// samples), which can leave ~20ms beyond the target. Decode/filter/re-encode
193+
// into M4A so ffmpeg records the exact presentation duration.
193194
return {
194195
operation: "trim",
195196
steps: [
196-
{ kind: "trim", args: ["-i", audioPath, "-t", targetSec, "-c:a", "copy", "-y", outputPath] },
197+
{
198+
kind: "trim",
199+
args: [
200+
"-i",
201+
audioPath,
202+
"-af",
203+
`atrim=duration=${targetSec},asetpts=PTS-STARTPTS`,
204+
"-c:a",
205+
"aac",
206+
"-b:a",
207+
"192k",
208+
"-y",
209+
outputPath,
210+
],
211+
},
197212
],
198213
cleanupPaths: [],
199214
};

‎packages/producer/src/services/render/stages/assembleStage.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("runAssembleStage audio duration parity", () => {
4949
muxVideoWithAudioMock.mockResolvedValue({ success: true });
5050
padOrTrimAudioMock.mockResolvedValue({
5151
success: true,
52-
outputPath: "/tmp/audio.duration-normalized.aac",
52+
outputPath: "/tmp/audio.duration-normalized.m4a",
5353
targetDurationSeconds: 1,
5454
sourceDurationSeconds: 1.024,
5555
operation: "trim",
@@ -62,11 +62,11 @@ describe("runAssembleStage audio duration parity", () => {
6262
expect(padOrTrimAudioMock).toHaveBeenCalledWith({
6363
videoPath: "/tmp/video-only.mp4",
6464
audioPath: "/tmp/audio.aac",
65-
outputPath: "/tmp/audio.duration-normalized.aac",
65+
outputPath: "/tmp/audio.duration-normalized.m4a",
6666
});
6767
expect(muxVideoWithAudioMock).toHaveBeenCalledWith(
6868
"/tmp/video-only.mp4",
69-
"/tmp/audio.duration-normalized.aac",
69+
"/tmp/audio.duration-normalized.m4a",
7070
"/tmp/output.mp4",
7171
undefined,
7272
{ audioCodec: "aac" },
@@ -80,14 +80,14 @@ describe("runAssembleStage audio duration parity", () => {
8080
expect(padOrTrimAudioMock).toHaveBeenCalledWith({
8181
videoPath: "/tmp/video-only.mp4",
8282
audioPath: "/tmp/audio.m4a",
83-
outputPath: "/tmp/audio.duration-normalized.aac",
83+
outputPath: "/tmp/audio.duration-normalized.m4a",
8484
});
8585
});
8686

8787
it("fails instead of muxing an unnormalized AAC tail", async () => {
8888
padOrTrimAudioMock.mockResolvedValue({
8989
success: false,
90-
outputPath: "/tmp/audio.duration-normalized.aac",
90+
outputPath: "/tmp/audio.duration-normalized.m4a",
9191
targetDurationSeconds: 1,
9292
sourceDurationSeconds: 1.024,
9393
operation: "trim",

‎packages/producer/src/services/render/stages/assembleStage.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function runAssembleStage(input: AssembleStageInput): Promise<Assem
6161
const audioStem = audioExtension
6262
? audioOutputPath.slice(0, -audioExtension.length)
6363
: audioOutputPath;
64-
const normalizedAudioPath = `${audioStem}.duration-normalized.aac`;
64+
const normalizedAudioPath = `${audioStem}.duration-normalized.m4a`;
6565
const normalizeResult = await padOrTrimAudioToVideoFrameCount({
6666
videoPath: videoOnlyPath,
6767
audioPath: audioOutputPath,

0 commit comments

Comments
 (0)