Skip to content

Commit a960f8a

Browse files
committed
perf(producer): assemble chunks that carry audio under one faststart index
Reads each chunk's moov, merges the sample tables into one moov at the front, and writes the chunks' mdat payloads after it unchanged: the same bytes storage can copy into place. The assembler no longer needs the plan audio for such plans.
1 parent 42b6b73 commit a960f8a

6 files changed

Lines changed: 598 additions & 23 deletions

File tree

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
import { encoderFailureError } from "../render/encoderInterruption.js";
5353
import type { ChunkSliceJson, LockedRenderConfig } from "../render/stages/freezePlan.js";
5454
import { DISTRIBUTED_RENDER_CAPABILITIES, readPlanProtocolV1 } from "./planProtocol.js";
55+
import { writeStitchedMp4 } from "./mp4Stitch.js";
5556
import { validatePlanV2MaterializedTarget } from "./planV2.js";
5657
import type { DistributedFormat } from "./shared.js";
5758

@@ -141,6 +142,16 @@ export async function assemble(
141142
return mergePngFrameDirs(chunkPaths, outputPath, plan.totalFrames, audioPath, start);
142143
}
143144

145+
if (chunksAreStitchedMp4(planDir, cfr)) {
146+
await writeStitchedMp4(chunkPaths, outputPath);
147+
return {
148+
outputPath,
149+
durationMs: Date.now() - start,
150+
framesEncoded: plan.totalFrames,
151+
fileSize: statSync(outputPath).size,
152+
};
153+
}
154+
144155
// ── 2b. mp4 / mov / webm: join + audio mux + faststart in one pass ─────
145156
const format = plan.dimensions.format;
146157
const cfrEncoder = cfr ? readCfrEncoder(planDir, format) : null;
@@ -155,9 +166,6 @@ export async function assemble(
155166
format,
156167
fps,
157168
fpsArg: fpsToFfmpegArg(fps),
158-
chunkSeconds: chunksCarryAudio(planDir)
159-
? plan.chunks.map((chunk) => ((chunk.endFrame - chunk.startFrame) * fps.den) / fps.num)
160-
: null,
161169
signal: options?.abortSignal,
162170
timeout: resolveConfig().ffmpegProcessTimeout,
163171
log,
@@ -200,10 +208,7 @@ export async function assemble(
200208
};
201209
}
202210

203-
function readAssemblePlan(
204-
planDir: string,
205-
chunkPaths: readonly string[],
206-
): PlanJsonForAssemble & { chunks: ChunkSliceJson[] } {
211+
function readAssemblePlan(planDir: string, chunkPaths: readonly string[]): PlanJsonForAssemble {
207212
// ── 1. Validate planDir manifest matches chunkPaths shape ──────────────
208213
const planJsonPath = join(planDir, "plan.json");
209214
const chunksJsonPath = join(planDir, "meta", "chunks.json");
@@ -230,7 +235,7 @@ function readAssemblePlan(
230235
}
231236
}
232237

233-
return { ...plan, chunks };
238+
return plan;
234239
}
235240

236241
interface JoinContext {
@@ -242,15 +247,14 @@ interface JoinContext {
242247
timeout: number;
243248
log: ProducerLogger;
244249
timed: <T>(stage: string, run: () => Promise<T>) => Promise<T>;
245-
chunkSeconds: number[] | null;
246250
}
247251

248252
// A one-entry concat list ignores the input-side `-r` (observed `359/12` in the
249253
// container), so a single file is read directly; every write also sets output `-r`.
250254
function videoInputArgs(ctx: JoinContext, videoSources: readonly string[]): string[] {
251255
return videoSources.length === 1
252256
? ["-i", videoSources[0]!]
253-
: concatDemuxerInput(videoSources, ctx.workDir, ctx.fpsArg, ctx.chunkSeconds);
257+
: concatDemuxerInput(videoSources, ctx.workDir, ctx.fpsArg);
254258
}
255259

256260
// ── 2c. Optional exact-CFR re-encode ──────────────────────────────────
@@ -347,18 +351,11 @@ function concatDemuxerInput(
347351
chunkPaths: readonly string[],
348352
workDir: string,
349353
fpsArg: string,
350-
chunkSeconds: readonly number[] | null,
351354
): string[] {
352355
// ffmpeg's concat list escapes a single quote as `'\''`.
353356
const listPath = join(workDir, "concat-list.txt");
354-
// Chunks that carry audio: read only their video (track 1), and end each chunk at its last frame
355-
// rather than after its audio, which starts with the AAC priming and can run one packet longer.
356-
const head = chunkSeconds ? ["stream", "exact_stream_id 1"] : [];
357-
const body = chunkPaths.map((path, i) => {
358-
const line = `file '${path.replace(/'/g, "'\\''")}'`;
359-
return chunkSeconds ? `${line}\nduration ${chunkSeconds[i]!.toFixed(6)}` : line;
360-
});
361-
writeFileSync(listPath, `${[...head, ...body].join("\n")}\n`, "utf-8");
357+
const body = chunkPaths.map((path) => `file '${path.replace(/'/g, "'\\''")}'`).join("\n");
358+
writeFileSync(listPath, `${body}\n`, "utf-8");
362359
return ["-r", fpsArg, "-f", "concat", "-safe", "0", "-i", listPath];
363360
}
364361

@@ -393,6 +390,15 @@ async function runAssembleFfmpeg(
393390
}
394391
}
395392

393+
/** Chunks that carry their audio are stitched under one index, the same bytes as the S3 stitch. */
394+
function chunksAreStitchedMp4(planDir: string, cfr: boolean): boolean {
395+
if (!chunksCarryAudio(planDir)) return false;
396+
if (cfr) {
397+
throw new Error("[assemble] cfr=true cannot re-encode a plan whose mp4 chunks carry audio");
398+
}
399+
return true;
400+
}
401+
396402
function chunksCarryAudio(planDir: string): boolean {
397403
const encoderJsonPath = join(planDir, "meta", "encoder.json");
398404
if (!existsSync(encoderJsonPath)) return false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe.skipIf(!hasFfmpeg)("chunks that carry their audio", () => {
7979
["three", [0, 47, 90, 91]],
8080
["one", [0, 91]],
8181
] as const)(
82-
"%s chunk(s) join through today's assemble like video-only chunks",
82+
"%s chunk(s) assemble into the frames and audio of today's video-only join",
8383
async (name, edges) => {
8484
const CHUNKS = edges.slice(1).map((end, index) => ({
8585
index,
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { describe, expect, it } from "bun:test";
2+
import { buildStitchedHeader, readMp4Layout, readTracks } from "./mp4Stitch.js";
3+
4+
function box(type: string, ...body: Buffer[]): Buffer {
5+
const header = Buffer.alloc(8);
6+
header.writeUInt32BE(8 + body.reduce((n, part) => n + part.length, 0));
7+
header.write(type, 4, "latin1");
8+
return Buffer.concat([header, ...body]);
9+
}
10+
11+
function u32(...values: number[]): Buffer {
12+
const buf = Buffer.alloc(values.length * 4);
13+
values.forEach((value, i) => buf.writeUInt32BE(value, i * 4));
14+
return buf;
15+
}
16+
const full = (type: string, ...body: Buffer[]) => box(type, u32(0), ...body);
17+
// Version 0 mvhd/tkhd/mdhd with only the fields the stitcher reads filled in.
18+
const header = (type: string, timescaleOrTrack: number, duration: number) =>
19+
type === "tkhd"
20+
? full(type, u32(0, 0, timescaleOrTrack, 0, duration), Buffer.alloc(60))
21+
: full(type, u32(0, 0, timescaleOrTrack, duration), Buffer.alloc(type === "mvhd" ? 80 : 4));
22+
23+
interface FakeTrack {
24+
handler: "vide" | "soun";
25+
timescale: number;
26+
mediaTime: number;
27+
durations: number[];
28+
sizes: number[];
29+
sync?: number[];
30+
}
31+
32+
/** A chunk file shaped like ffmpeg's: ftyp, mdat with samples interleaved one per mp4 chunk, moov. */
33+
function fakeChunk(tag: number, tracks: FakeTrack[]): { file: Buffer; samples: Buffer[][] } {
34+
const samples = tracks.map((track, t) =>
35+
track.sizes.map((size, k) => Buffer.alloc(size, (tag * 61 + t * 17 + k * 5 + 1) & 255)),
36+
);
37+
const ftyp = box("ftyp", Buffer.from("isom\0\0\x02\0isom", "latin1"));
38+
const offsets = tracks.map(() => [] as number[]);
39+
const payload: Buffer[] = [];
40+
let at = ftyp.length + 8;
41+
for (let k = 0; k < Math.max(...tracks.map((track) => track.sizes.length)); k++) {
42+
samples.forEach((track, t) => {
43+
if (!track[k]) return;
44+
offsets[t]!.push(at);
45+
payload.push(track[k]!);
46+
at += track[k]!.length;
47+
});
48+
}
49+
const traks = tracks.map((track, t) => {
50+
const duration = track.durations.reduce((n, d) => n + d, 0);
51+
const stbl = box(
52+
"stbl",
53+
full("stsd", u32(1), box(track.handler === "vide" ? "avc1" : "mp4a", Buffer.alloc(8, t))),
54+
full("stts", u32(track.durations.length, ...track.durations.flatMap((d) => [1, d]))),
55+
...(track.sync ? [full("stss", u32(track.sync.length, ...track.sync))] : []),
56+
full("stsc", u32(1, 1, 1, 1)),
57+
full("stsz", u32(0, track.sizes.length, ...track.sizes)),
58+
full("stco", u32(offsets[t]!.length, ...offsets[t]!)),
59+
);
60+
return box(
61+
"trak",
62+
header("tkhd", t + 1, 0),
63+
box("edts", full("elst", u32(1, 0, track.mediaTime, 0x10000))),
64+
box(
65+
"mdia",
66+
header("mdhd", track.timescale, duration),
67+
full("hdlr", u32(0), Buffer.from(track.handler), Buffer.alloc(13)),
68+
box("minf", stbl),
69+
),
70+
);
71+
});
72+
const moov = box("moov", header("mvhd", 1000, 0), ...traks);
73+
return { file: Buffer.concat([ftyp, box("mdat", ...payload), moov]), samples };
74+
}
75+
76+
const memoryRange = (file: Buffer) => async (start: number, end: number) =>
77+
file.subarray(start, end);
78+
79+
describe("buildStitchedHeader", () => {
80+
const video = (sizes: number[], mediaTime = 0): FakeTrack => ({
81+
handler: "vide",
82+
timescale: 15360,
83+
mediaTime,
84+
durations: sizes.map(() => 512),
85+
sizes,
86+
sync: [1],
87+
});
88+
const audio = (durations: number[], mediaTime: number): FakeTrack => ({
89+
handler: "soun",
90+
timescale: 48000,
91+
mediaTime,
92+
durations,
93+
sizes: durations.map((_, k) => 40 + k),
94+
});
95+
96+
it("indexes every chunk's samples at their place after the header", async () => {
97+
const chunks = [
98+
fakeChunk(0, [video([500, 120, 130]), audio([1024, 1024, 1024, 1024], 1024)]),
99+
fakeChunk(1, [video([480, 110]), audio([1024, 1024, 640], 0)]),
100+
];
101+
const layouts = await Promise.all(chunks.map((c) => readMp4Layout(memoryRange(c.file))));
102+
const header = buildStitchedHeader(layouts);
103+
const file = Buffer.concat([
104+
header,
105+
...layouts.map((l, i) => chunks[i]!.file.subarray(l.payloadStart, l.payloadEnd)),
106+
]);
107+
108+
const stitched = await readMp4Layout(memoryRange(file));
109+
expect(stitched.payloadStart).toBe(header.length);
110+
expect(stitched.payloadEnd).toBe(file.length);
111+
const tracks = readTracks(stitched.moov);
112+
tracks.forEach((track, t) => {
113+
const read: Buffer[] = [];
114+
let k = 0;
115+
track.chunkOffsets.forEach((offset, c) => {
116+
for (let s = 0, at = offset; s < track.samplesPerChunk[c]!; s++) {
117+
read.push(file.subarray(at, at + track.sizes[k]!));
118+
at += track.sizes[k++]!;
119+
}
120+
});
121+
expect(read).toEqual(chunks.flatMap((chunk) => chunk.samples[t]!));
122+
});
123+
expect(tracks.map((track) => [track.handler, track.mediaTime])).toEqual([
124+
["vide", 0],
125+
["soun", 1024],
126+
]);
127+
expect(tracks[0]!.sync).toEqual([true, false, false, true, false]);
128+
expect(tracks[1]!.durations).toEqual([1024, 1024, 1024, 1024, 1024, 1024, 640]);
129+
130+
const moov = stitched.moov;
131+
const after = (type: string, from = 0) => moov.indexOf(type, from, "latin1") + 4;
132+
// Only audio needs an edit list (version 1): it shows its media minus the 1024-sample
133+
// priming, (6784 - 1024) / 48000 s = 120 ms in the 1000-tick movie timescale.
134+
const audioElst = after("elst");
135+
expect(moov.indexOf("elst", audioElst, "latin1")).toBe(-1);
136+
expect(audioElst).toBeGreaterThan(after("vide"));
137+
expect(Number(moov.readBigUInt64BE(audioElst + 8))).toBe(120);
138+
expect(Number(moov.readBigInt64BE(audioElst + 16))).toBe(1024);
139+
expect(moov.readUInt32BE(after("mdhd", audioElst) + 16)).toBe(6784);
140+
expect(moov.readUInt32BE(after("mvhd") + 16)).toBe(167);
141+
});
142+
143+
it("refuses a chunk whose video starts late, as B-frames make it", async () => {
144+
const chunks = [fakeChunk(0, [video([500, 120])]), fakeChunk(1, [video([480, 110], 512)])];
145+
const layouts = await Promise.all(chunks.map((c) => readMp4Layout(memoryRange(c.file))));
146+
expect(() => buildStitchedHeader(layouts)).toThrow("chunk 1 video starts at media time 512");
147+
});
148+
});

0 commit comments

Comments
 (0)