// REELS TRACK THEIR SOURCE (lane/reel-bench, 2 Nov). // // A reel is an edit of a rendered master that keeps changing: a new take, a // clip's edges moved, a clip taken out of the cut, the chrome or the fx over // a clip changed. A beat stays ADDRESSED in its clip's source seconds -- that // is what survives a re-render -- and carries an ANCHOR: what it was cut // against when it was made or last edited. The anchor is provenance, never // the address: // // take, build, masterMtimeMs the take, its build (take.json builtAt), and // its master's mtime // clip { start, end, gaps } the source seconds the clip's segment played // (its cut record: start/end, the gaps it left out) // seg the segment's content key (the cut record's // segKey: the picture and sound of the clip) // fx a fingerprint of the chrome and the fx over // the clip's segment (the schedule's entry for it // and the receipts/zoom it carries, timed from the // segment's start -- so a shift is not a change) // master { start, end } the master span the beat resolved to // at when // // On load each beat is CLASSIFIED against the take the reel plays now // (classifyBeat, pure): // // unanchored written before anchors (or by hand): nothing to compare // orphan its clip is gone from the cut -- remove it, or re-point it // clipped the clip's new limits cut into the beat -- clamp, or re-place // edges the clip's edges or gaps moved; the beat still fits (old vs new) // look the chrome or fx in its span changed -- look at it // rerendered another render, this clip identical -- still good // unchanged the very master it was cut against // // Plain ESM: the page's view, the writer's route and `umtool trailer-beats // status` read it. import { createHash } from "node:crypto"; import { readFile } from "node:fs/promises"; import path from "node:path"; import { recordGaps } from "umtool-report-to-video/gaps"; import { TrailerError, loadTakeBuild, parseBeats, planTrailer } from "umtool-report-to-video/trailer"; export const BEAT_STATES = Object.freeze(["unanchored", "orphan", "clipped", "edges", "look", "rerendered", "unchanged"]); /** The states that ask for a person: the walk's "changed beats" filter. */ export const CHANGED_STATES = Object.freeze(["orphan", "clipped", "edges", "look"]); const r3 = (n) => Number(Number(n).toFixed(3)); const EPS = 0.02; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const sha = (v) => createHash("sha256").update(JSON.stringify(v)).digest("hex").slice(0, 16); /** The chrome and fx over one clip's segment, timed from the segment's start: their fingerprint. */ export function fxFingerprint(schedule, id) { const seg = (schedule?.segments ?? []).find((s) => s?.id === id); if (!seg) return null; const { start, end, duration, ...chrome } = seg; const rel = (t) => (Number.isFinite(t) ? r3(t - start) : t); const fx = {}; for (const [k, v] of Object.entries(schedule?.fx ?? {})) { if (!isObj(v) || !Array.isArray(v.clips)) continue; const mine = v.clips .filter((c) => c?.segment === id) .map((c) => ({ ...c, in: rel(c.in), out: rel(c.out), markAt: rel(c.markAt), cards: (c.cards ?? []).map((x) => ({ ...x, at: rel(x.at) })) })); if (mine.length) fx[k] = { ...v, clips: mine }; } return sha({ chrome, fx }); } /** What a take's build says of one clip now: present, the source span its segment plays, its content key, its fx. */ export function clipSnapshot(build, id) { const rec = build.records.get(id); const inSchedule = (build.schedule?.segments ?? []).some((s) => s?.id === id); if (!rec || !inSchedule || !Number.isFinite(rec.start) || !Number.isFinite(rec.end)) return { present: false }; return { present: true, clip: { start: r3(rec.start), end: r3(rec.end), gaps: recordGaps(rec).map((g) => ({ from: r3(g.from), to: r3(g.to) })) }, seg: typeof rec.segKey === "string" ? rec.segKey : sha({ start: rec.start, end: rec.end, gaps: recordGaps(rec) }), fx: fxFingerprint(build.schedule, id), }; } /** * A take's anchoring context: its build, and `anchorFor(beat)` -- the anchor * a beat gets when it is written against this take (null when the beat does * not map onto it: the beat is written without one, and says so on load). * * @param {string} takeDir * @param {{ take: string, masterMtimeMs?: number | null, now?: () => string, build?: any }} o `build`: loadTakeBuild's, when it is read already */ export async function anchorContext(takeDir, { take, masterMtimeMs = null, now = () => new Date().toISOString(), build: loaded = null }) { const build = loaded ?? (await loadTakeBuild(takeDir)); const meta = await readFile(path.join(takeDir, "take.json"), "utf8").then(JSON.parse, () => null); const buildId = typeof meta?.builtAt === "string" ? meta.builtAt : null; const anchorFor = (beat) => { let piece = null; try { const [b] = parseBeats([beat]); piece = planTrailer({ schedule: build.schedule, records: build.records, beats: [b], xfade: 0, snap: false }).pieces[0]; } catch (e) { if (e instanceof TrailerError) return null; throw e; } const base = { take, build: buildId, masterMtimeMs, master: { start: piece.master.start, end: piece.master.end }, at: now() }; if (beat.id === undefined) return base; const s = clipSnapshot(build, beat.id); if (!s.present) return null; return { ...base, clip: s.clip, seg: s.seg, fx: s.fx }; }; // Named by its take, for the history the writer keeps (trailerBeats.mjs anchorTake). anchorFor.take = take; return { take, build: buildId, masterMtimeMs, schedule: build.schedule, records: build.records, anchorFor, snapshot: (id) => clipSnapshot(build, id) }; } const sameGaps = (a = [], b = []) => a.length === b.length && a.every((g, i) => Math.abs(g.from - b[i].from) < EPS && Math.abs(g.to - b[i].to) < EPS); /** * One beat against the take it plays now. Pure. * * @param {Record} beat the reel file's beat (its `anchor`, if any) * @param {{ take: string, build: string | null, masterMtimeMs: number | null, snapshot: (id: string) => ReturnType }} now * @returns {{ state: string, why: string, was?: any, now?: any }} */ export function classifyBeat(beat, now) { const a = isObj(beat?.anchor) ? beat.anchor : null; const isClip = beat?.id !== undefined; const snap = isClip ? now.snapshot(beat.id) : null; // Gone from the cut is said whether or not the beat was anchored. if (isClip && !snap.present) return { state: "orphan", why: `${beat.id} is not in take ${now.take}'s cut any more — remove the beat, or re-point it to another clip`, was: a?.clip ?? null }; if (isClip) { const c = snap.clip; if (beat.start < c.start - EPS || beat.end > c.end + EPS) { return { state: "clipped", why: `${beat.id} now plays source ${c.start}–${c.end}${a?.clip ? ` (was ${a.clip.start}–${a.clip.end})` : ""}: the beat ${beat.start}–${beat.end} reaches past it — clamp it, or re-place it`, was: a?.clip ?? null, now: c, }; } } if (!a) return { state: "unanchored", why: "written before beats were anchored: nothing to compare (anchor it to this take)" }; if (isClip) { const c = snap.clip; if (a.clip && (Math.abs(a.clip.start - c.start) > EPS || Math.abs(a.clip.end - c.end) > EPS || !sameGaps(a.clip.gaps, c.gaps))) { const moved = [ Math.abs(a.clip.start - c.start) > EPS ? `start ${a.clip.start} → ${c.start}` : null, Math.abs(a.clip.end - c.end) > EPS ? `end ${a.clip.end} → ${c.end}` : null, !sameGaps(a.clip.gaps, c.gaps) ? "its gaps" : null, ].filter(Boolean); return { state: "edges", why: `${beat.id}'s ${moved.join(", ")} since take ${a.take}; the beat still fits — check its edges`, was: a.clip, now: c }; } if ((a.fx ?? null) !== (snap.fx ?? null) || (a.seg && snap.seg && a.seg !== snap.seg)) { return { state: "look", why: `the ${a.fx !== snap.fx ? "chrome or fx" : "picture or sound"} over ${beat.id} changed since take ${a.take} — look at it`, was: { seg: a.seg, fx: a.fx }, now: { seg: snap.seg, fx: snap.fx } }; } } if (a.take !== now.take || (a.build ?? null) !== (now.build ?? null) || (a.masterMtimeMs ?? null) !== (now.masterMtimeMs ?? null)) { return { state: "rerendered", why: `cut against take ${a.take}${a.take === now.take ? " (rendered again since)" : ""}; ${isClip ? `${beat.id} is the same in take ${now.take}` : "a master beat: its seconds are as written"}` }; } return { state: "unchanged", why: `the master it was cut against (take ${a.take})` }; } /** * Every beat of a reel against the take it plays now, and the reel's line: * which takes it was cut against, and the beats that changed. * * @param {any[]} beats * @param {Parameters[1]} now */ export function reelStatus(beats, now) { const rows = (beats ?? []).map((b, i) => ({ n: i + 1, id: b?.id ?? null, ...classifyBeat(b, now) })); const against = [...new Set((beats ?? []).map((b) => b?.anchor?.take).filter((t) => typeof t === "string"))]; const changed = rows.filter((r) => CHANGED_STATES.includes(r.state)).map((r) => r.n); const counts = Object.fromEntries(BEAT_STATES.map((s) => [s, rows.filter((r) => r.state === s).length])); const others = against.filter((t) => t !== now.take); const line = !rows.length ? "no beats" : others.length ? `cut against ${others.join(", ")}; take ${now.take} ${changed.length ? `changes beat${changed.length === 1 ? "" : "s"} ${changed.join(", ")}` : "changes none of them"}` : changed.length ? `take ${now.take} changes beat${changed.length === 1 ? "" : "s"} ${changed.join(", ")}` : counts.unanchored === rows.length ? "no beat is anchored yet" : `every anchored beat matches take ${now.take}`; return { take: now.take, against, changed, counts, line, rows }; } /** * The fix a status offers, as a beat patch (or null): `clamp` pulls a clipped * beat inside the clip's new limits (its gaps kept where they still fit), * `repoint` names another clip of the cut for it (the seconds kept; refused * by the writer when they do not fit that clip). * * @param {Record} beat * @param {{ clip: { start: number, end: number } }} snap clipSnapshot's, present */ export function clampPatch(beat, snap) { const start = Math.max(beat.start, snap.clip.start); const end = Math.min(beat.end, snap.clip.end); if (!(end - start >= 0.2)) return null; const gaps = (beat.gaps ?? []).filter((g) => g.from >= start + 0.5 && g.to <= end - 0.5); return { start: Number(start.toFixed(2)), end: Number(end.toFixed(2)), ...(beat.gaps ? { gaps } : {}) }; }