Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(audio): fit PCM to video frames before AAC delivery (#6089)
Miguel Ángel9da107aef545
diff --git a/packages/cli/src/ui/renderDiagnostic.test.ts b/packages/cli/src/ui/renderDiagnostic.test.tsindex a6b9d4b3eb..013f9f8f27 100644--- a/packages/cli/src/ui/renderDiagnostic.test.ts+++ b/packages/cli/src/ui/renderDiagnostic.test.ts@@ -260,7 +260,8 @@ it.skipIf(process.platform === "win32")(projectDir: root,compiledDir: root,workDir: join(root, "work"),- duration: 1,+ totalFrames: 30,+ fps: { num: 30, den: 1 },ffmpegProcessTimeout: 5_000,audioGain: 1,abortSignal: undefined,diff --git a/packages/engine/src/services/aacEncode.test.ts b/packages/engine/src/services/aacEncode.test.tsindex 7a0ce0d416..b7dcd25ead 100644--- a/packages/engine/src/services/aacEncode.test.ts+++ b/packages/engine/src/services/aacEncode.test.ts@@ -1,10 +1,11 @@import { spawnSync } from "node:child_process";-import { mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";-import { afterAll, describe, expect, it } from "vitest";+import { afterAll, describe, expect, it, vi } from "vitest";import { getFfmpegBinary, getFfprobeBinary } from "../utils/ffmpegBinaries.js";import { aacSegmentCount, encodeAac, splitAdtsFrames } from "./aacEncode.js";+import * as ffmpegRunner from "../utils/runFfmpeg.js";const HAS_FFMPEG = spawnSync(getFfmpegBinary(), ["-version"], { encoding: "utf-8" }).status === 0;@@ -102,6 +103,54 @@ describe.skipIf(!HAS_FFMPEG)("encodeAac", () => {const dir = mkdtempSync(join(tmpdir(), "hf-aac-encode-"));afterAll(() => rmSync(dir, { recursive: true, force: true }));+ it("uses identical scalar AAC arguments and output bytes on repeated encodes", async () => {+ const source = join(dir, "scalar.wav");+ ffmpeg([+ "-f",+ "lavfi",+ "-i",+ "anoisesrc=seed=2:amplitude=0.1:duration=0.5:sample_rate=48000",+ "-ac",+ "2",+ "-c:a",+ "pcm_f32le",+ source,+ ]);+ const first = join(dir, "scalar-first.m4a");+ const second = join(dir, "scalar-second.m4a");+ const run = vi.spyOn(ffmpegRunner, "runFfmpeg");+ try {+ expect((await encodeAac(source, first, 0.5, { timeout: 30_000 })).success).toBe(true);+ expect((await encodeAac(source, second, 0.5, { timeout: 30_000 })).success).toBe(true);+ const encoderCalls = run.mock.calls.filter(([args]) => args.includes("-acodec"));+ expect(encoderCalls).toHaveLength(2);+ for (const [args] of encoderCalls) {+ expect(args).toEqual([+ "-cpuflags",+ "0",+ "-i",+ source,+ "-af",+ "aresample=48000,adelay=delays=480256S:all=1",+ "-acodec",+ "aac",+ "-b:a",+ "192k",+ "-f",+ "adts",+ "-y",+ expect.stringMatching(/[/\\]0\.aac$/),+ ]);+ }+ expect(readFileSync(first).equals(readFileSync(second))).toBe(true);+ const muxCalls = run.mock.calls.filter(([args]) => args.includes("-bsf:a"));+ expect(muxCalls).toHaveLength(2);+ for (const [args] of muxCalls) expect(args).not.toContain("-cpuflags");+ } finally {+ run.mockRestore();+ }+ });+it("encodes a long mix in parallel segments that decode like one encoder's output", async () => {const source = join(dir, "mix.wav");ffmpeg([diff --git a/packages/engine/src/services/aacEncode.ts b/packages/engine/src/services/aacEncode.tsindex 898070ae37..e413314842 100644--- a/packages/engine/src/services/aacEncode.ts+++ b/packages/engine/src/services/aacEncode.ts@@ -77,6 +77,8 @@ export async function encodeAac(segments.map(({ trim, path }) =>runFfmpeg([+ "-cpuflags",+ "0","-i",input,"-af",diff --git a/packages/producer/src/services/distributed/plan.masterBus.test.ts b/packages/producer/src/services/distributed/plan.masterBus.test.tsindex 1fe95cda02..ace9a6b397 100644--- a/packages/producer/src/services/distributed/plan.masterBus.test.ts+++ b/packages/producer/src/services/distributed/plan.masterBus.test.ts@@ -5,7 +5,15 @@ import { join } from "node:path";import { plan } from "./plan.js";const MASTER_CHAIN = '{"version":1,"nodes":[]}';-const seen = vi.hoisted((): { audioInput?: { masterFxChain?: string } } => ({}));+const seen = vi.hoisted(+ (): {+ audioInput?: {+ masterFxChain?: string;+ totalFrames?: number;+ fps?: { num: number; den: number };+ };+ } => ({}),+);vi.mock("../render/stages/audioStage.js", () => ({runAudioStage: async (input: { masterFxChain?: string; workDir: string }) => {@@ -28,7 +36,7 @@ afterEach(() => {rmSync(runRoot, { recursive: true, force: true });});-it("plan() gives the audio stage the composition's master chain", async () => {+it("plan() gives the shared audio stage its master chain and video clock", async () => {const projectDir = join(runRoot, "project");mkdirSync(projectDir, { recursive: true });writeFileSync(@@ -47,4 +55,6 @@ it("plan() gives the audio stage the composition's master chain", async () => {await plan(projectDir, { fps: 30, width: 320, height: 240, format: "mp4" }, planDir);expect(seen.audioInput?.masterFxChain).toBe(MASTER_CHAIN);+ expect(seen.audioInput?.totalFrames).toBe(30);+ expect(seen.audioInput?.fps).toEqual({ num: 30, den: 1 });});diff --git a/packages/producer/src/services/distributed/plan.test.ts b/packages/producer/src/services/distributed/plan.test.tsindex e2472d30c5..df4a8189a3 100644--- a/packages/producer/src/services/distributed/plan.test.ts+++ b/packages/producer/src/services/distributed/plan.test.ts@@ -33,6 +33,8 @@ import {buildChromeArgs,FFMPEG_PATH_ENV,getFfmpegBinary,+ getFfprobeBinary,+ encodeAac,} from "@hyperframes/engine";import { recomputePlanHashFromPlanDir } from "../render/stages/freezePlan.js";import { defaultLogger } from "../../logger.js";@@ -57,6 +59,13 @@ import {resolveMaxParallelChunks,} from "./plan.js";import { buildSyntheticRenderJob } from "./shared.js";+import { assemble } from "./assemble.js";+import { constrainAacTruePeak } from "../render/audioPadTrim.js";++function ffmpegOrThrow(args: string[]): void {+ const ffmpeg = spawnSync(getFfmpegBinary(), ["-nostdin", "-loglevel", "error", ...args]);+ if (ffmpeg.status !== 0) throw new Error(`ffmpeg fixture failed: ${ffmpeg.stderr?.toString()}`);+}// Composition the tests render. `data-duration="1"` keeps the probe stage's// `needsBrowser` gate `false` so plan() completes without launching Chrome.@@ -911,11 +920,7 @@ describe("plan() — golden planDir + planHash determinism", () => {// variant generates a 1s silent wav via `ffmpeg`, references it from// the composition, and runs plan() — exercising the audio-mix path// that produced the original bug.- const HAS_FFMPEG = spawnSync("ffmpeg", ["-version"]).status === 0;- const ffmpegOrThrow = (args: string[]) => {- const ffmpeg = spawnSync("ffmpeg", ["-nostdin", "-loglevel", "error", ...args]);- if (ffmpeg.status !== 0) throw new Error(`ffmpeg fixture failed: ${ffmpeg.stderr?.toString()}`);- };+ const HAS_FFMPEG = spawnSync(getFfmpegBinary(), ["-version"]).status === 0;it.skipIf(!HAS_FFMPEG)("plan.json.planHash matches recompute on an audio-bearing composition",@@ -1011,189 +1016,271 @@ describe("plan() — golden planDir + planHash determinism", () => {);});-describe.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)("stitched mp4 plans", () => {- function audioProject(name: string, amplitude = 0.99, duration = 1): string {- const dir = join(runRoot, name);- mkdirSync(dir, { recursive: true });- const tone = spawnSync("ffmpeg", [- "-nostdin",- "-v",- "error",- "-f",- "lavfi",- "-i",- `aevalsrc=${amplitude}*sin(2*PI*440*t):s=48000:d=0.6`,- "-c:a",- "pcm_s16le",- join(dir, "tone.wav"),- ]);- if (tone.status !== 0) throw new Error(`ffmpeg tone failed: ${tone.stderr}`);- writeFileSync(- join(dir, "index.html"),- `<!doctype html><html><head><meta charset="utf-8"></head><body>+describe.skipIf(spawnSync(getFfmpegBinary(), ["-version"]).status !== 0)(+ "stitched mp4 plans",+ () => {+ function audioProject(name: string, amplitude = 0.99, duration = 1): string {+ const dir = join(runRoot, name);+ mkdirSync(dir, { recursive: true });+ const tone = spawnSync(getFfmpegBinary(), [+ "-nostdin",+ "-v",+ "error",+ "-f",+ "lavfi",+ "-i",+ `aevalsrc=${amplitude}*sin(2*PI*440*t):s=48000:d=0.6`,+ "-c:a",+ "pcm_s16le",+ join(dir, "tone.wav"),+ ]);+ if (tone.status !== 0) throw new Error(`ffmpeg tone failed: ${tone.stderr}`);+ writeFileSync(+ join(dir, "index.html"),+ `<!doctype html><html><head><meta charset="utf-8"></head><body><div data-composition-id="root" data-width="320" data-height="240" data-duration="${duration}"><audio data-composition-audio src="tone.wav"></audio></div></body></html>`,- );- return dir;- }+ );+ return dir;+ }- it("fits the audio length and peak and records the layout only for mp4 without cfr", async () => {- const project = audioProject("project-stitch");- const config = { fps: 30, width: 320, height: 240, format: "mp4" } as const;- const stitched = join(runRoot, "plan-stitch");- await buildLocalExecutionPlan(project, config, stitched, { chunksCarryAudio: true });- const encoder = JSON.parse(readFileSync(join(stitched, "meta", "encoder.json"), "utf-8"));- expect(encoder.chunksCarryAudio).toBe(true);- const probe = spawnSync(- "ffprobe",- [+ it("fits the audio length and peak and records the layout only for mp4 without cfr", async () => {+ const project = audioProject("project-stitch");+ const config = { fps: 30, width: 320, height: 240, format: "mp4" } as const;+ const stitched = join(runRoot, "plan-stitch");+ await buildLocalExecutionPlan(project, config, stitched, { chunksCarryAudio: true });+ const encoder = JSON.parse(readFileSync(join(stitched, "meta", "encoder.json"), "utf-8"));+ expect(encoder.chunksCarryAudio).toBe(true);+ const probe = spawnSync(+ getFfprobeBinary(),+ [+ "-v",+ "error",+ "-show_entries",+ "format=duration",+ "-of",+ "csv=p=0",+ join(stitched, "audio.m4a"),+ ],+ { encoding: "utf-8" },+ );+ expect(Math.abs(Number(probe.stdout) - 1)).toBeLessThan(0.001);+ const volume = spawnSync(+ getFfmpegBinary(),+ ["-nostdin", "-i", join(stitched, "audio.m4a"), "-af", "volumedetect", "-f", "null", "-"],+ { encoding: "utf-8" },+ );+ expect(Number(/max_volume: (-?[\d.]+) dB/.exec(volume.stderr)?.[1])).toBeLessThan(-1);++ const withCfr = join(runRoot, "plan-stitch-cfr");+ await buildLocalExecutionPlan(project, { ...config, cfr: true }, withCfr, {+ chunksCarryAudio: true,+ });+ const cfrEncoder = JSON.parse(readFileSync(join(withCfr, "meta", "encoder.json"), "utf-8"));+ expect(cfrEncoder.chunksCarryAudio).toBeUndefined();+ }, 60_000);++ it("leaves the audio to buildPlanAudio, which makes the track the plan would", async () => {+ // A hot tone, so the true-peak hold re-encodes as well.+ const project = audioProject("project-separate-audio");+ const config = { fps: 30, width: 320, height: 240, format: "mp4" } as const;+ const carried = join(runRoot, "plan-carried-audio");+ await buildLocalExecutionPlan(project, config, carried, { chunksCarryAudio: true });++ const separate = join(runRoot, "plan-separate-audio");+ await buildLocalExecutionPlan(project, config, separate, {+ chunksCarryAudio: true,+ deferAudio: true,+ });+ const encoder = JSON.parse(readFileSync(join(separate, "meta", "encoder.json"), "utf-8"));+ expect([encoder.chunksCarryAudio, encoder.separateAudio]).toEqual([undefined, true]);+ expect(existsSync(join(separate, "audio.m4a"))).toBe(false);++ const work = join(runRoot, "separate-audio-work");+ mkdirSync(work, { recursive: true });+ const audio = join(work, "audio.m4a");+ expect(await buildPlanAudio(project, config, work, audio)).toEqual({ hasAudio: true });+ expect(readFileSync(audio).equals(readFileSync(join(carried, "audio.m4a")))).toBe(true);++ const withCfr = join(runRoot, "plan-separate-audio-cfr");+ await buildLocalExecutionPlan(project, { ...config, cfr: true }, withCfr, {+ deferAudio: true,+ });+ const cfrEncoder = JSON.parse(readFileSync(join(withCfr, "meta", "encoder.json"), "utf-8"));+ expect(cfrEncoder.separateAudio).toBeUndefined();+ expect(existsSync(join(withCfr, "audio.m4a"))).toBe(true);+ }, 60_000);++ it("gives buildPlanAudio the sound of a video whose src the scene script sets", async () => {+ const project = join(runRoot, "project-script-src");+ mkdirSync(project, { recursive: true });+ const clip = spawnSync(getFfmpegBinary(), [+ "-nostdin","-v","error",- "-show_entries",- "format=duration",- "-of",- "csv=p=0",- join(stitched, "audio.m4a"),- ],- { encoding: "utf-8" },- );- expect(Math.abs(Number(probe.stdout) - 1)).toBeLessThan(0.001);- const volume = spawnSync(- "ffmpeg",- ["-nostdin", "-i", join(stitched, "audio.m4a"), "-af", "volumedetect", "-f", "null", "-"],- { encoding: "utf-8" },- );- expect(Number(/max_volume: (-?[\d.]+) dB/.exec(volume.stderr)?.[1])).toBeLessThan(-1);-- const withCfr = join(runRoot, "plan-stitch-cfr");- await buildLocalExecutionPlan(project, { ...config, cfr: true }, withCfr, {- chunksCarryAudio: true,- });- const cfrEncoder = JSON.parse(readFileSync(join(withCfr, "meta", "encoder.json"), "utf-8"));- expect(cfrEncoder.chunksCarryAudio).toBeUndefined();- }, 60_000);-- it("leaves the audio to buildPlanAudio, which makes the track the plan would", async () => {- // A hot tone, so the true-peak hold re-encodes as well.- const project = audioProject("project-separate-audio");- const config = { fps: 30, width: 320, height: 240, format: "mp4" } as const;- const carried = join(runRoot, "plan-carried-audio");- await buildLocalExecutionPlan(project, config, carried, { chunksCarryAudio: true });-- const separate = join(runRoot, "plan-separate-audio");- await buildLocalExecutionPlan(project, config, separate, {- chunksCarryAudio: true,- deferAudio: true,- });- const encoder = JSON.parse(readFileSync(join(separate, "meta", "encoder.json"), "utf-8"));- expect([encoder.chunksCarryAudio, encoder.separateAudio]).toEqual([undefined, true]);- expect(existsSync(join(separate, "audio.m4a"))).toBe(false);-- const work = join(runRoot, "separate-audio-work");- mkdirSync(work, { recursive: true });- const audio = join(work, "audio.m4a");- expect(await buildPlanAudio(project, config, work, audio)).toEqual({ hasAudio: true });- expect(readFileSync(audio).equals(readFileSync(join(carried, "audio.m4a")))).toBe(true);-- const withCfr = join(runRoot, "plan-separate-audio-cfr");- await buildLocalExecutionPlan(project, { ...config, cfr: true }, withCfr, { deferAudio: true });- const cfrEncoder = JSON.parse(readFileSync(join(withCfr, "meta", "encoder.json"), "utf-8"));- expect(cfrEncoder.separateAudio).toBeUndefined();- expect(existsSync(join(withCfr, "audio.m4a"))).toBe(true);- }, 60_000);-- it("gives buildPlanAudio the sound of a video whose src the scene script sets", async () => {- const project = join(runRoot, "project-script-src");- mkdirSync(project, { recursive: true });- const clip = spawnSync("ffmpeg", [- "-nostdin",- "-v",- "error",- "-f",- "lavfi",- "-i",- "testsrc=s=320x240:r=30:d=1",- "-f",- "lavfi",- "-i",- "sine=frequency=440:sample_rate=48000:d=1",- "-c:v",- "libx264",- "-pix_fmt",- "yuv420p",- "-c:a",- "aac",- "-shortest",- join(project, "clip.mp4"),- ]);- if (clip.status !== 0) throw new Error(`ffmpeg clip failed: ${clip.stderr}`);- writeFileSync(- join(project, "index.html"),- `<!doctype html><html><head><meta charset="utf-8"></head><body>+ "-f",+ "lavfi",+ "-i",+ "testsrc=s=320x240:r=30:d=1",+ "-f",+ "lavfi",+ "-i",+ "sine=frequency=440:sample_rate=48000:d=1",+ "-c:v",+ "libx264",+ "-pix_fmt",+ "yuv420p",+ "-c:a",+ "aac",+ "-shortest",+ join(project, "clip.mp4"),+ ]);+ if (clip.status !== 0) throw new Error(`ffmpeg clip failed: ${clip.stderr}`);+ writeFileSync(+ join(project, "index.html"),+ `<!doctype html><html><head><meta charset="utf-8"></head><body><div data-composition-id="root" data-width="320" data-height="240" data-duration="1" data-no-timeline><video id="v" data-has-audio="true" data-start="0" data-duration="1" playsinline></video></div><script>document.getElementById("v").src = "clip.mp4";</script></body></html>`,- );- const config = { fps: 30, width: 320, height: 240, format: "mp4" } as const;- const carried = join(runRoot, "plan-script-src");- await buildLocalExecutionPlan(project, config, carried, { chunksCarryAudio: true });- const work = join(runRoot, "script-src-work");- mkdirSync(work, { recursive: true });- const audio = join(work, "audio.m4a");- expect(await buildPlanAudio(project, config, work, audio)).toEqual({ hasAudio: true });- expect(readFileSync(audio).equals(readFileSync(join(carried, "audio.m4a")))).toBe(true);- }, 60_000);-- it("encodes the audio once, at the frames' exact length", async () => {- // 1.01 s at 30 fps is 31 frames: 49,600 samples, 48.4375 AAC frames of 1,024.- const project = audioProject("project-one-encode", 0.3, 1.01);- const shimDir = join(runRoot, "ffmpeg-shim");- mkdirSync(shimDir, { recursive: true });- const shim = join(shimDir, "ffmpeg");- const calls = join(shimDir, "calls.txt");- writeFileSync(shim, `#!/bin/sh\necho "$*" >> "${calls}"\nexec "${getFfmpegBinary()}" "$@"\n`);- chmodSync(shim, 0o755);- const planDir = join(runRoot, "plan-one-encode");- const previous = process.env[FFMPEG_PATH_ENV];- process.env[FFMPEG_PATH_ENV] = shim;- try {- await buildLocalExecutionPlan(- project,- { fps: 30, width: 320, height: 240, format: "mp4" },- planDir,- { chunksCarryAudio: true },);- } finally {- if (previous === undefined) delete process.env[FFMPEG_PATH_ENV];- else process.env[FFMPEG_PATH_ENV] = previous;- }- const aacEncodes = readFileSync(calls, "utf-8")- .split("\n")- .filter((line) => / aac -b:a /.test(line));- expect(aacEncodes).toHaveLength(1);- const planAudio = join(planDir, "audio.m4a");- const shown = spawnSync(- "ffprobe",- [- "-v",- "error",- "-select_streams",- "a:0",- "-show_entries",- "stream=duration_ts",- "-of",- "csv=p=0",- "--",- planAudio,- ],- { encoding: "utf-8" },+ const config = { fps: 30, width: 320, height: 240, format: "mp4" } as const;+ const carried = join(runRoot, "plan-script-src");+ await buildLocalExecutionPlan(project, config, carried, { chunksCarryAudio: true });+ const work = join(runRoot, "script-src-work");+ mkdirSync(work, { recursive: true });+ const audio = join(work, "audio.m4a");+ expect(await buildPlanAudio(project, config, work, audio)).toEqual({ hasAudio: true });+ expect(readFileSync(audio).equals(readFileSync(join(carried, "audio.m4a")))).toBe(true);+ }, 60_000);++ it("keeps assembled loudness within 0.1 LUFS of the frame-fitted float delivery", async () => {+ const project = audioProject("project-loudness-delivery", 0.3, 3.01);+ ffmpegOrThrow([+ "-f",+ "lavfi",+ "-i",+ "anoisesrc=seed=2:amplitude=0.8:sample_rate=48000:duration=3.01",+ "-af",+ "pan=stereo|c0=c0|c1=c0",+ "-c:a",+ "pcm_f32le",+ "-y",+ join(project, "tone.wav"),+ ]);+ const video = join(runRoot, "loudness-video.mp4");+ ffmpegOrThrow([+ "-f",+ "lavfi",+ "-i",+ "color=black:size=320x240:rate=30",+ "-frames:v",+ "91",+ "-c:v",+ "libx264",+ "-pix_fmt",+ "yuv420p",+ "-y",+ video,+ ]);+ const config = { fps: 30, width: 320, height: 240, format: "mp4", chunkSize: 1000 } as const;+ const planDir = join(runRoot, "plan-loudness-delivery");+ const planned = await buildLocalExecutionPlan(project, config, planDir);+ expect(planned.totalFrames).toBe(91);+ expect(planned.chunkCount).toBe(1);+ const delivered = join(runRoot, "loudness-delivered.mp4");+ await assemble(planDir, [video], join(planDir, "audio.m4a"), delivered);+ const reference = join(runRoot, "loudness-reference.m4a");+ const encoded = await encodeAac(join(project, "tone.wav"), reference, 91 / 30, {+ timeout: 30_000,+ filter: "apad,atrim=0:3.033333",+ });+ expect(encoded.success, encoded.stderr).toBe(true);+ const held = await constrainAacTruePeak({+ audioPath: reference,+ sourcePath: join(project, "tone.wav"),+ targetDurationSeconds: 91 / 30,+ });+ expect(held.success, held.error).toBe(true);+ const measure = (path: string) => {+ const result = spawnSync(+ getFfmpegBinary(),+ ["-nostdin", "-i", path, "-vn", "-af", "ebur128=peak=true", "-f", "null", "-"],+ { encoding: "utf-8" },+ );+ expect(result.status, result.stderr).toBe(0);+ const integrated = [...result.stderr.matchAll(/^\s*I:\s*([+-]?[\d.]+) LUFS$/gm)].at(+ -1,+ )?.[1];+ const peak = [...result.stderr.matchAll(/^\s*Peak:\s*([+-]?[\d.]+) dBFS$/gm)].at(-1)?.[1];+ if (integrated === undefined || peak === undefined)+ throw new Error(`Missing loudness summary: ${result.stderr}`);+ return { integratedTenths: Math.round(Number(integrated) * 10), peak: Number(peak) };+ };+ const actual = measure(delivered);+ const expected = measure(reference);+ expect(actual.peak).toBeLessThanOrEqual(-1);+ // FFmpeg reports tenths of a LUFS. Compare those integer units without binary subtraction drift.+ expect(Math.abs(actual.integratedTenths - expected.integratedTenths)).toBeLessThanOrEqual(1);+ }, 60_000);++ it.each([false, true])(+ "encodes the audio once, at the frames' exact length (chunks carry audio: %s)",+ async (chunksCarryAudio) => {+ // 1.01 s at 30 fps is 31 frames: 49,600 samples, 48.4375 AAC frames of 1,024.+ const project = audioProject(`project-one-encode-${chunksCarryAudio}`, 0.3, 1.01);+ const shimDir = join(runRoot, `ffmpeg-shim-${chunksCarryAudio}`);+ mkdirSync(shimDir, { recursive: true });+ const shim = join(shimDir, "ffmpeg");+ const calls = join(shimDir, "calls.txt");+ writeFileSync(+ shim,+ `#!/bin/sh\necho "$*" >> "${calls}"\nexec "${getFfmpegBinary()}" "$@"\n`,+ );+ chmodSync(shim, 0o755);+ const planDir = join(runRoot, `plan-one-encode-${chunksCarryAudio}`);+ const previous = process.env[FFMPEG_PATH_ENV];+ process.env[FFMPEG_PATH_ENV] = shim;+ try {+ await buildLocalExecutionPlan(+ project,+ { fps: 30, width: 320, height: 240, format: "mp4" },+ planDir,+ { chunksCarryAudio },+ );+ } finally {+ if (previous === undefined) delete process.env[FFMPEG_PATH_ENV];+ else process.env[FFMPEG_PATH_ENV] = previous;+ }+ const aacEncodes = readFileSync(calls, "utf-8")+ .split("\n")+ .filter((line) => / aac -b:a /.test(line));+ expect(aacEncodes).toHaveLength(1);+ const planAudio = join(planDir, "audio.m4a");+ const shown = spawnSync(+ getFfprobeBinary(),+ [+ "-v",+ "error",+ "-select_streams",+ "a:0",+ "-show_entries",+ "stream=duration_ts",+ "-of",+ "csv=p=0",+ "--",+ planAudio,+ ],+ { encoding: "utf-8" },+ );+ // The edit list counts in ms, so 49,600 samples (1.0333 s) show as 1.033 s.+ expect(Math.abs(Number(shown.stdout) - 49_600)).toBeLessThan(48);+ },+ 60_000,);- // The edit list counts in ms, so 49,600 samples (1.0333 s) show as 1.033 s.- expect(Math.abs(Number(shown.stdout) - 49_600)).toBeLessThan(48);- }, 60_000);-});+ },+);describe("plan() — codec knob", () => {const TIMEOUT_MS = 30_000;diff --git a/packages/producer/src/services/distributed/plan.ts b/packages/producer/src/services/distributed/plan.tsindex 7f9197440e..8e1465e394 100644--- a/packages/producer/src/services/distributed/plan.ts+++ b/packages/producer/src/services/distributed/plan.ts@@ -60,8 +60,6 @@ import {} from "../renderOrchestrator.js";import { closeFileServerSafely } from "../fileServer.js";import { measurePlanSecondsPerFrame } from "./planFrameCost.js";-import { constrainAacTruePeak } from "../render/audioPadTrim.js";-import { encoderFailureError } from "../render/encoderInterruption.js";import { runAudioStage } from "../render/stages/audioStage.js";import { runCompileStage } from "../render/stages/compileStage.js";import {@@ -479,7 +477,6 @@ export function chunkSizingUsesFrameCost(export const CHUNK_EXCEEDS_WORKER_TIMEOUT = "CHUNK_EXCEEDS_WORKER_TIMEOUT";export class ChunkExceedsWorkerTimeoutError extends Error {- // fallow-ignore-next-line unused-class-memberreadonly code: typeof CHUNK_EXCEEDS_WORKER_TIMEOUT = CHUNK_EXCEEDS_WORKER_TIMEOUT;constructor(predictedChunkSeconds: number, workerTimeoutSeconds: number) {super(@@ -628,7 +625,6 @@ export const FORMAT_NOT_SUPPORTED_IN_DISTRIBUTED = "FORMAT_NOT_SUPPORTED_IN_DIST* gate.*/export class FormatNotSupportedInDistributedError extends Error {- // fallow-ignore-next-line unused-class-memberreadonly code: typeof FORMAT_NOT_SUPPORTED_IN_DISTRIBUTED = FORMAT_NOT_SUPPORTED_IN_DISTRIBUTED;readonly format: string;readonly reason: string;@@ -1326,26 +1322,25 @@ async function compilePlanComposition(forceScreenshot,width,height,- probeResult,totalFrames,};}type PlanComposition = Awaited<ReturnType<typeof compilePlanComposition>>;-/** The composition's mix at `seconds`, keeping the float mix at `pcmPath` when given. */-function mixPlanAudio(c: PlanComposition, compiledDir: string, seconds: number, pcmPath?: string) {+/** The composition's audio fitted to the video frames by the shared audio stage. */+function mixPlanAudio(c: PlanComposition, compiledDir: string) {return runAudioStage({projectDir: c.projectDir,workDir: c.workDir,compiledDir,urlByAbsentLocalPath: c.urlByAbsentLocalPath,- duration: seconds,+ totalFrames: c.totalFrames,+ fps: c.job.config.fps,ffmpegProcessTimeout: c.cfg.ffmpegProcessTimeout,audioGain: c.cfg.audioGain,audios: c.composition.audios,masterFxChain: c.composition.masterFxChain,- ...(pcmPath ? { keepEncoderInputAt: pcmPath } : {}),abortSignal: c.abortSignal,assertNotAborted: c.assertNotAborted,});@@ -1374,23 +1369,6 @@ async function extractPlanVideos(c: PlanComposition, deferRangeExtraction: boolereturn extractResult;}-/** Holds the mixed track at `audioPath` to the delivery true-peak ceiling, from its float mix. */-async function holdPlanAudioPeak(- c: PlanComposition,- audioPath: string,- pcmPath: string,- seconds: number,-): Promise<void> {- const peak = await constrainAacTruePeak({- audioPath,- ...(existsSync(pcmPath) ? { sourcePath: pcmPath } : {}),- targetDurationSeconds: seconds,- signal: c.abortSignal,- timeoutMs: c.cfg.ffmpegProcessTimeout,- }).finally(() => rmSync(pcmPath, { force: true }));- if (!peak.success) throw encoderFailureError("[plan] audio true-peak check failed", peak);-}-export async function buildLocalExecutionPlan(projectDir: string,config: DistributedRenderConfig,@@ -1417,7 +1395,6 @@ export async function buildLocalExecutionPlan(forceScreenshot,width,height,- probeResult,totalFrames,} = c;@@ -1527,20 +1504,11 @@ export async function buildLocalExecutionPlan(const digests: PlanFileDigests = options.digests ?? new Map();const separateAudio = plansSeparateAudio(config, options);const chunksCarryAudio = !separateAudio && carriesChunkAudio(config, options);- // Chunks cut their audio from this track, so it is mixed to the frames' exact length in its one- // encode; the assembler pads or trims the track of any other plan.- const audioSeconds = chunksCarryAudio ? totalFrames / config.fps : probeResult.duration;- const planMixPcmPath = join(workDir, "plan-mix.wav");stageStarted = Date.now();const [audioOutcome, overlapOutcome, frameCostOutcome] = await Promise.allSettled([separateAudio? null- : mixPlanAudio(- c,- finalCompiledDir,- audioSeconds,- chunksCarryAudio ? planMixPcmPath : undefined,- ).finally(() => logPlanStage(log, "audio", stageStarted)),+ : mixPlanAudio(c, finalCompiledDir).finally(() => logPlanStage(log, "audio", stageStarted)),(async () => {for (const dir of [finalCompiledDir, videoFramesDst, videoSourcesDir]) {await prehashPlanFiles(dir, digests);@@ -1601,9 +1569,6 @@ export async function buildLocalExecutionPlan(renameSync(audioResult.audioOutputPath, planAudioPath);}if (chunksCarryAudio && existsSync(planAudioPath)) {- stageStarted = Date.now();- await holdPlanAudioPeak(c, planAudioPath, planMixPcmPath, audioSeconds);- logPlanStage(log, "audio-peak", stageStarted);if (options.sliceChunkAudio) {stageStarted = Date.now();await writeChunkAudioSlices({@@ -1748,22 +1713,17 @@ export async function buildPlanAudio(executionPlanSizeLimitBytes: Number.MAX_SAFE_INTEGER,});try {- const seconds = c.totalFrames / config.fps;- const pcmPath = join(c.workDir, "plan-mix.wav");let stageStarted = Date.now();await extractPlanVideos(c, true);logPlanStage(c.log, "extract", stageStarted);stageStarted = Date.now();- const mixed = await mixPlanAudio(c, c.compiledDir, seconds, pcmPath);+ const mixed = await mixPlanAudio(c, c.compiledDir);logPlanStage(c.log, "audio", stageStarted);if (mixed.audioError) {applyDistributedAudioWarningPolicy(c.job, mixed.audioError, mixed.audioFailures, c.log);}if (!mixed.hasAudio || !existsSync(mixed.audioOutputPath)) return { hasAudio: false };renameSync(mixed.audioOutputPath, outputPath);- stageStarted = Date.now();- await holdPlanAudioPeak(c, outputPath, pcmPath, seconds);- logPlanStage(c.log, "audio-peak", stageStarted);return { hasAudio: true };} finally {rmSync(c.workDir, { recursive: true, force: true });diff --git a/packages/producer/src/services/render/stages/assembleStage.ts b/packages/producer/src/services/render/stages/assembleStage.tsindex 2e9ed28cd1..3fb4505a92 100644--- a/packages/producer/src/services/render/stages/assembleStage.ts+++ b/packages/producer/src/services/render/stages/assembleStage.ts@@ -57,7 +57,7 @@ export interface AssembleStageResult {assembleMs: number;}-function recordLimiterAttenuation(job: RenderJob, audioLoweredDb: number | undefined): void {+export function recordLimiterAttenuation(job: RenderJob, audioLoweredDb: number | undefined): void {if (audioLoweredDb === undefined) return;job.audioLoweredDb = audioLoweredDb;const ceiling = `−${Math.abs(AAC_DELIVERY_TRUE_PEAK_DBFS)} dBTP`;diff --git a/packages/producer/src/services/render/stages/audioStage.test.ts b/packages/producer/src/services/render/stages/audioStage.test.tsindex 03632ad04c..bdaefcf7c0 100644--- a/packages/producer/src/services/render/stages/audioStage.test.ts+++ b/packages/producer/src/services/render/stages/audioStage.test.ts@@ -1,13 +1,20 @@import { afterEach, describe, expect, it, vi } from "vitest";-import { mkdtempSync, rmSync } from "node:fs";+import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";import { join } from "node:path";import { tmpdir } from "node:os";import { parseMasterFxChain, type AudioElement, type VideoElement } from "@hyperframes/engine";-const { processCompositionAudioMock } = vi.hoisted(() => ({+const { processCompositionAudioMock, constrainAacTruePeakMock } = vi.hoisted(() => ({processCompositionAudioMock: vi.fn(),+ constrainAacTruePeakMock: vi.fn(async () => ({+ success: true,+ error: undefined as string | undefined,+ loweredDb: undefined as number | undefined,+ })),}));+vi.mock("../audioPadTrim.js", () => ({ constrainAacTruePeak: constrainAacTruePeakMock }));+vi.mock("@hyperframes/engine", async (importOriginal) => {const actual = await importOriginal<typeof import("@hyperframes/engine")>();return { ...actual, processCompositionAudio: processCompositionAudioMock };@@ -30,6 +37,7 @@ describe("runAudioStage", () => {afterEach(() => {processCompositionAudioMock.mockClear();+ constrainAacTruePeakMock.mockClear();for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });});@@ -40,7 +48,8 @@ describe("runAudioStage", () => {projectDir: workDir,workDir,compiledDir: join(workDir, "compiled"),- duration: 5,+ totalFrames: 150,+ fps: { num: 30, den: 1 },ffmpegProcessTimeout: 3_600_000,audioGain: 1,audios,@@ -50,6 +59,60 @@ describe("runAudioStage", () => {};}+ it.each([+ { totalFrames: 91, fps: { num: 30, den: 1 }, seconds: 3.033333333333333 },+ { totalFrames: 31, fps: { num: 30000, den: 1001 }, seconds: 1.0343666666666667 },+ ])(+ "mixes the exact frame duration: $totalFrames at $fps.num/$fps.den",+ async ({ totalFrames, fps, seconds }) => {+ processCompositionAudioMock.mockResolvedValueOnce({ success: true, tracksProcessed: 1 });+ await runAudioStage(makeInput({ totalFrames, fps }));+ expect(processCompositionAudioMock.mock.calls[0]?.[4]).toBe(seconds);+ expect(constrainAacTruePeakMock).toHaveBeenCalledWith(+ expect.objectContaining({ targetDurationSeconds: seconds }),+ );+ },+ );++ it("keeps PCM through the encoded peak check and removes it afterwards", async () => {+ const input = makeInput();+ const pcm = join(input.workDir, "audio-source.wav");+ processCompositionAudioMock.mockImplementationOnce(async () => {+ writeFileSync(pcm, "retained mix");+ return { success: true, tracksProcessed: 1 };+ });+ constrainAacTruePeakMock.mockImplementationOnce(async () => {+ expect(existsSync(pcm)).toBe(true);+ return { success: true, error: undefined, loweredDb: 1.44 };+ });+ const result = await runAudioStage(input);+ expect(constrainAacTruePeakMock).toHaveBeenCalledWith({+ audioPath: result.audioOutputPath,+ sourcePath: pcm,+ targetDurationSeconds: 5,+ signal: undefined,+ timeoutMs: 3_600_000,+ });+ expect(existsSync(pcm)).toBe(false);+ expect(result.audioLoweredDb).toBe(1.44);+ });++ it("surfaces an encoded peak failure and still removes PCM", async () => {+ const input = makeInput();+ const pcm = join(input.workDir, "audio-source.wav");+ processCompositionAudioMock.mockImplementationOnce(async () => {+ writeFileSync(pcm, "retained mix");+ return { success: true, tracksProcessed: 1 };+ });+ constrainAacTruePeakMock.mockResolvedValueOnce({+ success: false,+ error: "peak probe failed",+ loweredDb: undefined,+ });+ await expect(runAudioStage(input)).rejects.toThrow("peak probe failed");+ expect(existsSync(pcm)).toBe(false);+ });+it("surfaces the mixer's error as audioError when the mix fails", async () => {processCompositionAudioMock.mockResolvedValue({success: false,@@ -83,7 +146,11 @@ describe("runAudioStage", () => {expect.any(String),5,undefined,- { ffmpegProcessTimeout: 3_600_000, audioGain: 1 },+ {+ ffmpegProcessTimeout: 3_600_000,+ audioGain: 1,+ keepEncoderInputAt: expect.stringMatching(/audio-source\.wav$/),+ },expect.any(String),);});@@ -132,7 +199,12 @@ describe("runAudioStage", () => {expect.any(String),5,undefined,- { ffmpegProcessTimeout: 3_600_000, audioGain: 1, masterFxChain: chain },+ {+ ffmpegProcessTimeout: 3_600_000,+ audioGain: 1,+ masterFxChain: chain,+ keepEncoderInputAt: expect.stringMatching(/audio-source\.wav$/),+ },expect.any(String),);});@@ -206,6 +278,7 @@ describe("runAudioStage", () => {const result = await runAudioStage(makeInput({ audios: [] }));expect(processCompositionAudioMock).not.toHaveBeenCalled();+ expect(constrainAacTruePeakMock).not.toHaveBeenCalled();expect(result.hasAudio).toBe(false);expect(result.audioError).toBeUndefined();expect(result.audioFailures).toBeUndefined();diff --git a/packages/producer/src/services/render/stages/audioStage.ts b/packages/producer/src/services/render/stages/audioStage.tsindex 3d0c7c488f..d9d4584266 100644--- a/packages/producer/src/services/render/stages/audioStage.ts+++ b/packages/producer/src/services/render/stages/audioStage.ts@@ -18,6 +18,8 @@*/import { join } from "node:path";+import { rmSync } from "node:fs";+import type { Fps } from "@hyperframes/core";import {MIXED_AUDIO_FILENAME,audioFailureDiagnostics,@@ -27,14 +29,16 @@ import {import type { CompositionMetadata } from "../shared.js";import type { ProducerLogger } from "../../../logger.js";import { encoderFailureError } from "../encoderInterruption.js";+import { constrainAacTruePeak } from "../audioPadTrim.js";export interface AudioStageInput {projectDir: string;workDir: string;/** `join(workDir, "compiled")`; passed through to the audio mixer for asset resolution. */compiledDir: string;- /** Composition duration (post-probe). Must be > 0 — probeStage guarantees this. */- duration: number;+ /** Probed video frame count and exact frame rate define the delivered audio length. */+ totalFrames: number;+ fps: Fps;/** Timeout forwarded to ffmpeg subprocesses used by the audio mixer. */ffmpegProcessTimeout: number;/** Master gain forwarded to the audio mixer. */@@ -43,8 +47,6 @@ export interface AudioStageInput {audios: CompositionMetadata["audios"];masterFxChain?: string;urlByAbsentLocalPath?: ReadonlyMap<string, string>;- /** Keep the float mix the AAC was encoded from here, for a later re-encode from it. */- keepEncoderInputAt?: string;abortSignal: AbortSignal | undefined;assertNotAborted: () => void;/** Where a per-track failure's detail goes. Optional so tests need not pass one. */@@ -52,6 +54,8 @@ export interface AudioStageInput {}export interface AudioStageResult {+ /** Actual delivery attenuation, for the render warning and metadata. */+ audioLoweredDb?: number;/** Always `join(workDir, MIXED_AUDIO_FILENAME)`. */audioOutputPath: string;/** True iff the audio mix actually produced a file. False when there are no audio elements. */@@ -68,31 +72,62 @@ export interface AudioStageResult {audioFailures?: AudioProcessingFailure[];}-function triggerFirst(- failures: readonly AudioProcessingFailure[] | undefined,- trigger: AudioProcessingFailure,-): AudioProcessingFailure[] {- return [trigger, ...(failures ?? []).filter((failure) => failure !== trigger)];+function assertAudioNotInterrupted(failures: readonly AudioProcessingFailure[] | undefined): void {+ const interrupted = failures?.find((failure) => failure.reason === "external_interruption");+ if (!interrupted) return;+ throw encoderFailureError("Audio processing failed", {+ error: interrupted.detail,+ failureReason: "external_interruption",+ audioFailures: audioFailureDiagnostics([+ interrupted,+ ...(failures ?? []).filter((failure) => failure !== interrupted),+ ]),+ });}export async function runAudioStage(input: AudioStageInput): Promise<AudioStageResult> {+ const stageStart = Date.now();+ const pcmSourcePath = join(input.workDir, "audio-source.wav");+ const duration = (input.totalFrames * input.fps.den) / input.fps.num;+ try {+ const result = await mixAudioForDelivery(input, duration, pcmSourcePath);+ let audioLoweredDb: number | undefined;+ if (result.hasAudio) {+ const peak = await constrainAacTruePeak({+ audioPath: result.audioOutputPath,+ sourcePath: pcmSourcePath,+ targetDurationSeconds: duration,+ signal: input.abortSignal,+ timeoutMs: input.ffmpegProcessTimeout,+ });+ if (!peak.success) throw encoderFailureError("Audio true-peak check failed", peak);+ audioLoweredDb = peak.loweredDb;+ }+ return { ...result, audioLoweredDb, audioProcessMs: Date.now() - stageStart };+ } finally {+ rmSync(pcmSourcePath, { force: true });+ }+}++async function mixAudioForDelivery(+ input: AudioStageInput,+ duration: number,+ pcmSourcePath: string,+): Promise<Omit<AudioStageResult, "audioProcessMs">> {const {projectDir,workDir,compiledDir,- duration,ffmpegProcessTimeout,audioGain,audios,masterFxChain,urlByAbsentLocalPath,- keepEncoderInputAt,abortSignal,assertNotAborted,log,} = input;- const stage3Start = Date.now();const audioOutputPath = join(workDir, MIXED_AUDIO_FILENAME);let hasAudio = false;let audioError: string | undefined;@@ -119,7 +154,7 @@ export async function runAudioStage(input: AudioStageInput): Promise<AudioStageRaudioGain,masterFxChain,urlByAbsentLocalPath,- keepEncoderInputAt,+ keepEncoderInputAt: pcmSourcePath,},compiledDir,);@@ -130,7 +165,6 @@ export async function runAudioStage(input: AudioStageInput): Promise<AudioStageRreturn {audioOutputPath,hasAudio: false,- audioProcessMs: Date.now() - stage3Start,audioError: detail,// Synthesised rather than left undefined. The warning policy reads// owner, retryability, reason and stage off this list, so reporting the@@ -152,16 +186,7 @@ export async function runAudioStage(input: AudioStageInput): Promise<AudioStageR}assertNotAborted();- const interrupted = audioResult.failures?.find(- (failure) => failure.reason === "external_interruption",- );- if (interrupted) {- throw encoderFailureError("Audio processing failed", {- error: interrupted.detail,- failureReason: "external_interruption",- audioFailures: audioFailureDiagnostics(triggerFirst(audioResult.failures, interrupted)),- });- }+ assertAudioNotInterrupted(audioResult.failures);hasAudio = audioResult.success;audioFailures = audioResult.failures;@@ -187,12 +212,10 @@ export async function runAudioStage(input: AudioStageInput): Promise<AudioStageR}}}- const audioProcessMs = Date.now() - stage3Start;return {audioOutputPath,hasAudio,- audioProcessMs,audioError,audioFailures,};diff --git a/packages/producer/src/services/renderOrchestrator.masterBus.test.ts b/packages/producer/src/services/renderOrchestrator.masterBus.test.tsindex 039c107c47..ceec4d1485 100644--- a/packages/producer/src/services/renderOrchestrator.masterBus.test.ts+++ b/packages/producer/src/services/renderOrchestrator.masterBus.test.ts@@ -5,7 +5,15 @@ import { join } from "node:path";import { createRenderJob, executeRenderJob } from "./renderOrchestrator.js";const MASTER_CHAIN = '{"version":1,"nodes":[]}';-const seen = vi.hoisted((): { audioInput?: { masterFxChain?: string } } => ({}));+const seen = vi.hoisted(+ (): {+ audioInput?: {+ masterFxChain?: string;+ totalFrames?: number;+ fps?: { num: number; den: number };+ };+ } => ({}),+);vi.mock("@hyperframes/engine", async (importOriginal) => ({...(await importOriginal<typeof import("@hyperframes/engine")>()),@@ -70,7 +78,7 @@ afterEach(() => {for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });});-it("the local renderer gives the audio stage the composition's master chain", async () => {+it("the local renderer gives the shared audio stage its master chain and video clock", async () => {const root = mkdtempSync(join(tmpdir(), "hf-master-bus-orchestrator-"));dirs.push(root);writeFileSync(join(root, "index.html"), "<div></div>");@@ -84,4 +92,6 @@ it("the local renderer gives the audio stage the composition's master chain", as"audio stage reached",);expect(seen.audioInput?.masterFxChain).toBe(MASTER_CHAIN);+ expect(seen.audioInput?.totalFrames).toBe(30);+ expect(seen.audioInput?.fps).toEqual({ num: 30, den: 1 });});diff --git a/packages/producer/src/services/renderOrchestrator.ts b/packages/producer/src/services/renderOrchestrator.tsindex daaeb93a23..6e3a525498 100644--- a/packages/producer/src/services/renderOrchestrator.ts+++ b/packages/producer/src/services/renderOrchestrator.ts@@ -217,7 +217,7 @@ import {runEncodeStage,type EncodeStageInput,} from "./render/stages/encodeStage.js";-import { runAssembleStage } from "./render/stages/assembleStage.js";+import { recordLimiterAttenuation, runAssembleStage } from "./render/stages/assembleStage.js";import { shouldUseLayeredComposite } from "./hdrCompositor.js";import {resolveCaptureImageFormat,@@ -3372,7 +3372,8 @@ async function executeRenderPipeline(input: {projectDir,workDir,compiledDir,- duration: probeResult.duration,+ totalFrames: probeResult.totalFrames,+ fps: job.config.fps,ffmpegProcessTimeout: cfg.ffmpegProcessTimeout,audioGain: cfg.audioGain,audios: composition.audios,@@ -3384,6 +3385,7 @@ async function executeRenderPipeline(input: {);const { audioOutputPath, hasAudio } = audioResult;perfStages.audioProcessMs = audioResult.audioProcessMs;+ recordLimiterAttenuation(job, audioResult.audioLoweredDb);if (audioResult.audioError) {const audioFailures = audioResult.audioFailures ?? [];const failureOwner =diff --git a/packages/producer/src/utils/audioRegression.test.ts b/packages/producer/src/utils/audioRegression.test.tsindex 814bd622e7..539cb7035b 100644--- a/packages/producer/src/utils/audioRegression.test.ts+++ b/packages/producer/src/utils/audioRegression.test.ts@@ -87,12 +87,39 @@ describe.skipIf(!HAS_FFMPEG)("computeAudioResidualRmsDb", () => {join(tmp, "sine-880.wav"),);expect(result.ok).toBe(false);- // The residual of two uncorrelated unit-amplitude sines is roughly- // the sum of both signals at near-full level — typically around- // -3 dBFS in this resampled-stereo configuration.expect(result.overallDb).toBeGreaterThan(-30);});+ it("measures the whole unscaled difference when both streams end in silence", () => {+ for (const [name, source] of [+ ["prefix.wav", "aevalsrc=0.25|0.25:s=48000:d=1"],+ ["silence.wav", "anullsrc=r=48000:cl=stereo:d=1"],+ ] as const) {+ const generated = spawnSync(+ "ffmpeg",+ [+ "-nostdin",+ "-v",+ "error",+ "-f",+ "lavfi",+ "-i",+ source,+ "-af",+ "apad=whole_dur=2",+ "-c:a",+ "pcm_s16le",+ join(tmp, name),+ ],+ { encoding: "utf-8" },+ );+ expect(generated.status, generated.stderr).toBe(0);+ }+ const result = computeAudioResidualRmsDb(join(tmp, "prefix.wav"), join(tmp, "silence.wav"));+ expect(result.overallDb).toBeCloseTo(-15.0515, 2);+ expect(result.ok).toBe(false);+ });+it("reports ok=false when an input has no audio stream", () => {// A bare empty file: ffmpeg can't probe it, so the function reports// a parse failure (ok=false, NaN). Callers decide whether to treatdiff --git a/packages/producer/src/utils/audioRegression.ts b/packages/producer/src/utils/audioRegression.tsindex 44c4cfd418..3d4c6340b4 100644--- a/packages/producer/src/utils/audioRegression.ts+++ b/packages/producer/src/utils/audioRegression.ts@@ -185,14 +185,11 @@ export function computeAudioResidualRmsDb("-i",snapshot,"-filter_complex",- // Align both streams (resample + stereo + zero-based PTS), invert the- // snapshot, sum via amix, run astats. Avoids amix's `normalize`- // option (not available on ffmpeg 4.x) — we use volume=-1 + amix to- // subtract.+ // Measure the unscaled difference over the whole track, including before a silent tail.["[0:a]aresample=48000,pan=stereo|c0=c0|c1=c1,asetpts=N/SR/TB[a0]","[1:a]aresample=48000,pan=stereo|c0=c0|c1=c1,asetpts=N/SR/TB,volume=-1[a1]",- "[a0][a1]amix=inputs=2:duration=shortest:dropout_transition=0,astats=metadata=1:reset=1[out]",+ "[a0][a1]amix=inputs=2:duration=shortest:dropout_transition=0:normalize=0,astats=metadata=1:reset=0[out]",].join(";"),"-map","[out]",diff --git a/packages/producer/tests/speed-ramp/output/output.mp4 b/packages/producer/tests/speed-ramp/output/output.mp4index 4beb857d08..4cde8b2afc 100644--- a/packages/producer/tests/speed-ramp/output/output.mp4+++ b/packages/producer/tests/speed-ramp/output/output.mp4@@ -1,3 +1,3 @@version https://git-lfs.github.com/spec/v1-oid sha256:c74c635dae28d95137a446769a2c92359bf306ade885b8f705ad1610ba0f37fa-size 260303+oid sha256:91d58f16a9976a9b140ffe0c22ef4bd14b82e5bbc19c9cc6d4bbba9439c1ed22+size 260519