// The render's cut, from the render's code: a fixture RENDERED (build-video.mjs // buildVideo) and read back through the cut-points route's function // (cutPoints.mjs cutPointsFor) -- every clip's start, end, which edge // snapped, room's audible span and the loudness gain must be the cut // records', to the millisecond. And the route measures once: a second read is // the cache, and a moved cut (the bench's edge drag) re-snaps on the cached // silences without ffmpeg. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; import { readdir, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import test from "node:test"; // The corpus and the cache: set before the build (cues.mjs) and paths.mjs are loaded. const CORPUS = mkdtempSync(path.join(os.tmpdir(), "cutpoints-corpus-")); const CACHE = mkdtempSync(path.join(os.tmpdir(), "cutpoints-cache-")); process.env.CHANNELS_DIR = CORPUS; process.env.UMTOOL_CACHE_DIR = CACHE; const { buildVideo, setProgressMode } = await import("umtool-report-to-video/build-video"); const { MONO_BOLD_FONT_FILE, MONO_FONT_FILE } = await import("umtool-report-to-video/brand"); const { renderCut } = await import("umtool-report-to-video/snap"); const { cutPointsFor } = await import("./cutPoints.mjs"); test.after(async () => { await rm(CORPUS, { recursive: true, force: true }); await rm(CACHE, { recursive: true, force: true }); }); const have = ["ffmpeg", "ffprobe"].every((b) => spawnSync(b, ["-version"]).status === 0); const CHANNEL = "synth"; const PALETTE = { bg: "#000000", fg: "#ffffff", muted: "#888888", accent: "#c8752a", amber: "#ffc860" }; const RENDER = { width: 320, height: 180, fps: 30, audioRate: 48000, audioChannels: 2, preset: "ultrafast", headerHeight: 0, footerHeight: 0, qr: false, palette: PALETTE, fontRegular: MONO_FONT_FILE, fontBold: MONO_BOLD_FONT_FILE, fetchPad: 1, snapWindow: 0.8, silenceMinDur: 0.15, snapLead: 0.3, snapTail: 0.4, }; const dir = path.join(CORPUS, CHANNEL, "data", "vid1"); mkdirSync(dir, { recursive: true }); writeFileSync(path.join(CORPUS, CHANNEL, "config.json"), JSON.stringify({ name: "Synth" })); writeFileSync(path.join(dir, "transcript.cues.json"), JSON.stringify({ id: "vid1", title: "Synthetic", duration: 20, uploadDate: "20240102", cues: [] })); writeFileSync(path.join(dir, "metadata.info.json"), JSON.stringify({ id: "vid1", title: "Synthetic", duration: 20 })); /** * A project cut from one 20 s window in out/clips-raw: a 440 Hz tone with * gaps (pauses between "words") at 2.6-2.9, 5.4-5.9, 8.1-8.4, 11.2-11.6, * 14.5-15.2 -- so every edge below has a pause within the snap window, and * one (c3's start) has none. */ async function project(render = {}) { const root = mkdtempSync(path.join(os.tmpdir(), "cutpoints-")); mkdirSync(path.join(root, "out", "clips-raw"), { recursive: true }); const raw = path.join(root, "out", "clips-raw", "vid1_0.00-20.00.mp4"); const gaps = [[2.6, 2.9], [5.4, 5.9], [8.1, 8.4], [11.2, 11.6], [14.5, 15.2]]; const vol = gaps.map(([a, b]) => `volume=enable='between(t,${a},${b})':volume=0`).join(","); const r = spawnSync("ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", "color=c=gray:s=320x180:r=30:d=20", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=20", "-filter_complex", `[1:a]${vol},volume=0.5[a]`, "-map", "0:v", "-map", "[a]", "-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", "-shortest", raw, ]); assert.equal(r.status, 0, String(r.stderr)); const manifestPath = path.join(root, "video.manifest.json"); await writeFile(manifestPath, JSON.stringify({ slug: "cutpoints", title: "Cut points", provenance: { channelSlug: CHANNEL, channelsDir: CORPUS }, render: { ...RENDER, ...render }, timeline: [ // A cut inside the extent (the lead-in before it): both edges snap. { type: "clip", id: "c1", channel: CHANNEL, video: "vid1", start: 2, end: 6, cutStart: 3.3, cutEnd: 5.2, cite: 3, quote: "q" }, // The extent alone: the start snaps back to 8.4 - lead, the end to 11.2 + tail. { type: "clip", id: "c2", channel: CHANNEL, video: "vid1", start: 8.2, end: 11.0, cite: 9, quote: "q" }, // No pause near its start (12.6): it stays; the end snaps into the long pause. { type: "clip", id: "c3", channel: CHANNEL, video: "vid1", start: 12.6, end: 14.4, cite: 13, quote: "q" }, ], })); return { root, manifestPath }; } const quiet = () => setProgressMode(() => {}); const record = async (root, id) => JSON.parse(await readFile(path.join(root, "out", "sourced", "segments", `${id}.cut.json`), "utf8")); async function agree(p, render) { await buildVideo({ manifestPath: p.manifestPath, opts: { noNetwork: true, noXfade: render.transition === 0 } }); const got = await cutPointsFor({ id: "fixture", dir: p.root }); assert.equal(got.clips.length, 3); for (const c of got.clips) { const rec = await record(p.root, c.id); assert.equal(c.cutStart, rec.start, `${c.id} start`); assert.equal(c.cutEnd, rec.end, `${c.id} end`); assert.deepEqual(c.snapped, rec.snapped, `${c.id} snapped`); if (rec.room) assert.deepEqual(c.sound, rec.room.sound, `${c.id} room's sound`); else assert.equal(c.sound, undefined, `${c.id} has no room`); if (rec.loudness) assert.equal(c.gainDb, rec.loudness.gain, `${c.id} loudness gain`); else assert.equal(c.gainDb, undefined); } return got; } test("cut points: the route's edges are the render's cut records, edge for edge", { skip: !have && "needs ffmpeg" }, async () => { quiet(); const p = await project({ transition: 0 }); try { const got = await agree(p, { transition: 0 }); const byId = Object.fromEntries(got.clips.map((c) => [c.id, c])); // Something actually moved, both ways: c1's start earlier than its play window, c2's end later. assert.ok(byId.c1.cutStart < byId.c1.playFrom - 0.05, "c1's start snaps earlier"); assert.ok(byId.c2.cutEnd > byId.c2.playTo + 0.05, "c2's end snaps later"); assert.deepEqual(byId.c3.snapped, { start: false, end: true }); // The client's snap (the bench's ghost marker) from the silences the route returned: the same edges. for (const c of got.clips) { const again = renderCut({ sil: c.silences, fetchStart: c.fetchStart, playFrom: c.playFrom, playTo: c.playTo, render: { ...RENDER, transition: 0 } }); assert.equal(again.start, c.cutStart); assert.equal(again.end, c.cutEnd); } } finally { await rm(p.root, { recursive: true, force: true }); } }); test("cut points: with room at the edges and even loudness, the sound's span and the gain are the render's too", { skip: !have && "needs ffmpeg" }, async () => { quiet(); const render = { transition: 0.4, room: { tail: true, head: true }, loudness: { target: -16 } }; const p = await project(render); try { const got = await agree(p, render); assert.ok(got.clips.some((c) => c.sound), "room widened at least one clip"); assert.ok(got.clips.every((c) => typeof c.gainDb === "number"), "each clip has its gain"); } finally { await rm(p.root, { recursive: true, force: true }); } }); test("cut points: measured once -- a second read and a moved cut reuse the cached silences", { skip: !have && "needs ffmpeg" }, async () => { quiet(); const p = await project({ transition: 0 }); try { const t0 = performance.now(); const first = await cutPointsFor({ id: "fixture", dir: p.root }); const cold = performance.now() - t0; const files = (await readdir(path.join(CACHE, "cut-points"))).filter((f) => f.startsWith("silences-")); assert.ok(files.length >= 1, "the silences are on disk"); // The cut moved (an edge edit): no new measurement, the edge follows. const m = JSON.parse(await readFile(p.manifestPath, "utf8")); m.timeline[0].cutEnd = 4.9; await writeFile(p.manifestPath, JSON.stringify(m)); const t1 = performance.now(); const moved = await cutPointsFor({ id: "fixture", dir: p.root }); const warm = performance.now() - t1; assert.deepEqual((await readdir(path.join(CACHE, "cut-points"))).filter((f) => f.startsWith("silences-")).sort(), files.sort()); assert.notEqual(moved.clips[0].playTo, first.clips[0].playTo); assert.ok(warm < cold, `warm ${warm.toFixed(0)} ms < cold ${cold.toFixed(0)} ms`); } finally { await rm(p.root, { recursive: true, force: true }); } });