Skip to content

Commit b847622

Browse files
fix(cli): snapshot warns when a video on screen has no source to read a frame from (#6656)
1 parent 85efaec commit b847622

2 files changed

Lines changed: 71 additions & 7 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
4+
import { afterAll, expect, it, vi } from "vitest";
5+
import snapshotCommand from "./snapshot.js";
6+
7+
const root = mkdtempSync(join(tmpdir(), "hf-snapshot-sourceless-video-"));
8+
9+
afterAll(() => rmSync(root, { recursive: true, force: true }));
10+
11+
function writeProject(name: string, clipStart: number): string {
12+
const dir = join(root, name);
13+
mkdirSync(dir, { recursive: true });
14+
writeFileSync(
15+
join(dir, "index.html"),
16+
`<!doctype html><html><body style="margin:0;background:#000">
17+
<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">
18+
<video id="v" class="clip" data-start="${clipStart}" data-duration="2" data-track-index="0" muted style="position:absolute;inset:0;width:320px;height:180px"></video>
19+
</div>
20+
<script>
21+
window.__timelines = window.__timelines || {};
22+
window.__timelines["main"] = { pause() {}, time() { return 0; }, duration() { return 5; }, seek() {} };
23+
</script></body></html>`,
24+
);
25+
return dir;
26+
}
27+
28+
async function warningsAt(dir: string, time: string): Promise<string[]> {
29+
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
30+
vi.spyOn(console, "error").mockImplementation(() => undefined);
31+
vi.spyOn(console, "warn").mockImplementation(() => undefined);
32+
try {
33+
await snapshotCommand.run?.({
34+
args: {
35+
dir,
36+
at: time,
37+
end: false,
38+
json: true,
39+
output: join(dir, "out"),
40+
describe: "false",
41+
"browser-gpu": false,
42+
},
43+
} as never);
44+
return JSON.parse(String(log.mock.calls.at(-1)?.[0])).warnings ?? [];
45+
} finally {
46+
vi.restoreAllMocks();
47+
}
48+
}
49+
50+
it("warns when a video on screen has no source to read its frame from", async () => {
51+
const warnings = await warningsAt(writeProject("on-screen", 1), "1.5");
52+
53+
expect(warnings).toEqual(["1 video frames missing at 1.5s"]);
54+
}, 120_000);
55+
56+
it("says nothing about a sourceless video whose clip has not started", async () => {
57+
const warnings = await warningsAt(writeProject("not-yet", 3), "1.5");
58+
59+
expect(warnings).toEqual([]);
60+
}, 120_000);

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ async function captureSnapshots(
976976
});
977977
});
978978
const compositionDuration = duration;
979+
let unreadable = 0;
979980
const active = candidates.flatMap((candidate) => {
980981
const start = resolveSnapshotVideoClipStart(candidate);
981982
const playbackRate = resolveSnapshotVideoRateSpec(candidate);
@@ -997,7 +998,6 @@ async function captureSnapshots(
997998
candidate.mediaStart +
998999
((relTime - candidate.mediaStart) % (candidate.srcDuration - candidate.mediaStart));
9991000
}
1000-
if (!candidate.id || !candidate.src) return [];
10011001
const frameTime = resolveSnapshotVideoFrameTime({
10021002
globalTime: time,
10031003
clipStart: start,
@@ -1006,9 +1006,12 @@ async function captureSnapshots(
10061006
sourceDuration: candidate.srcDuration,
10071007
compositionDuration,
10081008
});
1009-
return frameTime === null
1010-
? []
1011-
: [{ ...candidate, start, playbackRate, duration, relTime: frameTime }];
1009+
if (frameTime === null) return [];
1010+
if (!candidate.id || !candidate.src) {
1011+
unreadable += 1;
1012+
return [];
1013+
}
1014+
return [{ ...candidate, start, playbackRate, duration, relTime: frameTime }];
10121015
});
10131016

10141017
const extracted = await runWithConcurrencyLimit(
@@ -1056,11 +1059,12 @@ async function captureSnapshots(
10561059
);
10571060
const updates = extracted.filter((update) => update !== null);
10581061

1059-
if (active.length > 0 && updates.length < active.length) {
1060-
const missed = active.length - updates.length;
1062+
const onScreen = active.length + unreadable;
1063+
if (updates.length < onScreen) {
1064+
const missed = onScreen - updates.length;
10611065
warnings.push(`${missed} video frames missing at ${time}s`);
10621066
console.warn(
1063-
` ${c.warn("⚠")} ${missed}/${active.length} active <video> frame(s) could not be extracted at ${time.toFixed(1)}s — those videos will appear black/stale in this snapshot`,
1067+
` ${c.warn("⚠")} ${missed}/${onScreen} active <video> frame(s) could not be extracted at ${time.toFixed(1)}s — those videos will appear black/stale in this snapshot`,
10641068
);
10651069
}
10661070

0 commit comments

Comments
 (0)