// trailer.mjs: a GAPPED beat (lane/reel-bench) -- two moments of one clip as // one beat of jump cuts, cut from the master as its kept pieces joined the // way a clip's gap renders (hard cuts, the sound faded GAP_FADE), not by the // trailer's beat-to-beat crossfade. The rule, the plan, the graph, and one // real encode of a synthesized master. import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { TrailerError, beatGaps, parseBeats, pieceFrames, planTrailer, trailerArgs, trailerRuns } from "./trailer.mjs"; const SCHEDULE = { version: 1, kind: "deck", fps: 30, transition: 0.5, total: 40.167, segments: [ { id: "brand-intro", type: "intro", start: 0, duration: 4.167, end: 4.167 }, { id: "c1", type: "clip", start: 3.667, duration: 12, end: 15.667 }, { id: "c2", type: "clip", start: 15.167, duration: 10, end: 25.167 }, { id: "c3", type: "clip", start: 24.667, duration: 15.5, end: 40.167 }, ], }; const RECORDS = new Map([ ["c1", { version: 1, id: "c1", start: 100.2, end: 112.2 }], // c2's render left 504–505 out: its segment plays 500–504 then 505–511. ["c2", { version: 1, id: "c2", start: 500, end: 511, gaps: [{ from: 504, to: 505 }] }], ["c3", { version: 1, id: "c3", start: 2000, end: 2015.5 }], ]); const plan = (beats, o = {}) => planTrailer({ schedule: SCHEDULE, records: RECORDS, beats: parseBeats(beats), snap: false, ...o }); const close = (a, b, eps = 1 / 60) => assert.ok(Math.abs(a - b) <= eps, `${a} ≉ ${b}`); test("beatGaps: a clip's gap rule on a beat -- sorted, 2 dp, refused by name", () => { assert.deepEqual(beatGaps({ start: 1, end: 9, gaps: [{ from: 6.004, to: 7 }, { from: 2, to: 3.5 }] }), [{ from: 2, to: 3.5 }, { from: 6, to: 7 }]); assert.deepEqual(beatGaps({ start: 1, end: 9 }), []); const no = (gaps, re) => assert.throws(() => beatGaps({ start: 1, end: 9, gaps }, "beat 2 (c1)"), (e) => e instanceof TrailerError && re.test(e.message)); no([{ from: 2, to: 2.1 }], /beat 2 \(c1\): the gap 2–2.1 is under 0.2 s/); no([{ from: 1.2, to: 3 }], /leaves 0.20 s after the beat's start; keep at least 0.5 s/); no([{ from: 2, to: 4 }, { from: 3, to: 5 }], /overlaps the gap before it/); no([{ from: 7, to: 8.8 }], /leaves 0.20 s before the beat's end/); no([{ from: 7, to: 9.5 }], /runs past the beat's end/); no("2-3", /gaps is a list/); // parseBeats carries them on a clip beat. assert.deepEqual(parseBeats([{ id: "c1", start: 1, end: 9, gaps: [{ from: 2, to: 3 }] }])[0].gaps, [{ from: 2, to: 3 }]); }); test("a gapped beat is one piece: its span on the master, the gap's master seconds cut, the kept seconds counted", () => { const p = plan([{ id: "c1", start: 102, end: 110, gaps: [{ from: 104, to: 106 }] }]); const [c] = p.pieces; // 3.667 + (102 − 100.2) = 5.467 → frame 164; 8 s = 240 frames; the gap 104–106 is master 7.467–9.467. assert.equal(c.frames.start, 164); assert.equal(c.frames.count, 240); assert.equal(c.cuts.length, 1); assert.deepEqual(c.cuts[0].frames, { start: 224, count: 60 }); close(c.cuts[0].from, 7.467); assert.equal(c.seconds, 6); assert.equal(pieceFrames(c), 180); assert.equal(p.total, 6); assert.deepEqual(c.gaps, [{ from: 104, to: 106 }]); }); test("one gapped beat runs as long as the two beats it replaces, less their crossfade (within the 0.35 s the brief allows)", () => { const two = plan([{ id: "c1", start: 102, end: 104 }, { id: "c1", start: 106, end: 110 }], { xfade: 0.3 }); const one = plan([{ id: "c1", start: 102, end: 110, gaps: [{ from: 104, to: 106 }] }], { xfade: 0.3 }); assert.equal(two.total, 5.7); assert.equal(one.total, 6); assert.ok(Math.abs(one.total - two.total) <= 0.35); // Among other beats, it lands and is counted by what it plays. const p = plan([{ id: "c1", start: 102, end: 110, gaps: [{ from: 104, to: 106 }] }, { id: "c3", start: 2001, end: 2004 }], { xfade: 0.3 }); close(p.pieces[1].at, 6 - 0.3, 1e-6); close(p.total, 6 + 3 - 0.3, 1e-6); }); test("over a gap the master already cut: the master seconds either side; wholly inside one: refused by name", () => { // c2's 504–505 is out of the master: a beat gap 503.5–506 cuts master 15.167+3.5 → 15.167+5 (1.5 s, not 2.5). const p = plan([{ id: "c2", start: 501, end: 509, gaps: [{ from: 503.5, to: 506 }] }]); const [c] = p.pieces; close(c.cuts[0].from, 18.667); close(c.cuts[0].to, 20.167); // 8 s of source, 1 s already out of the master, 1.5 s more cut: 5.5 s. close(c.seconds, 5.5, 1e-6); assert.throws(() => plan([{ id: "c2", start: 501, end: 509, gaps: [{ from: 504.2, to: 504.8 }] }]), /c2's gap 504.2–504.8 lies inside a gap the master already cut out/); }); test("the graph: the gapped run's cuts as a gap filter with its own labels, the crossfade offset on the kept seconds", () => { const p = plan([{ id: "c1", start: 102, end: 110, gaps: [{ from: 104, to: 106 }] }, { id: "c3", start: 2001, end: 2004 }], { xfade: 0.3 }); const runs = trailerRuns(p); assert.deepEqual(runs[0], { start: 164, count: 240, cuts: [{ start: 224, count: 60 }], kept: 180 }); assert.equal(runs[1].count, 90); assert.equal(runs[1].cuts, undefined); const argv = trailerArgs({ plan: p, master: "/m.mp4", render: { fps: 30 }, outPath: "/o.mp4" }); const graph = argv[argv.indexOf("-filter_complex") + 1]; assert.match(graph, /\[m0v\]trim=start=[\d.]+:end=[\d.]+,setpts=PTS-STARTPTS\[r0v\]/); assert.match(graph, /\[r0v\]split=2\[gr0vs0\]\[gr0vs1\]/); assert.match(graph, /\[gr0vs0\]trim=start=0:end=2,setpts=PTS-STARTPTS\[gr0vp0\]/); assert.match(graph, /\[gr0vs1\]trim=start=4:end=8,setpts=PTS-STARTPTS\[gr0vp1\]/); assert.match(graph, /concat=n=2:v=1:a=0\[g0v\]/); // Every picture on one timebase: xfade refuses two (the gapped run's concat is on the default one). assert.match(graph, /\[g0v\]settb=AVTB\[v0\]/); assert.match(graph, /\[m1v\]trim=[^;]*,setpts=PTS-STARTPTS,settb=AVTB\[v1\]/); assert.match(graph, /afade=t=out:st=1\.975:d=0\.025/); assert.match(graph, /concat=n=2:v=0:a=1\[a0\]/); // The crossfade into the next beat starts 0.3 s before the gapped beat's KEPT end (6 s). assert.match(graph, /\[v0\]\[v1\]xfade=transition=fade:duration=0\.300000:offset=5\.700000\[xv1\]/); }); test("a real encode: a gapped beat cut from a synthesized master, crossfaded into the next, runs its kept seconds", { timeout: 60_000 }, async (t) => { try { execFileSync("ffmpeg", ["-version"], { stdio: "ignore" }); } catch { t.skip("no ffmpeg"); return; } const dir = await mkdtemp(path.join(tmpdir(), "trailer-gaps-")); try { const master = path.join(dir, "m.mp4"); execFileSync("ffmpeg", ["-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=160x90:rate=30:duration=12", "-f", "lavfi", "-i", "sine=frequency=440:duration=12", "-map", "0:v", "-map", "1:a", "-c:v", "libx264", "-preset", "ultrafast", "-g", "15", "-pix_fmt", "yuv420p", "-c:a", "aac", "-ar", "48000", "-ac", "2", master]); const schedule = { version: 1, kind: "deck", fps: 30, transition: 0, total: 12, segments: [{ id: "c", type: "clip", start: 0, duration: 6, end: 6 }, { id: "d", type: "clip", start: 6, duration: 6, end: 12 }] }; const records = new Map([["c", { id: "c", start: 10, end: 16 }], ["d", { id: "d", start: 50, end: 56 }]]); // A gapped beat crossfaded into the next: 5 s less its 1 s gap, then 4 s, less the 0.3 s crossfade. const p = planTrailer({ schedule, records, beats: parseBeats([{ id: "c", start: 10.5, end: 15.5, gaps: [{ from: 12, to: 13 }] }, { id: "d", start: 51, end: 55 }]), xfade: 0.3, snap: false }); assert.equal(p.total, 7.7); const out = path.join(dir, "t.mp4"); execFileSync("ffmpeg", trailerArgs({ plan: p, master, render: { fps: 30, crf: 35, preset: "ultrafast", audioBitrate: "64k", audioRate: 48000, audioChannels: 2 }, outPath: out })); const dur = Number(execFileSync("ffprobe", ["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", out], { encoding: "utf8" }).trim()); close(dur, 7.7, 0.1); } finally { await rm(dir, { recursive: true, force: true }); } });