// A `video` timeline entry (video-entry.mjs): a file of the project played // full frame with its own sound, no chrome over it. Pure parts here; the // build is exercised on a real file at the end (skipped without ffmpeg). // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdtempSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { applyVideoPatch, blackoutEnable, validateVideoEntries, validateVideoEntry, videoBlackouts, videoDip, videoEnter, videoHidesChrome, videoLead, videoLoudnessTarget, videoSeconds, videoSegmentGraph, videoSegmentKey, videoSpan, } from "./video-entry.mjs"; import { deckSchedule, dipOf, estimatedDuration, hidesDeck, resolveDeck } from "./deck.mjs"; import { chromeOverlayChain, cutJoins } from "./build-video.mjs"; import { validateClipAudioEntries } from "./clip-audio.mjs"; const FIN = { type: "video", id: "fin", src: "posts-media/x/1/1_1.mp4", from: 32.26, fileSeconds: 46.32, audio: { bass: 8 } }; test("a video entry validates; the vocabulary stays open", () => { assert.deepEqual(validateVideoEntry(FIN), []); assert.deepEqual(validateVideoEntry({ type: "video", id: "v", src: "a.mp4", to: 3, fit: "cover", enter: "cut", holdLast: 1, deck: "shown", audio: { gain: -3 } }), []); const bad = validateVideoEntry({ type: "video", id: "v", src: "../x.mp4", from: -1, fit: "wide", audio: { bass: 40, treble: 1 }, enter: { out: 9 }, deck: "maybe", holdLast: 99 }); for (const w of ["climbs out", ".from", ".fit", "audio.bass", "audio.treble", "enter.out", ".deck", ".holdLast"]) { assert.ok(bad.some((e) => e.includes(w)), `${w} in ${bad.join(" | ")}`); } assert.ok(validateVideoEntry({ type: "video", id: "v" })[0].includes("needs `src`")); assert.ok(validateVideoEntry({ type: "video", id: "v", src: "/abs.mp4" })[0].includes("relative")); assert.ok(validateVideoEntry({ type: "video", id: "v", src: "a.mp4", from: 5, to: 5.1 })[0].includes("0.2 s")); // Other types are none of its business, and a clip's `audio` rule leaves a video's alone. assert.deepEqual(validateVideoEntries({ timeline: [{ type: "mystery", id: "m" }, { type: "card", id: "k" }, FIN] }), []); assert.deepEqual(validateClipAudioEntries({ timeline: [FIN] }), []); }); test("its span, lead and segment length: to − from (+ holdLast), never shortened by the fades", () => { assert.deepEqual(videoSpan(FIN), { from: 32.26, to: 46.32 }); assert.deepEqual(videoSpan({ ...FIN, to: 50 }, 46.32), { from: 32.26, to: 46.32 }); assert.equal(videoLead(FIN, 0.4), 0.4); assert.equal(videoLead({ ...FIN, enter: "cut" }, 0.4), 0); assert.equal(videoSeconds(FIN, 0.4), 14.46); // 0.4 lead + 14.06 assert.equal(videoSeconds({ ...FIN, enter: "cut" }, 0.4), 14.06); assert.equal(videoSeconds({ ...FIN, holdLast: 1.5 }, 0), 15.56); assert.equal(estimatedDuration(FIN, { transition: 0.4 }, 0.4), 14.46); assert.deepEqual(videoEnter(FIN), { out: 0.5, in: 0.5 }); assert.deepEqual(videoEnter({ ...FIN, enter: { out: 1 } }), { out: 1, in: 0.5 }); assert.equal(videoEnter({ ...FIN, enter: "cut" }), null); // The entrance is a dip on the item before: no held black (the fade up is its own). assert.deepEqual(dipOf(FIN), { fade: 0.5, black: 0 }); assert.deepEqual(videoDip({ ...FIN, enter: { out: 0.8, in: 0.2 } }), { fade: 0.8, black: 0 }); assert.equal(dipOf({ ...FIN, enter: "cut" }), null); }); test("no chrome over it by default; `deck: shown` keeps it", () => { const deck = resolveDeck({}); assert.equal(videoHidesChrome(FIN), true); assert.equal(hidesDeck(FIN, deck), true); assert.equal(hidesDeck({ ...FIN, deck: "shown" }, deck), false); }); test("its graph: fit, the bass before the level, the fade up after the lead, no fade at the end", () => { const render = { width: 1920, height: 1080, fps: 30, audioRate: 48000, audioChannels: 2 }; const span = videoSpan(FIN); const g = videoSegmentGraph({ entry: FIN, render, seconds: 14.46, lead: 0.4, gainDb: -2.5, span }); assert.match(g, /trim=start=32\.26:end=46\.32/); assert.match(g, /scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:\(ow-iw\)\/2:\(oh-ih\)\/2:color=black/); assert.match(g, /tpad=start_duration=0\.4:start_mode=add:color=black/); assert.match(g, /fade=t=in:st=0\.4:d=0\.5/); assert.doesNotMatch(g, /fade=t=out/); // Bass first, then the measured level. assert.ok(g.indexOf("bass=g=8:f=100:w=0.7") < g.indexOf("volume=-2.5dB")); assert.match(g, /adelay=400:all=1/); // A boost's peaks limited, after the level; none for a plain video. assert.ok(g.indexOf("alimiter=limit=0.891:level=false") > g.indexOf("volume=-2.5dB")); assert.doesNotMatch(videoSegmentGraph({ entry: { ...FIN, audio: undefined }, render, seconds: 14.46, lead: 0.4, span }), /alimiter/); const cover = videoSegmentGraph({ entry: { ...FIN, fit: "cover", enter: "cut" }, render, seconds: 14.06, lead: 0, span }); assert.match(cover, /force_original_aspect_ratio=increase,crop=1920:1080/); assert.doesNotMatch(cover, /fade=t=in|tpad|adelay/); const silent = videoSegmentGraph({ entry: FIN, render, seconds: 14.46, lead: 0.4, hasAudio: false, span }); assert.match(silent, /anullsrc=r=48000:cl=stereo,atrim=end=14\.46\[a\]/); }); test("its key: src, span, fit, audio, entrance, hold, lead and the file", () => { const render = { width: 1920, height: 1080, fps: 30 }; const file = { size: 100, mtimeMs: 5, seconds: 46.32 }; const k = videoSegmentKey({ entry: FIN, file, render, lead: 0.4 }); assert.equal(k, videoSegmentKey({ entry: { ...FIN }, file, render, lead: 0.4 })); for (const changed of [{ ...FIN, from: 30 }, { ...FIN, to: 40 }, { ...FIN, fit: "cover" }, { ...FIN, audio: { bass: 6 } }, { ...FIN, src: "b.mp4" }, { ...FIN, holdLast: 1 }, { ...FIN, enter: "cut" }]) { assert.notEqual(videoSegmentKey({ entry: changed, file, render, lead: 0.4 }), k, JSON.stringify(changed)); } assert.notEqual(videoSegmentKey({ entry: FIN, file: { ...file, mtimeMs: 6 }, render, lead: 0.4 }), k); assert.notEqual(videoSegmentKey({ entry: FIN, file, render, lead: 0 }), k); }); test("the schedule carries it; the chrome is off from the black to the cut's end", () => { const render = { width: 1920, height: 1080, fps: 30, transition: 0.4, chrome: { engine: "hyperframes", layout: "deck", deck: {} } }; const entries = [{ type: "card", id: "k01", seconds: 3, heading: "x" }, FIN]; const doc = deckSchedule({ entries, durs: [3, 14.46], D: 0.4, render }); const v = doc.segments[1]; assert.equal(v.type, "video"); assert.equal(v.hideDeck, true); assert.equal(v.noChrome, true); assert.deepEqual(v.enter, { out: 0.5, in: 0.5 }); assert.equal(doc.segments[0].noChrome, undefined); const bo = videoBlackouts(doc); assert.equal(bo.length, 1); // From the frame the dip made black (2.6 + 0.4 − 1/30) past the end. assert.ok(Math.abs(bo[0][0] - (2.6 + 0.4 - 1 / 30)) < 1e-3, String(bo[0][0])); assert.ok(bo[0][1] > doc.total); // Shown: no blackout. A cut without one: none. assert.deepEqual(videoBlackouts(deckSchedule({ entries: [entries[0], { ...FIN, deck: "shown" }], durs: [3, 14.46], D: 0.4, render })), []); assert.deepEqual(videoBlackouts(deckSchedule({ entries: [entries[0]], durs: [3], D: 0.4, render })), []); }); test("every overlay leaves the blackout out; without one the chain is the one it was", () => { const render = { fps: 30 }; const regions = [{ name: "deck", frames: "/f/deck", x: 0, y: 900 }, { name: "posts", frames: "/f/p", x: 10, y: 10, offset: 2 }]; const plain = chromeOverlayChain(render, regions, "[v]", 3, {}); const none = chromeOverlayChain(render, regions, "[v]", 3, { blackouts: [] }); assert.equal(none.chain, plain.chain); assert.doesNotMatch(plain.chain, /enable=/); const off = chromeOverlayChain(render, regions, "[v]", 3, { blackouts: [[2.9667, 20]] }); assert.equal((off.chain.match(/enable='not\(between\(t,2\.9667,20\)\)'/g) ?? []).length, 2); // A chunk's clock starts `shift` seconds in. assert.equal(blackoutEnable([[2.9667, 20]], 2), ":enable='not(between(t,0.9667,18))'"); assert.equal(blackoutEnable([], 2), ""); }); test("the cut ENDS on the video's final frame: no end fade when it is last; the dip into it on the item before", async () => { const dir = mkdtempSync(path.join(os.tmpdir(), "video-entry-")); const segs = ["a.mp4", "b.mp4"].map((n) => path.join(dir, n)); let ffmpeg = true; try { for (const [i, s] of segs.entries()) execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", `color=c=black:s=64x36:r=30:d=${i ? 2 : 1}`, "-f", "lavfi", "-i", "anullsrc=r=48000:cl=stereo", "-t", String(i ? 2 : 1), "-c:v", "libx264", "-c:a", "aac", s]); } catch { ffmpeg = false; } if (!ffmpeg) return; const render = { fps: 30, endFade: 1, transition: 0.4 }; const card = { type: "card", id: "k01", seconds: 1 }; const withVideo = await cutJoins({ entries: [card, FIN], segments: segs, render }); assert.ok(!withVideo?.[1]?.fade, "no end fade on the video"); assert.deepEqual(withVideo[0].dip.seconds, 0.5); const withCard = await cutJoins({ entries: [FIN, card], segments: segs, render }); assert.ok(withCard?.[1]?.fade, "the end fade as ever when it is not last"); const cutIn = await cutJoins({ entries: [card, { ...FIN, enter: "cut" }], segments: segs, render: { fps: 30, transition: 0.4 } }); assert.equal(cutIn, null); }); test("applyVideoPatch: dotted keys, enter forms, clearing, refusing a key it does not set", () => { const e = applyVideoPatch(FIN, { "audio.bass": "6", "audio.gain": -2, enter: "0.8,0.3", to: "44", fit: "cover" }); assert.deepEqual(e.audio, { bass: 6, gain: -2 }); assert.deepEqual(e.enter, { out: 0.8, in: 0.3 }); assert.equal(e.to, 44); assert.equal(applyVideoPatch(e, { enter: "cut" }).enter, "cut"); assert.equal(applyVideoPatch(e, { "audio.bass": null, "audio.gain": "" }).audio, undefined); assert.equal(applyVideoPatch(e, { to: null }).to, undefined); assert.throws(() => applyVideoPatch(e, { speed: 2 }), /not a video entry's setting/); assert.throws(() => applyVideoPatch(e, { from: "x" }), /must be a number/); // The original is untouched. assert.deepEqual(FIN.audio, { bass: 8 }); }); test("its level: the cut's target, else the level its narration sits at", () => { assert.deepEqual(videoLoudnessTarget({}, [], -14), { target: -14, basis: "render.loudness" }); assert.deepEqual(videoLoudnessTarget({}, [{ type: "post" }]), { target: -21, basis: "render.postNarration.lufs" }); assert.deepEqual(videoLoudnessTarget({ postNarration: { lufs: -19 } }, [{ type: "post" }]), { target: -19, basis: "render.postNarration.lufs" }); assert.deepEqual(videoLoudnessTarget({}, [{ type: "clip" }]), { target: -17.7, basis: "narration" }); });