Skip to content

Commit ddb59d3

Browse files
committed
fix(producer): materialize sparse video directories
1 parent 45b458c commit ddb59d3

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

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

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
mkdirSync,
55
mkdtempSync,
66
readFileSync,
7+
readdirSync,
78
rmSync,
89
statSync,
910
symlinkSync,
@@ -58,6 +59,8 @@ function createV1Plan(
5859
videoId?: string;
5960
videoSrcPath?: string;
6061
framePattern?: string;
62+
videoStart?: number;
63+
videoEnd?: number;
6164
},
6265
): string {
6366
const {
@@ -70,6 +73,8 @@ function createV1Plan(
7073
videoId = "hero",
7174
videoSrcPath = "/fixture/hero.mp4",
7275
framePattern = "frame_%05d.jpg",
76+
videoStart = 0,
77+
videoEnd = 1,
7378
} = options ?? {};
7479
const planDir = join(root, "v1");
7580
mkdirSync(join(planDir, "compiled"), { recursive: true });
@@ -98,8 +103,8 @@ function createV1Plan(
98103
{
99104
id: videoId,
100105
src: "hero.mp4",
101-
start: 0,
102-
end: 1,
106+
start: videoStart,
107+
end: videoEnd,
103108
mediaStart: 0,
104109
loop: false,
105110
hasAudio: false,
@@ -328,6 +333,41 @@ describe("Plan v2 manifest", () => {
328333
);
329334
});
330335

336+
it("materializes empty video directories for chunks where the video is inactive", () => {
337+
const root = tempPath("hf-plan-v2-inactive-video-directory-");
338+
const v1 = createV1Plan(root, {
339+
video: true,
340+
videoStart: 1 / 30,
341+
videoEnd: 1,
342+
});
343+
const result = createPlanV2FromV1(v1, join(root, "v2"));
344+
const manifest = readPlanV2Manifest(result.planDir);
345+
const chunk0Artifacts = listPlanV2ArtifactsForTarget(manifest, {
346+
role: "chunk",
347+
chunkIndex: 0,
348+
});
349+
const chunk1Artifacts = listPlanV2ArtifactsForTarget(manifest, {
350+
role: "chunk",
351+
chunkIndex: 1,
352+
});
353+
const chunk0Dir = join(root, "chunk-0");
354+
const chunk1Dir = join(root, "chunk-1");
355+
356+
expect(chunk0Artifacts.some((artifact) => artifact.path.startsWith("video-frames/hero/"))).toBe(
357+
false,
358+
);
359+
expect(
360+
chunk1Artifacts.some((artifact) => artifact.path === "video-frames/hero/frame_00001.jpg"),
361+
).toBe(true);
362+
363+
materializePlanV2Target(result.planDir, { role: "chunk", chunkIndex: 0 }, chunk0Dir);
364+
materializePlanV2Target(result.planDir, { role: "chunk", chunkIndex: 1 }, chunk1Dir);
365+
366+
expect(existsSync(join(chunk0Dir, "video-frames", "hero"))).toBe(true);
367+
expect(readdirSync(join(chunk0Dir, "video-frames", "hero"))).toEqual([]);
368+
expect(existsSync(join(chunk1Dir, "video-frames", "hero", "frame_00001.jpg"))).toBe(true);
369+
});
370+
331371
const partialColorSpaceCases = [
332372
{
333373
name: "matrix-only",
@@ -438,6 +478,15 @@ describe("Plan v2 manifest", () => {
438478
});
439479
}
440480

481+
it("rejects an extracted video identifier that escapes the video-frame root", () => {
482+
const root = tempPath("hf-plan-v2-unsafe-video-id-");
483+
const v1 = createV1Plan(root, { video: true, videoId: "../escape" });
484+
485+
expect(() => createPlanV2FromV1(v1, join(root, "v2"))).toThrow(
486+
'unsafe extracted video id: "../escape"',
487+
);
488+
});
489+
441490
it("falls back to the full source frame pack when video metadata is absent", () => {
442491
const root = tempPath("hf-plan-v2-video-fallback-");
443492
const result = createPlanV2FromV1(

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ function isExtractionCacheCompleteSentinelPath(path: string): boolean {
248248
);
249249
}
250250

251+
function resolveExtractedVideoOutputDir(planDir: string, videoId: string): string {
252+
const videoRoot = resolve(planDir, "video-frames");
253+
const outputDir = resolve(videoRoot, videoId);
254+
if (outputDir === videoRoot || !outputDir.startsWith(`${videoRoot}${sep}`)) {
255+
throw new PlanV2IntegrityError(`unsafe extracted video id: ${JSON.stringify(videoId)}`);
256+
}
257+
return outputDir;
258+
}
259+
251260
// This is the fail-safe policy table for every v1 artifact class. Keeping the
252261
// branches together makes new artifact classes visibly fall through to both roles.
253262
// fallow-ignore-next-line complexity
@@ -279,7 +288,7 @@ function artifactTargets(
279288

280289
function listVideoFramePaths(planV1Dir: string, videos: PlanVideosJson): ExtractedFrames[] {
281290
return videos.extracted.map((video) => {
282-
const outputDir = join(planV1Dir, "video-frames", video.videoId);
291+
const outputDir = resolveExtractedVideoOutputDir(planV1Dir, video.videoId);
283292
const frameNames = readdirSync(outputDir).sort();
284293
const framePaths = new Map<number, string>();
285294
for (const frameName of frameNames) {
@@ -403,6 +412,15 @@ function parsePlanVideosJson(value: unknown): PlanVideosJson {
403412
return { videos, extracted };
404413
}
405414

415+
function materializeExtractedVideoDirectories(planDir: string): void {
416+
const videosPath = join(planDir, PLAN_VIDEOS_META_RELATIVE_PATH);
417+
if (!existsSync(videosPath)) return;
418+
const videos = parsePlanVideosJson(readJsonFile(videosPath, PLAN_VIDEOS_META_RELATIVE_PATH));
419+
for (const video of videos.extracted) {
420+
mkdirSync(resolveExtractedVideoOutputDir(planDir, video.videoId), { recursive: true });
421+
}
422+
}
423+
406424
function parseChunkSlices(value: unknown): ChunkSliceJson[] {
407425
if (!Array.isArray(value)) {
408426
throw new PlanV2IntegrityError("meta/chunks.json must be an array");
@@ -919,6 +937,12 @@ export function materializePlanV2Target(
919937
mkdirSync(dirname(destinationPath), { recursive: true });
920938
copyFileSync(sourcePath, destinationPath);
921939
}
940+
// Plan v2 transports only files, so a chunk where a video is inactive has
941+
// no selected frame artifact from which to create its per-video directory.
942+
// renderChunk consumes a v1-compatible layout and intentionally validates
943+
// every extracted-video directory from meta/videos.json. Recreate those
944+
// zero-byte structural directories without downloading unused frame data.
945+
if (target.role === "chunk") materializeExtractedVideoDirectories(tempDir);
922946
writeFileSync(
923947
join(tempDir, PLAN_V2_MATERIALIZATION_MARKER),
924948
canonicalJsonStringify({ manifest, target }),

0 commit comments

Comments
 (0)