Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { DISTRIBUTED_FPS, type DistributedFps, isDistributedFps } from "@hyperframes/core";
import { parseHTML } from "linkedom";
import { c } from "../ui/colors.js";
/**
* Read a composition's declared frame rate from its root element's `data-fps`
* attribute — the same attribute the runtime honors (core/runtime/init.ts) — so
* `hyperframes render` can default to it instead of a hard-coded 30 when `--fps`
* is not passed. Returns the raw attribute string (for the caller to validate
* via `parseFps`, which supports fractional rates like `30000/1001`), or `null`
* when no root `data-fps` is present.
*
* Root resolution mirrors the runtime: prefer an explicit
* `[data-composition-id][data-root="true"]`, else the outermost
* `[data-composition-id]` (one with no `[data-composition-id]` ancestor).
*/
export function readCompositionFps(html: string): string | null {
let doc: Document;
try {
doc = parseHTML(html).document as unknown as Document;
} catch {
return null;
}
const explicitRoot = doc.querySelector('[data-composition-id][data-root="true"]');
const root =
explicitRoot ??
Array.from(doc.querySelectorAll("[data-composition-id]")).find(
(el) => !el.parentElement?.closest("[data-composition-id]"),
) ??
null;
const raw = root?.getAttribute("data-fps")?.trim();
return raw ? raw : null;
}
const DEFAULT_CLOUD_FPS: DistributedFps = 30;
/**
* Cloud render fps when `--fps` is not passed: `<projectDir>/index.html`'s
* data-fps if it is in `DISTRIBUTED_FPS`, otherwise 30, with a warning when the
* project declared a rate cloud renders cannot use.
*/
export function defaultCloudFps(projectDir: string): DistributedFps {
let html: string;
try {
html = readFileSync(join(projectDir, "index.html"), "utf8");
} catch {
return DEFAULT_CLOUD_FPS;
}
const raw = readCompositionFps(html);
if (raw == null) return DEFAULT_CLOUD_FPS;
const n = Number(raw);
if (isDistributedFps(n)) return n;
console.warn(
c.warn(
`The composition declares data-fps="${raw}", which cloud renders do not support ` +
`(${DISTRIBUTED_FPS.join(", ")}). Rendering at ${DEFAULT_CLOUD_FPS} fps; pass --fps to choose.`,
),
);
return DEFAULT_CLOUD_FPS;
}