// takePreview.mjs: the preview tier's knobs, the tier keyed apart from the full render's chunks, what // changed between two takes (per kind), the reel's runs and pieces, and the full-cut time math. import { test } from "node:test"; import assert from "node:assert/strict"; import { PREVIEW_DEFAULTS, atPreviewTier, clockOf, diffTimelines, entryChangeKinds, fullCutTimes, labelFilter, pieceLabel, planRuns, previewKnobs, previewPaths, reelPieces, renderLevelChanges, } from "./takePreview.mjs"; import { fastKindOf } from "../queue/cli.mjs"; import { chunkJobs, chunkVideoArgs, outputTier, tierFinalArgs } from "umtool-report-to-video/build-video"; const R = { width: 1920, height: 1080, fps: 30, preset: "medium", crf: 20, audioRate: 48000, audioChannels: 2 }; test("previewKnobs: the defaults, a knob over them, never larger than the cut, checked", () => { assert.deepEqual(previewKnobs(R), { ...PREVIEW_DEFAULTS }); assert.deepEqual(previewKnobs({ ...R, preview: { width: 960, height: 540, crf: 30 } }), { width: 960, height: 540, preset: "veryfast", crf: 30, segments: "shared" }); // A 640x360 cut previews at its own size. assert.deepEqual(previewKnobs({ ...R, width: 640, height: 360 }).width, 640); // A 9:16 cut previews 9:16 (lane vertical-deck): built 1080x1920 whatever its width and height say. assert.deepEqual(previewKnobs({ ...R, format: "vertical" }), { ...PREVIEW_DEFAULTS, width: 720, height: 1280 }); assert.deepEqual(previewKnobs({ ...R, width: 640, height: 360 }).height, 360); assert.throws(() => previewKnobs({ ...R, preview: { preset: "warp" } }), /x264 preset/); assert.throws(() => previewKnobs({ ...R, preview: { crf: 99 } }), /0–51/); assert.throws(() => previewKnobs({ ...R, preview: { segments: "half" } }), /shared/); assert.throws(() => previewKnobs({ ...R, preview: "yes" }), /an object/); }); test("atPreviewTier: render.tier set, the cut's own encode kept unless segments: preview", () => { const doc = { slug: "s", render: R, timeline: [] }; const t = atPreviewTier(doc); assert.deepEqual(t.render.tier, { name: "preview", width: 1280, height: 720, preset: "veryfast", crf: 26 }); assert.equal(t.render.preset, "medium"); assert.equal(t.render.crf, 20); assert.equal(doc.render.tier, undefined, "the manifest is copied"); const s = atPreviewTier({ ...doc, render: { ...R, preview: { segments: "preview" } } }); assert.equal(s.render.preset, "veryfast"); assert.equal(s.render.crf, 26); // A changes reel's default (segments at the tier), unless the manifest says. assert.equal(atPreviewTier(doc, { segments: "preview" }).render.preset, "veryfast"); assert.equal(atPreviewTier({ ...doc, render: { ...R, preview: { segments: "shared" } } }, { segments: "preview" }).render.preset, "medium"); assert.deepEqual(outputTier(t.render), t.render.tier); assert.equal(outputTier(R), null); }); test("a preview's chunk keys are never a full render's: the tier's scale, preset and crf are in the key", () => { const job = { segments: ["/w/a.mp4", "/w/b.mp4"], durs: [2, 2], starts: [0, 2], span: { a: 0, b: 60 }, D: 0, totalFrames: 120, outPath: "/w/c.mp4", }; const full = chunkVideoArgs({ ...job, render: R }); const prev = chunkVideoArgs({ ...job, render: atPreviewTier({ render: R }).render }); assert.notDeepEqual(full.keyArgv, prev.keyArgv); assert.ok(prev.keyArgv.join(" ").includes("scale=1280:720")); assert.ok(prev.argv.includes("veryfast") && prev.argv.includes("26")); // The full render's argv is what it always was: no scale, its own preset and crf. assert.ok(!full.keyArgv.join(" ").includes("scale=1280")); assert.ok(full.argv.includes("medium") && full.argv.includes("20")); assert.equal(tierFinalArgs(R, "a", "b"), null); // A preview encodes more chunks at once (its fast encode leaves the decode the bottleneck); the env still wins. assert.equal(chunkJobs({}, R), 2); assert.equal(chunkJobs({}, atPreviewTier({ render: R }).render), 4); assert.equal(chunkJobs({ REPORT_VIDEO_CHUNK_JOBS: "3" }, atPreviewTier({ render: R }).render), 3); assert.ok(tierFinalArgs(atPreviewTier({ render: R }).render, "a", "b")?.join(" ").includes("scale=1280:720")); }); test("previewPaths: the outputs beside the full render's, the builds in their own dirs", () => { const p = previewPaths("/r/p/takes/t", "slug"); assert.equal(p.previewMp4, "/r/p/takes/t/out/slug.preview.mp4"); assert.equal(p.previewOut, "/r/p/takes/t/out/preview"); assert.equal(p.changesMp4, "/r/p/takes/t/out/slug.changes.mp4"); assert.equal(p.changesFile, "/r/p/takes/t/changes.json"); }); const post = (id, o = {}) => ({ type: "post", id, text: "words", narration: { script: "words", pick: 0, takes: [{ file: `n/${id}/take-1.wav` }] }, ...o }); test("entryChangeKinds: each kind of change by its keys, the narration read closely", () => { const a = post("p1"); const k = (b) => entryChangeKinds(a, b).kinds; assert.deepEqual(k(a), []); // A new take picked: re-read; one with a revoice step: re-voiced; an older one picked again: re-picked. assert.deepEqual(k({ ...a, narration: { ...a.narration, pick: 1, takes: [...a.narration.takes, { file: "n/p1/take-2.wav" }] } }), ["re-read"]); assert.deepEqual(k({ ...a, narration: { ...a.narration, pick: 1, takes: [...a.narration.takes, { file: "n/p1/take-2.revoice.wav", steps: [{ op: "revoice" }] }] } }), ["re-voiced"]); const two = { ...a, narration: { ...a.narration, takes: [...a.narration.takes, { file: "n/p1/take-2.wav" }] } }; assert.deepEqual(entryChangeKinds(two, { ...two, narration: { ...two.narration, pick: 1 } }).kinds, ["re-picked"]); assert.deepEqual(k({ ...a, narration: { ...a.narration, rate: 1.1 } }), ["read knobs"]); assert.deepEqual(k({ ...a, fx: [{ kind: "caption" }] }), ["fx"]); assert.deepEqual(k({ ...a, hold: 2 }), ["hold"]); assert.deepEqual(k({ ...a, panel: "side" }), ["panel"]); assert.deepEqual(k({ ...a, pictureOnly: true }), ["picture only"]); assert.deepEqual(k({ ...a, thread: { read: true } }), ["thread"]); assert.deepEqual(k({ ...a, redact: ["x"] }), ["slurs"]); assert.deepEqual(k({ ...a, media: [{ start: 2, end: 9 }] }), ["media trim"]); assert.deepEqual(k({ ...a, wibble: 1 }), ["edited"]); const clip = { type: "clip", id: "c1", start: 1, end: 4 }; assert.deepEqual(entryChangeKinds(clip, { ...clip, cutEnd: 3.5 }).kinds, ["media trim"]); }); test("diffTimelines: added, changed, moved and removed, in the new timeline's order", () => { const A = [post("a"), post("b"), post("c"), post("d")]; const B = [post("a"), post("c"), post("b", { hold: 1 }), { type: "video", id: "v", src: "x.mp4" }]; const d = diffTimelines(A, B); assert.deepEqual(d.changed.map((c) => [c.id, c.kind, c.kinds]), [ ["c", "moved", ["moved"]], ["b", "changed", ["moved", "hold"]], ["v", "added", ["added video"]], ]); assert.deepEqual(d.removed, [{ id: "d", after: "c", before: null, type: "post" }]); assert.deepEqual(diffTimelines(A, A), { changed: [], removed: [] }); }); test("renderLevelChanges: render leaves and top-level keys, not the timeline nor render.preview", () => { const a = { slug: "s", timeline: [post("a")], render: { crf: 20, chrome: { deck: { x: 1 } } } }; const b = { slug: "s", timeline: [post("b")], render: { crf: 20, chrome: { deck: { x: 2 } }, preview: { crf: 30 } }, title: "T" }; assert.deepEqual(renderLevelChanges(a, b), ["render.chrome.deck.x", "title"]); }); test("planRuns: a change with a neighbour each side, a removal as its join, touching runs merged", () => { const ids = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; const diff = { changed: [{ id: "b", kind: "changed", kinds: ["re-read"] }, { id: "c", kind: "changed", kinds: ["fx"] }, { id: "i", kind: "added", kinds: ["added video"] }], removed: [{ id: "x", after: "e", before: "f", type: "post" }], }; const runs = planRuns(ids, diff); assert.deepEqual(runs.map((r) => r.ids), [["a", "b", "c", "d", "e", "f"], ["h", "i", "j"]]); assert.deepEqual(runs[0].focus.map((f) => [f.type, f.id]), [["item", "b"], ["item", "c"], ["join", "x"]]); assert.deepEqual(planRuns(ids, { changed: [{ id: "a", kind: "changed", kinds: ["fx"] }], removed: [] }).map((r) => r.ids), [["a", "b"]]); }); test("fullCutTimes: the base's measured schedule moved by the changed items' measured lengths", () => { // Base: a 10, b 20, c 5, d 7 (an intro-less hard-cut cut). New: b re-read to 25 s, x (6 s) added after c, d removed. const base = { estimated: false, transition: 0, segments: [ { id: "a", start: 0, duration: 10 }, { id: "b", start: 10, duration: 20 }, { id: "c", start: 30, duration: 5 }, { id: "d", start: 35, duration: 7 }, { id: "e", start: 42, duration: 3 }, ] }; const timeline = [post("a"), post("b"), post("c"), post("x"), post("e")]; const measured = new Map([["b", 25], ["x", 6]]); const r = fullCutTimes({ timeline, changedIds: new Set(["b", "x"]), base, measured }); assert.equal(r.basis, "measured"); const at = (id) => [r.times.get(id)?.start, r.times.get(id)?.end]; assert.deepEqual(at("a"), [0, 10]); assert.deepEqual(at("b"), [10, 35]); assert.deepEqual(at("c"), [35, 40]); assert.deepEqual(at("x"), [40, 46]); // e: 42 in the base; +25 +6 for b and x, −20 (b's old) −7 (d removed) = 46. assert.deepEqual(at("e"), [46, 49]); // A changed item not measured: from then on an estimate. const est = fullCutTimes({ timeline, changedIds: new Set(["b", "x"]), base, measured: new Map([["b", 25]]), estimate: () => 4 }); assert.equal(est.basis, "estimate"); assert.equal(est.times.get("a")?.exact, true); assert.equal(est.times.get("e")?.exact, false); // No base schedule: every length estimated but the measured ones. const none = fullCutTimes({ timeline, changedIds: new Set(["b"]), base: null, measured: new Map([["b", 25]]), estimate: () => 4 }); assert.equal(none.basis, "estimate"); assert.deepEqual([none.times.get("c")?.start, none.times.get("e")?.start], [29, 37]); }); test("reelPieces: the item ± pad within its run, a removal's join, labelled with the full cut's time", () => { const runs = [{ ids: ["a", "b", "c"], focus: [{ type: "item", id: "b", index: 1, kind: "changed", kinds: ["re-voiced"] }] }, { ids: ["d", "e"], focus: [{ type: "join", id: "x", after: "d", before: "e", index: 3, kind: "removed", kinds: ["removed post"] }] }]; const build = new Map([["a", { start: 0, duration: 2 }], ["b", { start: 2, duration: 10 }], ["c", { start: 12, duration: 1 }], ["d", { start: 13, duration: 4 }], ["e", { start: 17, duration: 5 }]]); const full = new Map([["b", { start: 1279.5, end: 1289.5, exact: true }], ["d", { start: 1500, end: 1504, exact: false }], ["e", { start: 1504, end: 1509, exact: false }]]); const p = reelPieces(runs, build, full, 3); assert.deepEqual([p[0].from, p[0].to], [0, 13]); assert.equal(p[0].label, "21:19 · b · re-voiced"); assert.deepEqual([p[1].from, p[1].to], [14, 20]); assert.equal(p[1].label, "25:04 · x removed — the join · removed post"); assert.equal(p[1].exact, false); }); test("clockOf, pieceLabel and the label's filter", () => { assert.equal(clockOf(1279.9), "21:19"); assert.equal(clockOf(3725), "1:02:05"); assert.equal(pieceLabel({ type: "item", id: "k", kinds: ["fx", "hold"] }, 61), "1:01 · k · fx, hold"); const f = labelFilter({ textFile: "/w/label 1.txt", font: "/f/a:b.ttf", height: 720 }); assert.ok(f.startsWith("drawtext=textfile='/w/label 1.txt'")); assert.ok(f.includes("fontfile='/f/a\\:b.ttf'")); assert.ok(f.includes("fontsize=24")); }); test("fastKindOf: a take step's preview or changes, never the take's own render", () => { assert.equal(fastKindOf(["take", "p", "--id", "t", "--preview"]), "preview"); assert.equal(fastKindOf(["take", "p", "--id", "t", "--changes", "--preview"]), "changes"); assert.equal(fastKindOf(["take", "p", "--id", "t", "--full"]), null); assert.equal(fastKindOf(["takes", "ls", "p"]), null); });