// RENDER THIS CLIP (the clip editor's live preview, plans/live-preview.md): // one clip's segment with its on-screen chrome, pixel-true, in a minute or // two -- build-video.mjs `--clip-preview ` as one job on the app's runner // (lib/jobs.ts: one at a time, a log, Cancel), which build-video's CLI runs // inside the machine's heavy slot. The file is // `/out//clip-preview/.mp4`, with a record beside it // (`.json`: when, the clip's window then, the cut seconds it was laid at) // so the editor can say when the clip has moved since. // // Built from what is on disk (`--skip-fetch`): the editor's player already // plays that window, and a preview must never spend a fetch. import { readFile, stat } from "node:fs/promises"; import path from "node:path"; import { PIPELINE_DIR } from "./driver.mjs"; import { safeClipId } from "./shorts.mjs"; /** A clip's render is a minute or two; a stuck render is not left forever. */ export const CLIP_RENDER_TIMEOUT_MS = 20 * 60_000; /** build-video.mjs's CLIP_PREVIEW_DIR, under a variant's out dir. */ export const CLIP_RENDER_DIR = "clip-preview"; const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); const fin = (v) => typeof v === "number" && Number.isFinite(v); /** `/out//clip-preview/.mp4`, or null for an id that is not a file name. */ export function clipRenderFile(dir, variant, clipId) { if (!safeClipId(clipId) || !/^[a-z]+$/.test(String(variant))) return null; return path.join(/* turbopackIgnore: true */ dir, "out", variant, CLIP_RENDER_DIR, `${clipId}.mp4`); } /** * The job's one step: build-video `--clip-preview`, NDJSON progress, from * what is on disk. No path or argv from a client. E2E_UMTOOL_CLIP_RENDER_SCRIPT * (test-only, playwright.config.ts) names a stub with the same command line. * * @param {{ dir: string }} project * @param {string} clipId * @param {{ variant?: string | null, env?: Record }} [opts] */ export function clipRenderSteps(project, clipId, { variant = null, env = process.env } = {}) { if (!safeClipId(clipId)) throw new Error(`not a clip id: ${clipId}`); const bin = env.E2E_UMTOOL_CLIP_RENDER_SCRIPT || path.join(/* turbopackIgnore: true */ PIPELINE_DIR, "build-video.mjs"); return [ { cwd: PIPELINE_DIR, env: {}, label: `render ${clipId} with its on-screen chrome`, argv: [ "node", bin, path.join(/* turbopackIgnore: true */ project.dir, "video.manifest.json"), "--out", path.join(/* turbopackIgnore: true */ project.dir, "out"), ...(variant ? ["--variant", variant] : []), "--skip-fetch", "--clip-preview", clipId, "--progress", "ndjson", ], ndjson: true, timeoutMs: CLIP_RENDER_TIMEOUT_MS, }, ]; } /** * The render of one clip on disk, or null: `{ mtimeMs, size, record, stale }`. * `stale`: the clip's window (start, end, cut) moved since it was rendered. * * @param {string} dir the project * @param {string} variant * @param {string} clipId * @param {Record | null} [entry] the clip as the manifest has it now */ export async function readClipRender(dir, variant, clipId, entry = null) { const abs = clipRenderFile(dir, variant, clipId); if (!abs) return null; const st = await stat(abs).catch(() => null); if (!st?.isFile()) return null; const record = await readFile(abs.replace(/\.mp4$/, ".json"), "utf8").then(JSON.parse, () => null); return { mtimeMs: Math.round(st.mtimeMs), size: st.size, record: isObj(record) ? record : null, stale: windowMoved(record, entry) }; } /** Has the clip's window moved since `record` was made? Unknown (no record or entry) is not "moved". */ export function windowMoved(record, entry) { if (!isObj(record) || !isObj(record.window) || !isObj(entry)) return false; const w = record.window; const same = (a, b) => (fin(a) && fin(b) ? Math.abs(a - b) < 0.011 : (a ?? null) === (b ?? null)); return !(same(w.start, entry.start) && same(w.end, entry.end) && same(w.cutStart, entry.cutStart) && same(w.cutEnd, entry.cutEnd)); }