// trailer.mjs: each clip beat's edges into the pauses (pauses.mjs), on the // beats of the Ember draft-24 trailer as they were BEFORE the operator moved // their ends by hand (fixtures/ember-draft-24-trailer.json: the take's own // schedule, cut records, master silences and caption words around the edges, // no corpus read). The cut-off sentences that started it: b01 "man.", c01 // "door is." and kr1 "woman." faded out, and tr3 ended before "type.". Also: // bounded by the segment's own clip and the max shift, `--no-snap`, and the // source labels counted past a clip's gaps. import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { MIN_WORD, SNAP_MAX_SHIFT } from "./pauses.mjs"; import { makeTrailer, parseBeats, planTrailer, trailerPauses } from "./trailer.mjs"; const EMBER = JSON.parse(readFileSync(new URL("./fixtures/ember-draft-24-trailer.json", import.meta.url), "utf8")); /** The fixture as a take's build: the brand intro, its clips in the cut's order, a closing segment after them. */ function emberBuild() { const segments = [ { id: "brand-intro", type: "intro", start: 0, end: 4.733 }, ...EMBER.order.map((id) => EMBER.clips[id].segment), { id: "closer", type: "clip", start: EMBER.total - 10, end: EMBER.total }, ]; return { schedule: { version: 1, kind: "deck", fps: EMBER.fps, transition: EMBER.transition, total: EMBER.total, segments }, records: new Map(EMBER.order.map((id) => [id, { version: 1, id, ...EMBER.clips[id].record }])), manifest: { timeline: EMBER.order.map((id) => ({ type: "clip", id, video: id })), provenance: {} }, master: "master.mp4", render: { fps: 30 }, }; } /** trailerPauses over the fixture: its silences for a segment, its words for a clip (as clip-words.mjs gives them). */ function emberPauses(build, beats) { return trailerPauses({ build, beats, silences: async (_file, _render, scan) => { const id = EMBER.order.find((k) => Math.abs(EMBER.clips[k].segment.start - scan.from) < 1e-6); return id ? EMBER.clips[id].silences : []; }, words: async (entry, _prov, from, to) => { const c = EMBER.clips[entry.id]; if (!c?.words) return null; return { words: c.words.filter((w) => w.start >= from && w.start <= to), timed: !!c.timed, from: c.wordsFrom }; }, }); } const BEATS = parseBeats(EMBER.beats); const build = emberBuild(); const pauses = await emberPauses(build, BEATS); const plan = (o = {}) => planTrailer({ schedule: build.schedule, records: build.records, beats: BEATS, intro: true, xfade: 0.3, pauses, ...o }); const SNAPPED = plan(); const PLAIN = plan({ snap: false }); /** A beat's piece (the n-th of the beats, 1-based; the intro is piece 0). */ const piece = (p, n) => p.pieces[n]; /** Its clip's words in MASTER seconds, as the plan saw them. */ const wordsOf = (id) => pauses.get(id).words; const silencesOf = (id) => EMBER.clips[id].silences; const inSilence = (id, t) => silencesOf(id).some((s) => t >= s.s - 1e-3 && t <= s.e + 1e-3); test("the fixture: the beats before the hand fix, b01 c01 tr3 u01 kr1 bk3 tr3", () => { assert.deepEqual(BEATS.map((b) => b.id), ["b01", "c01", "tr3", "u01", "kr1", "bk3", "tr3"]); assert.equal(BEATS[2].end, 3011.2); }); test("tr3's first beat ends after \"type.\" -- carried through the sentence, into the pause after it, before \"You\"", () => { const p = piece(SNAPPED, 3); assert.equal(p.id, "tr3"); const ws = wordsOf("tr3"); const type = ws.find((w) => w.text === "type."); const you = ws.find((w) => w.text === "You"); assert.ok(p.master.end >= type.start + MIN_WORD, `ends ${p.master.end}, "type." starts ${type.start}`); assert.ok(p.master.end <= you.start + 1 / 30, `ends ${p.master.end}, "You" starts ${you.start}`); assert.ok(inSilence("tr3", p.master.end)); assert.ok(p.source.end > 3011.4 + MIN_WORD); // The unsnapped plan ended before "type." started. assert.ok(piece(PLAIN, 3).master.end < type.start); assert.ok(SNAPPED.notes.includes('beat 3 tr3: end 3011.20 → 3012.09 (pause after "type.", the sentence\'s end)'), SNAPPED.notes.join("\n")); assert.equal(p.snapped.end.after, "type."); assert.ok(Math.abs(p.snapped.end.from - 765.778) < 1e-3 && Math.abs(p.snapped.end.to - 766.667) < 1e-3, JSON.stringify(p.snapped.end)); }); test("b01, c01 and kr1 end in a pause, after their last word has sounded; each move said", () => { for (const [n, id, last] of [[1, "b01", "man."], [2, "c01", "is."], [5, "kr1", "woman."]]) { const p = piece(SNAPPED, n); assert.equal(p.id, id); assert.ok(inSilence(id, p.master.end), `${id} ends at ${p.master.end}, in no silence`); const w = wordsOf(id).filter((x) => x.text === last).at(-1); assert.ok(p.master.end > w.start + MIN_WORD, `${id}: "${last}" at ${w.start}`); assert.ok(p.master.end > piece(PLAIN, n).master.end, `${id} moved forward`); assert.ok(p.master.end - piece(PLAIN, n).master.end <= SNAP_MAX_SHIFT + 1 / 30); } const said = SNAPPED.notes.join("\n"); assert.match(said, /beat 1 b01: end 1818\.65 → 1818\.95 \(pause after "So,"\)/); assert.match(said, /beat 2 c01: end 220\.95 → 221\.09 \(pause after "is\."\)/); assert.match(said, /beat 5 kr1: end 4818\.10 → 4818\.21 \(pause after "woman\."\)/); // c01's fade (its last 0.3 s) holds no word's start: "door is." is heard whole. const c01 = piece(SNAPPED, 2); assert.ok(!wordsOf("c01").some((w) => w.start > c01.master.end - 0.3 + 1e-6 && w.start < c01.master.end - 1e-6)); }); test("starts go back into the pause before them, never across a gap's join or into the master's crossfade", () => { // kr1 starts on "This", now in the pause before it. const kr1 = piece(SNAPPED, 5); assert.ok(kr1.master.start < piece(PLAIN, 5).master.start); assert.match(SNAPPED.notes.join("\n"), /beat 5 kr1: start 4814\.95 → 4814\.81 \(pause before "This"\)/); // u01 starts at its gap's join: the silence across the jump cut is no reason to play what the gap took out. const u01 = piece(SNAPPED, 4); assert.equal(u01.master.start, piece(PLAIN, 4).master.start); assert.ok(u01.source.start >= 9476.7 - 0.02, `u01 starts at source ${u01.source.start}`); // Every clip piece stays out of the master's own crossfades as far as the trailer's hides them. for (const p of SNAPPED.pieces.filter((x) => x.kind === "clip")) { const seg = build.schedule.segments.find((s) => s.id === p.id); assert.ok(p.master.start >= seg.start + EMBER.transition - 0.3 - 1e-3, `${p.id} start`); assert.ok(p.master.end <= seg.end - EMBER.transition + 0.3 + 1e-3, `${p.id} end`); } }); test("bounded by the max shift and the segment's own clip, on a made-up clip", () => { const schedule = { fps: 30, transition: 0.5, total: 40, segments: [{ id: "a", type: "clip", start: 0, end: 20 }, { id: "c", type: "clip", start: 19.5, end: 30 }, { id: "z", type: "clip", start: 29.5, end: 40 }], }; const records = new Map([["c", { start: 100, end: 110 }]]); const one = (beat, ps) => planTrailer({ schedule, records, beats: parseBeats([beat]), xfade: 0.3, pauses: new Map([["c", { pauses: ps, words: null }]]) }); // A pause whose quiet (a hard end's SNAP_HARD_ROOM into it) is 1.1 s after the end: too far, said. const far = one({ id: "c", start: 102, end: 105 }, [{ from: 25.5, to: 26 }]); assert.equal(far.pieces[0].master.end, 24.5); assert.match(far.notes.join("\n"), /beat 1 c: end 105\.00 kept — the pause is more than 1\.00 s away/); // A pause running past the segment's own end (the cut record's 110 is master 29.5): it stops there. const edge = one({ id: "c", start: 102, end: 109.7 }, [{ from: 29.3, to: 30 }]); assert.ok(edge.pieces[0].master.end <= 29.5 + 1e-6); // The last piece has no trailer crossfade out: a hard end wants SNAP_HARD_ROOM of quiet. const hard = one({ id: "c", start: 102, end: 105 }, [{ from: 24.6, to: 25.4 }]); assert.ok(Math.abs(hard.pieces[0].master.end - 24.7) < 1 / 30 + 1e-6); }); test("--no-snap: every edge as written, no pause measured", async () => { for (const p of PLAIN.pieces) assert.equal(p.snapped, undefined); assert.ok(!PLAIN.notes.some((n) => /→|kept —/.test(n))); assert.deepEqual(plan({ pauses: null }).pieces.map((p) => p.master), PLAIN.pieces.map((p) => p.master)); // makeTrailer --no-snap never measures. const dir = await mkdtemp(path.join(tmpdir(), "trailer-snap-")); try { const takeDir = path.join(dir, "takes", "t1"); await mkdir(path.join(takeDir, "out", "sourced", "segments"), { recursive: true }); await writeFile(path.join(takeDir, "video.manifest.json"), JSON.stringify({ slug: "s", render: { fps: 30 }, timeline: build.manifest.timeline })); await writeFile(path.join(takeDir, "out", "sourced", "schedule.json"), JSON.stringify(build.schedule)); for (const [id, rec] of build.records) await writeFile(path.join(takeDir, "out", "sourced", "segments", `${id}.cut.json`), JSON.stringify(rec)); let measured = 0; const measure = async (b, beats) => { measured += 1; return emberPauses(b, beats); }; const common = { takeDir, beats: BEATS, intro: true, dryRun: true, probe: async () => EMBER.total, measure }; const off = await makeTrailer({ ...common, snap: false }); assert.equal(measured, 0); assert.equal(off.record.snap, false); const on = await makeTrailer(common); assert.equal(measured, 1); assert.equal(on.plan.pieces[3].master.end, SNAPPED.pieces[3].master.end); } finally { await rm(dir, { recursive: true, force: true }); } }); test("the source labels count a clip's gaps: u01 and bk3 say the seconds they play, not a gap's length early", () => { // u01's gaps take 21.77 s out before its beat; bk3's 3.27 s. const u01 = piece(PLAIN, 4); const bk3 = piece(PLAIN, 6); assert.ok(Math.abs(u01.source.start - 9476.7) < 1 / 30 + 0.02, `u01 ${u01.source.start}`); assert.ok(Math.abs(u01.source.end - 9484.95) < 1 / 30 + 0.02, `u01 ${u01.source.end}`); assert.ok(Math.abs(bk3.source.start - 119) < 1 / 30 + 0.02, `bk3 ${bk3.source.start}`); assert.ok(Math.abs(bk3.source.end - 123.3) < 1 / 30 + 0.02, `bk3 ${bk3.source.end}`); // The master span is the same as ever (it was always right). const rec = build.records.get("u01"); const seg = build.schedule.segments.find((s) => s.id === "u01"); const gapsBefore = rec.gaps.reduce((n, g) => n + (g.to - g.from), 0); assert.ok(Math.abs(u01.master.start - (seg.start + 9476.7 - rec.start - gapsBefore)) < 1 / 30 + 1e-6); });