Skip to content

Commit 9289551

Browse files
committed
fix(render): scope M4A priming preservation to trims
1 parent 59c56d3 commit 9289551

5 files changed

Lines changed: 36 additions & 5 deletions

File tree

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ describe("muxVideoWithAudio audio codec handling", () => {
418418
});
419419
});
420420

421-
it("preserves M4A priming edit lists instead of shifting copied video", async () => {
421+
it("keeps negative-timestamp repair for an M4A without a known priming edit list", async () => {
422422
const { spawn, calls } = createSpawnSpy();
423423
vi.resetModules();
424424
vi.doMock("child_process", () => ({ spawn }));
@@ -433,6 +433,30 @@ describe("muxVideoWithAudio audio codec handling", () => {
433433
{ num: 30, den: 1 },
434434
);
435435

436+
await flushMuxCodecResolution();
437+
expect(calls).toHaveLength(1);
438+
expect(calls[0]!.args).toContain("copy");
439+
expect(calls[0]!.args).toContain("-avoid_negative_ts");
440+
441+
emitClose(calls[0]!.proc, 0);
442+
await expect(muxPromise).resolves.toMatchObject({ success: true });
443+
});
444+
445+
it("preserves a known M4A priming edit list instead of shifting copied video", async () => {
446+
const { spawn, calls } = createSpawnSpy();
447+
vi.resetModules();
448+
vi.doMock("child_process", () => ({ spawn }));
449+
450+
const { muxVideoWithAudio } = await import("./chunkEncoder.js");
451+
const muxPromise = muxVideoWithAudio(
452+
"/tmp/video-only.mp4",
453+
"/tmp/audio.duration-normalized.m4a",
454+
"/tmp/output.mp4",
455+
undefined,
456+
{ audioCodec: "aac", preserveAudioPrimingEditList: true },
457+
{ num: 30, den: 1 },
458+
);
459+
436460
await flushMuxCodecResolution();
437461
expect(calls).toHaveLength(1);
438462
expect(calls[0]!.args).toContain("copy");

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export interface MuxVideoWithAudioOptions extends Partial<
7777
* depend on the file extension alone.
7878
*/
7979
audioCodec?: "aac";
80+
/** Preserve a priming edit list known to have been created by AAC re-encoding. */
81+
preserveAudioPrimingEditList?: boolean;
8082
}
8183

8284
async function shouldCopyAacSidecar(
@@ -691,7 +693,7 @@ export async function muxVideoWithAudio(
691693
}
692694
}
693695
const copiesContainerizedAac =
694-
!isWebm && shouldCopyAudio && extname(audioPath).toLowerCase() === ".m4a";
696+
!isWebm && shouldCopyAudio && config?.preserveAudioPrimingEditList === true;
695697
// PTS bases can diverge during mux and reintroduce negative DTS. See
696698
// buildEncoderArgs for the full reasoning on why that breaks playback.
697699
// A freshly encoded M4A is the exception: its edit list already hides the

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ export async function assemble(
290290

291291
// ── 3. Audio: pad-or-trim then mux ────────────────────────────────────
292292
let audioForMux: string | null = null;
293+
let preserveAudioPrimingEditList = false;
293294
if (audioPath !== null && existsSync(audioPath)) {
294295
const paddedAudioPath = join(workDir, "audio-padded.m4a");
295296
const padTrimResult = await padOrTrimAudioToVideoFrameCount({
@@ -302,6 +303,7 @@ export async function assemble(
302303
throw new Error(`[assemble] audio pad/trim failed: ${padTrimResult.error}`);
303304
}
304305
audioForMux = paddedAudioPath;
306+
preserveAudioPrimingEditList = padTrimResult.operation === "trim";
305307
log.info("[assemble] audio normalized for mux", {
306308
operation: padTrimResult.operation,
307309
targetDurationSeconds: padTrimResult.targetDurationSeconds,
@@ -321,7 +323,7 @@ export async function assemble(
321323
audioForMux,
322324
muxOutputPath,
323325
abortSignal,
324-
{ audioCodec: "aac" },
326+
{ audioCodec: "aac", preserveAudioPrimingEditList },
325327
{ num: plan.dimensions.fpsNum, den: plan.dimensions.fpsDen },
326328
);
327329
if (!muxResult.success) {

‎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" },
72+
{ audioCodec: "aac", preserveAudioPrimingEditList: true },
7373
{ num: 30, den: 1 },
7474
);
7575
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ export async function runAssembleStage(input: AssembleStageInput): Promise<Assem
7676
normalizeResult.outputPath,
7777
outputPath,
7878
abortSignal,
79-
{ audioCodec: "aac" },
79+
{
80+
audioCodec: "aac",
81+
preserveAudioPrimingEditList: normalizeResult.operation === "trim",
82+
},
8083
job.config.fps,
8184
);
8285
assertNotAborted();

0 commit comments

Comments
 (0)