From b23b51f4f0b02cbe2caa8e71d2a35b4cb2f8995d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 2 Nov 2026 13:48:20 +0000 Subject: [PATCH] fix(cli): snapshot fails when an on-screen video frame cannot be read (#7011) * fix(cli): snapshot reads video frames through any ffprobe and fails on an unreadable one * fix(cli): snapshot reads video frames through any ffprobe and fails on an unreadable one * fix(cli): snapshot counts only a failed frame read as unreadable * fix(cli): snapshot fails when an on-screen video frame cannot be read --- packages/cli/src/commands/snapshot.ts | 16 +++- .../snapshot.unreadableVideo.browser.test.ts | 85 +++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 packages/cli/src/commands/snapshot.unreadableVideo.browser.test.ts diff --git a/packages/cli/src/commands/snapshot.ts b/packages/cli/src/commands/snapshot.ts index efe6dc1e45..f3ab73b39d 100644 --- a/packages/cli/src/commands/snapshot.ts +++ b/packages/cli/src/commands/snapshot.ts @@ -941,6 +941,7 @@ export async function captureSnapshots( }> = []; const replaced: string[] = []; const warnings: string[] = []; + let unreadVideoFrames = 0; let completed = false; const numFrames = opts.frames ?? 5; let review: ReviewPlan | undefined; @@ -1026,7 +1027,7 @@ export async function captureSnapshots( }); if (duration <= 0 && !opts.at?.length) { - return { paths: [], cleared: [], warnings, pairSheetsSkipped: false }; + return { paths: [], cleared: [], warnings, unreadVideoFrames, pairSheetsSkipped: false }; } const readTiming = () => @@ -1296,6 +1297,7 @@ export async function captureSnapshots( } const want = wantAt(time)!; const banked = await frameBank.take(ffmpegInput, want); + const warned = warnings.length; const png = await frameOrWarn( warnings, async () => @@ -1308,7 +1310,10 @@ export async function captureSnapshots( want.displayedPixels, ), ); - if (!png) return null; + if (!png) { + if (warnings.length > warned) unreadVideoFrames += 1; + return null; + } return { videoId: v.id, dataUri: `data:image/png;base64,${png.toString("base64")}` }; }, ); @@ -1486,6 +1491,7 @@ export async function captureSnapshots( paths: savedPaths, cleared: replaced, warnings, + unreadVideoFrames, pairSheetsSkipped: pairSheetFailure !== null, ...(review && reviewList ? { @@ -1508,6 +1514,7 @@ interface CapturedSnapshots { cleared: string[]; /** Why a frame may differ from the composition: shown on the console and in `--json`. */ warnings: string[]; + unreadVideoFrames: number; /** --against wrote no pair sheets because sharp could not load. */ pairSheetsSkipped: boolean; review?: { plan: ReviewPlan; frames: Array }; @@ -1884,6 +1891,11 @@ export default defineCommand({ `${c.error("✗")} Only ${paths.length} of ${captured.paths.length} snapshots are on disk in ${snapshotDir}.${gone}`, ); } + if (captured.unreadVideoFrames > 0) { + fail( + `${c.error("✗")} ${captured.unreadVideoFrames} video frame(s) could not be read, so those videos are black or stale in the snapshots saved to ${snapshotDir}.`, + ); + } if (against) { log( captured.pairSheetsSkipped diff --git a/packages/cli/src/commands/snapshot.unreadableVideo.browser.test.ts b/packages/cli/src/commands/snapshot.unreadableVideo.browser.test.ts new file mode 100644 index 0000000000..cbe35c77c6 --- /dev/null +++ b/packages/cli/src/commands/snapshot.unreadableVideo.browser.test.ts @@ -0,0 +1,85 @@ +import { execFileSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterAll, expect, it, vi } from "vitest"; +import { findFFmpeg } from "../browser/ffmpeg.js"; +import snapshotCommand from "./snapshot.js"; + +const root = mkdtempSync(join(tmpdir(), "hf-snapshot-unreadable-video-")); + +afterAll(() => rmSync(root, { recursive: true, force: true })); + +function writeProject(name: string, clipStart: number, clipDuration = 2): string { + const dir = join(root, name); + mkdirSync(dir, { recursive: true }); + writeFileSync( + join(dir, "index.html"), + ` +
+ +
+`, + ); + writeFileSync(join(dir, "clip.mp4"), "this is not a video"); + return dir; +} + +async function resultAt( + dir: string, + time: string, +): Promise<{ ok: boolean; error?: { message: string } }> { + const log = vi.spyOn(console, "log").mockImplementation(() => undefined); + vi.spyOn(console, "error").mockImplementation(() => undefined); + vi.spyOn(console, "warn").mockImplementation(() => undefined); + try { + await snapshotCommand + .run?.({ + args: { + dir, + at: time, + end: false, + json: true, + output: join(dir, "out"), + describe: "false", + "browser-gpu": false, + }, + } as never) + .catch(() => undefined); + return JSON.parse(String(log.mock.calls.at(-1)?.[0])); + } finally { + vi.restoreAllMocks(); + } +} + +it("fails, naming the folder, when a video on screen cannot be read", async () => { + const dir = writeProject("unreadable", 1); + const result = await resultAt(dir, "1.5"); + + expect(result.ok).toBe(false); + expect(result.error?.message).toMatch(/video frame\(s\) could not be read.*black or stale/); + expect(readdirSync(join(dir, "out")).some((name) => /at-1\.5s\.png$/.test(name))).toBe(true); +}, 120_000); + +const ffmpeg = findFFmpeg(); +const hasX265 = + !!ffmpeg && + execFileSync(ffmpeg, ["-hide_banner", "-encoders"], { encoding: "utf-8" }).includes("libx265"); + +it.skipIf(!hasX265)( + "does not fail for a readable clip held past the end of its source", + async () => { + const dir = writeProject("held-past-end", 1, 3); + execFileSync(ffmpeg!, [ + ...["-hide_banner", "-loglevel", "error", "-f", "lavfi", "-i", "testsrc=d=1:r=24:s=160x90"], + ...["-c:v", "libx265", "-tag:v", "hvc1", "-pix_fmt", "yuv420p", "-y", join(dir, "clip.mp4")], + ]); + const result = await resultAt(dir, "3.5"); + + expect(result.ok).toBe(true); + }, + 120_000, +);