// A TAKE'S MANIFEST WITHOUT RENDERING IT (the Play view, umtool/docs/knobs.md, "Play the cut"): // the project's manifest as it is NOW with the take's style patch, then its // own patch, applied (RFC 7386 merge patches, takeMaker.mjs's order) -- so a // take is played in the browser with every edit made to the cut since it was // last rendered, and a take a recipe names but nobody has rendered yet plays // too. // // takes//take.json a made take: its `style` and `patch` // takes/recipe.json, a take not made yet: its spec in a recipe // takes/all-styles.recipe.json (parseRecipe's shape) // // A SAVED VERSION (versions.mjs) is frozen: when takes//base.manifest.json // is there, the take resolves over THAT -- the draft as it was when it was // saved -- and never over the project's manifest as it is now. A legacy take // (no base) resolves exactly as it always did. // // A style that is no longer installed falls back to the take's own // takes//video.manifest.json (the manifest it was rendered from), and // says so; a take with neither is an error. Nothing here writes but // writePlayManifest: `.take-.play.json` beside the project's manifest // (so a relative asset in it resolves as the project's do), what the chrome // preview composes a take's pages from -- a scratch file, rewritten on every // compose, never read by a build. import { resolvePresetLayers } from "umtool-report-to-video/presets"; import { readFile, stat, writeFile, rename } from "node:fs/promises"; import path from "node:path"; import { ALL_STYLES_RECIPE, MANIFEST, RECIPE_FILE, loadMontageStyles, mergePatch, parseRecipe } from "./takeMaker.mjs"; import { BASE_FILE, frozenBaseText, isTakeId, listTakes, parseTake, takesDirOf, TAKE_FILE } from "./takes.mjs"; import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs"; export { frozenBaseText }; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); async function readJson(file) { try { return JSON.parse(await readFile(/* turbopackIgnore: true */ file, "utf8")); } catch { return null; } } /** Where a take's play manifest is written (beside the project's manifest). */ export const playManifestPath = (projectDir, id) => path.join(/* turbopackIgnore: true */ projectDir, `.take-${id}.play.json`); /** The directory a take's preview pages are composed under (`/out//chrome/*-preview`). */ export const takePlayDir = (projectDir, id) => path.join(takesDirOf(projectDir), id); /** * A take's spec as the Play view needs it: from takes//take.json, else * from a recipe. null when no file names it. * * @param {string} projectDir * @param {string} id * @returns {Promise | null, made: boolean, from: string }>} */ export async function takeSpecOf(projectDir, id) { if (!isTakeId(id)) return null; const raw = await readJson(path.join(takesDirOf(projectDir), id, TAKE_FILE)); if (raw) { const r = parseTake(id, raw); if (!("error" in r)) { return { id, label: r.take.label, summary: r.take.summary, style: r.take.style, patch: r.take.patch, made: true, from: `takes/${id}/${TAKE_FILE}` }; } } for (const name of [RECIPE_FILE, ALL_STYLES_RECIPE]) { const doc = await readJson(path.join(takesDirOf(projectDir), name)); if (!doc) continue; let recipe; try { recipe = parseRecipe(doc); } catch { continue; } const spec = recipe.takes.find((t) => t.id === id); if (spec) return { id, label: spec.label, summary: spec.summary, style: spec.style, patch: spec.patch, made: false, from: `takes/${name}` }; } return null; } /** * Every take the Play view can play: the made ones (listTakes's order), then * the ones a recipe names that are not made yet. * * @param {string} projectDir * @returns {Promise>} */ export async function playableTakes(projectDir) { const { takes } = await listTakes(projectDir); const out = takes.map((t) => ({ id: t.id, label: t.label, made: true })); const seen = new Set(out.map((t) => t.id)); for (const name of [RECIPE_FILE, ALL_STYLES_RECIPE]) { const doc = await readJson(path.join(takesDirOf(projectDir), name)); if (!doc) continue; try { for (const t of parseRecipe(doc).takes) { if (seen.has(t.id)) continue; seen.add(t.id); out.push({ id: t.id, label: t.label, made: false }); } } catch { // A recipe that does not parse names no take here; the Takes page says why. } } return out; } /** * The take's manifest: its base + the style's patch + the take's patch. The * base is the version's frozen manifest when it has one (`frozen`, the * default), else the project's manifest now (`base`, when the caller has read * it already). Throws (an Error whose message is the reason) when the take is * not named anywhere or there is no manifest to lay it on. * * `frozen: false` lays the take over the project's manifest even when it is a * frozen version: "this take's patch, over the draft as it is now" -- what * the working cut's edit count compares (workingCut.mjs). * * @param {string} projectDir * @param {string} id * @param {{ styles?: Record }> | null, base?: Record | null, frozen?: boolean }} [opts] * @returns {Promise<{ manifest: Record, take: NonNullable>>, note: string | null, frozen: boolean }>} */ export async function resolveTakeManifest(projectDir, id, { styles = null, base = null, frozen: preferFrozen = true } = {}) { if (!isTakeId(id)) throw new Error(`"${id}" is not a take id`); const take = await takeSpecOf(projectDir, id); if (!take) throw new Error(`no take ${id} (no takes/${id}/${TAKE_FILE}, and no recipe names it)`); const frozenText = preferFrozen && take.made ? await frozenBaseText(projectDir, id) : null; let frozenDoc = null; if (frozenText !== null) { try { frozenDoc = JSON.parse(frozenText); } catch (e) { throw new Error(`take ${id}: its takes/${id}/${BASE_FILE} does not parse (${e instanceof Error ? e.message : e})`); } } const project = frozenDoc ?? base ?? (await readJson(path.join(projectDir, MANIFEST))); if (!isObj(project)) throw new Error(`the project has no ${MANIFEST}`); let stylePatch = null; let note = null; if (take.style) { const all = styles ?? (await loadMontageStyles()).styles; stylePatch = all[take.style]?.patch ?? null; if (!stylePatch) { // The style is gone: the manifest the take was rendered from, as it was. // A frozen version keeps its own base; only the style is gone. if (frozenDoc) { const m = resolvePresetLayers(frozenDoc, [take.patch]); if (!isObj(m) || !Array.isArray(m.timeline)) throw new Error(`take ${id}: the patched manifest has no timeline`); return { manifest: /** @type {Record} */ (m), take, note: `the style "${take.style}" is not installed: playing the version without it`, frozen: true }; } const rendered = take.made ? await readJson(path.join(takesDirOf(projectDir), id, MANIFEST)) : null; if (!isObj(rendered)) throw new Error(`take ${id}: its style "${take.style}" is not installed, and it has no rendered manifest`); return { manifest: rendered, take, note: `the style "${take.style}" is not installed: playing the manifest the take was rendered from`, frozen: true }; } } // preset → manifest → style → take patch (presets.mjs). const manifest = resolvePresetLayers(project, [stylePatch, take.patch]); if (!isObj(manifest) || !Array.isArray(manifest.timeline)) throw new Error(`take ${id}: the patched manifest has no timeline`); return { manifest: /** @type {Record} */ (manifest), take, note, frozen: frozenDoc !== null }; } /** * A take FROZEN, as it plays: a saved version's frozen base with its patches * (resolveTakeManifest), else -- a legacy take -- the manifest it was rendered * from (takes//video.manifest.json, the fallback the working cut has * always compared with). null when it has neither (a legacy draft never * rendered: it follows the project's manifest). * * @param {string} projectDir * @param {string} id * @returns {Promise, source: "base" | "rendered" }>} */ export async function frozenManifestOf(projectDir, id) { if (!isTakeId(id)) return null; if ((await frozenBaseText(projectDir, id)) !== null) { try { return { manifest: (await resolveTakeManifest(projectDir, id)).manifest, source: "base" }; } catch { return null; } } const rendered = await readJson(path.join(takesDirOf(projectDir), id, MANIFEST)); return isObj(rendered) ? { manifest: rendered, source: "rendered" } : null; } // One merged manifest per (take, the files it is made from), for the media // routes: a