// A clip's cold open (lane clip-kit, slice G): the rules, the plan (its span first, a hard cut into the clip), and the // join -- cut from the segments, never the build's crossfade. import { test } from "node:test"; import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdtempSync } from "node:fs"; import { rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { coldOpenErrors, coldOpenNote } from "./clip-kit.mjs"; import { coldOpenEntry, piecesWhole, planVerticalCut } from "./vertical-plan.mjs"; import { assembleShort, probeSeconds } from "./reframe.mjs"; const clip = (o = {}) => ({ type: "clip", id: "w01", video: "v", start: 10, end: 40, cutStart: 12, cutEnd: 38, title: "T", ...o }); test("the rules: {from, to} inside the clip's cut, 1 to 4 s; a note outside 1.5 to 3 s", () => { assert.deepEqual(coldOpenErrors({ from: 35, to: 37.5 }, "c", clip()), []); const no = (v, re) => assert.match(coldOpenErrors(v, "c", clip()).join("\n"), re, JSON.stringify(v)); no({ from: 35 }, /\{from, to\}, from before to/); no({ from: 35, to: 35.5 }, /0\.5 s: a cold open is 1–4 s/); no({ from: 30, to: 36 }, /6 s: a cold open is 1–4 s/); no({ from: 38, to: 40 }, /outside the clip's cut \(12–38\)/); no({ from: 35, to: 37, play: true }, /play is not a key/); // A gap inside it or across either edge: refused by the clip's name and the gap, never shortened. const gapped = clip({ gaps: [{ from: 36, to: 36.5 }] }); for (const v of [{ from: 35, to: 37.5 }, { from: 36.2, to: 38 }, { from: 34, to: 36.3 }]) { assert.match(coldOpenErrors(v, "timeline w01.coldOpen", gapped).join(), /timeline w01\.coldOpen .* runs across w01's gap 36–36\.5 s/, JSON.stringify(v)); } assert.deepEqual(coldOpenErrors({ from: 34, to: 36 }, "w01.coldOpen", gapped), [], "up to the gap"); assert.doesNotMatch(coldOpenErrors({ from: 35, to: 37.5 }, "timeline w01.coldOpen", gapped).join(), /~co/); // The whole cut is not a cold open. assert.match(coldOpenErrors({ from: 12, to: 15 }, "c", clip({ cutEnd: 15 })).join(), /is the whole cut/); assert.equal(coldOpenNote(clip({ coldOpen: { from: 35, to: 37.5 } }), "w01"), null); assert.match(coldOpenNote(clip({ coldOpen: { from: 34, to: 37.8 } }), "w01"), /3\.8 s; 1\.5–3 s reads best/); }); test("the plan: the span first as ~co (no card, no code, its own zooms), then the clip; both joined hard", () => { const e = clip({ coldOpen: { from: 35, to: 37.5 }, qr: { corner: "tl" }, promo: { hold: 2 }, zooms: [{ at: 20 }, { at: 35.2, hold: 0.5, ramp: 0.3 }] }); const co = coldOpenEntry(e); assert.equal(co.id, "w01~co"); assert.deepEqual([co.start, co.end, co.cutStart, co.promo, co.qr, co.coldOpen], [35, 37.5, undefined, false, false, undefined]); assert.deepEqual(co.zooms, [{ at: 35.2, hold: 0.5, ramp: 0.3 }], "only the zooms inside its span"); const plan = planVerticalCut({ timeline: [e, clip({ id: "w02" })], items: ["w01", "w02"] }); assert.deepEqual(plan.timeline.map((x) => x.id), ["w01~co", "w01", "w02"]); assert.deepEqual(plan.pieces, [{ id: "w01~co", from: 0, to: null, hard: true }, { id: "w01", from: 0, to: null, hard: true }, { id: "w02", from: 0, to: null }]); assert.equal(piecesWhole(plan.pieces), false); // Not asked (--no-cold-open, a landscape or a reframe): the plan it always made. const off = planVerticalCut({ timeline: [e], items: ["w01"], coldOpens: false }); assert.deepEqual(off.pieces, [{ id: "w01", from: 0, to: null }]); assert.equal(piecesWhole(off.pieces), true); assert.equal(coldOpenEntry(clip()), null); }); const have = ["ffmpeg", "ffprobe"].every((b) => spawnSync(b, ["-version"]).status === 0); test("the hard join: the pieces cut from their segments end to end -- the cold open, then the clip, no crossfade", { skip: !have && "needs ffmpeg" }, async () => { const root = mkdtempSync(path.join(os.tmpdir(), "rtv-coldopen-")); try { const seg = (name, colour, d) => { const r = spawnSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", `color=c=${colour}:s=320x568:r=30:d=${d}`, "-f", "lavfi", "-i", `sine=frequency=440:sample_rate=48000:duration=${d}`, "-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", "-shortest", path.join(root, name)]); assert.equal(r.status, 0, String(r.stderr)); }; seg("w01~co.mp4", "white", 2); seg("w01.mp4", "black", 3); // The build's own joined file (a 0.4 s crossfade): never what a hard join plays. seg("built.mp4", "gray", 4.6); const out = path.join(root, "short.mp4"); const r = await assembleShort({ pieces: [{ id: "w01~co", from: 0, to: null, hard: true }, { id: "w01", from: 0, to: null, hard: true }], segmentsDir: root, built: path.join(root, "built.mp4"), outFile: out, render: { fps: 30, vertical: { loudness: -14 } }, }); assert.ok(Math.abs(r.seconds - 5) < 0.1, `the two pieces end to end (${r.seconds})`); const y = (t) => Number((/YAVG=([0-9.]+)/.exec(String(spawnSync("ffmpeg", ["-v", "error", "-ss", String(t), "-i", out, "-frames:v", "1", "-vf", "signalstats,metadata=print:file=-", "-f", "null", "-"]).stdout)) ?? [])[1]); assert.ok(y(0.5) > 200, "the cold open first"); assert.ok(y(3) < 40, "then the clip"); assert.ok(Math.abs((await probeSeconds(out)) - r.seconds) < 0.01); } finally { await rm(root, { recursive: true, force: true }); } });