feat(cli): accept ffmpeg-style rational fps (NTSC, PAL, slow-mo)

Replaces the rigid `--fps 24|30|60` whitelist with a numeric range and
adds support for ffmpeg-style fractional framerates so NTSC stays exact
end-to-end.

- `--fps 30` keeps working (integer fps)
- `--fps 30000/1001` now means exact NTSC 29.97 (not the lossy decimal)
- `--fps 24000/1001`, `--fps 60000/1001`, `--fps 25/50/120/240` all work
- Decimals like `--fps 29.97` are rejected with a friendly error pointing
  the user at the rational form, since `29.97` and `30000/1001` round
  to different framerates inside ffmpeg

Carries an `Fps = { num: number; den: number }` rational end-to-end:
RenderConfig, EncoderOptions, StreamingEncoderOptions, CaptureOptions,
DockerRenderOptions, Studio API request body, regression-harness
meta.json. The `-r` and `-framerate` ffmpeg args emit the rational form
verbatim (`30000/1001`) so no decimal round-trip happens at the encoder
boundary. Frame-interval math uses `1000 * den / num` ms (33.366… for
NTSC, 33.333… for integer 30).

Helpers live in @hyperframes/core:
- `parseFps(input: string | number): FpsParseResult` — discriminated
  parser used by both the CLI and the Studio API route
- `fpsToFfmpegArg(fps: Fps): string` — emits "30" or "30000/1001"
- `fpsToNumber(fps: Fps): number` — for arithmetic (telemetry, frame
  count, frame-index → time)

Studio API wire format accepts polymorphic `fps: number | string`:
- number → integer fps (`30`)
- string → rational (`"30000/1001"`)
Decimals are rejected; matches the same rule as the CLI.

Existing meta.json fixtures with integer `"fps": 30` continue to load
unchanged — the regression-harness validator now normalizes both number
and string inputs through `parseFps`.
This commit is contained in:
Theodor Kleynhans
2026-05-09 00:09:15 +02:00
parent b58e447305
commit 5dcc89c930
27 changed files with 725 additions and 91 deletions
@@ -79,7 +79,7 @@ describe("getEncoderPreset", () => {
});
describe("buildEncoderArgs anti-banding", () => {
const baseOptions = { fps: 30, width: 1920, height: 1080 };
const baseOptions = { fps: { num: 30, den: 1 }, width: 1920, height: 1080 };
it("adds aq-mode=3 x264-params for h264 CPU encoding", () => {
const args = buildEncoderArgs(
@@ -148,8 +148,35 @@ describe("buildEncoderArgs anti-banding", () => {
});
});
describe("buildEncoderArgs fps rational forwarding", () => {
// Regression for the fps fraction-syntax feature: rational fps must reach
// ffmpeg's `-r` flag verbatim (e.g. "30000/1001") so NTSC stays exact end-
// to-end rather than being rounded to 29.97 decimal at the encoder boundary.
it("emits integer -r for { num: 30, den: 1 }", () => {
const args = buildEncoderArgs(
{ fps: { num: 30, den: 1 }, width: 1920, height: 1080, codec: "h264" },
["-framerate", "30", "-i", "frames/%04d.png"],
"out.mp4",
);
const rIdx = args.indexOf("-r");
expect(rIdx).toBeGreaterThan(-1);
expect(args[rIdx + 1]).toBe("30");
});
it("emits rational -r for NTSC { num: 30000, den: 1001 }", () => {
const args = buildEncoderArgs(
{ fps: { num: 30000, den: 1001 }, width: 1920, height: 1080, codec: "h264" },
["-framerate", "30000/1001", "-i", "frames/%04d.png"],
"out.mp4",
);
const rIdx = args.indexOf("-r");
expect(rIdx).toBeGreaterThan(-1);
expect(args[rIdx + 1]).toBe("30000/1001");
});
});
describe("buildEncoderArgs GPU preset mapping", () => {
const baseOptions = { fps: 30, width: 1920, height: 1080 };
const baseOptions = { fps: { num: 30, den: 1 }, width: 1920, height: 1080 };
const inputArgs = ["-framerate", "30", "-i", "frames/%04d.png"];
function presetArg(args: string[]): string | undefined {
@@ -232,7 +259,7 @@ describe("buildEncoderArgs GPU preset mapping", () => {
});
describe("buildEncoderArgs color space", () => {
const baseOptions = { fps: 30, width: 1920, height: 1080 };
const baseOptions = { fps: { num: 30, den: 1 }, width: 1920, height: 1080 };
const inputArgs = ["-framerate", "30", "-i", "frames/%04d.png"];
it("adds bt709 color space metadata for h264 CPU encoding", () => {
@@ -365,7 +392,7 @@ describe("getEncoderPreset HDR", () => {
});
describe("buildEncoderArgs HDR color space", () => {
const baseOptions = { fps: 30, width: 1920, height: 1080 };
const baseOptions = { fps: { num: 30, den: 1 }, width: 1920, height: 1080 };
const inputArgs = ["-framerate", "30", "-i", "frames/%04d.png"];
it("emits BT.2020 + arib-std-b67 tags for HDR HLG (h265 SW)", () => {