Skip to content

Commit 317556a

Browse files
committed
fix(producer): surface the reason when audio mixing fails instead of silently shipping video-only
At least 4 independent post-release feedback reports of a render completing successfully (exit 0) with audio elements correctly authored and detected at compile time (audioCount > 0), but the final MP4 having no audio track — discovered only via ffprobe or manual playback, with the CLI giving no indication anything went wrong. Users worked around it by muxing the generated audio in manually with ffmpeg. Root cause: runAudioStage sets hasAudio from processCompositionAudio's success flag, but discarded its error field — the actual reason a per-element audio prep step or the final mix failed (source not found, extract failed, ffmpeg error) was computed and then thrown away. A real audio-mix failure was therefore indistinguishable from "no audio was authored": both just produced hasAudio: false with zero diagnostic output. Thread the mixer's error through as audioError (only set when audios.length > 0 but the mix failed) and log.warn it from both call sites (the main render path in renderOrchestrator.ts and the distributed plan() path) so a real failure is loud instead of silently downgrading to a video-only render. Tests: 4 new cases for runAudioStage (mixer error surfaced, generic fallback message when the mixer doesn't provide one, no audioError on success, no audioError when there's no audio to mix). renderOrchestrator.test.ts (68 tests) unaffected. plan.test.ts's one failure (an audio-bearing planHash determinism test timing out at 30s) is pre-existing — reproduces identically on unmodified main with these changes stashed.
1 parent 1d4c5d5 commit 317556a

4 files changed

Lines changed: 118 additions & 1 deletion

File tree

packages/producer/src/services/distributed/plan.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,9 @@ export async function plan(
903903
abortSignal,
904904
assertNotAborted,
905905
});
906+
if (audioResult.audioError) {
907+
log.warn(`[Render] Audio mix failed — output will be video-only: ${audioResult.audioError}`);
908+
}
906909

907910
// Promote staged artifacts from the temp work tree into the final planDir
908911
// shape. `workDir` is `<planDir>/.plan-work/` — always the same filesystem
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { mkdtempSync, rmSync } from "node:fs";
3+
import { join } from "node:path";
4+
import { tmpdir } from "node:os";
5+
import type { AudioElement } from "@hyperframes/engine";
6+
7+
const { processCompositionAudioMock } = vi.hoisted(() => ({
8+
processCompositionAudioMock: vi.fn(),
9+
}));
10+
11+
vi.mock("@hyperframes/engine", async (importOriginal) => {
12+
const actual = await importOriginal<typeof import("@hyperframes/engine")>();
13+
return { ...actual, processCompositionAudio: processCompositionAudioMock };
14+
});
15+
16+
import { runAudioStage } from "./audioStage.js";
17+
18+
// Regression: hasAudio flipping to false used to be indistinguishable from
19+
// "no audio was authored" — processCompositionAudio's error (per-element
20+
// failures, or the mix's own failure) was read into hasAudio and then
21+
// discarded, so a real audio-mix failure shipped a silent video-only render
22+
// with no indication anything went wrong. audioError carries that reason.
23+
describe("runAudioStage", () => {
24+
const tempDirs: string[] = [];
25+
const audios: AudioElement[] = [
26+
{ id: "a1", src: "narration.wav", start: 0, end: 5, mediaStart: 0, volume: 1, type: "audio" },
27+
];
28+
29+
afterEach(() => {
30+
processCompositionAudioMock.mockClear();
31+
for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
32+
});
33+
34+
function makeInput(overrides: Partial<Parameters<typeof runAudioStage>[0]> = {}) {
35+
const workDir = mkdtempSync(join(tmpdir(), "hf-audiostage-"));
36+
tempDirs.push(workDir);
37+
return {
38+
projectDir: workDir,
39+
workDir,
40+
compiledDir: join(workDir, "compiled"),
41+
duration: 5,
42+
audios,
43+
abortSignal: undefined,
44+
assertNotAborted: () => {},
45+
...overrides,
46+
};
47+
}
48+
49+
it("surfaces the mixer's error as audioError when the mix fails", async () => {
50+
processCompositionAudioMock.mockResolvedValue({
51+
success: false,
52+
outputPath: "audio.aac",
53+
durationMs: 1,
54+
tracksProcessed: 0,
55+
error: "Source not found: a1 (narration.wav)",
56+
});
57+
58+
const result = await runAudioStage(makeInput());
59+
60+
expect(result.hasAudio).toBe(false);
61+
expect(result.audioError).toBe("Source not found: a1 (narration.wav)");
62+
});
63+
64+
it("falls back to a generic message when the mixer fails without an error string", async () => {
65+
processCompositionAudioMock.mockResolvedValue({
66+
success: false,
67+
outputPath: "audio.aac",
68+
durationMs: 1,
69+
tracksProcessed: 0,
70+
});
71+
72+
const result = await runAudioStage(makeInput());
73+
74+
expect(result.hasAudio).toBe(false);
75+
expect(result.audioError).toBe("audio mix failed for an unknown reason");
76+
});
77+
78+
it("does not set audioError when the mix succeeds", async () => {
79+
processCompositionAudioMock.mockResolvedValue({
80+
success: true,
81+
outputPath: "audio.aac",
82+
durationMs: 1,
83+
tracksProcessed: 1,
84+
});
85+
86+
const result = await runAudioStage(makeInput());
87+
88+
expect(result.hasAudio).toBe(true);
89+
expect(result.audioError).toBeUndefined();
90+
});
91+
92+
it("does not set audioError when there is no audio to mix", async () => {
93+
const result = await runAudioStage(makeInput({ audios: [] }));
94+
95+
expect(processCompositionAudioMock).not.toHaveBeenCalled();
96+
expect(result.hasAudio).toBe(false);
97+
expect(result.audioError).toBeUndefined();
98+
});
99+
});

packages/producer/src/services/render/stages/audioStage.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export interface AudioStageResult {
3939
hasAudio: boolean;
4040
/** Wall-clock ms for the audio mix phase. Zero-elements path is near-zero but always set. */
4141
audioProcessMs: number;
42+
/**
43+
* Set when `audios.length > 0` but the mix failed (`hasAudio` is `false`
44+
* despite audio being expected) — the caller should surface this. `undefined`
45+
* both when there was no audio to mix and when the mix succeeded.
46+
*/
47+
audioError?: string;
4248
}
4349

4450
export async function runAudioStage(input: AudioStageInput): Promise<AudioStageResult> {
@@ -48,6 +54,7 @@ export async function runAudioStage(input: AudioStageInput): Promise<AudioStageR
4854
const stage3Start = Date.now();
4955
const audioOutputPath = join(workDir, "audio.aac");
5056
let hasAudio = false;
57+
let audioError: string | undefined;
5158

5259
if (audios.length > 0) {
5360
const audioResult = await processCompositionAudio(
@@ -63,8 +70,13 @@ export async function runAudioStage(input: AudioStageInput): Promise<AudioStageR
6370
assertNotAborted();
6471

6572
hasAudio = audioResult.success;
73+
// processCompositionAudio's error (per-element failures or the mix's own
74+
// error) used to be discarded here — the caller only saw hasAudio flip to
75+
// false with no explanation, so a real audio failure looked identical to
76+
// "no audio was authored" and shipped a silent video-only render.
77+
if (!hasAudio) audioError = audioResult.error ?? "audio mix failed for an unknown reason";
6678
}
6779
const audioProcessMs = Date.now() - stage3Start;
6880

69-
return { audioOutputPath, hasAudio, audioProcessMs };
81+
return { audioOutputPath, hasAudio, audioProcessMs, audioError };
7082
}

packages/producer/src/services/renderOrchestrator.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,9 @@ export async function executeRenderJob(
12141214
);
12151215
const { audioOutputPath, hasAudio } = audioResult;
12161216
perfStages.audioProcessMs = audioResult.audioProcessMs;
1217+
if (audioResult.audioError) {
1218+
log.warn(`[Render] Audio mix failed — output will be video-only: ${audioResult.audioError}`);
1219+
}
12171220

12181221
// ── Stage 4: Frame capture ──────────────────────────────────────────
12191222
const stage4Start = Date.now();

0 commit comments

Comments
 (0)