Skip to content

Commit ee64c3b

Browse files
miga-heygenmincuaclaude
authored
fix(engine): stop destroying the AAC priming edit list when muxing (#3505)
`muxVideoWithAudio` passed `-avoid_negative_ts make_zero` unless the caller set `preserveAudioPrimingEditList`. In practice the dominant path is an AAC sidecar copied into mp4, where that flag is actively harmful: ffmpeg's default is `auto`, which the mp4/mov muxers (AVFMT_TS_NEGATIVE) already resolve to `disabled`. Forcing `make_zero` overrides the correct default, discards the priming edit list the sidecar encode created, shifts the video start_time forward by one AAC frame and writes an empty video edit at t=0 — which edit-list-honoring players (QuickTime/Safari) render as a black first frame. Verified with ffprobe on a copy mux of a 30fps h264 mp4 and an AAC sidecar: with `make_zero` video start_time 0.066000, elst: [media time -1, dur 5940] + [media time 6000, dur 180000] audio start_time 0.042993, elst: [media time -1, ...] without (this fix) video start_time 0.000000, elst: [media time 6000, dur 180000] audio start_time 0.000000, elst: [media time 1024, ...] The empty leading edit and the offset both disappear, and the audio keeps its 1024-sample priming edit. The flag is now never passed for a mux, in any mode. `preserveAudioPrimingEditList` is part of the exported engine API, so it stays on `MuxVideoWithAudioOptions` as `@deprecated` and no-op rather than being removed; the two internal callers that set it (`assembleStage`, distributed `assemble`) drop it. `buildEncoderArgs` and `streamingEncoder` still pass the flag for video-only output and are deliberately left alone — those chunks are consumed as intermediates, not as a delivered mp4/mov. Fixes #3487 Co-authored-by: Alexandru Mincu <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 18409c9 commit ee64c3b

5 files changed

Lines changed: 38 additions & 29 deletions

File tree

‎packages/engine/src/services/chunkEncoder.test.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,6 @@ describe("muxVideoWithAudio audio codec handling", () => {
402402
"copy",
403403
"-movflags",
404404
"+faststart",
405-
"-avoid_negative_ts",
406-
"make_zero",
407405
...renderProvenanceArgs("/tmp/output.mp4"),
408406
"-r",
409407
"30",
@@ -424,7 +422,7 @@ describe("muxVideoWithAudio audio codec handling", () => {
424422
});
425423
});
426424

427-
it("keeps negative-timestamp repair for an M4A without a known priming edit list", async () => {
425+
it("never repairs negative timestamps for an M4A sidecar (regression #3487)", async () => {
428426
const { spawn, calls } = createSpawnSpy();
429427
vi.resetModules();
430428
vi.doMock("child_process", () => ({ spawn }));
@@ -442,13 +440,15 @@ describe("muxVideoWithAudio audio codec handling", () => {
442440
await flushMuxCodecResolution();
443441
expect(calls).toHaveLength(1);
444442
expect(calls[0]!.args).toContain("copy");
445-
expect(calls[0]!.args).toContain("-avoid_negative_ts");
443+
// `make_zero` would discard the sidecar's AAC priming edit list, shift
444+
// the copied video forward ~21ms and leave an empty video edit at t=0.
445+
expect(calls[0]!.args).not.toContain("-avoid_negative_ts");
446446

447447
emitClose(calls[0]!.proc, 0);
448448
await expect(muxPromise).resolves.toMatchObject({ success: true });
449449
});
450450

451-
it("preserves a known M4A priming edit list instead of shifting copied video", async () => {
451+
it("ignores the deprecated preserveAudioPrimingEditList option", async () => {
452452
const { spawn, calls } = createSpawnSpy();
453453
vi.resetModules();
454454
vi.doMock("child_process", () => ({ spawn }));
@@ -459,7 +459,7 @@ describe("muxVideoWithAudio audio codec handling", () => {
459459
"/tmp/audio.duration-normalized.m4a",
460460
"/tmp/output.mp4",
461461
undefined,
462-
{ audioCodec: "aac", preserveAudioPrimingEditList: true },
462+
{ audioCodec: "aac", preserveAudioPrimingEditList: false },
463463
{ num: 30, den: 1 },
464464
);
465465

@@ -582,6 +582,7 @@ describe("muxVideoWithAudio audio codec handling", () => {
582582
expect(calls[0]!.args[calls[0]!.args.indexOf("-c:a") + 1]).toBe("aac");
583583
expect(calls[0]!.args).toContain("-b:a");
584584
expect(calls[0]!.args).toContain("+faststart");
585+
expect(calls[0]!.args).not.toContain("-avoid_negative_ts");
585586

586587
emitClose(calls[0]!.proc, 0);
587588
await expect(muxPromise).resolves.toMatchObject({ success: true });
@@ -629,6 +630,10 @@ describe("muxVideoWithAudio audio codec handling", () => {
629630
if (ext !== ".webm") await flushMuxCodecResolution();
630631
const call = calls[calls.length - 1]!;
631632
expect(call.args).not.toContain("-shortest");
633+
// Same for every container we mux into: ffmpeg's `auto` default is
634+
// already `disabled` for mp4/mov, and forcing `make_zero` breaks the
635+
// AAC priming edit list (#3487).
636+
expect(call.args).not.toContain("-avoid_negative_ts");
632637
emitClose(call.proc, 0);
633638
await muxPromise;
634639
}

‎packages/engine/src/services/chunkEncoder.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ export interface MuxVideoWithAudioOptions extends Partial<
7878
* depend on the file extension alone.
7979
*/
8080
audioCodec?: "aac";
81-
/** Preserve a priming edit list known to have been created by AAC re-encoding. */
81+
/**
82+
* @deprecated No longer used. `-avoid_negative_ts` is never passed for
83+
* mp4/mov muxing (ffmpeg's `auto` default already resolves to `disabled`
84+
* for those containers), so the AAC priming edit list is preserved
85+
* unconditionally and this flag has no effect. See issue #3487. Kept for
86+
* source compatibility; it will be removed in a future major.
87+
*/
8288
preserveAudioPrimingEditList?: boolean;
8389
/** Hard cap copied audio to the already-encoded video's exact duration. */
8490
}
@@ -693,14 +699,18 @@ export async function muxVideoWithAudio(
693699
args.push("-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart");
694700
}
695701
}
696-
const copiesContainerizedAac =
697-
!isWebm && shouldCopyAudio && config?.preserveAudioPrimingEditList === true;
698-
// PTS bases can diverge during mux and reintroduce negative DTS. See
699-
// buildEncoderArgs for the full reasoning on why that breaks playback.
700-
// A freshly encoded M4A is the exception: its edit list already hides the
701-
// AAC priming packet. `make_zero` discards that edit and shifts copied video
702-
// forward by one AAC frame (~21ms), creating a visible first-frame offset.
703-
if (!copiesContainerizedAac) args.push("-avoid_negative_ts", "make_zero");
702+
// No `-avoid_negative_ts` here, in any mode. ffmpeg's default is `auto`,
703+
// which the mp4/mov muxers (AVFMT_TS_NEGATIVE) already resolve to
704+
// `disabled` — the correct behavior for the containers this function
705+
// writes. Passing `make_zero` explicitly overrides that default and, on the
706+
// dominant audio-copy path, discards the AAC priming edit list the sidecar
707+
// encode created: the video start_time shifts forward one AAC frame
708+
// (~21ms) and the muxer writes an empty video edit at t=0, which
709+
// edit-list-honoring players (QuickTime/Safari) show as a black first
710+
// frame. See issue #3487. The video-only encoder args (buildEncoderArgs)
711+
// still pass the flag deliberately — those chunks are consumed as raw
712+
// elementary output, not as a delivered mp4/mov.
713+
//
704714
// Re-assert provenance here: this stage re-muxes into the delivered
705715
// container, and the mp4 muxer drops the encode stage's tags without the
706716
// use_metadata_tags flag that appendRenderProvenanceArgs adds.

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,7 @@ export async function assemble(
302302
}
303303

304304
// ── 3. Audio: pad-or-trim then mux ────────────────────────────────────
305-
let normalizedAudio: {
306-
path: string;
307-
preserveAudioPrimingEditList: boolean;
308-
} | null = null;
305+
let normalizedAudioPath: string | null = null;
309306
if (audioPath !== null && existsSync(audioPath)) {
310307
const paddedAudioPath = join(workDir, "audio-padded.m4a");
311308
const padTrimResult = await padOrTrimAudioToVideoFrameCount({
@@ -317,10 +314,7 @@ export async function assemble(
317314
if (!padTrimResult.success) {
318315
throw new Error(`[assemble] audio pad/trim failed: ${padTrimResult.error}`);
319316
}
320-
normalizedAudio = {
321-
path: paddedAudioPath,
322-
preserveAudioPrimingEditList: padTrimResult.operation !== "copy",
323-
};
317+
normalizedAudioPath = paddedAudioPath;
324318
log.info("[assemble] audio normalized for mux", {
325319
operation: padTrimResult.operation,
326320
targetDurationSeconds: padTrimResult.targetDurationSeconds,
@@ -333,16 +327,17 @@ export async function assemble(
333327
// because it operates on a `RenderJob` and emits `updateJobStatus`
334328
// payloads — the distributed activity has no job to thread through.
335329
const muxOutputPath =
336-
normalizedAudio !== null ? join(workDir, `mux.${plan.dimensions.format}`) : postConcatPath;
337-
if (normalizedAudio !== null) {
330+
normalizedAudioPath !== null
331+
? join(workDir, `mux.${plan.dimensions.format}`)
332+
: postConcatPath;
333+
if (normalizedAudioPath !== null) {
338334
const muxResult = await muxVideoWithAudio(
339335
postConcatPath,
340-
normalizedAudio.path,
336+
normalizedAudioPath,
341337
muxOutputPath,
342338
abortSignal,
343339
{
344340
audioCodec: "aac",
345-
preserveAudioPrimingEditList: normalizedAudio.preserveAudioPrimingEditList,
346341
},
347342
{ num: plan.dimensions.fpsNum, den: plan.dimensions.fpsDen },
348343
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe("runAssembleStage audio duration parity", () => {
6969
"/tmp/audio.duration-normalized.m4a",
7070
"/tmp/output.mp4",
7171
undefined,
72-
{ audioCodec: "aac", preserveAudioPrimingEditList: true },
72+
{ audioCodec: "aac" },
7373
{ num: 30, den: 1 },
7474
);
7575
});

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export async function runAssembleStage(input: AssembleStageInput): Promise<Assem
7878
abortSignal,
7979
{
8080
audioCodec: "aac",
81-
preserveAudioPrimingEditList: normalizeResult.operation !== "copy",
8281
},
8382
job.config.fps,
8483
);

0 commit comments

Comments
 (0)