// VERTICAL SHORTS (release 36 lane C): one clip of a report video as a // 1080x1920 file, `/shorts/.mp4`, made by build-video.mjs // `--short ` (report-to-video/look.mjs shortRender: the clip's // `vertical.crop`, large speaker captions, no header or QR, the loudness // target) as a job through lib/jobs.ts, which build-video's CLI runs inside // the machine's heavy slot. A record beside each (`.json`) says what // it was made from, so the list can say when the clip has moved since. import { readFile, readdir, rename, stat } from "node:fs/promises"; import path from "node:path"; import { PIPELINE_DIR } from "./driver.mjs"; export const SHORTS_DIR = "shorts"; /** A short of a clip is minutes at most; a stuck encode is not left forever. */ export const SHORT_TIMEOUT_MS = 20 * 60_000; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); /** A clip id as a file name: letters, digits, `.`, `_`, `-` only. */ export const safeClipId = (id) => typeof id === "string" && /^[A-Za-z0-9][A-Za-z0-9._-]{0,80}$/.test(id) && !id.includes(".."); export const shortsDirOf = (dir) => path.join(/* turbopackIgnore: true */ dir, SHORTS_DIR); /** `/shorts/.mp4`, or null for an id that is not a file name. */ export function shortFile(dir, clipId) { return safeClipId(clipId) ? path.join(/* turbopackIgnore: true */ shortsDirOf(dir), `${clipId}.mp4`) : null; } /** * The job's one step: build-video `--short`, NDJSON progress, beside the * build's own steps (lib/report/driver.mjs). No path or argv from a client. * * `offline` (a live share, lib/report/liveShare.mjs): `--skip-fetch * --no-network` — the window is already cut, and nothing may be fetched. * * @param {{ dir: string }} project * @param {string} clipId * @param {{ offline?: boolean }} [o] */ export function shortSteps(project, clipId, { offline = false } = {}) { if (!safeClipId(clipId)) throw new Error(`not a clip id: ${clipId}`); return [ { cwd: PIPELINE_DIR, env: {}, label: `make the short of ${clipId} (1080×1920)`, argv: [ "node", path.join(/* turbopackIgnore: true */ PIPELINE_DIR, "build-video.mjs"), path.join(/* turbopackIgnore: true */ project.dir, "video.manifest.json"), "--out", path.join(/* turbopackIgnore: true */ project.dir, "out"), "--short", clipId, "--progress", "ndjson", ...(offline ? ["--skip-fetch", "--no-network"] : []), ], ndjson: true, timeoutMs: SHORT_TIMEOUT_MS, }, ]; } const sameNums = (a, b) => Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((x, i) => Math.abs(x - b[i]) < 0.011); /** * Every short on disk, newest first: `{ clip, size, mtimeMs, record, stale, * gone }`. `stale`: the clip's cut or crop moved since the short was made; * `gone`: no clip has that id now. * * @param {string} dir the project * @param {any} [manifest] the manifest as read (for stale / gone) */ export async function readShorts(dir, manifest = null) { const names = await readdir(shortsDirOf(dir)).catch(() => []); const clips = new Map((manifest?.timeline ?? []).filter((e) => e?.type === "clip").map((e) => [e.id, e])); const out = []; for (const name of names) { // A short set aside (`..mp4`) is kept, not listed. if (!name.endsWith(".mp4") || /\.\d{8}-\d{4}\.mp4$/.test(name)) continue; const clip = name.slice(0, -4); if (!safeClipId(clip)) continue; const st = await stat(path.join(/* turbopackIgnore: true */ shortsDirOf(dir), name)).catch(() => null); if (!st?.isFile()) continue; const record = await readFile(path.join(/* turbopackIgnore: true */ shortsDirOf(dir), `${clip}.json`), "utf8").then(JSON.parse, () => null); const e = clips.get(clip) ?? null; let stale = false; if (e && isObj(record)) { const want = [e.cutStart ?? e.start, e.cutEnd ?? e.end]; const had = isObj(record.requested) ? [record.requested.cutStart, record.requested.cutEnd] : null; const crop = Array.isArray(e.vertical?.crop) ? e.vertical.crop : null; stale = (had !== null && had.every(fin) && !sameNums(want, had)) || JSON.stringify(crop) !== JSON.stringify(record.crop ?? null); } out.push({ clip, size: st.size, mtimeMs: Math.round(st.mtimeMs), record: isObj(record) ? record : null, stale, gone: manifest ? !e : false }); } return out.sort((a, b) => b.mtimeMs - a.mtimeMs); } /** `20260817-1408`: a short set aside before it is made again. */ function stamp(d = new Date()) { const p = (n) => String(n).padStart(2, "0"); return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}`; } /** * The replace guard: a short that is there is never overwritten unasked. * With `replace`, it is moved aside to `shorts/..mp4` (its * record too) and the path it went to is returned; without, an error naming * `replace`. No short there: null. * * @param {string} dir * @param {string} clipId * @param {{ replace?: boolean, now?: Date }} [o] */ export async function setShortAside(dir, clipId, { replace = false, now = new Date() } = {}) { const file = shortFile(dir, clipId); if (!file) throw new Error(`not a clip id: ${clipId}`); const st = await stat(file).catch(() => null); if (!st) return null; if (!replace) { const err = new Error(`${clipId} has a short already — make it again to set that one aside (replace)`); /** @type {any} */ (err).needsReplace = true; throw err; } const aside = file.replace(/\.mp4$/, `.${stamp(now)}.mp4`); await rename(file, aside); await rename(file.replace(/\.mp4$/, ".json"), aside.replace(/\.mp4$/, ".json")).catch(() => {}); return aside; }