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
This commit is contained in:
Miguel Ángel
2026-11-02 13:48:20 +00:00
committed by GitHub
parent 036e41fa09
commit b23b51f4f0
2 changed files with 99 additions and 2 deletions
+14 -2
View File
@@ -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<ReviewFrame & { path: string }> };
@@ -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
@@ -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"),
`<!doctype html><html><body style="margin:0;background:#000">
<div id="root" data-composition-id="main" data-start="0" data-duration="5" data-width="320" data-height="180" style="position:relative;width:320px;height:180px">
<video id="v" class="clip" src="clip.mp4" data-start="${clipStart}" data-duration="${clipDuration}" data-track-index="0" muted style="position:absolute;inset:0;width:320px;height:180px"></video>
</div>
<script>
window.__timelines = window.__timelines || {};
window.__timelines["main"] = { pause() {}, time() { return 0; }, duration() { return 5; }, seek() {} };
</script></body></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,
);