// Room at a clip's edges (render.room, release 36 lane E), rendered for real: // a clip's sound runs on to the end of its words and is silent after it while // its picture runs the join's length further; the next clip's picture starts // a join's length early, muted; the joined cut is the segments' sum less the // joins, with each clip's sound still on its picture; captions stop at the // silence point; loudness is measured on what is heard; with no words the // silence is found in the sound. Absent, null or with neither edge on, the // render is byte for byte what it was. // // 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 { readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import test from "node:test"; // The corpus the build reads metadata and cues from: set before build-video // (cues.mjs) is loaded, so it is this file's own. const CORPUS = mkdtempSync(path.join(os.tmpdir(), "rtv-room-corpus-")); process.env.CHANNELS_DIR = CORPUS; const { buildVideo, measureLoudness, probeDuration, setProgressMode } = await import("./build-video.mjs"); const { lintManifest } = await import("./lint.mjs"); const { MONO_BOLD_FONT_FILE, MONO_FONT_FILE } = await import("./brand.mjs"); test.after(() => rm(CORPUS, { recursive: true, force: true })); const have = ["ffmpeg", "ffprobe"].every((b) => spawnSync(b, ["-version"]).status === 0) && /\bass\b/.test(String(spawnSync("ffmpeg", ["-hide_banner", "-filters"]).stdout)); const FPS = 30; const PALETTE = { bg: "#000000", fg: "#ffffff", muted: "#888888", accent: "#c8752a", amber: "#ffc860" }; const RENDER = { width: 320, height: 180, fps: FPS, audioRate: 48000, audioChannels: 2, preset: "ultrafast", transition: 0.4, headerHeight: 0, footerHeight: 0, qr: false, palette: PALETTE, fontRegular: MONO_FONT_FILE, fontBold: MONO_BOLD_FONT_FILE, fetchPad: 0, snapWindow: 0.05, silenceMinDur: 0.1, }; const CHANNEL = "synth"; // vid1 has cues (one long one); vid2 an empty cue list. for (const [video, cues] of [["vid1", true], ["vid2", false]]) { const dir = path.join(CORPUS, CHANNEL, "data", video); 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: video, title: "Synthetic", duration: 12, uploadDate: "20240102", cues: cues ? [{ start: 0.2, end: 11.8, text: "filler words only and more filler words here" }] : [] })); writeFileSync(path.join(dir, "metadata.info.json"), JSON.stringify({ id: video, title: "Synthetic", duration: 12 })); } // c1's words run on past its end (4.0) to 4.75, then a gap; c2's first word // is 0.05 s after its start (6.0); nothing near c2's end (9.0). const WORDS = [[1.1, 1.5], [2, 2.4], [3.5, 3.9], [3.95, 4.3], [4.4, 4.75], [5.5, 5.8], [6.05, 6.4], [6.6, 7], [8, 8.5]] .map(([start, end], i) => ({ w: `w${i}`, start, end })); /** * A project: two clips of `video` (c1 1-4 s, c2 6-9 s), cut from one window * in out/clips-raw (12 s, a grey picture, a 440 Hz tone -- silent over * `gap` when given), with reactions.json + words.json for vid1 when `words`. */ async function project(render = {}, { video = "vid1", words = true, gap = null, clips = [{}, {}] } = {}) { const root = mkdtempSync(path.join(os.tmpdir(), "rtv-room-")); mkdirSync(path.join(root, "out", "clips-raw"), { recursive: true }); const raw = path.join(root, "out", "clips-raw", `${video}_0.00-12.00.mp4`); const vol = gap ? `volume=enable='between(t,${gap[0]},${gap[1]})':volume=0,volume=0.5` : "volume=0.5"; const r = spawnSync("ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", `color=c=gray:s=320x180:r=${FPS}:d=12`, "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=12", "-filter_complex", `[1:a]${vol}[a]`, "-map", "0:v", "-map", "[a]", "-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", "-shortest", raw, ]); assert.equal(r.status, 0, String(r.stderr)); if (words) { await writeFile(path.join(root, "reactions.json"), JSON.stringify({ version: 1, source: { channel: CHANNEL, video, duration: 12, words: "words.json" }, speakers: [{ index: 0, role: "host", label: "Host", how: "auto" }, { index: 1, role: "subject", label: "Subject", how: "auto" }], turns: [{ start: 0, end: 12, speaker: 1 }], laughs: [], moments: [], proposals: [], })); await writeFile(path.join(root, "words.json"), JSON.stringify({ words: WORDS })); } const manifestPath = path.join(root, "video.manifest.json"); await writeFile(manifestPath, JSON.stringify({ slug: "room", title: "Room", provenance: { channelSlug: CHANNEL, channelsDir: CORPUS }, render: { ...RENDER, ...render }, timeline: [ { type: "clip", id: "c1", channel: CHANNEL, video, start: 1, end: 4, cite: 1, quote: "q", ...clips[0] }, { type: "clip", id: "c2", channel: CHANNEL, video, start: 6, end: 9, cite: 6, quote: "q", ...clips[1] }, ], })); return { root, manifestPath, raw, seg: (id) => path.join(root, "out", "sourced", "segments", `${id}.mp4`) }; } const md5 = (f) => spawnSync("md5sum", [f]).stdout.toString().split(" ")[0]; const quiet = () => setProgressMode(() => {}); const record = async (p, id) => JSON.parse(await readFile(p.seg(id).replace(/\.mp4$/, ".cut.json"), "utf8")); /** The RMS of a file's sound over [a, b] (s), mono 8 kHz, 16-bit. */ function rms(file, a, b) { const r = spawnSync("ffmpeg", ["-v", "error", "-i", file, "-ac", "1", "-ar", "8000", "-f", "s16le", "-"], { maxBuffer: 1 << 26 }); const pcm = new Int16Array(r.stdout.buffer, r.stdout.byteOffset, Math.floor(r.stdout.length / 2)); let sum = 0; let n = 0; for (let i = Math.floor(a * 8000); i < Math.min(pcm.length, Math.floor(b * 8000)); i += 1) { sum += pcm[i] * pcm[i]; n += 1; } return n ? Math.sqrt(sum / n) : 0; } const LOUD = 1500; const SILENT = 30; const near = (a, b, tol, what) => assert.ok(Math.abs(a - b) <= tol, `${what}: ${a} vs ${b}`); test("render: without room (absent, null, or neither edge on) the cut is byte for byte the same", { skip: !have && "needs ffmpeg with libass" }, async () => { quiet(); const ps = [await project({}), await project({ room: null }), await project({ room: { tail: false, head: false } })]; try { for (const p of ps) await buildVideo({ manifestPath: p.manifestPath, opts: { noNetwork: true } }); for (const f of ["room.mp4", "sourced/segments/c1.mp4", "sourced/segments/c2.mp4", "sourced/segments/c1.cut.json", "sourced/segments/c2.cut.json"]) { const sums = ps.map((p) => md5(path.join(p.root, "out", f))); assert.equal(new Set(sums).size, 1, `${f}: ${sums.join(" ")}`); } assert.equal((await record(ps[0], "c1")).room, undefined); } finally { for (const p of ps) await rm(p.root, { recursive: true, force: true }); } }); test("render: the sound runs to the end of the words and is silent after; the picture runs D + pad past it; a muted head; sum − joins", { skip: !have && "needs ffmpeg with libass" }, async () => { quiet(); const p = await project({ room: { tail: true, head: true }, loudness: { target: -16 }, captions: { style: "speakers" } }); try { const r = await buildVideo({ manifestPath: p.manifestPath, opts: { noNetwork: true } }); const c1 = await record(p, "c1"); const c2 = await record(p, "c2"); // c1: the first clip, so no head; its words run to 4.75; picture to 4.75 + 0.4 + 0.1. assert.deepEqual([c1.start, c1.end], [1, 5.25]); assert.deepEqual(c1.room.sound, [1, 4.75]); assert.equal(c1.room.via, "words.json"); assert.equal(c1.room.head, 0); assert.equal(c1.room.tail, 0.5); // c2: speech 0.05 s after its start -> 0.4 s of muted head; no word near its end. assert.deepEqual([c2.start, c2.end], [5.6, 9.5]); assert.deepEqual(c2.room.sound, [6, 9]); assert.equal(c2.room.head, 0.4); const d1 = await probeDuration(p.seg("c1"), FPS); const d2 = await probeDuration(p.seg("c2"), FPS); near(d1, 4.25, 1 / FPS + 1e-6, "c1's length"); near(d2, 3.9, 1 / FPS + 1e-6, "c2's length"); // The tail: heard to the silence point (3.75 s into c1), silent after it. assert.ok(rms(p.seg("c1"), 3.2, 3.7) > LOUD, "c1 heard before the silence point"); assert.ok(rms(p.seg("c1"), 3.85, 4.2) < SILENT, `c1 silent after it: ${rms(p.seg("c1"), 3.85, 4.2)}`); // The head: silent until the cut's start (0.4 s into c2), heard after. assert.ok(rms(p.seg("c2"), 0, 0.33) < SILENT, "c2's head muted"); assert.ok(rms(p.seg("c2"), 0.45, 1) > LOUD, "c2 heard from its cut"); // Loudness measured on what is heard. near(await measureLoudness(p.seg("c1"), 0, 3.7), -16, 0.6, "c1's audible loudness"); // Captions stop at the silence point: c1's last line ends by 3.75 s. const ass = await readFile(p.seg("c1").replace(/\.mp4$/, ".captions.ass"), "utf8"); const ends = ass.split("\n").filter((l) => l.startsWith("Dialogue:")).map((l) => { const [h, m, s] = l.split(",")[2].split(":").map(Number); return h * 3600 + m * 60 + s; }); assert.ok(ends.length > 0 && Math.max(...ends) <= 3.751, `caption ends ${ends}`); // The cut: the sum less one join; the join plays over silence; c2's sound on its picture. const total = await probeDuration(r.out, FPS); near(total, d1 + d2 - 0.4, 2 / FPS, "the cut's length"); const join = d1 - 0.4; // c2 starts here in the cut assert.ok(rms(r.out, join + 0.02, d1 - 0.1) < SILENT, "the join is quiet"); assert.ok(rms(r.out, join + 0.5, join + 1) > LOUD, "c2 heard from its cut, in the cut"); assert.ok(rms(r.out, 2.5, 3.6) > LOUD, "c1 heard in the cut"); } finally { await rm(p.root, { recursive: true, force: true }); } }); test("render: no words -- the silence is found in the sound; a clip's room: false keeps its edge", { skip: !have && "needs ffmpeg with libass" }, async () => { quiet(); // vid2 has no cues and no words file; its tone is silent 4.6-5.6 s. const p = await project({ room: { tail: true, head: true } }, { video: "vid2", words: false, gap: [4.6, 5.6], clips: [{}, { room: false }] }); try { await buildVideo({ manifestPath: p.manifestPath, opts: { noNetwork: true } }); const c1 = await record(p, "c1"); assert.equal(c1.room.via, "silence"); near(c1.room.sound[1], 4.6, 0.05, "c1's sound stops where the silence starts"); near(c1.end, c1.room.sound[1] + 0.5, 0.002, "and its picture 0.5 s later"); const c2 = await record(p, "c2"); assert.equal(c2.room, undefined); assert.deepEqual([c2.start, c2.end], [6, 9]); } finally { await rm(p.root, { recursive: true, force: true }); } }); test("the lint: a fetched window the room can reach past; validators refuse a bad room", () => { const m = { slug: "x", title: "x", provenance: { channelSlug: CHANNEL }, render: { ...RENDER, fetchPad: 1, room: { tail: true } }, timeline: [{ type: "clip", id: "c1", video: "v", start: 1, end: 4 }], }; const lint = lintManifest(m, { manifestDir: os.tmpdir() }); assert.ok(lint.warnings.some((w) => /c1: its room can run 3 s past the cut but a fetched window ends 1 s after it/.test(w)), lint.warnings.join("; ")); const bad = lintManifest({ ...m, render: { ...m.render, room: { tail: "yes" } } }, { manifestDir: os.tmpdir() }); assert.ok(bad.errors.some((x) => /render\.room\.tail must be true or false/.test(x)), bad.errors.join("; ")); });