/** * Contact sheets for a `--hub` run: one PNG per state, built with ffmpeg. * Phone = light|dark at 390 (plus 360 light|dark under --audit) side by side; * desktop = one image scaled to 1100 wide. */ import { execFileSync } from 'node:child_process' import * as path from 'node:path' import { imageName, routeSlug, type ShotRecord } from './report.js' const PHONE_SHEET_HEIGHT = 1100 const DESKTOP_SHEET_WIDTH = 1100 export interface SheetPlan { readonly output: string readonly inputs: readonly string[] } /** The ffmpeg argv that joins `inputs` into `output`. */ export function sheetArgs(plan: SheetPlan): string[] { const inputs = plan.inputs.flatMap((f) => ['-i', f]) if (plan.inputs.length === 1) { return [ '-y', '-loglevel', 'error', ...inputs, '-vf', `scale=${DESKTOP_SHEET_WIDTH}:-2`, '-frames:v', '1', plan.output, ] } const scales = plan.inputs.map((_, i) => `[${i}]scale=-2:${PHONE_SHEET_HEIGHT}[s${i}]`).join(';') const joined = plan.inputs.map((_, i) => `[s${i}]`).join('') const stack = plan.inputs.length === 2 ? 'hstack' : `hstack=inputs=${plan.inputs.length}` return [ '-y', '-loglevel', 'error', ...inputs, '-filter_complex', `${scales};${joined}${stack}`, '-frames:v', '1', plan.output, ] } /** Which sheets the shots allow, per route: phone 390 (+360), desktop. */ export function planSheets(shots: readonly ShotRecord[], outDir: string): SheetPlan[] { const plans: SheetPlan[] = [] const routes = [...new Set(shots.map((s) => s.route))] for (const route of routes) { const has = (vp: string, theme: string): boolean => shots.some((s) => s.route === route && s.viewport.name === vp && s.theme === theme) const imageOf = (vp: string, theme: 'light' | 'dark'): string | null => { const s = shots.find((x) => x.route === route && x.viewport.name === vp && x.theme === theme) return s === undefined ? null : path.join(outDir, imageName(route, s.viewport, theme)) } const phone = (['phone', 'phone-360'] as const).flatMap((vp) => has(vp, 'light') && has(vp, 'dark') ? [imageOf(vp, 'light'), imageOf(vp, 'dark')].flatMap((f) => (f === null ? [] : [f])) : [], ) if (phone.length >= 2) { plans.push({ output: path.join(outDir, `${routeSlug(route)}--sheet-phone.png`), inputs: phone, }) } const desktop = imageOf('desktop', has('desktop', 'light') ? 'light' : 'dark') if (desktop !== null && (has('desktop', 'light') || has('desktop', 'dark'))) { plans.push({ output: path.join(outDir, `${routeSlug(route)}--sheet-desktop.png`), inputs: [desktop], }) } } return plans } /** Build every sheet; without ffmpeg print a note and skip. Returns the files written. */ export function buildSheets( shots: readonly ShotRecord[], outDir: string, log: (line: string) => void, ): string[] { try { execFileSync('ffmpeg', ['-version'], { stdio: 'ignore' }) } catch { log(' note: ffmpeg not found, contact sheets skipped') return [] } const written: string[] = [] for (const plan of planSheets(shots, outDir)) { try { execFileSync('ffmpeg', sheetArgs(plan), { stdio: 'ignore' }) written.push(plan.output) } catch (err: unknown) { log( ` note: ffmpeg failed for ${path.basename(plan.output)}: ${err instanceof Error ? err.message : String(err)}`, ) } } return written }