refactor(producer): extract audioStage from executeRenderJob

Move the audio mixing sub-stage of `executeRenderJob` into
`services/render/stages/audioStage.ts`. Trivial wrapper around
`processCompositionAudio` with the same skip-when-empty path.

No behavior change:
- `audioOutputPath` is still `join(workDir, "audio.aac")` regardless of
  whether the composition has audio.
- `hasAudio` still reflects `audioResult.success` (false when no audio
  elements or when the mixer returns success: false).
- `perfStages.audioProcessMs` is set at the same end-of-stage point
  whether or not the mixer ran.
- The "Processing audio tracks" progress callback fires at 20% at the
  same code point.

Removes the now-unused `processCompositionAudio` import from the
orchestrator (oxlint flagged it).

Verified inside `Dockerfile.test` against `font-variant-numeric`,
`many-cuts`, `variables-prod` — 3/3 pass with audio correlations
1.000 / 0.994 / 0.975 (identical to prior PRs in the stack).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
James
2026-05-12 00:35:16 +00:00
co-authored by Claude Opus 4.7
parent 8951ed939c
commit aec29fe599
2 changed files with 85 additions and 23 deletions
@@ -0,0 +1,72 @@
/**
* audioStage — mix the composition's audio tracks into `workDir/audio.aac`.
*
* Trivial wrapper around `processCompositionAudio`. The stage is skipped
* (no ffmpeg invocation) when the composition has no audio elements; the
* timer is still set so the perf summary stays consistent across renders.
*
* Hard constraints preserved verbatim:
* - `audioOutputPath` is always `join(workDir, "audio.aac")`, regardless
* of whether any audio was actually produced.
* - `hasAudio` reflects `audioResult.success` from
* `processCompositionAudio`; it is `false` when there are no audio
* elements (skips the call entirely) and also when the call returns
* `success: false`.
* - `perfStages.audioProcessMs` is set whether or not the call ran.
*/
import { join } from "node:path";
import { processCompositionAudio } from "@hyperframes/engine";
import type { RenderJob } from "../../renderOrchestrator.js";
import type { CompositionMetadata } from "../shared.js";
export interface AudioStageInput {
projectDir: string;
workDir: string;
/** `join(workDir, "compiled")`; passed through to the audio mixer for asset resolution. */
compiledDir: string;
job: RenderJob;
/** Composition duration (post-probe). Must be > 0 — probeStage guarantees this. */
duration: number;
/** Read-only view of `composition.audios`. */
audios: CompositionMetadata["audios"];
abortSignal: AbortSignal | undefined;
assertNotAborted: () => void;
}
export interface AudioStageResult {
/** Always `join(workDir, "audio.aac")`. */
audioOutputPath: string;
/** True iff the audio mix actually produced a file. False when there are no audio elements. */
hasAudio: boolean;
/** Wall-clock ms for the audio mix phase. Zero-elements path is near-zero but always set. */
audioProcessMs: number;
}
export async function runAudioStage(input: AudioStageInput): Promise<AudioStageResult> {
const { projectDir, workDir, compiledDir, duration, audios, abortSignal, assertNotAborted } =
input;
const stage3Start = Date.now();
const audioOutputPath = join(workDir, "audio.aac");
let hasAudio = false;
if (audios.length > 0) {
const audioResult = await processCompositionAudio(
audios,
projectDir,
join(workDir, "audio-work"),
audioOutputPath,
duration,
abortSignal,
undefined,
compiledDir,
);
assertNotAborted();
hasAudio = audioResult.success;
}
const audioProcessMs = Date.now() - stage3Start;
return { audioOutputPath, hasAudio, audioProcessMs };
}
@@ -53,7 +53,6 @@ import {
muxVideoWithAudio,
applyFaststart,
getEncoderPreset,
processCompositionAudio,
calculateOptimalWorkers,
distributeFrames,
executeParallelCapture,
@@ -101,6 +100,7 @@ import {
import { runCompileStage } from "./render/stages/compileStage.js";
import { runProbeStage } from "./render/stages/probeStage.js";
import { runExtractVideosStage } from "./render/stages/extractVideosStage.js";
import { runAudioStage } from "./render/stages/audioStage.js";
/**
* Wrap a cleanup operation so it never throws, but logs any failure.
@@ -2076,30 +2076,20 @@ export async function executeRenderJob(
}
// ── Stage 3: Audio processing ───────────────────────────────────────
const stage3Start = Date.now();
updateJobStatus(job, "preprocessing", "Processing audio tracks", 20, onProgress);
const audioOutputPath = join(workDir, "audio.aac");
let hasAudio = false;
if (composition.audios.length > 0) {
const audioResult = await processCompositionAudio(
composition.audios,
projectDir,
join(workDir, "audio-work"),
audioOutputPath,
job.duration,
abortSignal,
undefined,
compiledDir,
);
assertNotAborted();
hasAudio = audioResult.success;
perfStages.audioProcessMs = Date.now() - stage3Start;
} else {
perfStages.audioProcessMs = Date.now() - stage3Start;
}
const audioResult = await runAudioStage({
projectDir,
workDir,
compiledDir,
job,
duration: job.duration,
audios: composition.audios,
abortSignal,
assertNotAborted,
});
const { audioOutputPath, hasAudio } = audioResult;
perfStages.audioProcessMs = audioResult.audioProcessMs;
// ── Stage 4: Frame capture ──────────────────────────────────────────
const stage4Start = Date.now();