Skip to content

Commit 99223d9

Browse files
fix(cli): diff warns when yours runs at another frame rate (#6976)
A root without data-fps on a 24 fps film rendered at 30 fps, and nothing said so. fails without the fix: warns about a render at another frame rate
1 parent f0b2dd0 commit 99223d9

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { existsSync } from "node:fs";
1+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
22
import { join } from "node:path";
33
import { describe, expect, it } from "vitest";
44
import {
55
cellsOutside,
66
diffVideos,
77
longRuns,
88
pickedFrames,
9+
projectRate,
10+
rateWarning,
911
regionRoi,
1012
selectFilter,
1113
wholeFrameCuts,
@@ -183,6 +185,33 @@ describe("person gates", () => {
183185
});
184186
});
185187

188+
describe("frame rate", () => {
189+
const original = { fps: 24, fpsText: "24" } as Parameters<typeof rateWarning>[0];
190+
191+
it("warns when yours runs at another rate than the original", () => {
192+
expect(rateWarning(original, 30)).toContain("yours runs at 30 fps and the original at 24");
193+
expect(rateWarning(original, 30)).toContain('data-fps="24"');
194+
expect(rateWarning(original, 24)).toBeUndefined();
195+
expect(
196+
rateWarning({ fps: 29.97, fpsText: "30000/1001" } as typeof original, 30000 / 1001),
197+
).toBe(undefined);
198+
});
199+
200+
it("reads a project's rate from its root, and 30 when the root has none", () => {
201+
const project = (name: string, attrs: string) => {
202+
const path = join(dir, name);
203+
mkdirSync(path, { recursive: true });
204+
writeFileSync(
205+
join(path, "index.html"),
206+
`<!doctype html><div data-composition-id="main" ${attrs}></div>`,
207+
);
208+
return path;
209+
};
210+
expect(projectRate(project("rate-24", 'data-fps="24"'))).toBe(24);
211+
expect(projectRate(project("rate-none", ""))).toBe(30);
212+
});
213+
});
214+
186215
describe("frame picks", () => {
187216
it("keeps a range's frames end-exclusive and renumbers them on the original's clock", () => {
188217
expect(pickedFrames({ kind: "range", k0: 3, k1: 6 }, 90)).toEqual([3, 4, 5]);
@@ -199,6 +228,12 @@ describe.skipIf(noTools)("diffVideos", () => {
199228
loadOpenCvFirst();
200229
const ref = noTools ? "" : clip("ref.mp4", REF_ARGS);
201230

231+
it("warns about a render at another frame rate", async () => {
232+
const yours = clip("rate-24.mp4", ["-i", ref, "-r", "24"]);
233+
const r = await diffVideos({ reference: ref, yours, outDir: join(dir, "rate") });
234+
expect(r.warnings.join(" ")).toContain("yours runs at 24 fps");
235+
});
236+
202237
it("passes a video against itself", async () => {
203238
const r = await diffVideos({ reference: ref, yours: ref, outDir: join(dir, "same") });
204239
expect(r.pass).toBe(true);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
} from "node:fs";
1616
import { tmpdir } from "node:os";
1717
import { basename, dirname, join, resolve } from "node:path";
18+
import { fpsToNumber, parseFps } from "@hyperframes/core";
19+
import { readCompositionFps } from "../utils/compositionFps.js";
1820
import { classifiedCuts, confirmsCut, frameChanges, signatures } from "./cuts.js";
1921
import {
2022
drawnFrames,
@@ -1124,6 +1126,19 @@ function choosePick(opts: DiffOptions, ref: VideoInfo): Pick {
11241126
return { kind: "all" };
11251127
}
11261128

1129+
/** Tells when a render, or a project's root, runs at another rate than the original. */
1130+
export function rateWarning(ref: VideoInfo, rate: number): string | undefined {
1131+
if (Math.abs(rate - ref.fps) <= 0.01) return undefined;
1132+
return `yours runs at ${round(rate, 3)} fps and the original at ${ref.fpsText}: set data-fps="${ref.fpsText}" on the root and render again`;
1133+
}
1134+
1135+
export function projectRate(dir: string): number {
1136+
const parsed = parseFps(
1137+
readCompositionFps(readFileSync(join(dir, "index.html"), "utf8")) ?? "30",
1138+
);
1139+
return parsed.ok ? fpsToNumber(parsed.value) : 30;
1140+
}
1141+
11271142
// fallow-ignore-next-line complexity
11281143
export async function diffVideos(options: DiffOptions): Promise<DiffResult> {
11291144
// ffmpeg runs in a scratch folder (see ssimSeries), so every path it reads is absolute.
@@ -1176,6 +1191,9 @@ export async function diffVideos(options: DiffOptions): Promise<DiffResult> {
11761191
};
11771192
}
11781193

1194+
const offRate = rateWarning(ref, yoursInfo?.fps ?? projectRate(opts.yours));
1195+
if (offRate) warnings.push(offRate);
1196+
11791197
try {
11801198
mkdirSync(opts.outDir, { recursive: true });
11811199
for (const f of readdirSync(opts.outDir))

0 commit comments

Comments
 (0)