fix(producer): preserve the output container in segmented capture

This commit is contained in:
Miguel Angel Simon Sierra
2026-10-07 01:38:02 -07:00
parent dbb8222c8d
commit 4f7ef12ef1
5 changed files with 139 additions and 18 deletions
@@ -27,6 +27,7 @@ const hashInput: SegmentPlanHashInput = {
bitrate: undefined,
pixelFormat: "yuv420p",
imageFormat: "jpeg",
containerExtension: ".mp4",
useGpu: false,
outputWidth: 1920,
outputHeight: 1080,
@@ -34,6 +35,12 @@ const hashInput: SegmentPlanHashInput = {
};
describe("computeSegmentPlanHash", () => {
it("changes when the segment container changes even with the same codec", () => {
const mp4 = computeSegmentPlanHash(hashInput);
expect(computeSegmentPlanHash({ ...hashInput, containerExtension: ".webm" })).not.toBe(mp4);
expect(computeSegmentPlanHash({ ...hashInput, containerExtension: ".mov" })).not.toBe(mp4);
});
it("is stable and 16 hex chars", () => {
const a = computeSegmentPlanHash(hashInput);
expect(a).toMatch(/^[0-9a-f]{16}$/);
@@ -182,6 +189,24 @@ describe("validateCompletedSegments", () => {
expect((await validateCompletedSegments(manifest, "h", async () => 3)).size).toBe(0);
});
it("rejects completed segments when the resolved container changes", async () => {
dir = mkdtempSync(join(tmpdir(), "hf-seg-container-"));
const good = join(dir, "segment_00000.mp4");
writeFileSync(good, "xx");
const manifest: SegmentManifest = {
version: 1,
planHash: computeSegmentPlanHash(hashInput),
totalFrames: 3,
segmentFrames: 3,
completed: [{ index: 0, startFrame: 0, endFrame: 3, path: good, bytes: 2, completedAt: "" }],
};
expect((await validateCompletedSegments(manifest, manifest.planHash, async () => 3)).size).toBe(
1,
);
const changed = computeSegmentPlanHash({ ...hashInput, containerExtension: ".webm" });
expect((await validateCompletedSegments(manifest, changed, async () => 3)).size).toBe(0);
});
it("skips nothing when the plan hash differs", async () => {
// The entry is otherwise perfectly reusable — present, right size, right
// frame count — so only the hash check can reject it. With an empty
@@ -41,6 +41,7 @@ export interface SegmentPlanHashInput {
bitrate: string | undefined;
pixelFormat: string | undefined;
imageFormat: string;
containerExtension: string;
/** Hardware encoder or not: h264_nvenc and libx264 both report codec "h264" but produce different bytes. */
useGpu: boolean;
/** Device-scaled capture size; `width`/`height` are the composition's CSS size. */
@@ -67,6 +68,7 @@ export function computeSegmentPlanHash(input: SegmentPlanHashInput): string {
input.bitrate ?? "",
input.pixelFormat ?? "",
input.imageFormat,
input.containerExtension,
input.useGpu ? "gpu" : "cpu",
`${input.outputWidth}x${input.outputHeight}`,
input.motionBlur,
@@ -250,11 +250,94 @@ function fakeStageInput(overrides: { totalFrames: number }) {
describe("segmentOutputPath", () => {
it("zero-pads so lexical order equals frame order", () => {
expect(segmentOutputPath("/w/segments", 7)).toBe("/w/segments/segment_00007.mp4");
expect(segmentOutputPath("/w/segments", 7, ".mp4")).toBe("/w/segments/segment_00007.mp4");
});
});
describe("runCaptureSegmentedStage", () => {
it.each(["mov", "webm"])(
"uses the resolved %s container for capture, retry, manifest and concat",
async (format) => {
const segmentDir = join(fixtureRoot, `container-${format}`);
const expected = [
join(segmentDir, `segment_00000.${format}`),
join(segmentDir, `segment_00001.${format}`),
];
const spawned: string[] = [];
const removed: string[] = [];
const completed: string[] = [];
let failed = false;
let nextSession = 0;
const concat = mock(async () => ({ success: true as const }));
const input = fakeStageInput({ totalFrames: 4 });
const result = await runCaptureSegmentedStage({
...input,
outputFormat: format,
videoOnlyPath: join(fixtureRoot, `video-only.${format}`),
segmentFrames: 2,
segmentDir,
sessionFactory: { create: async () => fakeSession(nextSession++) },
onSegmentComplete: (entry) => completed.push(entry.path),
deps: {
spawnEncoder: mock(async (path: string) => {
spawned.push(path);
return okEncoder();
}),
captureFrame: mock(async (_session: unknown, frame: number) => {
if (frame === 2 && !failed) {
failed = true;
throw new Error("Protocol error (Page.captureScreenshot): Target closed");
}
return { buffer: Buffer.alloc(1) };
}),
removeFile: (path) => removed.push(path),
closeSession: mock(async () => {}),
concat,
},
});
expect(result.success).toBe(true);
if (!result.success) throw new Error("Expected segmented capture to complete");
expect(spawned).toEqual([expected[0], expected[1], expected[1]]);
expect(removed).toEqual([expected[1]!]);
expect(completed).toEqual(expected);
expect(result.segmentPaths).toEqual(expected);
expect(concat).toHaveBeenCalledWith(
expected,
join(fixtureRoot, `video-only.${format}`),
undefined,
input.cfg,
);
},
);
it.each(["mov", "webm"])(
"resumes completed %s segments without capturing again",
async (format) => {
const segmentDir = join(fixtureRoot, `resume-${format}`);
const concat = mock(async () => ({ success: true as const }));
const create = mock(async () => fakeSession(0));
const input = fakeStageInput({ totalFrames: 2 });
const result = await runCaptureSegmentedStage({
...input,
outputFormat: format,
videoOnlyPath: join(fixtureRoot, `video-only.${format}`),
segmentFrames: 1,
segmentDir,
completedSegments: new Set([0, 1]),
sessionFactory: { create },
deps: { concat },
});
expect(result.success).toBe(true);
expect(create).not.toHaveBeenCalled();
expect(concat).toHaveBeenCalledWith(
[join(segmentDir, `segment_00000.${format}`), join(segmentDir, `segment_00001.${format}`)],
join(fixtureRoot, `video-only.${format}`),
undefined,
input.cfg,
);
},
);
it("spawns one encoder per segment, writes every frame once in order, then concats", async () => {
const written: Array<{ segment: string; frame: number }> = [];
const encoders: string[] = [];
@@ -287,9 +370,9 @@ describe("runCaptureSegmentedStage", () => {
expect(result.success).toBe(true);
expect(encoders).toEqual([
segmentOutputPath(join(fixtureRoot, "segments"), 0),
segmentOutputPath(join(fixtureRoot, "segments"), 1),
segmentOutputPath(join(fixtureRoot, "segments"), 2),
segmentOutputPath(join(fixtureRoot, "segments"), 0, ".mp4"),
segmentOutputPath(join(fixtureRoot, "segments"), 1, ".mp4"),
segmentOutputPath(join(fixtureRoot, "segments"), 2, ".mp4"),
]);
// Every frame exactly once, in order, across the segment boundaries.
expect(written.map((w) => w.frame)).toEqual([0, 1, 2, 3, 4, 5, 6]);
@@ -347,14 +430,17 @@ describe("runCaptureSegmentedStage", () => {
expect(result.success).toBe(true);
// Segment 0's frames are not re-captured, and no encoder is spawned for it.
expect(captured).toEqual([3, 4, 5, 6, 7, 8]);
expect(spawned).toEqual([segmentOutputPath(stableDir, 1), segmentOutputPath(stableDir, 2)]);
expect(spawned).toEqual([
segmentOutputPath(stableDir, 1, ".mp4"),
segmentOutputPath(stableDir, 2, ".mp4"),
]);
expect(completed).toEqual([1, 2]);
// The skipped segment is still concatenated, in order.
expect(concat).toHaveBeenCalledWith(
[
segmentOutputPath(stableDir, 0),
segmentOutputPath(stableDir, 1),
segmentOutputPath(stableDir, 2),
segmentOutputPath(stableDir, 0, ".mp4"),
segmentOutputPath(stableDir, 1, ".mp4"),
segmentOutputPath(stableDir, 2, ".mp4"),
],
join(fixtureRoot, "video-only.mp4"),
undefined,
@@ -381,7 +467,7 @@ describe("runCaptureSegmentedStage", () => {
// The probe handed in by the orchestrator is still closed on this path.
expect(closeSession).toHaveBeenCalledWith(input.probeSession);
expect(concat).toHaveBeenCalledWith(
[0, 1, 2].map((i) => segmentOutputPath(stableDir, i)),
[0, 1, 2].map((i) => segmentOutputPath(stableDir, i, ".mp4")),
join(fixtureRoot, "video-only.mp4"),
undefined,
expect.anything(),
@@ -581,7 +667,7 @@ describe("runCaptureSegmentedStage", () => {
// Segment 1 restarts from its first frame on the fresh session.
expect(captured).toEqual([0, 1, 2, 3]);
// The partial segment file is deleted so a later resume cannot see it.
expect(removed).toEqual([segmentOutputPath(join(fixtureRoot, "retry"), 1)]);
expect(removed).toEqual([segmentOutputPath(join(fixtureRoot, "retry"), 1, ".mp4")]);
expect(next).toBe(2);
});
@@ -677,7 +763,7 @@ describe("runCaptureSegmentedStage", () => {
expect(perWorkerSessions.size).toBe(3);
const concatInputs = concat.mock.calls[0]?.[0];
expect(concatInputs).toEqual(
Array.from({ length: 7 }, (_, i) => segmentOutputPath(stableDir, i)),
Array.from({ length: 7 }, (_, i) => segmentOutputPath(stableDir, i, ".mp4")),
);
});
@@ -2,7 +2,7 @@
* Segmented capture (spec §5 Phase 2a).
*
* The frame range is split into fixed-size segments; each one gets its own
* closed-GOP ffmpeg process writing `segments/segment_NNNNN.mp4`, and the
* closed-GOP ffmpeg process writing segments in the output container, and the
* segments are concat-copied into the video-only file the assemble stage
* muxes. Against the single-encoder streaming path this buys two things on a
* long render: scratch is encoded video rather than one open pipe's worth of
@@ -13,7 +13,7 @@
* recycling and retry are 2c, multi-worker is 2d.
*/
import { existsSync, mkdirSync, rmSync, statSync } from "node:fs";
import { join } from "node:path";
import { extname, join } from "node:path";
import {
captureFrameToBuffer,
closeCaptureSession,
@@ -139,8 +139,8 @@ export type CaptureSegmentedStageResult =
| { success: false };
/** Zero-padded so lexical order equals frame order in the concat list and on disk. */
export function segmentOutputPath(segmentDir: string, index: number): string {
return join(segmentDir, `segment_${String(index).padStart(5, "0")}.mp4`);
export function segmentOutputPath(segmentDir: string, index: number, extension: string): string {
return join(segmentDir, `segment_${String(index).padStart(5, "0")}${extension}`);
}
/**
@@ -178,6 +178,7 @@ interface SegmentCaptureContext {
totalFrames: number;
segmentCount: number;
segmentDir: string;
segmentExtension: string;
skipped: number;
streamingEncoderOptions: StreamingEncoderOptions;
spawnEncoder: SegmentedStageDeps["spawnEncoder"];
@@ -229,7 +230,7 @@ async function captureSegmentFrames(
/** Capture one segment into its own encoder. Returns the encoder's encode ms. */
async function captureSegment(ctx: SegmentCaptureContext, segment: SegmentSlice): Promise<number> {
const segmentPath = segmentOutputPath(ctx.segmentDir, segment.index);
const segmentPath = segmentOutputPath(ctx.segmentDir, segment.index, ctx.segmentExtension);
let encoder: StreamingEncoder;
try {
encoder = await ctx.spawnEncoder(
@@ -378,6 +379,7 @@ interface SegmentRun {
segments: readonly SegmentSlice[];
pending: readonly SegmentSlice[];
segmentDir: string;
segmentExtension: string;
skipSize: number;
workerCount: number;
recycleEvery: number;
@@ -399,6 +401,7 @@ async function createSegmentWorker(run: SegmentRun, id: number): Promise<Segment
totalFrames: input.totalFrames,
segmentCount: run.segments.length,
segmentDir: run.segmentDir,
segmentExtension: run.segmentExtension,
skipped: run.skipSize,
streamingEncoderOptions: input.streamingEncoderOptions,
spawnEncoder: deps.spawnEncoder,
@@ -488,7 +491,7 @@ async function runOneSegment(
worker: SegmentWorker,
segment: SegmentSlice,
): Promise<"done" | "fallback"> {
const segmentPath = segmentOutputPath(run.segmentDir, segment.index);
const segmentPath = segmentOutputPath(run.segmentDir, segment.index, run.segmentExtension);
if (run.recycleEvery > 0 && worker.sessionSegments >= run.recycleEvery) {
await recycleWorkerSession(run, worker, "cadence");
}
@@ -546,6 +549,7 @@ export async function runCaptureSegmentedStage(
const segments = planSegments(input.totalFrames, input.segmentFrames);
const segmentDir = input.segmentDir ?? join(input.workDir, "segments");
const segmentExtension = extname(input.videoOnlyPath);
mkdirSync(segmentDir, { recursive: true });
const skip = input.completedSegments ?? new Set<number>();
const pending = segments.filter((s) => !skip.has(s.index));
@@ -557,6 +561,7 @@ export async function runCaptureSegmentedStage(
segments,
pending,
segmentDir,
segmentExtension,
skipSize: skip.size,
workerCount: Math.max(1, input.workerCount ?? 1),
recycleEvery: input.browserRecycleEverySegments ?? 0,
@@ -565,7 +570,9 @@ export async function runCaptureSegmentedStage(
const queue = createSegmentQueue(pending);
// Built from the plan, not from completion order: workers finish out of
// order, so the concat list must not depend on who finished when.
const segmentPaths = segments.map((s) => segmentOutputPath(segmentDir, s.index));
const segmentPaths = segments.map((s) =>
segmentOutputPath(segmentDir, s.index, segmentExtension),
);
if (pending.length === 0) return concatResumedRun(run, segmentPaths);
let fellBackToStreaming = false;
@@ -4549,6 +4549,7 @@ async function executeRenderPipeline(input: {
bitrate: effectiveBitrate,
pixelFormat: preset.pixelFormat,
imageFormat: segmentImageFormat,
containerExtension: videoExt,
useGpu: job.config.useGpu === true,
// Device-scaled: the capture buffer, not the CSS composition size.
outputWidth: captureCompositionWidth ?? width,