Skip to content

Commit 13c8672

Browse files
vanceingallsclaude
andauthored
fix(producer): decode percent-encoded video src in HDR pre-extract (#2759)
* fix(producer): decode percent-encoded video src in HDR pre-extract (PRINFRA-349) * fix(producer): decode percent-encoded src in HDR image probe The HDR image probe still hand-rolled the path join the video probe had already delegated to resolveProjectRelativeSrc, so a percent-encoded non-ASCII `<img src>` (`图1.png` -> `%E5%9B%BE1.png`) never resolved: the image never entered nativeHdrImageIds, resolveEffectiveHdrMode saw no HDR sources, and the composition rendered through the SDR fallback with wrong color -- silently, unlike the video path which errored at ffmpeg. Both probes now call resolveProjectRelativeSrc directly, with no isAbsolute() pre-check. The resolver already returns an absolute path that exists and otherwise treats a leading slash as a browser origin-root URL, so a pre-check would hand back `/assets/%E5%9B%BE1.png` undecoded and re-open the same bug for root-relative srcs. This matches planHdrResources, so the two halves of the fix can no longer disagree. Widening resolution also makes previously-unresolvable files reachable for the first time, including truncated or 0-byte assets on which ffprobe exits non-zero. These probes run inside a bare Promise.all, so an unguarded throw aborted the whole render over one unreadable image; probeColorSpaceSafely now logs and treats such a source as SDR. Tests cover percent-encoded CJK, origin-root percent-encoded CJK, compiledDir-over-projectDir precedence, and existing-absolute passthrough, with distinct projectDir/compiledDir so the precedence is actually pinned. Fault-injection verified: reintroducing the isAbsolute short-circuit fails the origin-root test. Refs PRINFRA-349 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> * fix(producer): restore the vitest runner import in extractVideosStage tests The rebase merged the new `node:fs` / `node:os` / `node:path` imports into line 1 and took the incoming side, so `import { describe, expect, it } from "vitest"` was replaced rather than kept alongside. The file still uses all three, and `bun run test:classification` regex-matches `/\bfrom\s+["']vitest["']/` to route each test file to a runner — so the file matched neither and hard-failed the gate, taking Producer unit + integration and the required Test check with it. --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
1 parent 7563b64 commit 13c8672

5 files changed

Lines changed: 180 additions & 28 deletions

File tree

‎packages/producer/src/services/render/stages/captureHdrResources.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ import {
2626
estimateHdrExtractionBytes,
2727
extractHdrVideoFrames,
2828
getHdrExtractionReservedBytes,
29+
planHdrResources,
2930
reserveHdrExtractionBytes,
3031
resolveHdrExtractionActiveBudgetBytes,
3132
resolveHdrExtractionBudgetBytes,
3233
resolveHdrExtractionWindow,
3334
} from "./captureHdrResources.js";
35+
import type { CompositionMetadata } from "../shared.js";
3436

3537
afterEach(() => {
3638
vi.unstubAllEnvs();
@@ -115,6 +117,57 @@ function hdrExtractionFixture(videos: VideoElement[], framesDir: string) {
115117
};
116118
}
117119

120+
function videoComposition(src: string): CompositionMetadata {
121+
return {
122+
duration: 5,
123+
width: 1920,
124+
height: 1080,
125+
audios: [],
126+
images: [],
127+
videos: [{ id: "a-roll", src, start: 0, end: 5, mediaStart: 0, loop: false, hasAudio: false }],
128+
};
129+
}
130+
131+
describe("planHdrResources non-ASCII src resolution (PRINFRA-349)", () => {
132+
it("decodes a percent-encoded CJK <video src> back to the real on-disk path", () => {
133+
const projectDir = mkdtempSync(join(tmpdir(), "hf-hdr-cjk-"));
134+
try {
135+
const realName = "视频1.mp4";
136+
writeFileSync(join(projectDir, realName), "x");
137+
// The compiled DOM carries the URL-encoded attribute value.
138+
const encoded = encodeURIComponent(realName); // %E8%A7%86%E9%A2%911.mp4
139+
const prep = planHdrResources({
140+
composition: videoComposition(encoded),
141+
nativeHdrVideoIds: new Set(["a-roll"]),
142+
nativeHdrImageIds: new Set(),
143+
projectDir,
144+
compiledDir: projectDir,
145+
});
146+
// Must be the decoded filesystem path ffmpeg can open, not the %-encoded string.
147+
expect(prep.hdrVideoSrcPaths.get("a-roll")).toBe(join(projectDir, realName));
148+
} finally {
149+
rmSync(projectDir, { recursive: true, force: true });
150+
}
151+
});
152+
153+
it("leaves an ASCII src untouched", () => {
154+
const projectDir = mkdtempSync(join(tmpdir(), "hf-hdr-ascii-"));
155+
try {
156+
writeFileSync(join(projectDir, "clip.mp4"), "x");
157+
const prep = planHdrResources({
158+
composition: videoComposition("clip.mp4"),
159+
nativeHdrVideoIds: new Set(["a-roll"]),
160+
nativeHdrImageIds: new Set(),
161+
projectDir,
162+
compiledDir: projectDir,
163+
});
164+
expect(prep.hdrVideoSrcPaths.get("a-roll")).toBe(join(projectDir, "clip.mp4"));
165+
} finally {
166+
rmSync(projectDir, { recursive: true, force: true });
167+
}
168+
});
169+
});
170+
118171
describe("estimateHdrExtractionBytes", () => {
119172
it("sums 6 bytes per pixel per frame across videos", () => {
120173
// 10s @ 30fps of 1920x1080 = 300 frames * 1920*1080*6

‎packages/producer/src/services/render/stages/captureHdrResources.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
queryElementStacking,
3838
resampleRgb48leObjectFit,
3939
resolveFinalFrameExtractionWindow,
40+
resolveProjectRelativeSrc,
4041
resolveVideoExtractionWindow,
4142
runFfmpeg,
4243
type TimelineExtractionWindow,
@@ -78,7 +79,6 @@ export function planHdrResources(args: {
7879
nativeHdrImageIds: Set<string>;
7980
projectDir: string;
8081
compiledDir: string;
81-
existsSync: (p: string) => boolean;
8282
}): HdrResourcePrep {
8383
const { composition, nativeHdrVideoIds, nativeHdrImageIds, projectDir, compiledDir } = args;
8484
const hdrVideoIds = composition.videos
@@ -87,12 +87,14 @@ export function planHdrResources(args: {
8787
const hdrVideoSrcPaths = new Map<string, string>();
8888
for (const v of composition.videos) {
8989
if (!hdrVideoIds.includes(v.id)) continue;
90-
let srcPath = v.src;
91-
if (!srcPath.startsWith("/")) {
92-
const fromCompiled = join(compiledDir, srcPath);
93-
srcPath = args.existsSync(fromCompiled) ? fromCompiled : join(projectDir, srcPath);
94-
}
95-
hdrVideoSrcPaths.set(v.id, srcPath);
90+
// Resolve via the shared SDR resolver so a percent-encoded `<video src>`
91+
// (`视频1.mp4` → `%E8%A7%86%E9%A2%911.mp4` in the compiled DOM URL) decodes
92+
// back to the real on-disk filename before ffmpeg sees it. The old
93+
// hand-rolled join passed the encoded string straight through, so HDR
94+
// pre-extraction failed with "No such file" on non-ASCII media
95+
// (PRINFRA-349 symptom c). resolveProjectRelativeSrc also handles query
96+
// strings, origin-root URLs, and `..` traversal identically to the SDR path.
97+
hdrVideoSrcPaths.set(v.id, resolveProjectRelativeSrc(v.src, projectDir, compiledDir));
9698
}
9799
const hdrVideoStartTimes = new Map<string, number>();
98100
for (const v of composition.videos) {

‎packages/producer/src/services/render/stages/captureHdrStage.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ export async function runCaptureHdrStage(
202202
nativeHdrImageIds,
203203
projectDir,
204204
compiledDir,
205-
existsSync,
206205
});
207206

208207
const domSession = await createCaptureSession(

‎packages/producer/src/services/render/stages/extractVideosStage.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { describe, expect, it } from "vitest";
2+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
25
import type {
36
ExtractedFrames,
47
ExtractionResult,
58
VideoElement,
69
VideoExtractionFailure,
710
} from "@hyperframes/engine";
11+
import { resolveProjectRelativeSrc } from "@hyperframes/engine";
812
import {
913
appendAutoDetectedVideoAudio,
1014
assertVideoExtractionSucceeded,
@@ -121,6 +125,74 @@ describe("appendAutoDetectedVideoAudio", () => {
121125
});
122126
});
123127

128+
// The HDR probes in this stage resolve `<video>`/`<img>` srcs with
129+
// resolveProjectRelativeSrc and NO isAbsolute() pre-check. These pin the src
130+
// shapes that a pre-check would silently break — an earlier revision of the
131+
// PRINFRA-349 fix short-circuited on isAbsolute and left root-relative srcs
132+
// percent-encoded, so the HDR image never resolved and the render shipped SDR.
133+
describe("HDR probe src resolution (PRINFRA-349)", () => {
134+
it("decodes a percent-encoded CJK src to the real on-disk path", () => {
135+
const projectDir = mkdtempSync(join(tmpdir(), "hf-probe-cjk-"));
136+
const compiledDir = mkdtempSync(join(tmpdir(), "hf-probe-compiled-"));
137+
try {
138+
const realName = "图1.png";
139+
writeFileSync(join(projectDir, realName), "x");
140+
// The compiled DOM carries the URL-encoded attribute value.
141+
const encoded = encodeURIComponent(realName); // %E5%9B%BE1.png
142+
expect(resolveProjectRelativeSrc(encoded, projectDir, compiledDir)).toBe(
143+
join(projectDir, realName),
144+
);
145+
} finally {
146+
rmSync(projectDir, { recursive: true, force: true });
147+
rmSync(compiledDir, { recursive: true, force: true });
148+
}
149+
});
150+
151+
it("decodes a percent-encoded CJK src served from a browser origin-root URL", () => {
152+
// Regression guard: `isAbsolute("/assets/%E5%9B%BE1.png")` is true on POSIX,
153+
// so a pre-check would return it verbatim, existsSync would fail, and the
154+
// image would never enter nativeHdrImageIds — a silent SDR render.
155+
const projectDir = mkdtempSync(join(tmpdir(), "hf-probe-root-"));
156+
try {
157+
mkdirSync(join(projectDir, "assets"));
158+
const realName = "图1.png";
159+
writeFileSync(join(projectDir, "assets", realName), "x");
160+
const rootRelative = `/assets/${encodeURIComponent(realName)}`;
161+
expect(resolveProjectRelativeSrc(rootRelative, projectDir, projectDir)).toBe(
162+
join(projectDir, "assets", realName),
163+
);
164+
} finally {
165+
rmSync(projectDir, { recursive: true, force: true });
166+
}
167+
});
168+
169+
it("prefers compiledDir over projectDir when both hold the asset", () => {
170+
const projectDir = mkdtempSync(join(tmpdir(), "hf-probe-proj-"));
171+
const compiledDir = mkdtempSync(join(tmpdir(), "hf-probe-comp-"));
172+
try {
173+
writeFileSync(join(projectDir, "clip.mp4"), "x");
174+
writeFileSync(join(compiledDir, "clip.mp4"), "x");
175+
expect(resolveProjectRelativeSrc("clip.mp4", projectDir, compiledDir)).toBe(
176+
join(compiledDir, "clip.mp4"),
177+
);
178+
} finally {
179+
rmSync(projectDir, { recursive: true, force: true });
180+
rmSync(compiledDir, { recursive: true, force: true });
181+
}
182+
});
183+
184+
it("returns an existing absolute path unchanged", () => {
185+
const projectDir = mkdtempSync(join(tmpdir(), "hf-probe-abs-"));
186+
try {
187+
const abs = join(projectDir, "clip.mp4");
188+
writeFileSync(abs, "x");
189+
expect(resolveProjectRelativeSrc(abs, projectDir, projectDir)).toBe(abs);
190+
} finally {
191+
rmSync(projectDir, { recursive: true, force: true });
192+
}
193+
});
194+
});
195+
124196
describe("shouldCopyExtractedFrames", () => {
125197
it("copies frames on Windows (symlinkSync throws EPERM without Developer Mode)", () => {
126198
expect(shouldCopyExtractedFrames("win32")).toBe(true);

‎packages/producer/src/services/render/stages/extractVideosStage.ts‎

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030

3131
import { existsSync } from "node:fs";
32-
import { isAbsolute, join } from "node:path";
32+
import { join } from "node:path";
3333
import {
3434
type CaptureVideoMetadataHint,
3535
type EngineConfig,
@@ -247,6 +247,34 @@ function applyVideoExtractionFailurePolicy(
247247
return policy.failureMode === "enforce" ? error : null;
248248
}
249249

250+
/**
251+
* Probe a media file's color space, returning null when it can't be read.
252+
*
253+
* Both HDR probes below widened which files they resolve (PRINFRA-349: the
254+
* shared resolver percent-decodes non-ASCII names, so `%E5%9B%BE1.png` now
255+
* finds `图1.png`). Files that used to silently fail to resolve are therefore
256+
* reachable for the first time — including truncated / 0-byte assets, on which
257+
* ffprobe exits non-zero and `extractMediaMetadata` throws. These probes run
258+
* inside a bare `Promise.all`, so an unguarded throw aborts the whole render
259+
* over one unreadable image. A probe that can't read a file must skip it, not
260+
* kill the render.
261+
*/
262+
async function probeColorSpaceSafely(
263+
path: string,
264+
log: ProducerLogger | undefined,
265+
): Promise<VideoColorSpace | null> {
266+
try {
267+
const meta = await extractMediaMetadata(path);
268+
return meta.colorSpace;
269+
} catch (error) {
270+
log?.warn("HDR color-space probe failed; treating source as SDR", {
271+
path,
272+
error: error instanceof Error ? error.message : String(error),
273+
});
274+
return null;
275+
}
276+
}
277+
250278
export async function runExtractVideosStage(
251279
input: ExtractVideosStageInput,
252280
): Promise<ExtractVideosStageResult> {
@@ -282,14 +310,14 @@ export async function runExtractVideosStage(
282310
log?.info("Probing video color spaces...", { videoCount: composition.videos.length });
283311
const probeFailures = await Promise.all(
284312
composition.videos.map(async (v) => {
285-
// Use the shared resolver so a `<video src="../assets/foo">` in a
286-
// sub-composition resolves the same way the browser would (see
287-
// resolveProjectRelativeSrc in videoFrameExtractor for the full
288-
// explanation). isAbsolute (not `startsWith("/")`) so Windows
289-
// absolute paths like `C:\...` skip the join correctly.
290-
const videoPath = isAbsolute(v.src)
291-
? v.src
292-
: resolveProjectRelativeSrc(v.src, projectDir, compiledDir);
313+
// Shared resolver so a `<video src="../assets/foo">` in a sub-composition
314+
// resolves the same way the browser would, and a percent-encoded
315+
// non-ASCII name decodes to its real on-disk path. Called with no
316+
// isAbsolute() pre-check: the resolver already returns an absolute path
317+
// that exists, and otherwise treats a leading slash as a browser
318+
// origin-root URL — pre-checking would hand back `/assets/%E5%9B%BE1.png`
319+
// undecoded and re-open PRINFRA-349 for root-relative srcs.
320+
const videoPath = resolveProjectRelativeSrc(v.src, projectDir, compiledDir);
293321
if (!existsSync(videoPath)) return null;
294322
try {
295323
// Retries are separately opt-in from the failure gate. With the
@@ -338,21 +366,19 @@ export async function runExtractVideosStage(
338366
if (job.config.hdrMode !== "force-sdr" && composition.images.length > 0) {
339367
const probed = await Promise.all(
340368
composition.images.map(async (img) => {
341-
let imgPath = img.src;
342-
if (!imgPath.startsWith("/")) {
343-
const fromCompiled = existsSync(join(compiledDir, imgPath))
344-
? join(compiledDir, imgPath)
345-
: join(projectDir, imgPath);
346-
imgPath = fromCompiled;
347-
}
369+
// Same shared resolver as the video probe above — a percent-encoded
370+
// non-ASCII `<img src>` must decode to the on-disk path, or the HDR image
371+
// never enters nativeHdrImageIds and the composition silently renders
372+
// through the SDR fallback with wrong color (PRINFRA-349 symptom c).
373+
const imgPath = resolveProjectRelativeSrc(img.src, projectDir, compiledDir);
348374
if (!existsSync(imgPath)) return null;
349-
const meta = await extractMediaMetadata(imgPath);
350-
if (isHdrColorSpace(meta.colorSpace)) {
375+
const colorSpace = await probeColorSpaceSafely(imgPath, log);
376+
if (isHdrColorSpace(colorSpace)) {
351377
nativeHdrImageIds.add(img.id);
352-
imageTransfers.set(img.id, detectTransfer(meta.colorSpace));
378+
imageTransfers.set(img.id, detectTransfer(colorSpace));
353379
hdrImageSrcPaths.set(img.id, imgPath);
354380
}
355-
return meta.colorSpace;
381+
return colorSpace;
356382
}),
357383
);
358384
imageColorSpaces.push(...probed);

0 commit comments

Comments
 (0)