Skip to content

Commit a498cf2

Browse files
perf(producer): assemble in one pass and keep its disk use under one output copy (#5768)
* perf(producer): assemble in one pass and keep its disk use under one output copy The distributed assembler joined the chunks into a full-size file, muxed the audio into a second one and rewrote that again for faststart, so local disk peaked near four copies of the video and a 30-minute 1080p render came close to Lambda's 10 GB /tmp. The join, audio mux and faststart now run as one ffmpeg pass over the concat demuxer; the audio pad/trim takes its frame count from the chunks. Video and audio packets are unchanged. Assemble and the Lambda assemble handler log per-stage timings. * refactor(producer): split assemble into plan check, cfr re-encode and final write Keeps assemble under the complexity gate and drops an orphaned doc comment in chunkEncoder. No behaviour change.
1 parent c69fd2c commit a498cf2

5 files changed

Lines changed: 375 additions & 243 deletions

File tree

‎packages/aws-lambda/src/handler.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,21 +677,31 @@ async function handleAssembleV2(
677677
// `downloadAndMaterializePlanV2` materializes atomically. Audio is
678678
// assembler-only and lives at the familiar v1-compatible location.
679679
const audioPath = resolvePlanAudioPath(planDir);
680+
const chunksStarted = Date.now();
680681
const chunkPaths = await downloadChunkObjects(s3, event.ChunkS3Uris, work, event.Format);
682+
const chunksMs = Date.now() - chunksStarted;
681683
const finalOutput =
682684
event.Format === "png-sequence"
683685
? join(work, "output-frames")
684686
: join(work, `output${formatExtension(event.Format)}`);
685687
const result = await primitive(planDir, chunkPaths, audioPath, finalOutput, {
686688
cfr: event.Cfr === true,
687689
});
690+
const uploadStarted = Date.now();
688691
if (event.Format === "png-sequence") {
689692
const tarball = `${finalOutput}.tar.gz`;
690693
await tarDirectory(finalOutput, tarball);
691694
await uploadFileToS3(s3, tarball, event.OutputS3Uri, "application/gzip");
692695
} else {
693696
await uploadFileToS3(s3, finalOutput, event.OutputS3Uri);
694697
}
698+
logEvent({
699+
event: "assemble_stages",
700+
planMs: chunksStarted - started,
701+
chunksMs,
702+
assembleMs: result.durationMs,
703+
uploadMs: Date.now() - uploadStarted,
704+
});
695705
return {
696706
Action: "assemble",
697707
OutputS3Uri: event.OutputS3Uri,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ export interface MuxVideoWithAudioOptions extends Partial<
102102
* depend on the file extension alone.
103103
*/
104104
audioCodec?: "aac";
105+
/** Replaces `-i <videoPath>` (which is then unused), e.g. with a concat-demuxer input. */
106+
videoInputArgs?: readonly string[];
105107
/**
106108
* @deprecated No longer used. `-avoid_negative_ts` is never passed for
107109
* mp4/mov muxing (ffmpeg's `auto` default already resolves to `disabled`
@@ -110,7 +112,6 @@ export interface MuxVideoWithAudioOptions extends Partial<
110112
* source compatibility; it will be removed in a future major.
111113
*/
112114
preserveAudioPrimingEditList?: boolean;
113-
/** Hard cap copied audio to the already-encoded video's exact duration. */
114115
}
115116

116117
async function shouldCopyAacSidecar(
@@ -785,7 +786,7 @@ export async function muxVideoWithAudio(
785786
const isWebm = outputPath.endsWith(".webm");
786787
const isMov = outputPath.endsWith(".mov");
787788
const shouldCopyAudio = isWebm ? false : await shouldCopyAacSidecar(audioPath, config);
788-
const args = ["-i", videoPath, "-i", audioPath, "-c:v", "copy"];
789+
const args = [...(config?.videoInputArgs ?? ["-i", videoPath]), "-i", audioPath, "-c:v", "copy"];
789790

790791
if (isWebm) {
791792
args.push("-c:a", "libopus", "-b:a", "128k");

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

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ import {
2929
import { tmpdir } from "node:os";
3030
import { join } from "node:path";
3131
import {
32+
applyFaststart,
33+
muxVideoWithAudio,
3234
PROVENANCE_RENDERER_NAME,
3335
PROVENANCE_VERSION,
3436
readRenderProvenance,
3537
renderProvenanceArgs,
3638
} from "@hyperframes/engine";
39+
import { padOrTrimAudioToVideoFrameCount } from "../render/audioPadTrim.js";
3740
import type { ChunkSliceJson } from "../render/stages/freezePlan.js";
3841
import { assemble, cfrRateControlArgs } from "./assemble.js";
3942
import type { DistributedFormat } from "./shared.js";
@@ -91,14 +94,14 @@ function buildPlanDir(
9194
* independently concatenable because GOP === frame count and the first
9295
* frame is forced as a keyframe.
9396
*/
94-
function makeMp4Chunk(outputPath: string, frameCount: number): void {
97+
function makeMp4Chunk(outputPath: string, frameCount: number, size = "160x120"): void {
9598
const args = [
9699
"-v",
97100
"error",
98101
"-f",
99102
"lavfi",
100103
"-i",
101-
`testsrc=size=160x120:rate=30:duration=${frameCount / 30}`,
104+
`testsrc=size=${size}:rate=30:duration=${frameCount / 30}`,
102105
"-c:v",
103106
"libx264",
104107
"-preset",
@@ -229,6 +232,36 @@ function probeStream(
229232
return parsed.streams?.[0] ?? null;
230233
}
231234

235+
/** Per-packet pts, dts, duration, size and payload hash of one stream. */
236+
function streamPackets(path: string, stream: "v" | "a"): string {
237+
const result = spawnSync(
238+
"ffmpeg",
239+
["-v", "error", "-i", path, "-map", `0:${stream}:0`, "-c", "copy", "-f", "framemd5", "-"],
240+
{ stdio: "pipe" },
241+
);
242+
if (result.status !== 0) throw new Error(result.stderr.toString().slice(-400));
243+
return result.stdout.toString();
244+
}
245+
246+
/** Three 1 s chunks plus audio 0.5 s too long, so the trim branch runs. */
247+
function buildAudioFixture(): { planDir: string; chunkPaths: string[]; audioPath: string } {
248+
const chunks: ChunkSliceJson[] = [0, 1, 2].map((index) => ({
249+
index,
250+
startFrame: index * 30,
251+
endFrame: (index + 1) * 30,
252+
}));
253+
const planDir = buildPlanDir("mp4", chunks, 90, true);
254+
const chunkPaths = chunks.map(({ index }) => join(planDir, `chunk-${index}.mp4`));
255+
for (const chunkPath of chunkPaths) makeMp4Chunk(chunkPath, 30, "640x360");
256+
const audioPath = join(planDir, "audio.m4a");
257+
makeAacAudio(audioPath, 3.5);
258+
return { planDir, chunkPaths, audioPath };
259+
}
260+
261+
function diskKb(path: string): number {
262+
return Number(spawnSync("du", ["-sk", path]).stdout.toString().split("\t")[0]);
263+
}
264+
232265
describe("assemble()", () => {
233266
const TIMEOUT_MS = 30_000;
234267

@@ -484,6 +517,86 @@ describe("assemble()", () => {
484517
TIMEOUT_MS,
485518
);
486519

520+
it(
521+
"keeps local disk under the chunks plus one copy of the output",
522+
async () => {
523+
if (!hasFfmpeg || process.platform === "win32") return;
524+
const { planDir, chunkPaths, audioPath } = buildAudioFixture();
525+
const outputPath = join(planDir, "output.mp4");
526+
// Every intermediate is complete when its ffmpeg exits, so the disk
527+
// total at each exit bounds the peak.
528+
const diskLog = join(planDir, "disk.log");
529+
const wrapper = join(runRoot, `ffmpeg-disk-${Date.now()}.sh`);
530+
const realFfmpeg = process.env.HYPERFRAMES_FFMPEG_PATH?.trim() || "ffmpeg";
531+
writeFileSync(
532+
wrapper,
533+
`#!/bin/sh\n"${realFfmpeg}" "$@"\nstatus=$?\ndu -sk '${planDir}' | cut -f1 >> '${diskLog}'\nexit $status\n`,
534+
{ mode: 0o755 },
535+
);
536+
const baselineKb = diskKb(planDir);
537+
const previous = process.env.HYPERFRAMES_FFMPEG_PATH;
538+
process.env.HYPERFRAMES_FFMPEG_PATH = wrapper;
539+
try {
540+
await assemble(planDir, chunkPaths, audioPath, outputPath);
541+
} finally {
542+
if (previous === undefined) delete process.env.HYPERFRAMES_FFMPEG_PATH;
543+
else process.env.HYPERFRAMES_FFMPEG_PATH = previous;
544+
}
545+
const peakKb = Math.max(...readFileSync(diskLog, "utf8").trim().split("\n").map(Number));
546+
const outputKb = statSync(outputPath).size / 1024;
547+
expect(peakKb - baselineKb).toBeLessThan(outputKb * 1.5);
548+
},
549+
TIMEOUT_MS,
550+
);
551+
552+
it(
553+
"writes the same video and audio packets as separate concat, mux and faststart passes",
554+
async () => {
555+
if (!hasFfmpeg) return;
556+
const { planDir, chunkPaths, audioPath } = buildAudioFixture();
557+
const outputPath = join(planDir, "output.mp4");
558+
await assemble(planDir, chunkPaths, audioPath, outputPath);
559+
560+
const concatList = join(planDir, "legacy-list.txt");
561+
writeFileSync(concatList, chunkPaths.map((path) => `file '${path}'`).join("\n"));
562+
const concatPath = join(planDir, "legacy-concat.mp4");
563+
const concat = spawnSync("ffmpeg", [
564+
...["-v", "error", "-r", "30", "-f", "concat", "-safe", "0", "-i", concatList],
565+
...["-c", "copy", ...renderProvenanceArgs(concatPath), "-y", concatPath],
566+
]);
567+
expect(concat.status).toBe(0);
568+
const paddedPath = join(planDir, "legacy-audio.m4a");
569+
expect(
570+
(
571+
await padOrTrimAudioToVideoFrameCount({
572+
videoPath: concatPath,
573+
audioPath,
574+
outputPath: paddedPath,
575+
})
576+
).success,
577+
).toBe(true);
578+
const fps = { num: 30, den: 1 };
579+
const muxPath = join(planDir, "legacy-mux.mp4");
580+
const mux = await muxVideoWithAudio(
581+
concatPath,
582+
paddedPath,
583+
muxPath,
584+
undefined,
585+
{ audioCodec: "aac" },
586+
fps,
587+
);
588+
expect(mux.success).toBe(true);
589+
const legacyPath = join(planDir, "legacy.mp4");
590+
expect((await applyFaststart(muxPath, legacyPath, undefined, undefined, fps)).success).toBe(
591+
true,
592+
);
593+
594+
expect(streamPackets(outputPath, "v")).toBe(streamPackets(legacyPath, "v"));
595+
expect(streamPackets(outputPath, "a")).toBe(streamPackets(legacyPath, "a"));
596+
},
597+
TIMEOUT_MS,
598+
);
599+
487600
it(
488601
"cfr:true re-encodes for exact avg_frame_rate matching r_frame_rate",
489602
async () => {

0 commit comments

Comments
 (0)