|
29 | 29 | */ |
30 | 30 |
|
31 | 31 | import { existsSync } from "node:fs"; |
32 | | -import { isAbsolute, join } from "node:path"; |
| 32 | +import { join } from "node:path"; |
33 | 33 | import { |
34 | 34 | type CaptureVideoMetadataHint, |
35 | 35 | type EngineConfig, |
@@ -247,6 +247,34 @@ function applyVideoExtractionFailurePolicy( |
247 | 247 | return policy.failureMode === "enforce" ? error : null; |
248 | 248 | } |
249 | 249 |
|
| 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 | + |
250 | 278 | export async function runExtractVideosStage( |
251 | 279 | input: ExtractVideosStageInput, |
252 | 280 | ): Promise<ExtractVideosStageResult> { |
@@ -282,14 +310,14 @@ export async function runExtractVideosStage( |
282 | 310 | log?.info("Probing video color spaces...", { videoCount: composition.videos.length }); |
283 | 311 | const probeFailures = await Promise.all( |
284 | 312 | 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); |
293 | 321 | if (!existsSync(videoPath)) return null; |
294 | 322 | try { |
295 | 323 | // Retries are separately opt-in from the failure gate. With the |
@@ -338,21 +366,19 @@ export async function runExtractVideosStage( |
338 | 366 | if (job.config.hdrMode !== "force-sdr" && composition.images.length > 0) { |
339 | 367 | const probed = await Promise.all( |
340 | 368 | 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); |
348 | 374 | 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)) { |
351 | 377 | nativeHdrImageIds.add(img.id); |
352 | | - imageTransfers.set(img.id, detectTransfer(meta.colorSpace)); |
| 378 | + imageTransfers.set(img.id, detectTransfer(colorSpace)); |
353 | 379 | hdrImageSrcPaths.set(img.id, imgPath); |
354 | 380 | } |
355 | | - return meta.colorSpace; |
| 381 | + return colorSpace; |
356 | 382 | }), |
357 | 383 | ); |
358 | 384 | imageColorSpaces.push(...probed); |
|
0 commit comments