// A BATCH OF SHORTS (lane clip-kit, slice A): the clipping job as one command. `umtool shorts --take // (--items a,b | --all) --formats vertical,landscape` builds every listed clip in every format through the ONE // builder behind `umtool vertical` (lib/report/verticalCli.mjs buildShort), into one folder of the project, // `shorts-/` -- a deliverable (storage.mjs isDeliverableName), never `shorts/`, which the clip editor's Make a // short owns and whose every mp4 the Deliver tab reads as `.mp4`. // // shorts-/..mp4 each with its record (..json, the vertical-short record) // shorts-/batch.json the batch: what was asked, each clip's name, files, notes and snapshot // // NAMES are stable: a clip's `name` when it pins one, else its title's slug, else its id. A name another clip of the // folder already holds gets `-` (two clips of one cut often share a title). Once a clip has a name in the // folder's batch.json it keeps it on every later run, whatever its title becomes or whatever else the batch holds. // // THE SNAPSHOT: each clip's PROJECT keys (vertical-plan.mjs PROJECT_KEYS -- set after the take was made, read over // it) as the batch read them. `--again` rebuilds from the record: the same items, formats and knobs, each clip // framed by its snapshot, so a later edit of the project does not silently change a rebuild. // // PURE: no fs. The command is lib/report/shortsCli.mjs. /** The formats a batch builds, each the builder's options and its length note's cap (s; a note, never a refusal). */ export const BATCH_FORMATS = Object.freeze({ vertical: Object.freeze({ landscape: false, desktop: false, maxSeconds: 180 }), // 16:9 with minimal chrome (slice C, vertical.mjs desktopGeometry): the X clip. desktop: Object.freeze({ landscape: false, desktop: true, maxSeconds: 140 }), landscape: Object.freeze({ landscape: true, desktop: false, maxSeconds: 140 }), }); /** * The knobs one format is built with: a short keeps its code at the top (tr, unless tl) -- a bottom corner asked for * the desktop clips is the short's default corner. Returns `{knobs, note}`. */ export function knobsForFormat(knobs, format) { // The take's own 16:9 look reads none of the per-clip builder's knobs (its look is the take's): none handed on. if (format === "landscape") { const asked = Object.keys(knobs ?? {}).filter((k) => !["maxSeconds"].includes(k)); return { knobs: {}, note: asked.length ? `the take's own 16:9 look: ${asked.join(", ")} ${asked.length > 1 ? "are" : "is"} not its (left out)` : null }; } const qr = knobs?.qr; if (format === "vertical" && qr && typeof qr === "object" && !["tr", "tl"].includes(qr.corner)) { return { knobs: { ...knobs, qr: true }, note: `its code at the top right (a short's captions run along its foot; ${qr.corner} is a desktop clip's corner)` }; } return { knobs: { ...(knobs ?? {}) }, note: null }; } export const FORMAT_NAMES = Object.freeze(Object.keys(BATCH_FORMATS)); /** The folder a take's batch lands in. */ export const BATCH_PREFIX = "shorts-"; export const batchDirName = (take) => `${BATCH_PREFIX}${take}`; export const BATCH_FILE = "batch.json"; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); /** A title as a file name: lower case, letters and digits, words joined by "-" (apostrophes dropped), ≤ 60. */ export function slugOf(text) { const s = String(text ?? "") .normalize("NFKD") .replace(/[̀-ͯ]/g, "") .toLowerCase() .replace(/['’‘`]/g, "") .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, ""); return s.slice(0, 60).replace(/-+$/, ""); } /** A clip's own name, a pinned `name` or its title's slug or its id: what its files are called before a tiebreak. */ export function baseNameOf(entry) { const pinned = typeof entry?.name === "string" ? slugOf(entry.name) : ""; if (pinned) return pinned; const title = typeof entry?.title === "string" ? slugOf(entry.title) : ""; return title || slugOf(entry?.id) || "clip"; } /** * Each clip's name in the folder: `prior` ({: name}, the folder's batch.json) kept as it is; a new clip its * base name, or `-` when another clip holds that already. In the order given; returns {: name}. * * @param {Array<{ id: string, name?: string, title?: string }>} entries * @param {Record} [prior] */ export function assignNames(entries, prior = {}) { const names = { ...(isObj(prior) ? prior : {}) }; const taken = new Map(Object.entries(names).map(([id, n]) => [n, id])); for (const e of entries) { if (typeof names[e.id] === "string") continue; const base = baseNameOf(e); let n = base; if (taken.has(n) && taken.get(n) !== e.id) n = `${base}-${slugOf(e.id) || "clip"}`; for (let k = 2; taken.has(n) && taken.get(n) !== e.id; k += 1) n = `${base}-${slugOf(e.id)}-${k}`; names[e.id] = n; taken.set(n, e.id); } return names; } /** A name read back from a record: a file name of the folder, nothing else (no separator, no dot path). */ export const safeName = (n) => typeof n === "string" && /^[a-z0-9][a-z0-9-]{0,80}$/.test(n); /** `--formats a,b` read: known names, each once, in the order given. Throws naming an unknown one. */ export function parseFormats(text) { const list = String(text ?? "vertical").split(",").map((s) => s.trim()).filter(Boolean); const bad = list.filter((f) => !FORMAT_NAMES.includes(f)); if (bad.length) throw new Error(`--formats ${bad.join(", ")}: not a format (${FORMAT_NAMES.join(", ")})`); if (!list.length) throw new Error(`--formats: one or more of ${FORMAT_NAMES.join(", ")}`); return [...new Set(list)]; } /** The keys of a clip a batch snapshots (vertical-plan.mjs PROJECT_KEYS): each set one, null for one it lacks. */ export function snapshotOf(entry, keys) { /** @type {Record} */ const out = {}; for (const k of keys) out[k] = entry?.[k] === undefined ? null : entry[k]; return out; } /** A file set aside before it is made again: `...mp4`. */ export function asideName(file, now = new Date()) { const p = (n) => String(n).padStart(2, "0"); const stamp = `${now.getFullYear()}${p(now.getMonth() + 1)}${p(now.getDate())}-${p(now.getHours())}${p(now.getMinutes())}${p(now.getSeconds())}`; return file.replace(/\.mp4$/, `.${stamp}.mp4`); } /** * What `--again` builds from a record: each clip of it, in its order, with the formats it has a file (or an error) * in, the knobs it was last built with and its snapshot. `[{id, formats, knobs, snapshot}]`. */ export function againOf(record) { if (!isObj(record) || record.kind !== "shorts-batch" || !Array.isArray(record.clips)) throw new Error("not a batch record (batch.json, kind shorts-batch)"); return record.clips .filter((c) => isObj(c) && typeof c.id === "string") .map((c) => ({ id: c.id, formats: FORMAT_NAMES.filter((f) => (isObj(c.files) && c.files[f]) || (isObj(c.errors) && c.errors[f])), knobs: isObj(c.knobs) ? c.knobs : {}, snapshot: isObj(c.snapshot) ? c.snapshot : null, look: isObj(c.look) ? c.look : null, })) .filter((c) => c.formats.length); } /** * A clip's row after one format's build: `prior` (its row, or null) with this run's name and title, and the format's * file, length and notes -- or its error, the file dropped when it is no longer there (`exists`). The knobs and the * snapshot are the last SUCCESSFUL build's. Pure. * * @param {Record | null} prior * @param {{ id: string, name: string, title: string | null, format: string, knobs: Record, snapshot: Record, * file?: string, seconds?: number | null, notes?: string[], error?: string, exists?: boolean, look?: Record | null }} r */ export function rowAfter(prior, r) { const row = { id: r.id, name: r.name, title: r.title, knobs: {}, snapshot: null, files: {}, seconds: {}, notes: {}, errors: {}, ...(isObj(prior) ? prior : {}) }; row.name = r.name; row.title = r.title; row.files = { ...row.files }; row.seconds = { ...row.seconds }; row.notes = isObj(row.notes) ? { ...row.notes } : {}; row.errors = { ...row.errors }; if (r.error) { row.errors[r.format] = r.error; if (!r.exists) { delete row.files[r.format]; delete row.seconds[r.format]; delete row.notes[r.format]; } return row; } delete row.errors[r.format]; row.files[r.format] = r.file; row.seconds[r.format] = r.seconds ?? null; row.notes[r.format] = r.notes ?? []; row.knobs = r.knobs; row.snapshot = r.snapshot; // The look it was built with (verticalCli.mjs LOOK_SNAPSHOT_KEYS): what `--again` builds from. if (isObj(r.look)) row.look = r.look; return row; } /** The clips a batch builds: `items` (each must be a clip of the take), else with `all` every clip of the take. */ export function batchItems(timeline, { items = null, all = false } = {}) { const clips = (timeline ?? []).filter((e) => e?.type === "clip" && typeof e.id === "string"); if (items?.length) { const byId = new Map(clips.map((e) => [e.id, e])); const missing = items.filter((id) => !byId.has(id)); if (missing.length) throw new Error(`${missing.join(", ")}: not ${missing.length > 1 ? "clips" : "a clip"} of the take`); return [...new Set(items)].map((id) => byId.get(id)); } if (all) return clips; throw new Error("--items ,… or --all: which clips"); }