// What a beat reel's Play view, walk and Cut panel read: GET /api/report/reel // (lib/report/reelPlay.mjs reelPlayView, plus the reels and the cut job). // Types and pure helpers only, so a client and a server page both import it. import { sourceAtOffset, offsetIn } from "umtool-report-to-video/gaps"; export type ReelPiece = { at: number; master: { start: number; end: number; duration: number }; frames: { start: number; count: number }; seamless: boolean; moved: unknown; source: { start: number; end: number } | null; /** A gapped beat: the master spans its gaps leave out (hard cuts), and the seconds it plays. */ cuts?: { from: number; to: number }[]; seconds?: number; }; export type ReelRow = { n: number; kind: "clip" | "raw"; id: string | null; label: string | null; verdict: "confirmed" | "note" | null; beat: { start?: number; end?: number; at?: number; dur?: number; gaps?: { from: number; to: number }[] }; problem: string | null; piece: ReelPiece | null; asked: { start: number; end: number } | null; limits: { start: number; end: number; source: { start: number; end: number } | null } | null; map: { segStart: number; recStart: number; gaps: { from: number; to: number }[] } | null; quote: string | null; title: string | null; channel: string | null; video: string | null; date: string | null; /** What the beat was cut against, read against the take now (lib/report/reelAnchor.mjs). */ status?: BeatStatus | null; }; export type BeatState = "unanchored" | "orphan" | "clipped" | "edges" | "look" | "rerendered" | "unchanged"; export type BeatStatus = { state: BeatState; why: string; was?: { start: number; end: number; gaps?: { from: number; to: number }[] } | Record | null; now?: { start: number; end: number; gaps?: { from: number; to: number }[] } | Record | null; }; /** The states the walk's "changed beats" filter keeps (reelAnchor.mjs CHANGED_STATES). */ export const CHANGED_STATES: BeatState[] = ["orphan", "clipped", "edges", "look"]; export const STATE_LABEL: Record = { unanchored: "not anchored", orphan: "clip gone", clipped: "clipped", edges: "clip's edges moved", look: "chrome changed", rerendered: "re-rendered, same", unchanged: "unchanged", }; export type ReelCut = { take: string; out: string; size: number; mtimeMs: number; record: string | null; builtAt: string; seconds: number | null; encodeSeconds: number | null; notes: number; stale: boolean; /** The reel was edited since this cut (its record names the reel's version). */ reelChanged?: boolean; }; export type ReelJob = { id: string; kind: string; state: "running" | "done" | "failed"; startedAt: number; endedAt: number | null; error: string | null; log: string[]; next: number; }; export type ReelListing = { name: string; file: string; exists: boolean; beats: number; title: string | null; limit: number; take: string | null; error: string | null }; export type ReelView = { project: string; reel: string; file: string; title: string | null; limit: number; xfade: number; introAsked: boolean; pinned: string | null; token: string | null; beats: number; takes: { id: string; label: string; built: boolean; rendering: boolean; draft: boolean; master: string | null; masterMtimeMs: number | null }[]; cuts: ReelCut[]; take: { id: string; why: string; label: string; slug?: string; master?: string; masterMtimeMs?: number | null; rendering?: boolean } | null; noMaster: { why: string; take: string | null } | null; fps?: number; transition?: number; masterSeconds?: number; intro?: { at: number; master: { start: number; end: number; duration: number } } | null; total?: number; rows?: ReelRow[]; /** The reel against the take now: the takes it was cut against, the beats that changed, the line. */ status?: { take: string; against: string[]; changed: number[]; counts: Record; line: string } | null; planNotes?: string[]; reels?: ReelListing[]; job?: ReelJob | null; busy?: string | null; error?: string; }; /** One run of the master the player plays: the intro, or a beat that maps. */ export type ReelItem = { key: string; n: number; at: number; mStart: number; mEnd: number; dur: number; seamless: boolean; row: ReelRow | null; cuts: { from: number; to: number }[] }; /** The master second `t` reel seconds into an item plays (its cuts skipped). */ export function masterAt(it: ReelItem, t: number): number { let m = it.mStart + Math.max(0, Math.min(it.dur, t - it.at)); for (const c of it.cuts) if (m >= c.from - 1e-6) m += c.to - c.from; return Math.min(it.mEnd, m); } /** The reel second a master second inside an item plays at (inside a cut: where the cut joins). */ export function reelAtMaster(it: ReelItem, m: number): number { let off = m - it.mStart; for (const c of it.cuts) { if (m >= c.to) off -= c.to - c.from; else if (m > c.from) off -= m - c.from; } return it.at + Math.max(0, Math.min(it.dur, off)); } /** The cut a master second is inside, if any. */ export const cutAt = (it: ReelItem, m: number) => it.cuts.find((c) => m >= c.from - 1e-3 && m < c.to - 1e-3) ?? null; /** The pieces in reel order. */ export function reelItems(view: ReelView): ReelItem[] { const out: ReelItem[] = []; if (view.intro) out.push({ key: "intro", n: 0, at: view.intro.at, mStart: view.intro.master.start, mEnd: view.intro.master.end, dur: view.intro.master.duration, seamless: false, row: null, cuts: [] }); for (const r of view.rows ?? []) { if (!r.piece) continue; out.push({ key: `b${r.n}`, n: r.n, at: r.piece.at, mStart: r.piece.master.start, mEnd: r.piece.master.end, dur: r.piece.seconds ?? r.piece.master.duration, seamless: r.piece.seamless, row: r, cuts: r.piece.cuts ?? [] }); } return out; } /** The item playing at reel second t: the last one started by then. */ export function itemAt(items: ReelItem[], t: number): number { let k = 0; for (let i = 0; i < items.length; i += 1) if (items[i].at <= t + 1e-6) k = i; return k; } /** * A master second as the source second it plays (a clip beat), through the * build record's gaps -- gaps.mjs, the cut's own mapper. At a gap's join the * master shows the material AFTER the gap, which is where a START edge there * begins; an END edge there ends the material BEFORE it (`edge: "end"`: the * gap's own start), or a round trip would stretch the beat across the gap. */ export function sourceOfMaster(row: ReelRow, masterT: number, edge: "start" | "end" = "start"): number | null { if (!row.map) return null; const off = masterT - row.map.segStart; if (edge === "end" && off > 0.002) return sourceAtOffset(off - 0.002, row.map.recStart, row.map.gaps) + 0.002; return sourceAtOffset(off, row.map.recStart, row.map.gaps); } /** A source second as the master second it plays at. */ export function masterOfSource(row: ReelRow, src: number): number | null { if (!row.map) return null; return row.map.segStart + offsetIn(src, row.map.recStart, row.map.gaps); } export const r2 = (n: number) => Math.round(n * 100) / 100; /** m:ss.s */ export function clock(s: number | null | undefined): string { if (s == null || !Number.isFinite(s)) return "—"; const neg = s < 0; const a = Math.abs(s); const m = Math.floor(a / 60); const r = a - m * 60; return `${neg ? "−" : ""}${m}:${r < 10 ? "0" : ""}${r.toFixed(1)}`; } /** h:mm:ss.s for a source second (a stream is hours long). */ export function sourceClock(s: number | null | undefined): string { if (s == null || !Number.isFinite(s)) return "—"; const h = Math.floor(s / 3600); const m = Math.floor((s - h * 3600) / 60); const r = s - h * 3600 - m * 60; return `${h ? `${h}:` : ""}${h && m < 10 ? "0" : ""}${m}:${r < 10 ? "0" : ""}${r.toFixed(2)}`; } export const masterSrc = (project: string, take: string, mtime: number | null | undefined) => `/api/report/reel/media?${new URLSearchParams({ project, take, what: "master", ...(mtime ? { v: String(mtime) } : {}) })}`; export const cutSrc = (project: string, take: string, reel: string, mtime: number | null | undefined) => `/api/report/reel/media?${new URLSearchParams({ project, take, what: "cut", reel, ...(mtime ? { v: String(mtime) } : {}) })}`; /** GET the reel's view. */ export async function readReelView(project: string, reel: string, take: string | null): Promise { const q = new URLSearchParams({ project, reel, ...(take ? { take } : {}) }); const r = await fetch(`/api/report/reel?${q}`, { cache: "no-store" }); const j = (await r.json().catch(() => ({}))) as ReelView; if (!r.ok) throw new Error(j.error ?? `HTTP ${r.status}`); return j; } /** One write to the reel's beats through their one writer (POST /api/report/trailer-beats). */ export async function reelWrite( project: string, reel: string, token: string | null, op: string, args: Record = {}, ): Promise<{ ok: true; token: string | null; undone?: string } | { ok: false; error: string; stale: boolean }> { const r = await fetch("/api/report/trailer-beats", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, reel, token, op, ...args }), }); const j = await r.json().catch(() => ({})); if (!r.ok) return { ok: false, error: j.stale ? "the reel changed since this page read it — read again; try again" : String(j.error ?? `HTTP ${r.status}`), stale: !!j.stale }; return { ok: true, token: j.token ?? null, ...(j.undone ? { undone: j.undone } : {}) }; }