Skip to content

Commit 43999ee

Browse files
committed
fix(cli): default render fps to the composition's data-fps
hyperframes render hard-coded fps to 30 when --fps was omitted, ignoring a data-fps declared on the composition root — so a composition authored at data-fps="24" silently rendered at 30fps unless the user knew to pass --fps 24. The runtime already honors data-fps; the CLI now matches it. Precedence: explicit --fps > composition root data-fps > 30. New pure readCompositionFps() extracts the root data-fps via linkedom (mirrors the runtime's root resolution: [data-composition-id][data-root=true], else the outermost [data-composition-id]); render validates it through parseFps and falls back to 30 on an absent/invalid value. Unit-tested.
1 parent e9c37b5 commit 43999ee

3 files changed

Lines changed: 89 additions & 2 deletions

File tree

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { cpus, freemem, tmpdir } from "node:os";
5151
import { resolve, dirname, join, basename } from "node:path";
5252
import { execFileSync, spawn } from "node:child_process";
5353
import { resolveProject } from "../utils/project.js";
54+
import { readCompositionFps } from "../utils/compositionFps.js";
5455
import { lintProject, shouldBlockRender } from "../utils/lintProject.js";
5556
import { formatLintFindings } from "../utils/lintFormat.js";
5657
import { loadProducer } from "../utils/producer.js";
@@ -388,9 +389,23 @@ export default defineCommand({
388389
// legitimate framerates (NTSC trio, PAL, 120/240 slow-mo) work without
389390
// CLI gymnastics. The exact rational survives end-to-end into FFmpeg's
390391
// `-r` / `-framerate` flags via `fpsToFfmpegArg`.
391-
const fpsParse = parseFps(args.fps ?? "30");
392+
// Precedence: explicit --fps, else the composition's root data-fps, else 30.
393+
// Honoring data-fps matches the runtime — render used to silently force 30
394+
// even when the composition declared e.g. data-fps="24".
395+
let fpsArg = args.fps;
396+
if (fpsArg == null) {
397+
try {
398+
const declared = readCompositionFps(readFileSync(join(project.dir, "index.html"), "utf8"));
399+
if (declared != null && parseFps(declared).ok) {
400+
fpsArg = declared;
401+
}
402+
} catch {
403+
// Unreadable index.html — fall back to the default fps below.
404+
}
405+
}
406+
const fpsParse = parseFps(fpsArg ?? "30");
392407
if (!fpsParse.ok) {
393-
errorBox("Invalid fps", formatFpsParseError(args.fps ?? "30", fpsParse.reason));
408+
errorBox("Invalid fps", formatFpsParseError(fpsArg ?? "30", fpsParse.reason));
394409
process.exit(1);
395410
}
396411
let fps: Fps = fpsParse.value;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it } from "vitest";
2+
import { readCompositionFps } from "./compositionFps.js";
3+
4+
const wrap = (body: string) => `<!DOCTYPE html><html><body>${body}</body></html>`;
5+
6+
describe("readCompositionFps", () => {
7+
it("reads data-fps from the explicit data-root composition element", () => {
8+
const html = wrap('<div data-composition-id="root" data-root="true" data-fps="24">x</div>');
9+
expect(readCompositionFps(html)).toBe("24");
10+
});
11+
12+
it("reads data-fps from the outermost composition when no data-root is marked", () => {
13+
const html = wrap(
14+
'<div data-composition-id="root" data-fps="48"><div data-composition-id="child" data-fps="12">x</div></div>',
15+
);
16+
expect(readCompositionFps(html)).toBe("48");
17+
});
18+
19+
it("preserves a fractional rate verbatim for parseFps to validate", () => {
20+
const html = wrap(
21+
'<div data-composition-id="root" data-root="true" data-fps="30000/1001">x</div>',
22+
);
23+
expect(readCompositionFps(html)).toBe("30000/1001");
24+
});
25+
26+
it("returns null when the root has no data-fps", () => {
27+
expect(readCompositionFps(wrap('<div data-composition-id="root">x</div>'))).toBeNull();
28+
});
29+
30+
it("returns null when there is no composition root", () => {
31+
expect(readCompositionFps(wrap("<div>plain</div>"))).toBeNull();
32+
});
33+
34+
it("returns null for a blank data-fps", () => {
35+
expect(
36+
readCompositionFps(wrap('<div data-composition-id="root" data-fps=" ">x</div>')),
37+
).toBeNull();
38+
});
39+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { parseHTML } from "linkedom";
2+
3+
/**
4+
* Read a composition's declared frame rate from its root element's `data-fps`
5+
* attribute — the same attribute the runtime honors (core/runtime/init.ts) — so
6+
* `hyperframes render` can default to it instead of a hard-coded 30 when `--fps`
7+
* is not passed. Returns the raw attribute string (for the caller to validate
8+
* via `parseFps`, which supports fractional rates like `30000/1001`), or `null`
9+
* when no root `data-fps` is present.
10+
*
11+
* Root resolution mirrors the runtime: prefer an explicit
12+
* `[data-composition-id][data-root="true"]`, else the outermost
13+
* `[data-composition-id]` (one with no `[data-composition-id]` ancestor).
14+
*/
15+
export function readCompositionFps(html: string): string | null {
16+
let doc: Document;
17+
try {
18+
doc = parseHTML(html).document as unknown as Document;
19+
} catch {
20+
return null;
21+
}
22+
23+
const explicitRoot = doc.querySelector('[data-composition-id][data-root="true"]');
24+
const root =
25+
explicitRoot ??
26+
Array.from(doc.querySelectorAll("[data-composition-id]")).find(
27+
(el) => !el.parentElement?.closest("[data-composition-id]"),
28+
) ??
29+
null;
30+
31+
const raw = root?.getAttribute("data-fps")?.trim();
32+
return raw ? raw : null;
33+
}

0 commit comments

Comments
 (0)