// A VERTICAL SHORT'S PLAN (lane vertical): which of a take's items a short is // made of, and the part of each it plays -- from a reel's beats, or a list of // items. Pure: umtool's vertical command (lib/report/verticalCli.mjs) reads // the files and runs the build; this decides. // // A beat is one of the two shapes the trailer's beats file holds (trailer.mjs), or a whole item: // {id} an item of the take, whole (a post item read through, a clip as cut) // {id, start, end} a clip's SOURCE span: the clip, cut to those seconds // {at, dur} a span of the take's 16:9 MASTER: every item it covers // (the take's schedule.json says where each sits), a clip // cut to the source seconds under it (its cut record's // start), a post item built whole and played from the // part the span covers // // A clip's own `vertical` (crop, layout) and `side` are read from the // PROJECT's manifest over the take's: they are set with `umtool set` after the // take was made, and a short is cut from the take's items as the project now // frames them. import { zoomEnd } from "./clip-zoom.mjs"; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const num = (v) => typeof v === "number" && Number.isFinite(v); const r3 = (n) => Math.round(n * 1000) / 1000; export class VerticalPlanError extends Error {} /** A beats file's beats: an array, or `{beats: [...]}` (a reel's). */ export function beatsOf(doc) { const list = Array.isArray(doc) ? doc : Array.isArray(doc?.beats) ? doc.beats : null; if (!list) throw new VerticalPlanError("a beats file is an array of beats, or {beats: [...]}"); return list.map((b, i) => { if (isObj(b) && typeof b.id === "string" && num(b.start) && num(b.end) && b.end > b.start) return { kind: "clip", id: b.id, start: b.start, end: b.end }; if (isObj(b) && num(b.at) && num(b.dur) && b.dur > 0) return { kind: "master", at: b.at, dur: b.dur, label: typeof b.label === "string" ? b.label : null }; if (isObj(b) && typeof b.id === "string" && b.start === undefined && b.end === undefined) return { kind: "item", id: b.id }; throw new VerticalPlanError(`beat ${i + 1}: not {id, start, end} (a clip's source span), {at, dur} (a span of the master) or {id} (a whole item)`); }); } /** * The keys of a clip a short reads from the PROJECT over the take's: set with `umtool set` after the take was made * (its framing, its side post, the clip kit's keys). A batch's record snapshots them (lib/report/shortsBatch.mjs). */ export const PROJECT_KEYS = Object.freeze(["vertical", "side", "qr", "promo", "zooms", "coldOpen", "censor"]); /** * A clip's hidden regions from two sources, ADD-ONLY (clip-censor.mjs: a privacy control): the boxes of both, never * fewer than either had. A side that says none (absent, null) adds nothing; `false` (an opt-out) only stands when * the other side has no boxes either. */ export function mergeCensor(a, b) { const boxesOf = (v) => (isObj(v) && Array.isArray(v.boxes) ? v.boxes : []); const all = [...boxesOf(a)]; for (const x of boxesOf(b)) if (!all.some((y) => JSON.stringify(y) === JSON.stringify(x))) all.push(x); if (all.length) return { ...(isObj(b) ? b : {}), ...(isObj(a) ? a : {}), boxes: all }; if (a === false || b === false) return false; return a ?? b ?? undefined; } /** The project's per-clip framing (PROJECT_KEYS) laid over a take's entry; its hidden regions added to the take's. */ export function withProjectFraming(entry, projectById) { const own = projectById?.get?.(entry?.id); if (!own || entry?.type !== "clip") return entry; const out = { ...entry }; for (const k of PROJECT_KEYS) { if (k === "censor") { const m = mergeCensor(own.censor, entry.censor); if (m === undefined) delete out.censor; else out.censor = m; } else if (own[k] !== undefined) out[k] = own[k]; } return out; } /** * The project's entries with a snapshot laid over them (`{: {: value | null}}`, a batch record's): each * named key set (null: removed), so a rebuild reads each clip as the record saw it. Null: the entries as they are. */ export function withOverrides(timeline, overrides) { if (!isObj(overrides)) return timeline; return (timeline ?? []).map((e) => { const o = typeof e?.id === "string" && isObj(overrides[e.id]) ? overrides[e.id] : null; if (!o) return e; const out = { ...e }; for (const [k, v] of Object.entries(o)) { if (!PROJECT_KEYS.includes(k)) continue; // Hidden regions: the snapshot's and the clip's boxes now, both (a box added since a batch was built is kept). if (k === "censor") { const m = mergeCensor(e.censor, v); if (m === undefined) delete out.censor; else out.censor = m; continue; } if (v === null) delete out[k]; else out[k] = v; } return out; }); } /** A clip entry cut to the source span [a, b]: its gaps inside kept, its own cut points dropped. */ export function cutClip(entry, a, b, id = entry.id) { const { cutStart: _a, cutEnd: _b, ...rest } = entry; const gaps = (Array.isArray(entry.gaps) ? entry.gaps : []).filter((g) => g.to > a + 0.2 && g.from < b - 0.2); const out = { ...rest, id, start: r3(a), end: r3(b) }; if (gaps.length) out.gaps = gaps; else delete out.gaps; // A side post's span is seconds into the clip AS IT PLAYED in the take: moved by the cut's new start. if (isObj(entry.side) && num(entry.side.from) && num(entry.side.to) && num(entry.start) && a > entry.start) { const d = a - entry.start; const side = { ...entry.side, from: r3(Math.max(0, entry.side.from - d)), to: r3(entry.side.to - d) }; if (side.to - side.from >= 1) out.side = side; else delete out.side; } return out; } /** * The plan: `{timeline, pieces}`. `timeline` the entries the vertical build * makes (each id once); `pieces` the order they play in, each * `{id, from, to}` -- seconds of that entry's vertical segment, `to` null for * the whole of it. * * @param {{ timeline: any[], beats?: any[] | null, items?: string[] | null, schedule?: any, cuts?: Map, * project?: any[] }} o */ /** * A clip's COLD OPEN (lane clip-kit, slice G; clip-kit.mjs coldOpenErrors): its `coldOpen` span cut out as an entry * of its own, `~co`, played before it with a hard cut -- its own words captioned, its own sound -- and ending on * no card, carrying no code and no zooms outside its span; null for a clip with none. */ export function coldOpenEntry(entry) { const co = entry?.coldOpen; if (entry?.type !== "clip" || !isObj(co) || !num(co.from) || !num(co.to) || co.to <= co.from) return null; const { coldOpen: _c, promo: _p, qr: _q, zooms, ...rest } = entry; const out = { ...cutClip(rest, co.from, co.to, `${entry.id}~co`), promo: false, qr: false }; const inside = Array.isArray(zooms) ? zooms.filter((z) => num(z?.at) && z.at >= co.from && zoomEnd(z) <= co.to) : []; if (inside.length) out.zooms = inside; return out; } export function planVerticalCut({ timeline, beats = null, items = null, schedule = null, cuts = new Map(), project = [], coldOpens = true }) { const projectById = new Map((project ?? []).filter((e) => typeof e?.id === "string").map((e) => [e.id, e])); const byId = new Map((timeline ?? []).filter((e) => typeof e?.id === "string").map((e) => [e.id, withProjectFraming(e, projectById)])); const out = []; const pieces = []; const used = new Map(); const uniq = (id) => { const n = (used.get(id) ?? 0) + 1; used.set(id, n); return n === 1 ? id : `${id}~${n}`; }; const add = (entry, piece) => { out.push(entry); pieces.push({ id: entry.id, from: piece?.from ?? 0, to: piece?.to ?? null }); }; if (items?.length) { for (const id of items) { const e = byId.get(id); if (!e) throw new VerticalPlanError(`no item ${id} in the take`); // Its cold open first (a hard cut into the clip: both pieces joined, not crossfaded by the build). const co = coldOpens ? coldOpenEntry(e) : null; if (co) { out.push(co); pieces.push({ id: co.id, from: 0, to: null, hard: true }); out.push({ ...e, id: uniq(id) }); pieces.push({ id: out[out.length - 1].id, from: 0, to: null, hard: true }); continue; } add({ ...e, id: uniq(id) }); } return { timeline: out, pieces }; } if (!beats?.length) throw new VerticalPlanError("a short needs beats (--reel / --beats) or items (--items)"); const segs = Array.isArray(schedule?.segments) ? schedule.segments : []; for (const b of beats) { if (b.kind === "item") { const e = byId.get(b.id); if (!e) throw new VerticalPlanError(`beat ${b.id}: no item ${b.id} in the take`); add({ ...e, id: uniq(b.id) }); continue; } if (b.kind === "clip") { const e = byId.get(b.id); if (!e) throw new VerticalPlanError(`beat ${b.id}: no clip ${b.id} in the take`); if (e.type !== "clip") throw new VerticalPlanError(`beat ${b.id}: a source span names a clip, and ${b.id} is a ${e.type}`); add(cutClip(e, b.start, b.end, uniq(b.id))); continue; } if (!segs.length) throw new VerticalPlanError("a master span needs the take's schedule.json (out/sourced/schedule.json)"); const A = b.at; const B = b.at + b.dur; const hit = segs.filter((s) => num(s.start) && num(s.end) && s.end > A + 0.05 && s.start < B - 0.05); if (!hit.length) throw new VerticalPlanError(`beat at ${A}: no item of the take's schedule is under it`); for (const s of hit) { const e = byId.get(s.id); if (!e) continue; const from = Math.max(0, A - s.start); const to = Math.min(s.end - s.start, B - s.start); if (e.type === "clip") { const cut = cuts.get(s.id); if (!cut || !num(cut.start)) throw new VerticalPlanError(`beat at ${A}: clip ${s.id} has no cut record (segments/${s.id}.cut.json) to map its master seconds`); add(cutClip(e, cut.start + from, cut.start + to, uniq(s.id))); } else { const whole = from < 0.05 && to > s.end - s.start - 0.05; add({ ...e, id: uniq(s.id) }, whole ? null : { from: r3(from), to: r3(to) }); } } } return { timeline: out, pieces }; } /** Is every piece played whole, none joined by a hard cut? (Then the build's own joined file is the short.) */ export const piecesWhole = (pieces) => pieces.every((p) => !p.from && p.to === null && !p.hard); /** A short's name for its files: the reel's, else "items". */ export const shortName = ({ reel = null, name = null } = {}) => String(name ?? reel ?? "items").replace(/[^A-Za-z0-9._-]+/g, "-"); /** The layout a short is named by: its clip, post and side layouts that differ from the defaults, or "reframe". */ export function layoutTag(knobs = {}, { reframe = false } = {}) { if (reframe) return "reframe"; const parts = []; if (knobs.clip) parts.push(knobs.clip); if (knobs.post) parts.push(knobs.post); if (knobs.side) parts.push(knobs.side); return parts.length ? parts.join("-") : "vertical"; } /** * A take's render for a PART of its timeline (the landscape build of a short's items): the deck's markers that * follow an entry the part does not carry are left out (the deck refuses a marker after a missing entry). */ export function renderForPart(render, timeline) { const ids = new Set((timeline ?? []).map((e) => String(e?.id ?? "").replace(/~\d+$/, "")).concat((timeline ?? []).map((e) => e?.id))); const deck = render?.chrome?.deck; if (!isObj(deck) || !Array.isArray(deck.markers)) return render; return { ...render, chrome: { ...render.chrome, deck: { ...deck, markers: deck.markers.filter((m) => isObj(m) && ids.has(m.after)) } } }; }