Skip to content

Commit df78041

Browse files
committed
fix(snapshot): say so once when video colours cannot be matched to render
1 parent 54bc274 commit df78041

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

‎packages/cli/src/commands/snapshot.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ describe("extractVideoFrameToBuffer", () => {
865865
});
866866

867867
it.skipIf(noProbe)("selects frames in a TS whose timestamps start below zero", async () => {
868+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
868869
const dir = mkdtempSync(join(tmpdir(), "hf-snapshot-negative-"));
869870
try {
870871
const clip = join(dir, "clip.ts");
@@ -908,7 +909,10 @@ describe("extractVideoFrameToBuffer", () => {
908909
[1, 30],
909910
] as const)
910911
await frameAt(time, index);
912+
// Colours cannot be matched to render without ffprobe: said once, not per frame.
913+
expect(warn.mock.calls.filter(([m]) => /colours may differ/.test(String(m)))).toHaveLength(1);
911914
} finally {
915+
warn.mockRestore();
912916
vi.unstubAllEnvs();
913917
rmSync(dir, { recursive: true, force: true });
914918
}

‎packages/cli/src/commands/snapshot.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
buildZeroDurationDiagnostic,
44
evaluateHfDiagnostic,
55
extractMediaMetadata,
6-
isHdrColorSpace,
76
sdrCanvasColourFilters,
87
} from "@hyperframes/engine";
98
import type { Page } from "puppeteer-core";
@@ -397,6 +396,17 @@ async function probeSeekLanding(
397396
}
398397
}
399398

399+
let warnedColourUnknown = false;
400+
401+
/** Render needs ffprobe for the source's colour space; without it snapshot cannot match render's colours. */
402+
function warnColourUnknownOnce(): void {
403+
if (warnedColourUnknown) return;
404+
warnedColourUnknown = true;
405+
console.warn(
406+
` ${c.warn("⚠")} snapshot colours may differ from render: install ffmpeg/ffprobe so video colours can be matched`,
407+
);
408+
}
409+
400410
class FrameExtractTimeoutError extends Error {}
401411

402412
export async function extractVideoFrameToBuffer(
@@ -423,11 +433,9 @@ export async function extractVideoFrameToBuffer(
423433
const filters: string[] = [];
424434
if (seek.select !== undefined) filters.push(`select=gte(t-start_t\\,${seek.select})`);
425435
// Render retags SDR footage the same way, so the injected frame matches the exported one.
426-
// Without ffprobe the source's colour space is unknown, so the frame is left as decoded.
427436
const metadata = findFFprobe() ? await extractMediaMetadata(videoPath) : null;
428-
if (metadata && !isHdrColorSpace(metadata.colorSpace)) {
429-
filters.push(...sdrCanvasColourFilters(metadata));
430-
}
437+
if (metadata) filters.push(...sdrCanvasColourFilters(metadata));
438+
else warnColourUnknownOnce();
431439
// Shrink so the short side still covers the displayed box at any rotation or fit; never enlarge.
432440
if (displayedPixels > 0) {
433441
filters.push(

‎packages/engine/src/services/videoFrameExtractor.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -907,10 +907,12 @@ function chromeGuessForUntaggedMatrix(metadata: VideoMetadata): string[] {
907907
return [`setparams=colorspace=${hd ? "bt709" : "smpte170m"}`];
908908
}
909909

910-
/** Tags an SDR source's decoded frame with the colours Chrome shows for it; render and snapshot share it. */
911-
export function sdrCanvasColourFilters(metadata: VideoMetadata): string[] {
912-
const isHdr = isHdrColorSpaceUtil(metadata.colorSpace);
913-
return [...(isHdr ? [] : chromeGuessForUntaggedMatrix(metadata)), SDR_CANVAS_PASSTHROUGH_FILTER];
910+
/** Tags a decoded frame with the colours Chrome shows for it; HDR yields none unless tone-mapped first. */
911+
export function sdrCanvasColourFilters(metadata: VideoMetadata, toneMappedToSdr = false): string[] {
912+
if (!isHdrColorSpaceUtil(metadata.colorSpace)) {
913+
return [...chromeGuessForUntaggedMatrix(metadata), SDR_CANVAS_PASSTHROUGH_FILTER];
914+
}
915+
return toneMappedToSdr ? [SDR_CANVAS_PASSTHROUGH_FILTER] : [];
914916
}
915917

916918
/**
@@ -1159,7 +1161,7 @@ async function extractVideoFramesRangeUnredacted(
11591161
vfFilters.push(hdrToSdrToneMapFilter(metadata.colorSpace ?? {}, frameColour));
11601162
}
11611163
if (toneMappedToSdr || (!isHdr && !options.sdrToHdrTransfer)) {
1162-
vfFilters.push(...sdrCanvasColourFilters(metadata));
1164+
vfFilters.push(...sdrCanvasColourFilters(metadata, toneMappedToSdr));
11631165
if (format === "jpg") {
11641166
if (options.shownBox) vfFilters.push(halveToCoverScaleFilter(options.shownBox));
11651167
vfFilters.push(SDR_JPEG_AS_BT601_FULL_RANGE_FILTER);

0 commit comments

Comments
 (0)