// MAKE VARIANTS and the SYNC report, read back for Deliver (video knobs, // plans/video-knobs-ui.md). The encode is the renderer's (report-to-video // variants.mjs encodeVariants, run as a job by driver.mjs variantsSteps); the // sync report is the build's (`//sync.json`). This only reads // what they wrote, by NAME -- a variant's name, a take's id -- never a path. // // /.mp4 the build (the `master` variant is this file) // /..mp4 one variant // /variants.json { at, source, items: [{ name, path, width, height, bytes, kbps }] } // /sourced/sync.json { fps, items: [{ id, start, lagMs, peak }] } // // `` is the project's out/, or a take's (takes//out). // // A TAKE's render ends as takes//preview.mp4 (whatever take.json names); // makeTake leaves it in the take's out/ as `.mp4` too, a hard link. A // take made before that, or whose out/ is on another filesystem, has only the // preview: it is then the build, and the encoder is pointed at it (--source). // // Plain ESM: `umtool variants` runs it under bare node. import { readdir, readFile, stat } from "node:fs/promises"; import path from "node:path"; import { DEFAULT_VARIANT, variantPaths } from "umtool-report-to-video/build-video"; import { resolveInRoots } from "../paths.mjs"; import { VARIANT_NAME_RE, VARIANTS_DEFAULT, variantsOf } from "./knobs.mjs"; import { isTakeId, parseTake, TAKE_FILE, takesDirOf } from "./takes.mjs"; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); /** A lag past one frame is flagged (the plan's acceptance: every clip within ±1 frame). */ export const frameMs = (fps) => 1000 / (fin(fps) && fps > 0 ? fps : 30); /** * The out/ a take or the project builds into. Throws on a take id that is not one. * @param {{ dir: string }} project * @param {string | null} [take] */ export function outDirOf(project, take = null) { if (take === null || take === undefined || take === "") return path.join(project.dir, "out"); if (!isTakeId(take)) throw new Error(`not a take id: ${take}`); return path.join(takesDirOf(project.dir), take, "out"); } async function fileInfo(file) { const abs = resolveInRoots(file); if (!abs) return null; const st = await stat(abs).catch(() => null); return st?.isFile() ? { abs, size: st.size, mtimeMs: Math.round(st.mtimeMs) } : null; } async function readJson(file) { const abs = resolveInRoots(file); if (!abs) return null; try { return JSON.parse(await readFile(abs, "utf8")); } catch { return null; } } /** * The file a build's variants are made of: `/.mp4`, or -- for a * take with none -- the take's finished preview (take.json written, so the * render is done). `from` says which. * * @param {{ dir: string }} project * @param {string} slug * @param {string | null} [take] * @returns {Promise<{ abs: string, size: number, mtimeMs: number, from: "build" | "preview" } | null>} */ export async function buildFileOf(project, slug, take = null) { const got = await fileInfo(variantPaths(outDirOf(project, take), slug, DEFAULT_VARIANT).final); if (got) return { ...got, from: "build" }; if (!take) return null; const takeDir = path.join(takesDirOf(project.dir), take); const parsed = parseTake(take, await readJson(path.join(takeDir, TAKE_FILE))); if (!("take" in parsed) || !parsed.take) return null; const preview = await fileInfo(path.join(takeDir, parsed.take.preview)); return preview ? { ...preview, from: "preview" } : null; } /** The takes with a build (their out/'s `.mp4`, else their finished preview), by id. */ export async function builtTakes(project, slug) { const dirs = await readdir(takesDirOf(project.dir), { withFileTypes: true }).catch(() => []); const out = []; for (const d of dirs) { if (!isTakeId(d.name)) continue; if (await buildFileOf(project, slug, d.name)) out.push(d.name); } return out.sort(); } /** * What Deliver shows for one out/: the build, the ladder, the variants made, * the sync report. * * @param {{ id: string, dir: string }} project * @param {{ slug?: string, render?: Record, timeline?: unknown[] }} manifest * @param {string | null} [take] */ export async function variantsState(project, manifest, take = null) { const slug = manifest.slug ?? path.basename(project.dir); const outDir = outDirOf(project, take); const built = await buildFileOf(project, slug, take); const doc = await readJson(path.join(outDir, "variants.json")); const items = []; if (isObj(doc) && Array.isArray(doc.items)) { for (const it of doc.items) { if (!isObj(it) || typeof it.name !== "string" || !VARIANT_NAME_RE.test(it.name)) continue; // The `master` variant is the build itself (`.mp4`, or a take's // preview), not a `.master.mp4`: it is there when the build is. const f = it.name === "master" ? built : await fileInfo(path.join(outDir, `${slug}.${it.name}.mp4`)); items.push({ name: it.name, width: fin(it.width) ? it.width : null, height: fin(it.height) ? it.height : null, bytes: f?.size ?? (fin(it.bytes) ? it.bytes : null), kbps: fin(it.kbps) ? it.kbps : null, exists: !!f, mtimeMs: f?.mtimeMs ?? null, }); } } const syncDoc = await readJson(path.join(outDir, DEFAULT_VARIANT, "sync.json")); const fps = isObj(syncDoc) && fin(syncDoc.fps) ? syncDoc.fps : null; const limit = frameMs(fps); const clipIds = new Set((manifest.timeline ?? []).filter((e) => isObj(e) && e.type === "clip").map((e) => e.id)); const sync = isObj(syncDoc) && Array.isArray(syncDoc.items) ? { fps, frameMs: Math.round(limit * 10) / 10, items: syncDoc.items .filter((x) => isObj(x) && typeof x.id === "string") .map((x) => ({ id: x.id, start: fin(x.start) ? x.start : null, lagMs: fin(x.lagMs) ? x.lagMs : null, peak: fin(x.peak) ? x.peak : null, flagged: fin(x.lagMs) && Math.abs(x.lagMs) > limit, clip: clipIds.has(x.id), })), } : null; const render = isObj(manifest.render) ? manifest.render : {}; return { take: take || null, slug, // `from: "preview"`: a take whose out/ holds no `.mp4`, made of its preview.mp4. built: built ? { size: built.size, mtimeMs: built.mtimeMs, from: built.from } : null, ladder: variantsOf(render), ladderIsDefault: !Array.isArray(render.variants), ladderDefault: VARIANTS_DEFAULT, made: isObj(doc) ? { at: typeof doc.at === "string" ? doc.at : null, items } : null, // A variants.json older than the build was made from another build. stale: !!(built && isObj(doc) && typeof doc.at === "string" && Date.parse(doc.at) < built.mtimeMs), sync, takes: await builtTakes(project, slug), }; } /** * One variant's file, by name, for the route that serves it. * @param {{ dir: string }} project * @param {{ slug?: string }} manifest * @param {string} name * @param {string | null} [take] * @returns {Promise<{ abs: string, size: number, mtimeMs: number, rel: string } | null>} */ export async function variantFile(project, manifest, name, take = null) { if (typeof name !== "string" || !VARIANT_NAME_RE.test(name)) return null; const slug = manifest.slug ?? path.basename(project.dir); const f = await fileInfo(path.join(outDirOf(project, take), `${slug}.${name}.mp4`)); return f ? { ...f, rel: `${slug}.${name}.mp4` } : null; }