// The frame scan's per-video files (release 30, lane P). // // channels//data//frame-scan..json the timeline sidecar // (readings, spoken numbers, the derived series/events/segments) — TEXT, // on the corpus disk with every other sidecar. Never published: nothing // in the export build reads it. // channels//media//frames// the frame CACHE — // JPEGs and their index, on the channel's MEDIA side (the media tier's // directory, which may be a link to another drive), so a 4-hour stream's // frames never land on /home when the channel's media lives elsewhere. // A classic channel (no `media/`) keeps them in `data//frames/`. // Evictable at any time (`frame-scan {evict: true}`): a re-run decodes // them again, and readings already in the sidecar are not asked again. // Deleting the video removes `media//` and so the cache with it. import path from "node:path"; import { lstat } from "node:fs/promises"; import { sidecar, sidecarField, type Sidecar } from "./sidecar-server"; import { FRAME_PROFILE_IDS } from "./frames/profiles"; import { FRAME_SCAN_FORMAT, type Derived, type SpokenReading } from "./frames/types"; import { coerceCorrections, type ReadingCorrection } from "./frames/corrections"; export const frameScanFilename = (profile: string) => `frame-scan.${profile}.json`; export type FrameScanRun = { at: string; jobId?: string; model: string; route: "api" | "local"; frames: number; read: number; failed: number; costUsd: number; stopped?: "cap" | "cancelled" | "errors" | "maxFrames"; }; export type FrameScanRecord = { format: typeof FRAME_SCAN_FORMAT; version: 1; profile: string; source: { savedVideo: string; durationSeconds: number | null }; sampling: { everySeconds: number; from: number; to: number; width: number; keyframesOnly: boolean; crop?: [number, number, number, number]; frames: number; kept: number; skippedDuplicates: number }; model: { route: "api" | "local"; model: string; promptVersion: number }; // What the model was told (the hero's screen name), kept for the next run and the derivation. hints?: { hero?: string }; costUsd: number; runs: FrameScanRun[]; readings: ({ t: number; phase: string } & Record)[]; // Frames not read because they duplicate a read one: [t, the read frame's t]. duplicates: [number, number][]; spoken: SpokenReading[]; derived: Derived; updatedAt: string; // When `derived` was last worked out again from the readings (no model). rederivedAt?: string; // A person's corrections of readings (release 32, lib/frames/corrections.ts): // kept beside the model's readings, laid over them by the derivation. corrections?: ReadingCorrection[]; }; const isObj = (v: unknown): v is Record => !!v && typeof v === "object" && !Array.isArray(v); export function coerceFrameScan(v: unknown): FrameScanRecord | null { if (!isObj(v) || v.format !== FRAME_SCAN_FORMAT || typeof v.profile !== "string") return null; if (!Array.isArray(v.readings) || !isObj(v.derived) || !isObj(v.sampling) || !isObj(v.source)) return null; const d = v.derived as Record; if (!Array.isArray(d.tournaments) || !Array.isArray(d.events) || !Array.isArray(d.segments)) return null; const corrections = coerceCorrections(v.corrections); const { corrections: _raw, ...rest } = v as Record; void _raw; return { ...(rest as unknown as FrameScanRecord), ...(corrections.length ? { corrections } : {}), runs: Array.isArray(v.runs) ? (v.runs as FrameScanRun[]) : [], spoken: Array.isArray(v.spoken) ? (v.spoken as SpokenReading[]) : [], duplicates: Array.isArray(v.duplicates) ? (v.duplicates as [number, number][]).filter((p) => Array.isArray(p) && p.length === 2) : [], derived: { ...(d as unknown as Derived), flags: Array.isArray(d.flags) ? (d.flags as Derived["flags"]) : [], misreads: Array.isArray(d.misreads) ? (d.misreads as Derived["misreads"]) : [] }, costUsd: typeof v.costUsd === "number" ? v.costUsd : 0, }; } // One declaration per profile, at load (sidecar()'s naming guard runs then). const SIDECARS: Record> = Object.fromEntries( FRAME_PROFILE_IDS.map((p) => [p, sidecar(frameScanFilename(p), sidecarField(coerceFrameScan), { indent: 0 })]), ); export function frameScanSidecar(profile: string): Sidecar { const s = SIDECARS[profile]; if (!s) throw new Error(`"${profile}" is not a frame-scan profile`); return s; } export const loadFrameScan = (videoDir: string, profile: string) => frameScanSidecar(profile).load(videoDir); /** Every profile's sidecar this video has. */ export async function loadFrameScans(videoDir: string): Promise { const out: FrameScanRecord[] = []; for (const p of FRAME_PROFILE_IDS) { const r = await loadFrameScan(videoDir, p); if (r) out.push(r); } return out; } /** Where a video's frame cache for a profile lives (see the header). */ export async function frameCacheDirOf(channelsDir: string, slug: string, id: string, profile: string): Promise { const media = path.join(channelsDir, slug, "media"); const tiered = await lstat(media).then( (l) => l.isDirectory() || l.isSymbolicLink(), () => false, ); return tiered ? path.join(media, id, "frames", profile) : path.join(channelsDir, slug, "data", id, "frames", profile); }