Skip to content

Commit 96c1b00

Browse files
fix(cli): rebase full-length diff offsets and reject overflow
Keep offset streams on a zero-based clock even when every frame is selected. Cover an untrimmed delayed render and reject non-finite offsets before file or project processing. — Jerrai
1 parent 4336869 commit 96c1b00

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

‎packages/cli/src/commands/diff.exit.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ describe("diff ends the process once its result is out", () => {
4141
expect(scheduleCliExit).toHaveBeenLastCalledWith(1);
4242
});
4343

44+
it.each([
45+
{ kind: "video", yours: join(dir, "missing.mp4"), offset: "1e309" },
46+
{ kind: "project folder", yours: dir, offset: "+1e309" },
47+
])(
48+
"refuses a non-finite --offset for a $kind before reading media",
49+
async ({ yours, offset }) => {
50+
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
51+
await expect(
52+
diffCommand.run?.({
53+
args: { reference: join(dir, "missing.mp4"), yours, json: true, offset },
54+
rawArgs: [],
55+
} as never),
56+
).rejects.toThrow();
57+
expect(JSON.parse(String(log.mock.calls.at(-1)?.[0]))).toMatchObject({
58+
ok: false,
59+
error: `--offset must be a number of seconds, got "${offset}"`,
60+
fix: expect.stringContaining("--offset 0.5"),
61+
});
62+
expect(scheduleCliExit).toHaveBeenLastCalledWith(1);
63+
},
64+
);
65+
4466
describe.skipIf(noTools)("with a clip", () => {
4567
loadOpenCvFirst();
4668

‎packages/cli/src/measure/diff-offset.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ describe("offset frame times", () => {
3232
]);
3333
expect(yoursFilters({ kind: "all" }, "30000/1001", -0.25)).toEqual([
3434
"fps=30000/1001:start_time=-0.25",
35+
"setpts=N/(30000/1001)/TB",
36+
]);
37+
expect(yoursFilters({ kind: "all" }, "30", 0.5)).toEqual([
38+
"fps=30:start_time=0.5",
39+
"setpts=N/(30)/TB",
3540
]);
3641
});
3742

@@ -73,7 +78,18 @@ describe("offset frame times", () => {
7378
expect(parseOffset("0.5")).toBe(0.5);
7479
expect(parseOffset("-0.5")).toBe(-0.5);
7580
expect(parseOffset(" 2 ")).toBe(2);
76-
for (const bad of ["", "abc", "0.5s", "1,2", "0x10", "Infinity", true])
81+
for (const bad of [
82+
"",
83+
"abc",
84+
"0.5s",
85+
"1,2",
86+
"0x10",
87+
"Infinity",
88+
"1e309",
89+
"+1e309",
90+
"-1e309",
91+
true,
92+
])
7793
expect(() => parseOffset(bad)).toThrow(Fix);
7894
try {
7995
parseOffset("abc");
@@ -119,6 +135,21 @@ describe.skipIf(noTools)("diffVideos with an offset", () => {
119135
expect(rows[1]!.split("\t").slice(0, 2)).toEqual(["0", "0"]);
120136
});
121137

138+
it("compares every original frame with an untrimmed late render", async () => {
139+
const yours = clip("late-full.mp4", ["-i", ref, "-vf", "tpad=start_duration=0.5"]);
140+
const r = await diffVerdicts({
141+
reference: ref,
142+
yours,
143+
offset: 0.5,
144+
outDir: join(dir, "late-full"),
145+
});
146+
expect(r.ssim.mean).toBeGreaterThan(0.99);
147+
expect(r.ssim.frames).toBe(90);
148+
expect(r.window).toEqual([0, 3]);
149+
expect(r.warnings).toEqual([]);
150+
expect(r.pass).toBe(true);
151+
});
152+
122153
it("compares an early render with a negative offset, clipped to its start", async () => {
123154
const r = await diffVerdicts({
124155
reference: ref,

‎packages/cli/src/measure/diff.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ export function selectFilter(pick: Pick, fpsText: string): string[] {
305305
/** Yours' filters: its n-th frame at the original's rate is yours at n / fps + offset, then the pick's frames. */
306306
export function yoursFilters(pick: Pick, fpsText: string, offset = 0): string[] {
307307
const rate = offset ? `fps=${fpsText}:start_time=${offset}` : `fps=${fpsText}`;
308-
return [rate, ...selectFilter(pick, fpsText)];
308+
const select = selectFilter(pick, fpsText);
309+
// SSIM pairs timestamps, so even an untrimmed offset stream must start at zero.
310+
return [rate, ...(offset && !select.length ? [`setpts=N/(${fpsText})/TB`] : select)];
309311
}
310312

311313
/** The original's frames [first, end) that land on yours at t + offset; a project's end is unknown before capture. */
@@ -338,7 +340,7 @@ export function captureTimes(ks: number[], fps: number, offset = 0): number[] {
338340
/** `--offset` seconds: a number, negative when yours runs ahead of the original. */
339341
export function parseOffset(text: unknown): number {
340342
const value = typeof text === "string" ? text.trim() : "";
341-
if (!/^[-+]?(\d+\.?\d*|\.\d+)(e[-+]?\d+)?$/i.test(value))
343+
if (!/^[-+]?(\d+\.?\d*|\.\d+)(e[-+]?\d+)?$/i.test(value) || !Number.isFinite(Number(value)))
342344
throw new Fix(
343345
`--offset must be a number of seconds, got "${String(text)}"`,
344346
"--offset 0.5 when yours runs half a second behind the original, --offset -0.5 when it runs ahead",

0 commit comments

Comments
 (0)