Skip to content

Write HTML. Render video. Built for agents.

perf(producer): local renders check the audio peak in parallel segments, as cloud plans do (#5942)

Miguel Ángele319e644920c
diff --git a/packages/producer/src/services/render/audioPadTrim.ts b/packages/producer/src/services/render/audioPadTrim.ts
index 522361064b..deda963538 100644
--- a/packages/producer/src/services/render/audioPadTrim.ts
+++ b/packages/producer/src/services/render/audioPadTrim.ts
@@ -449,11 +449,7 @@ export async function constrainAacTruePeak(input: {
audioPath: input.audioPath,
targetDurationSeconds: input.targetDurationSeconds,
signal: input.signal,
- probeTruePeak: async (audioPath, signal) => {
- const { sampleRate } = await extractAudioMetadata(audioPath, { signal });
- const segments = { durationSeconds: input.targetDurationSeconds, sampleRate };
- return measureTruePeakDbfs(audioPath, { timeoutMs, signal, segments });
- },
+ probeTruePeak: (audioPath, signal) => measureTruePeakDbfs(audioPath, { timeoutMs, signal }),
runner: (args) => defaultRunFfmpeg(args, timeoutMs, input.signal),
});
}
diff --git a/packages/producer/src/services/render/audioTruePeak.fakeFfmpeg.test.ts b/packages/producer/src/services/render/audioTruePeak.fakeFfmpeg.test.ts
index 9f328542cf..be6bbbebe1 100644
--- a/packages/producer/src/services/render/audioTruePeak.fakeFfmpeg.test.ts
+++ b/packages/producer/src/services/render/audioTruePeak.fakeFfmpeg.test.ts
@@ -1,9 +1,14 @@
-import { afterEach, describe, expect, it, vi } from "vitest";
-import { runFfmpeg, type RunFfmpegResult } from "@hyperframes/engine";
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { extractAudioMetadata, runFfmpeg, type RunFfmpegResult } from "@hyperframes/engine";
+import { padOrTrimAudioToVideoFrameCount } from "./audioPadTrim.js";
import { measureTruePeakDbfs } from "./audioTruePeak.js";
vi.mock("@hyperframes/engine", async (importOriginal) => ({
...(await importOriginal<typeof import("@hyperframes/engine")>()),
+ extractAudioMetadata: vi.fn(),
runFfmpeg: vi.fn(),
}));
@@ -17,8 +22,20 @@ const ran = (success: boolean, stderr: string): RunFfmpegResult => ({
durationMs: 1,
terminationReason: "exit",
});
-const segments = { durationSeconds: 180, sampleRate: 48_000 };
+const probed = (durationSeconds: number, sampleRate: number) =>
+ vi.mocked(extractAudioMetadata).mockResolvedValue({
+ durationSeconds,
+ sampleRate,
+ channels: 2,
+ audioCodec: "aac",
+ });
+const scanFilters = () =>
+ vi
+ .mocked(runFfmpeg)
+ .mock.calls.map(([args]) => args.find((arg) => arg.includes("ebur128")))
+ .filter(Boolean);
+beforeEach(() => probed(180, 48_000));
afterEach(() => vi.mocked(runFfmpeg).mockReset());
describe("measureTruePeakDbfs against ffmpeg's stderr", () => {
@@ -27,6 +44,7 @@ describe("measureTruePeakDbfs against ffmpeg's stderr", () => {
opts?.onStderr?.(`${FRAME}\n`);
return ran(true, FRAME);
});
+ probed(30, 48_000);
await expect(measureTruePeakDbfs("in.m4a", { timeoutMs: 1000 })).rejects.toThrow(
"FFmpeg did not report AAC true peak",
);
@@ -34,12 +52,17 @@ describe("measureTruePeakDbfs against ffmpeg's stderr", () => {
it("measures a rate other than 48 kHz in one whole-file pass", async () => {
vi.mocked(runFfmpeg).mockResolvedValue(ran(true, `${FRAME}\n${SUMMARY}`));
- const peak = await measureTruePeakDbfs("in.m4a", {
- timeoutMs: 1000,
- segments: { ...segments, sampleRate: 44_100 },
- });
+ probed(180, 44_100);
+ const peak = await measureTruePeakDbfs("in.m4a", { timeoutMs: 1000 });
expect(peak).toBe(-3);
- expect(runFfmpeg).toHaveBeenCalledOnce();
+ expect(scanFilters()).toEqual(["ebur128=peak=true"]);
+ });
+
+ it("measures a file of unknown length in one whole-file pass", async () => {
+ vi.mocked(runFfmpeg).mockResolvedValue(ran(true, `${FRAME}\n${SUMMARY}`));
+ probed(Number.NaN, 48_000);
+ expect(await measureTruePeakDbfs("in.m4a", { timeoutMs: 1000 })).toBe(-3);
+ expect(scanFilters()).toEqual(["ebur128=peak=true"]);
});
it("stops the other segment scans when one fails", async () => {
@@ -51,7 +74,29 @@ describe("measureTruePeakDbfs against ffmpeg's stderr", () => {
if (signal) await new Promise((stop) => signal.addEventListener("abort", stop));
return ran(false, "stopped");
});
- await expect(measureTruePeakDbfs("in.m4a", { timeoutMs: 1000, segments })).rejects.toThrow();
+ await expect(measureTruePeakDbfs("in.m4a", { timeoutMs: 1000 })).rejects.toThrow();
expect(others.map((signal) => signal?.aborted)).toEqual([true, true]);
});
});
+
+describe("a local render's audio pad/trim", () => {
+ const dir = mkdtempSync(join(tmpdir(), "hf-pad-trim-peak-"));
+ afterAll(() => rmSync(dir, { recursive: true, force: true }));
+
+ it("checks the true peak in the same parallel segments as the distributed plan", async () => {
+ vi.mocked(runFfmpeg).mockResolvedValue(ran(true, `${FRAME}\n${SUMMARY}`));
+ const result = await padOrTrimAudioToVideoFrameCount({
+ videoPath: join(dir, "video.mp4"),
+ audioPath: join(dir, "audio.m4a"),
+ outputPath: join(dir, "audio.duration-normalized.m4a"),
+ probeVideoFrameInfo: async () => ({ frameCount: 5400, fpsNum: 30, fpsDen: 1 }),
+ probeAudioInfo: async () => ({ durationSeconds: 180 }),
+ });
+ expect(result.success).toBe(true);
+ expect(scanFilters()).toEqual([
+ "atrim=start_sample=0:end_sample=2880000,ebur128=peak=true",
+ "atrim=start_sample=2875200:end_sample=5760000,ebur128=peak=true",
+ "atrim=start_sample=5755200,ebur128=peak=true",
+ ]);
+ });
+});
diff --git a/packages/producer/src/services/render/audioTruePeak.test.ts b/packages/producer/src/services/render/audioTruePeak.test.ts
index abff9f5c19..0d5d4ccd31 100644
--- a/packages/producer/src/services/render/audioTruePeak.test.ts
+++ b/packages/producer/src/services/render/audioTruePeak.test.ts
@@ -1,4 +1,4 @@
-import { execFileSync } from "node:child_process";
+import { execFileSync, spawnSync } from "node:child_process";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -49,12 +49,14 @@ function monoWav(name: string, burstAt?: number): string {
}
async function bothWays(path: string): Promise<[number, number]> {
- const timeoutMs = 60_000;
- const segments = { durationSeconds: SECONDS, sampleRate: RATE };
- return Promise.all([
- measureTruePeakDbfs(path, { timeoutMs }),
- measureTruePeakDbfs(path, { timeoutMs, segments }),
- ]);
+ const { stderr } = spawnSync(
+ getFfmpegBinary(),
+ ["-nostdin", "-i", path, "-af", "ebur128=peak=true", "-f", "null", "-"],
+ { encoding: "utf8" },
+ );
+ const whole = [...stderr.matchAll(/^\s*Peak:\s*(\S+) dBFS$/gm)].at(-1)?.[1];
+ const segmented = await measureTruePeakDbfs(path, { timeoutMs: 60_000 });
+ return [whole === "-inf" ? Number.NEGATIVE_INFINITY : Number(whole), segmented];
}
describe.skipIf(!hasFfmpeg)("measureTruePeakDbfs in segments", () => {
diff --git a/packages/producer/src/services/render/audioTruePeak.ts b/packages/producer/src/services/render/audioTruePeak.ts
index 7b5bc2e202..9eec1f9014 100644
--- a/packages/producer/src/services/render/audioTruePeak.ts
+++ b/packages/producer/src/services/render/audioTruePeak.ts
@@ -1,4 +1,9 @@
-import { aacSegmentCount, describeFfmpegFailure, runFfmpeg } from "@hyperframes/engine";
+import {
+ aacSegmentCount,
+ describeFfmpegFailure,
+ extractAudioMetadata,
+ runFfmpeg,
+} from "@hyperframes/engine";
/** Peaks in dBFS as ffmpeg's ebur128 prints them (one decimal). */
interface TruePeakScan {
@@ -56,25 +61,25 @@ async function scanTruePeak(
/**
* The true peak one `ebur128=peak=true` pass over the whole file reports, measured in parallel
- * segments when the length is known and the rate is 48 kHz: the same digit, not an estimate.
+ * segments when the file is 48 kHz with a known length: the same digit, not an estimate.
*/
export async function measureTruePeakDbfs(
audioPath: string,
- options: {
- timeoutMs: number;
- signal?: AbortSignal;
- segments?: { durationSeconds: number; sampleRate: number };
- },
+ options: { timeoutMs: number; signal?: AbortSignal },
): Promise<number> {
- const { timeoutMs, signal, segments } = options;
+ const { timeoutMs, signal } = options;
const wholeFile = async () => (await scanTruePeak(audioPath, "", timeoutMs, signal)).peak;
+ const { durationSeconds, sampleRate } = await extractAudioMetadata(audioPath, { signal });
// ebur128 resamples any other rate to 48 kHz, which would move samples near each cut.
// It logs one peak per 100 ms block; its 4x oversampler reads 15 samples back, 16 ahead.
const block = 4800;
- const count = segments?.sampleRate === 48_000 ? aacSegmentCount(segments.durationSeconds) : 1;
- if (!segments || count === 1) return wholeFile();
+ const count =
+ sampleRate === 48_000 && Number.isFinite(durationSeconds)
+ ? aacSegmentCount(durationSeconds)
+ : 1;
+ if (count === 1) return wholeFile();
- const cut = (k: number) => k * Math.floor((segments.durationSeconds * 10) / count) * block;
+ const cut = (k: number) => k * Math.floor((durationSeconds * 10) / count) * block;
const peers = new AbortController();
const segmentSignal = signal ? AbortSignal.any([signal, peers.signal]) : peers.signal;
const scans = await Promise.all(