Files
hyperframes/packages/cli/src/commands/grade-compare.ts
T
Miguel ÁngelandClaude Opus 4.8 57b3c78987 feat(media-use): color grading — grade/lut resolve, smart-grade, grade-compare + compare (#2041)
* feat(media-use): color grading — grade/lut resolve, smart-grade, grade-compare CLI

Add color grading to media-use as first-class resolve types plus a faithful
comparison command. All local, offline, deterministic — no model, no GPU.

- resolve -t grade / -t lut: produce a data-color-grading block (or a frozen
  .cube). Look cascade: core preset (no file) -> bundled .cube library ->
  parametric buildCube. Emitted .cube is Rec.709 and validated against core's
  colorLuts constraints (LUT_3D_SIZE <= 64) before it is frozen.
- smart grade (grade --for <media>): ffmpeg signalstats -> adjust suggestion
  (exposure / contrast / white balance), surfaced with the measured evidence on
  stderr as a starting point; never auto-applied.
- hyperframes grade-compare: renders N candidate grades onto a reference frame
  through the real runtime shader into one labeled comparison PNG, so an agent
  picks a look without opening Studio. Prepends an "original" baseline cell by
  default (--no-baseline to omit). Shares the headless-capture pipeline with
  snapshot via capture/captureCompositionFrame.
- media-use SKILL: proactive "media opportunity pass" guidance (grounded
  signal -> offer, ask once, surface don't mutate).

Verified: media-use 116/116, grade-compare 7/7, snapshot 9/9, lint + format
clean, full build green, comparison renders end to end.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* test(cli): narrow grade-compare baseline assertion off unknown-typed grading

Assert the whole cell via toEqual instead of reaching into .grading.preset /
.grading.lut on the unknown-typed field, keeping the test typecheck-clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* feat(media-use): agent-authored LUTs via --params + validate --from cube; never-read-.cube guardrail

- resolve -t lut / -t grade --params '<json>': build a parametric .cube from
  explicit params (bypassing the intent cascade), validate, and freeze in one
  step. --intent becomes the optional description. Lets an agent commit a look
  it computed itself.
- --from <file.cube> now validates the ingested LUT for lut/grade types and
  rejects an invalid/oversized cube (no partial write) — the escape hatch for a
  LUT the agent generated with its own code.
- SKILL.md: hard rule to never read a .cube body into context (~size^3 lines,
  zero legible signal) — inspect via grade-compare (see it) or cube-validate
  (ok/size), read the manifest description for meaning; plus both authoring
  paths and the parametric-vs-film-stock ceiling note.

Verified: media-use 116/116, lint + format clean; smokes — --params builds a
valid frozen cube, grade --params returns a lut block, bad JSON and an oversized
--from cube are both rejected with no stray file.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(cli): grade-compare validates referenced LUTs, warns on no-op cells, caps candidates

Bug-bash follow-ups — grade-compare silently accepted bad input:

- Validate LUT *content*, not just existence: each referenced .cube is parsed
  with core's parseCubeLut (now exported from @hyperframes/core) and rejected
  with a per-cell error ("LUT for \"<label>\" is not a valid .cube: ..."). A
  file that exists but isn't a valid cube no longer renders a silent no-op cell.
- Warn on inactive cells: a grading that normalizes to inactive (e.g. a
  malformed {lut:12345}) emits a stderr warning naming the cell; the
  auto-prepended "original" baseline is intentionally inactive and stays silent.
  stdout remains valid JSON.
- Cap candidates at 16 (excluding baseline): over-cap input renders the first N
  and reports {truncated:true, total:M} on stdout + a stderr note — no silent
  drop, no unbounded giant sheet.

Verified: grade-compare 10/10; non-cube LUT → clear error; {lut:12345} → warning
+ ok; 20 cells → cells=17 truncated total=20; valid runs unchanged. Lint/format
clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* feat(cli): general `hyperframes compare` visual-variant primitive

Generalize grade-compare's "render N variants → one labeled sheet → the agent
looks and picks" loop into a standalone command that works on ANY variation
(font, layout, motion, grade, whole compositions) — the tool never needs to
know what differs.

- `hyperframes compare <path...> [--at <sec>] [--labels a,b,c] [--out] [--cols]
  [--json]`: renders each agent-authored composition variant through the real
  runtime (captureCompositionFrame) and stitches one labeled comparison sheet +
  JSON ({ok, sheet, rendered, variants, truncated?/total?}). 2+ paths required;
  caps at 16 with loud truncation. It presents, it does not judge — choosing is
  the caller's job.
- Factored the shared "render a labeled set → contact sheet" path so compare,
  grade-compare, and snapshot all sit on it (no duplication). grade-compare is
  now the first color-specific specialization of this primitive.
- New pathArgs util + contactSheet test; hyperframes-cli SKILL documents compare
  as the agent's "see your own renders and choose" primitive.

Verified: 26/26 across compare + grade-compare + snapshot + contactSheet (no
regressions); compare renders 3 variants into one visibly-distinct labeled
sheet; 2+-path error path clean; lint/format clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(ci): green the skills CI — skip ffmpeg tests when absent, oxfmt markdown

The "Test: skills" CI job runs bare `node --test` with no ffmpeg on PATH (by
design — skills tests are meant to be node-builtin-only). The grade-analyzer +
smart-grade tests shell to ffmpeg and were failing there with ENOENT. Guard
them to skip when ffmpeg isn't on PATH; they still run locally / where it is.

Also oxfmt README.md + hyperframes/media-use SKILL.md (the whole-repo
`oxfmt --check .` Format job caught markdown left unformatted by the rebase
conflict resolution).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(ci): skip core-conformance test when tsx is unavailable

The "Test: skills" CI job installs no deps, so the normalizeHfColorGrading
conformance test (which imports core's TS via `node --import tsx`) failed there.
Guard it to skip when tsx can't resolve; runs locally / in the deps-installed
Test job. Completes the skills-CI greening (the ffmpeg guards handled the rest).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(cli): escape grade-compare src double-quotes (CodeQL XSS) + Windows-safe compare test

- grade-compare built `<img src="...">` (double-quoted) with the single-quote
  escaper, leaving `"` unescaped — a `"` in the frame path could break out
  (CodeQL: incomplete HTML attribute sanitization). Use escapeXml for src.
- compare label test hard-coded POSIX paths that can't match on Windows; assert
  the derived labels (the subject); path resolution is covered elsewhere.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* refactor(media-use): generate LUT library from params (drop committed .cube files)

The 3 bundled .cube files were 733 lines each (2,199 total) and were themselves
buildCube output — pure repo bloat. Replace with compact per-look params in
luts/index.json, generated on resolve; add an optional `url` for future scanned
LUTs to be CDN-hosted + downloaded on demand (freezeUrl) instead of committed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* feat(media-use): serve library LUTs from CDN on-demand (static.heygen.ai/luts), params fallback

Looks now carry a CDN `url` (hosted at s3://heygen-public/luts → static.heygen.ai/luts/<id>.cube);
resolve downloads + validates + freezes on demand, like bgm/image. `params` stays
as the deterministic offline fallback (--local-only, or if the download fails), so
resolution is never blocked on the network. Provider prefers url, falls back to params.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

* fix(media-use): address #2041 review — atomic LUT writes, compare telemetry, follow-ups

- Atomic .cube writes: library provider (url + params) and the parametric
  generator now write to a .tmp path, validate, then rename, so a crash can
  never orphan an invalid .cube at the final path (was validate-after-write).
- track("media_use_resolve") now emits provenance.via (url/params-fallback/params).
- grade-compare + compare: --timeout flag (was hardcoded 5000) and a
  media_use_compare event (cells, truncated, total, render_ready_timed_out);
  openSettledCompositionPage now surfaces the render-ready timeout.
- compare staging skips node_modules/.git; --for gets an upfront existence check.
- Rec.709 luma comment; HYPERFRAMES_ANALYZE_TIMEOUT_MS override; measured note
  uses basename; LUT s3 hosting moved from index.json into luts/README.md.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H5k87mPZ4d6yiFwcWSb8Vv

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 22:20:16 -04:00

699 lines
21 KiB
TypeScript

import {
copyFileSync,
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { basename, dirname, extname, join, resolve } from "node:path";
import {
HF_COLOR_GRADING_ATTR,
isHfColorGradingActive,
normalizeHfColorGrading,
parseCubeLut,
serializeHfColorGrading,
} from "@hyperframes/core";
import { defineCommand } from "citty";
import sharp from "sharp";
import type { Example } from "./_examples.js";
import { openSettledCompositionPage, runFfmpegOnce } from "../capture/captureCompositionFrame.js";
import { findFFmpeg } from "../browser/ffmpeg.js";
import { c } from "../ui/colors.js";
import { normalizeErrorMessage } from "../utils/errorMessage.js";
import { displayPathFromBase, readOptionalString, resolveFromBase } from "../utils/pathArgs.js";
import { trackCompareSheet } from "../telemetry/events.js";
import { serveStaticProjectHtml } from "../utils/staticProjectServer.js";
import { withMeta } from "../utils/updateCheck.js";
const COMPOSITION_ID = "grade-compare";
const COMPOSITION_DURATION = "1";
const DEFAULT_CELL_WIDTH = 560;
const MAX_CANDIDATE_CELLS = 16;
const MAX_COLUMNS = 4;
const GRID_PADDING = 16;
const LABEL_HEIGHT = 32;
const FFMPEG_EXTRACT_TIMEOUT_MS = 30_000;
export interface GradeCompareCell {
label: string;
grading: unknown;
}
interface ParsedGradeCompareArgs {
framePath: string;
projectDir: string;
outPath: string;
source: { kind: "grades"; path: string } | { kind: "luts"; value: string };
json: boolean;
timeoutMs: number;
}
interface PreparedGradeCompareProject {
tempDir: string;
html: string;
cells: GradeCompareCell[];
}
export interface CandidateCellCapResult {
cells: GradeCompareCell[];
truncated: boolean;
total: number;
}
export interface GradeCompareSuccessPayload {
ok: true;
sheet: string;
cells: number;
truncated?: true;
total?: number;
}
interface GradeCompareHtmlOptions {
cells: readonly GradeCompareCell[];
frameSrc: string;
frameWidth: number;
frameHeight: number;
}
interface ReferenceFrame {
buffer: Buffer;
width: number;
height: number;
stagedName: string;
}
export const examples: Example[] = [
[
"Compare grade presets on one reference frame",
"hyperframes grade-compare --for frame.png --grades grades.json",
],
[
"Compare LUT files and print agent-friendly JSON",
"hyperframes grade-compare --for frame.png --luts looks/a.cube,looks/b.cube --json",
],
];
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function hasOwn(record: Record<string, unknown>, key: string): boolean {
return Object.prototype.hasOwnProperty.call(record, key);
}
function cloneRecord(record: Record<string, unknown>): Record<string, unknown> {
const next: Record<string, unknown> = {};
for (const [key, value] of Object.entries(record)) {
next[key] = value;
}
return next;
}
function escapeXml(value: string): string {
return value
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&apos;");
}
function escapeSingleQuotedAttr(value: string): string {
return value
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/'/g, "&#39;");
}
function validateCell(label: string, grading: unknown): GradeCompareCell {
if (!normalizeHfColorGrading(grading)) {
throw new Error(`Invalid color grading for cell "${label}"`);
}
return { label, grading };
}
export function warnInactiveGradingCells(cells: readonly GradeCompareCell[]): void {
for (const cell of cells) {
const normalized = normalizeHfColorGrading(cell.grading);
if (!isHfColorGradingActive(normalized)) {
console.error(
c.warn(`Warning: grading for "${cell.label}" is inactive/no-op — it will render ungraded`),
);
}
}
}
export function capCandidateCells(cells: readonly GradeCompareCell[]): CandidateCellCapResult {
const total = cells.length;
if (total <= MAX_CANDIDATE_CELLS) {
return { cells: [...cells], truncated: false, total };
}
console.error(
c.warn(
`Warning: ${total} candidate grades exceed the ${MAX_CANDIDATE_CELLS}-cell cap — rendering the first ${MAX_CANDIDATE_CELLS} of ${total}; re-run with fewer grades or split into multiple runs.`,
),
);
return { cells: cells.slice(0, MAX_CANDIDATE_CELLS), truncated: true, total };
}
export function buildGradeCompareSuccessPayload(
sheet: string,
cells: number,
capResult: CandidateCellCapResult,
): GradeCompareSuccessPayload {
if (!capResult.truncated) {
return { ok: true, sheet, cells };
}
return { ok: true, sheet, cells, truncated: true, total: capResult.total };
}
function serializedGradingForCell(cell: GradeCompareCell): string {
const normalized = normalizeHfColorGrading(cell.grading);
if (!normalized) {
throw new Error(`Invalid color grading for cell "${cell.label}"`);
}
return serializeHfColorGrading(normalized);
}
function lutSrcFromGrading(grading: unknown): string | null {
if (!isRecord(grading) || !hasOwn(grading, "lut")) return null;
const lut = grading.lut;
if (typeof lut === "string" && lut.trim()) return lut.trim();
if (!isRecord(lut)) return null;
const src = lut.src;
return typeof src === "string" && src.trim() ? src.trim() : null;
}
function rewriteGradingLutSrc(grading: unknown, src: string): unknown {
if (!isRecord(grading) || !hasOwn(grading, "lut")) return grading;
const lut = grading.lut;
const next = cloneRecord(grading);
if (typeof lut === "string") {
next.lut = { src };
return next;
}
if (isRecord(lut)) {
const nextLut = cloneRecord(lut);
nextLut.src = src;
next.lut = nextLut;
}
return next;
}
export function parseGradesFile(filePath: string): GradeCompareCell[] {
if (!existsSync(filePath)) {
throw new Error(`Grades file not found: ${filePath}`);
}
let parsed: unknown;
try {
parsed = JSON.parse(readFileSync(filePath, "utf-8"));
} catch (err) {
throw new Error(`Could not parse grades JSON: ${normalizeErrorMessage(err)}`);
}
if (!Array.isArray(parsed)) {
throw new Error("Grades file must be a JSON array of { label, grading } objects");
}
return parsed.map((entry, index) => {
if (!isRecord(entry)) {
throw new Error(`Grade entry ${index + 1} must be an object with label and grading`);
}
const label = entry.label;
if (typeof label !== "string" || !label.trim()) {
throw new Error(`Grade entry ${index + 1} must have a non-empty string label`);
}
if (!hasOwn(entry, "grading")) {
throw new Error(`Grade entry "${label}" must include a grading value`);
}
return validateCell(label, entry.grading);
});
}
export function resolveLutCells(luts: string): GradeCompareCell[] {
const paths = luts
.split(",")
.map((part) => part.trim())
.filter(Boolean);
if (paths.length === 0) {
throw new Error("--luts must include at least one LUT path");
}
return paths.map((lutPath) =>
validateCell(basename(lutPath, extname(lutPath)), { lut: { src: lutPath } }),
);
}
// The ungraded frame as a leading reference cell. An empty grading object
// normalizes to inactive, so the runtime renders the source image untouched —
// giving the agent a baseline to judge every candidate look against.
export function prependBaselineCell(cells: GradeCompareCell[]): GradeCompareCell[] {
return [validateCell("original", {}), ...cells];
}
export function parseGradeCompareArgs(args: {
for?: unknown;
grades?: unknown;
luts?: unknown;
project?: unknown;
out?: unknown;
json?: unknown;
timeout?: unknown;
}): ParsedGradeCompareArgs {
const frameArg = readOptionalString(args.for);
if (!frameArg) throw new Error("--for <path> is required");
const gradesArg = readOptionalString(args.grades);
const lutsArg = readOptionalString(args.luts);
if (Boolean(gradesArg) === Boolean(lutsArg)) {
throw new Error("Exactly one of --grades or --luts is required");
}
const projectDir = resolve(readOptionalString(args.project) ?? process.cwd());
const framePath = resolveFromBase(projectDir, frameArg);
const outPath = resolveFromBase(projectDir, readOptionalString(args.out) ?? "grade-compare.png");
return {
framePath,
projectDir,
outPath,
source: gradesArg
? { kind: "grades", path: resolveFromBase(projectDir, gradesArg) }
: { kind: "luts", value: lutsArg ?? "" },
json: args.json === true,
timeoutMs: Number.parseInt(readOptionalString(args.timeout) ?? "", 10) || 5000,
};
}
function gridMetrics(
cellCount: number,
frameWidth: number,
frameHeight: number,
): {
columns: number;
rows: number;
cellImageWidth: number;
cellImageHeight: number;
width: number;
height: number;
} {
const columns = Math.max(1, Math.min(MAX_COLUMNS, Math.ceil(Math.sqrt(cellCount))));
const rows = Math.ceil(cellCount / columns);
const cellImageWidth = DEFAULT_CELL_WIDTH;
const aspect = frameHeight > 0 && frameWidth > 0 ? frameHeight / frameWidth : 9 / 16;
const cellImageHeight = Math.max(1, Math.round(cellImageWidth * aspect));
return {
columns,
rows,
cellImageWidth,
cellImageHeight,
width: columns * cellImageWidth + (columns + 1) * GRID_PADDING,
height: rows * (cellImageHeight + LABEL_HEIGHT) + (rows + 1) * GRID_PADDING,
};
}
export function buildGradeCompareHtml(options: GradeCompareHtmlOptions): string {
if (options.cells.length === 0) {
throw new Error("At least one grade cell is required");
}
const metrics = gridMetrics(options.cells.length, options.frameWidth, options.frameHeight);
const cellHtml = options.cells
.map((cell, index) => {
const serialized = escapeSingleQuotedAttr(serializedGradingForCell(cell));
const label = escapeXml(cell.label);
const row = Math.floor(index / metrics.columns);
const col = index % metrics.columns;
const left = GRID_PADDING + col * (metrics.cellImageWidth + GRID_PADDING);
const top = GRID_PADDING + row * (metrics.cellImageHeight + LABEL_HEIGHT + GRID_PADDING);
return ` <figure class="grade-cell" style="left:${left}px;top:${top}px;width:${metrics.cellImageWidth}px;height:${metrics.cellImageHeight + LABEL_HEIGHT}px">
<figcaption>${label}</figcaption>
<img src="${escapeXml(options.frameSrc)}" ${HF_COLOR_GRADING_ATTR}='${serialized}' alt="${label}" />
</figure>`;
})
.join("\n");
return `<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=${metrics.width}, height=${metrics.height}" />
<title>HyperFrames Grade Compare</title>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js"></script>
<style>
html,
body {
margin: 0;
width: ${metrics.width}px;
height: ${metrics.height}px;
overflow: hidden;
background: #191919;
font-family: Arial, Helvetica, sans-serif;
}
#${COMPOSITION_ID} {
position: relative;
width: ${metrics.width}px;
height: ${metrics.height}px;
overflow: hidden;
background: #191919;
}
.grade-cell {
position: absolute;
margin: 0;
background: #0f0f0f;
}
.grade-cell figcaption {
height: ${LABEL_HEIGHT}px;
line-height: ${LABEL_HEIGHT}px;
padding: 0 10px;
box-sizing: border-box;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
color: #ffffff;
background: #111111;
font-size: 14px;
font-weight: 700;
}
.grade-cell img {
display: block;
width: ${metrics.cellImageWidth}px;
height: ${metrics.cellImageHeight}px;
object-fit: contain;
background: #000000;
}
</style>
</head>
<body>
<div
id="${COMPOSITION_ID}"
data-composition-id="${COMPOSITION_ID}"
data-start="0"
data-duration="${COMPOSITION_DURATION}"
data-width="${metrics.width}"
data-height="${metrics.height}"
>
${cellHtml}
<script>
window.__timelines = window.__timelines || {};
window.__timelines["${COMPOSITION_ID}"] = gsap.timeline({ paused: true });
</script>
</div>
</body>
</html>
`;
}
function frameFileNameForPath(framePath: string): string {
const ext = extname(framePath).toLowerCase();
if (ext === ".jpg" || ext === ".jpeg") return `frame${ext}`;
return "frame.png";
}
export async function prepareGradeCompareTempProject(opts: {
projectDir: string;
framePath: string;
frameBuffer: Buffer;
cells: readonly GradeCompareCell[];
frameWidth: number;
frameHeight: number;
frameFileName?: string;
}): Promise<PreparedGradeCompareProject> {
const tempDir = mkdtempSync(join(tmpdir(), "hf-grade-compare-"));
try {
const frameFileName = opts.frameFileName ?? frameFileNameForPath(opts.framePath);
writeFileSync(join(tempDir, frameFileName), opts.frameBuffer);
let lutIndex = 0;
const stagedCells = opts.cells.map((cell) => {
const lutSrc = lutSrcFromGrading(cell.grading);
if (!lutSrc) return cell;
const sourcePath = resolveFromBase(opts.projectDir, lutSrc);
if (!existsSync(sourcePath)) {
throw new Error(`LUT file not found for "${cell.label}": ${sourcePath}`);
}
const lutText = readFileSync(sourcePath, "utf-8");
try {
parseCubeLut(lutText, { maxSize: 64 });
} catch (err) {
throw new Error(
`LUT for "${cell.label}" is not a valid .cube: ${normalizeErrorMessage(err)}`,
);
}
const lutExt = extname(sourcePath) || ".cube";
const stagedName = `lut-${lutIndex}${lutExt}`;
lutIndex += 1;
copyFileSync(sourcePath, join(tempDir, stagedName));
return {
label: cell.label,
grading: rewriteGradingLutSrc(cell.grading, stagedName),
};
});
const html = buildGradeCompareHtml({
cells: stagedCells,
frameSrc: frameFileName,
frameWidth: opts.frameWidth,
frameHeight: opts.frameHeight,
});
writeFileSync(join(tempDir, "index.html"), html);
return { tempDir, html, cells: stagedCells };
} catch (err) {
rmSync(tempDir, { recursive: true, force: true });
throw err;
}
}
function isVideoPath(filePath: string): boolean {
const ext = extname(filePath).toLowerCase();
return [".mp4", ".mov", ".m4v", ".webm", ".mkv", ".avi", ".mpeg", ".mpg", ".ogv"].includes(ext);
}
async function extractVideoFrameToBuffer(videoPath: string): Promise<Buffer | null> {
const tmp = mkdtempSync(join(tmpdir(), "hf-grade-compare-frame-"));
const outPath = join(tmp, "frame.png");
try {
const ffmpegPath = findFFmpeg();
if (!ffmpegPath) return null;
const args = [
"-hide_banner",
"-loglevel",
"error",
"-ss",
"0",
"-i",
videoPath,
"-frames:v",
"1",
"-q:v",
"2",
"-y",
outPath,
];
const result = await runFfmpegOnce(ffmpegPath, args, FFMPEG_EXTRACT_TIMEOUT_MS);
if (result.timedOut) {
throw new Error(`ffmpeg timed out extracting first frame from ${videoPath}`);
}
if (result.code !== 0 || !existsSync(outPath)) {
const detail = result.stderr.trim() ? `: ${result.stderr.trim()}` : "";
throw new Error(`ffmpeg could not extract first frame from ${videoPath}${detail}`);
}
return readFileSync(outPath);
} finally {
rmSync(tmp, { recursive: true, force: true });
}
}
async function loadReferenceFrame(framePath: string): Promise<ReferenceFrame> {
if (!existsSync(framePath)) {
throw new Error(`Reference frame not found: ${framePath}`);
}
const buffer = isVideoPath(framePath)
? await extractVideoFrameToBuffer(framePath)
: readFileSync(framePath);
if (!buffer) {
throw new Error(`Could not extract a frame from video: ${framePath}`);
}
const metadata = await sharp(buffer).metadata();
if (!metadata.width || !metadata.height) {
throw new Error(`Could not read reference frame dimensions: ${framePath}`);
}
return {
buffer,
width: metadata.width,
height: metadata.height,
stagedName: isVideoPath(framePath) ? "frame.png" : frameFileNameForPath(framePath),
};
}
async function captureGradeCompareSheet(
projectDir: string,
timeoutMs: number,
): Promise<{ sheetPath: string; renderReadyTimedOut: boolean }> {
const { bundleToSingleHtml } = await import("@hyperframes/core/compiler");
const html = await bundleToSingleHtml(projectDir);
const server = await serveStaticProjectHtml(projectDir, html);
const sheetPath = join(projectDir, "grade-compare.png");
try {
const {
browser: chromeBrowser,
page,
renderReadyTimedOut,
} = await openSettledCompositionPage(html, server.url, {
renderReadyTimeoutMs: timeoutMs,
renderReadyWarningSuffix: "grade comparison may be inaccurate",
});
try {
await page.screenshot({ path: sheetPath, type: "png" });
return { sheetPath, renderReadyTimedOut };
} finally {
await chromeBrowser.close();
}
} finally {
await server.close();
}
}
function printJson(payload: object): void {
console.log(JSON.stringify(withMeta(payload), null, 2));
}
export default defineCommand({
meta: {
name: "grade-compare",
description: "Render candidate color grades onto a reference frame as one comparison PNG",
},
args: {
for: {
type: "string",
description: "Reference image path, or a video path to sample at t=0",
required: true,
},
grades: {
type: "string",
description: "JSON array of { label, grading } candidate grades",
},
luts: {
type: "string",
description: "Comma-separated .cube LUT paths to compare",
},
project: {
type: "string",
description: "Base directory for relative --for, --grades, and LUT paths (default: cwd)",
},
out: {
type: "string",
description: "Output PNG path (default: <project>/grade-compare.png)",
},
json: {
type: "boolean",
description: "Output result as JSON",
default: false,
},
timeout: {
type: "string",
description: "Render-ready timeout in ms before capture (default: 5000)",
},
baseline: {
type: "boolean",
description:
"Prepend the ungraded frame as an 'original' reference cell (--no-baseline to omit)",
default: true,
},
},
async run({ args }) {
const jsonRequested = args.json === true;
let preparedDir: string | null = null;
try {
const parsed = parseGradeCompareArgs({
for: args.for,
grades: args.grades,
luts: args.luts,
project: args.project,
out: args.out,
json: args.json,
timeout: args.timeout,
});
let cells =
parsed.source.kind === "grades"
? parseGradesFile(parsed.source.path)
: resolveLutCells(parsed.source.value);
if (cells.length === 0) {
throw new Error("At least one grade candidate is required");
}
const capResult = capCandidateCells(cells);
cells = capResult.cells;
warnInactiveGradingCells(cells);
if (args.baseline !== false) {
cells = prependBaselineCell(cells);
}
if (!parsed.json) {
console.log(
`${c.accent("◆")} Rendering ${cells.length} grade candidates from ${c.accent(basename(parsed.framePath))}`,
);
}
const frame = await loadReferenceFrame(parsed.framePath);
const prepared = await prepareGradeCompareTempProject({
projectDir: parsed.projectDir,
framePath: parsed.framePath,
frameBuffer: frame.buffer,
frameWidth: frame.width,
frameHeight: frame.height,
frameFileName: frame.stagedName,
cells,
});
preparedDir = prepared.tempDir;
const { sheetPath: tempSheet, renderReadyTimedOut } = await captureGradeCompareSheet(
prepared.tempDir,
parsed.timeoutMs,
);
mkdirSync(dirname(parsed.outPath), { recursive: true });
copyFileSync(tempSheet, parsed.outPath);
trackCompareSheet({
command: "grade-compare",
cells: prepared.cells.length,
truncated: capResult.truncated,
total: capResult.total,
renderReadyTimedOut,
});
const sheet = displayPathFromBase(parsed.projectDir, parsed.outPath);
if (parsed.json) {
printJson(buildGradeCompareSuccessPayload(sheet, prepared.cells.length, capResult));
} else {
console.log(`\n${c.success("◇")} Grade comparison saved to ${sheet}`);
}
} catch (err) {
const message = normalizeErrorMessage(err);
if (jsonRequested) {
printJson({ ok: false, error: message });
} else {
console.error(`\n${c.error("✗")} Grade compare failed: ${message}`);
}
process.exit(1);
} finally {
if (preparedDir) {
rmSync(preparedDir, { recursive: true, force: true });
}
}
},
});