// `umtool trailer` (2026-10-26): a trailer cut from a finished take's master, // so the deck on screen shows the whole video's timeline. The cutting is // report-to-video/trailer.mjs; this is the command line around it. // // umtool trailer --take (--beats FILE | --beats-json JSON) // [--intro] [--finale S] [--xfade 0.3] [--no-snap] [--out NAME] [--dry-run] [--json] // umtool trailer --reel [--take ] [...] a beat reel (promo/-beats.json) with its own settings // umtool trailer --candidates [--json] the walk's ★ clips as beats import { readFile, stat } from "node:fs/promises"; import path from "node:path"; import { REPORTS_ROOT, resolveProject } from "../projects/core.mjs"; import { kindMakesTakes } from "../projects/kinds.mjs"; import { takesDirOf } from "./takes.mjs"; import { readManifest } from "../projects/report.mjs"; import { candidateBeats, trailerCandidates } from "./trailerCandidates.mjs"; import { pickReelTake, reelCuts, reelOutArg, takeMasters } from "./reelPlay.mjs"; import { isReelId, readTrailerBeats, reelFileOf, reelVersion } from "./trailerBeats.mjs"; import { TRAILER_ESTIMATE_MB, TRAILER_XFADE_DEFAULT, TrailerError, describePlan, makeTrailer, parseBeats, } from "umtool-report-to-video/trailer"; import { resolveOutro } from "umtool-report-to-video/outro"; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); export const TRAILER_USAGE = [ "usage: umtool trailer --take (--beats FILE | --beats-json JSON)", ` [--intro] [--finale S] [--xfade ${TRAILER_XFADE_DEFAULT}] [--no-snap] [--out NAME] [--dry-run] [--json]`, "", "A trailer cut from the take's finished master (takes//out/.mp4): spans of the master's own", "frames, so the deck's rail, dates and tallies show the whole video's timeline. Nothing is rendered again.", "A beat is { \"id\": \"\", \"start\": , \"end\": } (mapped to the master by the", "take's build records) or { \"at\": , \"dur\": }. --intro opens on the master's brand intro;", "--finale S closes on its last S seconds. Joined with S-second crossfades (--xfade 0: hard cuts),", "each clip beat's end moved forward into the next pause (through its sentence when one ends within 1 s)", "and its start back into the one before, so the crossfade lands in quiet -- the master's silences and the", "recording's words; never past the segment's own clip, each move said (--no-snap: the edges as written).", "encoded with the take's render settings, in the heavy slot. Writes takes//out/.trailer.mp4", "(--out NAME: that name there; a path: exactly there) and .trailer.json beside it, what was cut.", "--dry-run prints the mapping and writes nothing.", "", "umtool trailer --reel [--take ] [--intro] [--xfade S] [...]: a BEAT REEL -- the beats of", "promo/-beats.json (the reel `trailer` is promo/trailer-beats.json), with that file's own intro, outro card", "and crossfade unless a flag says otherwise (an intro the master lacks, or the reel's own, is rendered for it;", "the outro card fades in from the last beat and holds), written to takes//out/..mp4 (the trailer reel:", ".trailer.mp4). With no --take: the reel's pinned take, else the take it was last cut from, else the", "latest take with a finished master (said). The Trailer page's Cut the trailer runs exactly this.", "", "umtool trailer --reel --take --vertical [--layout reframe] [--clip …] [--post …]: the reel", "9:16 -- `umtool vertical` with the same reel (its usage says the layouts).", "", "umtool trailer --candidates [--json]: the clips starred (★) on the walk, in the cut's order, as beats", "over what each plays -- with --json exactly what --beats-json takes:", " umtool trailer

--take --beats-json \"$(umtool trailer

--candidates --json)\"", ].join("\n"); class CliError extends Error {} function parse(args) { const valued = new Set(["--take", "--beats", "--beats-json", "--finale", "--xfade", "--out", "--reel"]); /** @type {Record} */ const flags = {}; const positional = []; for (let i = 0; i < args.length; i += 1) { const a = args[i]; if (a.startsWith("--")) { if (valued.has(a)) { if (i + 1 >= args.length) throw new CliError(`${a} needs a value`); flags[a] = args[++i]; } else if (["--intro", "--json", "--dry-run", "--help", "--candidates", "--no-snap"].includes(a)) flags[a] = ""; else throw new CliError(`unknown flag ${a}`); } else positional.push(a); } return { flags, positional, has: (n) => n in flags }; } async function beatsOf(p) { if (p.has("--beats") && p.has("--beats-json")) throw new CliError("--beats or --beats-json, not both"); let value; if (p.has("--beats")) { const file = p.flags["--beats"]; try { value = JSON.parse(await readFile(path.resolve(file), "utf8")); } catch (err) { throw new CliError(`--beats ${file}: ${err instanceof Error ? err.message : err}`); } } else if (p.has("--beats-json")) { try { value = JSON.parse(p.flags["--beats-json"]); } catch (err) { throw new CliError(`--beats-json: ${err instanceof Error ? err.message : err}`); } } else if (p.has("--intro") || p.has("--finale")) { value = { beats: [] }; } else throw new CliError("--beats FILE or --beats-json JSON: the trailer's beats"); if (Array.isArray(value?.beats ?? value) && !(value?.beats ?? value).length) return []; return parseBeats(value); } function seconds(p, flag, fallback) { if (!p.has(flag)) return fallback; const n = Number(p.flags[flag]); if (!Number.isFinite(n) || n < 0) throw new CliError(`${flag} ${p.flags[flag]}: seconds ≥ 0`); return n; } /** * The heavy slot, loaded at run time from the report-to-video package (never * a module the umtool app bundles): `withRenderSlot` with the trailer's * declared estimate. An older gate ignores the estimate and runs it alone. */ async function realSlot() { const gate = new URL("./heavy-gate.mjs", import.meta.resolve("umtool-report-to-video/package.json")).href; const { withRenderSlot } = await import(/* webpackIgnore: true */ gate); return (cmd) => (fn) => withRenderSlot(cmd, fn, { estimateMb: TRAILER_ESTIMATE_MB }); } /** * `umtool trailer …` * @param {string[]} args everything after `trailer` * @param {{ reportsRoot?: string, log?: (s: string) => void, err?: (s: string) => void, deps?: Record }} [opts] * @returns {Promise} */ export async function trailerCommand(args, { reportsRoot = REPORTS_ROOT, log = console.log, err = console.error, deps = {} } = {}) { // `--vertical` (lane vertical): the reel 9:16, natively or reframed -- `umtool vertical` with the same reel and take. if (args.includes("--vertical")) { const rest = args.filter((a) => a !== "--vertical"); const li = rest.indexOf("--layout"); if (li >= 0) { const layout = rest[li + 1]; rest.splice(li, 2, ...(layout === "reframe" ? ["--reframe"] : [])); if (layout && layout !== "reframe") err(`umtool trailer --vertical: --layout ${layout} -- a native layout is set with --clip / --post / --side`); } const { verticalCommand } = await import("./verticalCli.mjs"); return verticalCommand(rest, { reportsRoot, log, err }); } try { const p = parse(args); if (p.has("--help") || !p.positional.length) { log(TRAILER_USAGE); return p.has("--help") ? 0 : 2; } const r = await resolveProject(p.positional[0], reportsRoot); if (r.ambiguous) throw new CliError(`"${p.positional[0]}" is the name of ${r.ambiguous.length} projects — pass its id`); if (!r.project) throw new CliError(`no project matches "${p.positional[0]}"`); if (!kindMakesTakes(r.project.kind)) throw new CliError(`${r.project.id} is a ${r.project.kind}, not a report video`); if (p.has("--candidates")) { const m = await readManifest(r.project.dir); if (!m) throw new CliError(`${r.project.id} has no video.manifest.json`); if (p.has("--json")) { log(JSON.stringify(candidateBeats(m))); return 0; } const list = trailerCandidates(m); if (!list.length) { log(`${r.project.id}: no clip is starred — ★ one on the walk (s), or \`umtool window

--star\``); return 0; } log(`${r.project.id}: ${list.length} trailer candidate${list.length === 1 ? "" : "s"} (★ on the walk), ${list.reduce((n, b) => n + b.end - b.start, 0).toFixed(1)} s in all`); for (const b of list) { log(` ${b.id.padEnd(6)} ${b.start.toFixed(2)}–${b.end.toFixed(2)} (${(b.end - b.start).toFixed(1)} s)${b.quote ? ` “${b.quote.slice(0, 70)}${b.quote.length > 70 ? "…" : ""}”` : ""}${b.walkNote ? ` — ${b.walkNote}` : ""}`); } return 0; } const json = p.has("--json"); const reel = p.has("--reel") ? p.flags["--reel"] : null; let take = p.flags["--take"] ?? null; /** The reel's own settings, under the flags. */ let reelSettings = /** @type {Record} */ ({}); let reelBeats = null; let reelProvenance = null; let outArg = p.flags["--out"] ?? null; if (reel !== null) { if (!isReelId(reel)) throw new CliError(`--reel ${reel}: a reel name (lower case, digits and dashes)`); if (p.has("--beats") || p.has("--beats-json")) throw new CliError("--reel reads the reel's own beats file: not with --beats or --beats-json"); const doc = await readTrailerBeats(r.project.dir, { reel }); if (!doc.exists) throw new CliError(`no reel ${reel}: ${reelFileOf(reel)} is not there (\`umtool trailer-beats ${r.project.id} new-reel ${reel}\`)`); reelSettings = doc.settings ?? {}; reelBeats = doc.beats; // The reel's version, named in the cut's record: a later edit makes the cut stale. reelProvenance = { name: reel, file: reelFileOf(reel), version: reelVersion(doc) }; const masters = await takeMasters(r.project.dir); if (!take) { const chosen = pickReelTake({ pinned: reelSettings.take ?? null, cuts: await reelCuts(masters, reel), masters }); if (!chosen) throw new CliError(`no take of ${r.project.id} has a finished master to cut the reel from — render a take first`); take = chosen.id; if (!json) err(`trailer: the reel ${reel} from take ${take} (${chosen.why})`); } // The take's slug names the output (a take made by hand may not be in the listing). const slug = masters.find((x) => x.id === take)?.slug ?? (await readFile(path.join(takesDirOf(r.project.dir), take, "video.manifest.json"), "utf8").then((t) => JSON.parse(t).slug, () => null)); if (!outArg && typeof slug === "string" && slug) outArg = reelOutArg(slug, reel); } if (!take) throw new CliError("--take : the finished take to cut the trailer from"); if (!/^[a-z0-9][a-z0-9-]*$/.test(take)) throw new CliError(`--take ${take}: a take id`); let beats; if (reelBeats) { try { beats = reelBeats.length ? parseBeats(reelBeats) : []; } catch (e) { throw new CliError(`${reelFileOf(reel)}: ${e instanceof Error ? e.message : e}`); } } else beats = await beatsOf(p); const takeDir = path.join(takesDirOf(r.project.dir), take); // The reel's outro card (report-to-video/outro.mjs): its words with the defaults, its image a file of the project's. let outro = null; const o = resolveOutro(reelSettings.outro, { reelTitle: reelSettings.title ?? null, projectTitle: (await readManifest(r.project.dir).catch(() => null))?.title ?? null }); if (o) { if (!o.title) throw new CliError(`the reel ${reel}'s outro has no title: give it one (\`umtool trailer-beats ${r.project.id} --reel ${reel} outro '{"title":"…"}'\`)`); const imageFile = o.image ? path.join(r.project.dir, o.image) : null; if (imageFile && !(await stat(imageFile).then((st) => st.isFile(), () => false))) throw new CliError(`the reel ${reel}'s outro image ${o.image} is not a file of ${r.project.id}`); outro = { ...o, imageFile }; } const slotFor = deps.slot ?? (await realSlot()); const res = await makeTrailer({ takeDir, beats, intro: p.has("--intro") || reelSettings.intro === true, introSpec: isObj(reelSettings.intro) ? reelSettings.intro : null, outro, finale: p.has("--finale") ? seconds(p, "--finale", null) : null, xfade: seconds(p, "--xfade", typeof reelSettings.xfade === "number" ? reelSettings.xfade : TRAILER_XFADE_DEFAULT), out: outArg, dryRun: p.has("--dry-run"), snap: !p.has("--no-snap"), ...(reelProvenance ? { reel: reelProvenance } : {}), ...(deps.measure ? { measure: deps.measure } : {}), ...(deps.run ? { run: deps.run } : {}), ...(deps.probe ? { probe: deps.probe } : {}), ...(deps.makeIntro ? { makeIntro: deps.makeIntro } : {}), ...(deps.makeOutro ? { makeOutro: deps.makeOutro } : {}), ...(deps.sitesDir ? { sitesDir: deps.sitesDir } : {}), slot: (fn) => { if (!json) err(`trailer: encoding ${res0(beats)} in the heavy slot …`); return slotFor(["umtool", "trailer", r.project.id, ...(reel ? ["--reel", reel] : []), "--take", take])(fn); }, }); if (json) { log(JSON.stringify({ project: r.project.id, ...(reel ? { reel } : {}), take, out: res.dryRun ? null : res.outPath, record: res.dryRun ? null : res.recordPath, wouldWrite: res.dryRun ? res.outPath : undefined, seconds: res.plan.total, encodeSeconds: res.encodeSeconds, pieces: res.plan.pieces, ...(res.plan.outro ? { outro: res.record.outro } : {}), notes: res.plan.notes, }, null, 2)); return 0; } for (const line of describePlan(res.plan)) log(line); for (const n of res.plan.notes) log(`note: ${n}`); if (res.dryRun) log(`dry run: would write ${res.outPath}`); else log(`wrote ${res.outPath} (${res.encodeSeconds} s to encode) and ${path.basename(res.recordPath)}`); return 0; } catch (e) { if (e instanceof CliError || e instanceof TrailerError) { err(`umtool trailer: ${e.message}`); return e instanceof CliError ? 2 : 1; } throw e; } } const res0 = (beats) => `${beats.length} beat${beats.length === 1 ? "" : "s"}`;