@aicopycoders/exodus 2026.10.2400-beta.3 → 2026.11.200-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,6 +32,12 @@
32
32
  // After the render it writes the whole ad's word times beside the MP4, as
33
33
  // <out>.words.json and <out>.srt, for captioning in post, and where each scene
34
34
  // plays in the cut as <out>.scenes.json (#3300).
35
+ //
36
+ // A kind can turn on finishing settings, which reach this file as the
37
+ // manifest's `finishing` (see finishingOf). Without it the cut is the one
38
+ // described above, byte for byte. The same holds for its sound settings, which
39
+ // reach this file as the manifest's `sound` (see soundOf), and for a voice
40
+ // that is one song laid whole, the manifest's `wholeVoice` (see wholeVoiceOf).
35
41
  import { execFileSync, spawnSync } from "node:child_process";
36
42
  import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
37
43
  import path from "node:path";
@@ -125,6 +131,177 @@ export const BROLL_LEAD_SEC = 0.5;
125
131
  export const BROLL_MIN_HOLD_SEC = 0.8;
126
132
  export const BROLL_MAX_HOLD_SEC = 4;
127
133
 
134
+ /**
135
+ * The finishing settings (a kind's cutPace, captions, cards, finishLoudness).
136
+ * cutPace: at most PACE_GAP_MAX_SEC of air where a new speaker answers; a pause
137
+ * over PACE_SPLIT_PAUSE_SEC where one line ends and the next begins is cut to
138
+ * PACE_GAP_MAX_SEC and the take is split there, every other piece punched in;
139
+ * the cut sped up, at most SPEED_MAX, when it runs past the kind's
140
+ * targetSeconds; and a pace report. A speed under SPEED_FLOOR is not worth
141
+ * the change and is left at 1.
142
+ */
143
+ export const PACE_GAP_MAX_SEC = 0.35;
144
+ export const PACE_SPLIT_PAUSE_SEC = 0.45;
145
+ export const SPEED_MAX = 1.15;
146
+ const SPEED_FLOOR = 1.02;
147
+ /** A shot longer than this counts as long in the pace report. */
148
+ export const LONG_SHOT_SEC = 4;
149
+ /** A pause in the voice at least this long is one the pace report reads. */
150
+ const PACE_PAUSE_SEC = 0.2;
151
+ /** A card lands this long before the first word of the line it is cued to. */
152
+ export const CARD_LEAD_SEC = 0.07;
153
+ /** finishLoudness: the whole mix's target, and the true peak a cut may not pass after it. */
154
+ export const FINISH_LUFS = -14;
155
+ export const FINISH_TRUE_PEAK = -1.5;
156
+ export const FINISH_LRA = 11;
157
+ export const TRUE_PEAK_MAX_DB = -1;
158
+
159
+ /**
160
+ * The manifest's finishing settings, or null when none is on. The worker
161
+ * writes them from the run's frozen kind settings, so a redo of the run cuts
162
+ * with the same ones.
163
+ */
164
+ export function finishingOf(manifest) {
165
+ const f = manifest?.finishing;
166
+ if (!f || typeof f !== "object") return null;
167
+ const on = {
168
+ cutPace: f.cutPace === true,
169
+ captions: f.captions === true,
170
+ cards: f.cards === true,
171
+ finishLoudness: f.finishLoudness === true,
172
+ };
173
+ if (!Object.values(on).some(Boolean)) return null;
174
+ const target = f.targetSeconds;
175
+ return on.cutPace && Number.isFinite(target) && target > 0 ? { ...on, targetSeconds: target } : on;
176
+ }
177
+
178
+ /**
179
+ * The sound settings (a kind's soundEffects, roomTone, music "suno" and
180
+ * musicOnlyMaster), as the manifest's `sound`: the files the worker made for
181
+ * this run. A sound lands SOUND_LEAD_SEC before the first word of the line it
182
+ * is cued to, SOUND_UNDER_DB under the voice there. A room-tone bed runs from
183
+ * its set's first scene (the first bed from the start) to the next bed, looped,
184
+ * BED_UNDER_DB under the voice, in place of the brown-noise room tone; a bed
185
+ * quieter than BED_FLOOR_LUFS, or with no loudness to measure, is left out and
186
+ * noted. With `duck` the music sits MUSIC_UNDER_DB under the voice and a
187
+ * sidechain compressor keyed on the voice ducks it (DUCK). With
188
+ * musicOnlyMaster a second file beside the cut carries the cut's picture and
189
+ * the music alone, at FINISH_LUFS. The voice's level is SEGMENT_LUFS where
190
+ * every segment is levelled to it, else each segment's file is measured.
191
+ */
192
+ export const SOUND_UNDER_DB = 10;
193
+ export const SOUND_LEAD_SEC = 0.03;
194
+ const SOUND_FALLBACK_DB = -22;
195
+ export const BED_UNDER_DB = 24;
196
+ export const BED_FLOOR_LUFS = -70;
197
+ const BED_FADE_SEC = 0.15;
198
+ export const MUSIC_UNDER_DB = 16.8;
199
+ const MUSIC_FALLBACK_DB = -20;
200
+ export const DUCK = { threshold: 0.05, ratio: 2.5, attack: 20, release: 350 };
201
+ const SEGMENT_LUFS = -16;
202
+ /** A voice at or under this is no level to set a sound against. */
203
+ const VOICE_FLOOR_LUFS = -60;
204
+
205
+ /** The manifest's sound settings, or null when it names none. */
206
+ export function soundOf(manifest) {
207
+ const s = manifest?.sound;
208
+ if (!s || typeof s !== "object") return null;
209
+ const listed = (xs, ok) => (Array.isArray(xs) ? xs.filter((x) => typeof x?.file === "string" && ok(x)) : []);
210
+ const sound = {
211
+ effects: listed(s.effects, (x) => typeof x.cueLineId === "string"),
212
+ beds: listed(s.beds, (x) => Number.isFinite(x.fromScene)),
213
+ duck: s.duck === true,
214
+ musicOnlyMaster: s.musicOnlyMaster === true,
215
+ };
216
+ return sound.effects.length || sound.beds.length || sound.duck || sound.musicOnlyMaster ? sound : null;
217
+ }
218
+
219
+ /** The voice's level over segments [{ seconds, lufs }], added by power and weighted by length. */
220
+ export function voiceLufsOf(levels) {
221
+ const heard = levels.filter((l) => Number.isFinite(l.lufs) && l.seconds > 0);
222
+ const total = heard.reduce((t, l) => t + l.seconds, 0);
223
+ if (total <= 0) return null;
224
+ return round2(10 * Math.log10(heard.reduce((t, l) => t + l.seconds * 10 ** (l.lufs / 10), 0) / total));
225
+ }
226
+
227
+ const heardVoice = (lufs) => Number.isFinite(lufs) && lufs > VOICE_FLOOR_LUFS;
228
+
229
+ /** The gain (dB) that puts a sound `underDb` under the voice; `fallback` when either level is unknown. */
230
+ export function gainUnder(voiceLufs, underDb, soundLufs, fallback) {
231
+ return heardVoice(voiceLufs) && Number.isFinite(soundLufs) ? round2(voiceLufs - underDb - soundLufs) : fallback;
232
+ }
233
+
234
+ /**
235
+ * Where each sound and bed plays and how loud, from the cut's timeline.
236
+ * `measure(file)` is a file's loudness in LUFS, or null.
237
+ */
238
+ export function soundPlan(sound, timeline, storyboard, { levelled, measure }) {
239
+ const notes = [];
240
+ const spine = timeline.spine;
241
+ const pictureSec = spine.reduce((t, seg) => t + seg.seconds, 0);
242
+ const segLufs = spine.map((seg) => {
243
+ if (seg.audio !== "narration" && seg.audio !== "clip") return null;
244
+ return levelled ? SEGMENT_LUFS : measure(seg.audio === "narration" ? seg.voice : seg.file);
245
+ });
246
+ const voiceLufs = voiceLufsOf(spine.map((seg, k) => ({ seconds: seg.seconds, lufs: segLufs[k] })));
247
+ const counts = lineWordCounts(storyboard);
248
+ const effects = [];
249
+ for (const e of sound.effects) {
250
+ const cue = cueOffsetOnSpine(spine, counts, e.cueLineId);
251
+ if (!cue) {
252
+ notes.push(`the sound cued to ${e.cueLineId} is left out: the cut does not speak that line`);
253
+ continue;
254
+ }
255
+ const k = spine.findIndex((seg) => cue.startSec < seg.startSec + seg.seconds);
256
+ const under = Number.isFinite(e.underDb) ? e.underDb : SOUND_UNDER_DB;
257
+ effects.push({
258
+ file: e.file, cueLineId: e.cueLineId, atSec: roundMs(Math.max(0, cue.startSec - SOUND_LEAD_SEC)),
259
+ gainDb: gainUnder(segLufs[k] ?? voiceLufs, under, measure(e.file), SOUND_FALLBACK_DB),
260
+ });
261
+ }
262
+ const bedRef = heardVoice(voiceLufs) ? voiceLufs : FINISH_LUFS;
263
+ const heard = [];
264
+ for (const b of sound.beds) {
265
+ const seg = spine.find((x) => x.sceneIndex === b.fromScene);
266
+ const lufs = measure(b.file);
267
+ if (!seg) notes.push(`the bed for scene ${b.fromScene} is left out: that scene is not in the cut`);
268
+ else if (!Number.isFinite(lufs) || lufs < BED_FLOOR_LUFS) {
269
+ notes.push(`the bed ${b.file} is left out: ${Number.isFinite(lufs) ? `${lufs} LUFS is under ${BED_FLOOR_LUFS}` : "it has no loudness to measure"}`);
270
+ } else {
271
+ const under = Number.isFinite(b.underDb) ? b.underDb : BED_UNDER_DB;
272
+ heard.push({ file: b.file, fromScene: b.fromScene, from: seg.startSec, gainDb: round2(bedRef - under - lufs) });
273
+ }
274
+ }
275
+ heard.sort((a, b) => a.from - b.from);
276
+ const beds = heard.map((b, k) => {
277
+ const startSec = k === 0 ? 0 : roundMs(b.from);
278
+ const end = k + 1 < heard.length ? heard[k + 1].from : pictureSec;
279
+ return { file: b.file, fromScene: b.fromScene, startSec, seconds: roundMs(Math.max(0.1, end - startSec)), gainDb: b.gainDb };
280
+ });
281
+ return { voiceLufs, effects, beds, notes };
282
+ }
283
+
284
+ /** The music's gain under the voice when it is ducked. */
285
+ export function duckedMusicGain(voiceLufs, musicLufs) {
286
+ return gainUnder(voiceLufs, MUSIC_UNDER_DB, musicLufs, MUSIC_FALLBACK_DB);
287
+ }
288
+
289
+ /** musicOnlyMaster: the cut's picture as it is, with the music alone in place of the mix. */
290
+ export function musicOnlyMasterArgs(cut, music, seconds, gainDb, out) {
291
+ return [
292
+ "-y", "-hide_banner", "-loglevel", "error", "-i", cut, "-stream_loop", "-1", "-i", music,
293
+ "-filter_complex", `[1:a]${stereo},atrim=0:${sec(seconds)},asetpts=PTS-STARTPTS,volume=${gainDb}dB[music]`,
294
+ "-map", "0:v:0", "-map", "[music]", "-c:v", "copy", "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart", out,
295
+ ];
296
+ }
297
+
298
+ /** How much a cut of `seconds` is sped up to come near the kind's target length: 1 when it is not. */
299
+ export function speedFor(seconds, targetSeconds) {
300
+ if (!(targetSeconds > 0) || !(seconds > 0)) return 1;
301
+ const want = Math.min(SPEED_MAX, seconds / targetSeconds);
302
+ return want > SPEED_FLOOR ? roundMs(want) : 1;
303
+ }
304
+
128
305
  /** The word rule the planner sized every line with (scout models.ts wordCount). */
129
306
  export function wordCount(text) {
130
307
  const trimmed = (text ?? "").trim();
@@ -491,8 +668,12 @@ export function asksPause(line) {
491
668
  * break's gap, the silence it keeps, and whether a sound went.
492
669
  *
493
670
  * lines: [{ text, asksPause, cuesBeat }] in the order the clip speaks them.
671
+ *
672
+ * lineGap ({ max, keep }, cutPace): where one line ends and the next begins,
673
+ * a silence over `max` is cut to `keep` instead, and that break's last span
674
+ * carries `atLine`, the index of the line after it, where the take is split.
494
675
  */
495
- export function sentenceGapCuts(words, lines, levels = null) {
676
+ export function sentenceGapCuts(words, lines, levels = null, lineGap = null) {
496
677
  const written = [];
497
678
  const pieces = [];
498
679
  let between = "";
@@ -532,13 +713,16 @@ export function sentenceGapCuts(words, lines, levels = null) {
532
713
  const sound = heard && { s: Math.max(floorFrame(heard.s), ceilFrame(earlier.e)), e: ceilFrame(heard.e) };
533
714
  const removed = sound ? sound.e - sound.s : 0;
534
715
  const spans = sound ? [sound] : [];
535
- if (gap - removed > SENTENCE_GAP_MAX_SEC + 1e-6) {
716
+ const [gapMax, gapKeep] =
717
+ lineGap && newLine ? [lineGap.max, lineGap.keep] : [SENTENCE_GAP_MAX_SEC, SENTENCE_GAP_KEEP_SEC];
718
+ const paused = gap - removed > gapMax + 1e-6;
719
+ if (paused) {
536
720
  // Placed on the gap with the sound already out, then moved back past it.
537
721
  // Frame edges only ever widen what is kept, so the silence left stays
538
722
  // between the kept length and the longest allowed.
539
723
  const back = (t) => (sound && t > sound.s ? t + removed : t);
540
- const s = back(ceilFrame(earlier.e + SENTENCE_GAP_KEEP_SEC / 2));
541
- const e = back(floorFrame(later.s - removed - SENTENCE_GAP_KEEP_SEC / 2));
724
+ const s = back(ceilFrame(earlier.e + gapKeep / 2));
725
+ const e = back(floorFrame(later.s - removed - gapKeep / 2));
542
726
  if (e > s) spans.push({ s, e });
543
727
  }
544
728
  const merged = [];
@@ -548,7 +732,11 @@ export function sentenceGapCuts(words, lines, levels = null) {
548
732
  else merged.push({ ...span });
549
733
  }
550
734
  const kept = gap - merged.reduce((sum, c) => sum + c.e - c.s, 0);
551
- for (const { s, e } of merged) cuts.push({ s, e, after: earlier.w, gap, kept, sound: Boolean(sound) });
735
+ const split = lineGap && newLine && paused ? b.li : null;
736
+ merged.forEach(({ s, e }, m) => {
737
+ const cut = { s, e, after: earlier.w, gap, kept, sound: Boolean(sound) };
738
+ cuts.push(split !== null && m === merged.length - 1 ? { ...cut, atLine: split } : cut);
739
+ });
552
740
  }
553
741
  return cuts;
554
742
  }
@@ -627,7 +815,7 @@ function clipLevels(file, filter = null) {
627
815
  * trims, reaction splits, cue times, captions) works in cut time. The renderer
628
816
  * removes seg.cuts from the picture and the sound before anything else.
629
817
  */
630
- function tightenSentenceGaps(spine, reactionCues, scriptLines, readLevels) {
818
+ function tightenSentenceGaps(spine, reactionCues, scriptLines, readLevels, lineGap = null) {
631
819
  for (const seg of spine) {
632
820
  if (seg.source !== "clip" || seg.audio !== "clip" || !seg.words?.length) continue;
633
821
  const lines = seg.lineIds.map((id) => ({
@@ -635,11 +823,14 @@ function tightenSentenceGaps(spine, reactionCues, scriptLines, readLevels) {
635
823
  asksPause: asksPause(scriptLines.get(id)),
636
824
  cuesBeat: reactionCues.has(id),
637
825
  }));
638
- const cuts = sentenceGapCuts(seg.words, lines, readLevels(seg.file));
826
+ const cuts = sentenceGapCuts(seg.words, lines, readLevels(seg.file), lineGap);
639
827
  if (cuts.length === 0) continue;
640
828
  const removedBefore = (t) => cuts.reduce((sum, c) => sum + (c.e <= t ? c.e - c.s : 0), 0);
641
829
  const removed = removedBefore(Infinity);
642
830
  seg.cuts = cuts.map(({ s, e }) => ({ s, e }));
831
+ // cutPace: where the take is split, in its time once the gaps are out.
832
+ const splits = cuts.filter((c) => c.atLine !== undefined).map((c) => ({ at: c.s - removedBefore(c.s), line: c.atLine }));
833
+ if (splits.length > 0) seg.splits = splits;
643
834
  seg.words = seg.words.map((w) => ({ ...w, s: w.s - removedBefore(w.s), e: w.e - removedBefore(w.s) }));
644
835
  seg.seconds -= removed;
645
836
  seg.clipSeconds -= removed;
@@ -699,6 +890,7 @@ export function scenePlans(storyboard) {
699
890
  durationSec: typeof scene.durationSec === "number" ? scene.durationSec : null,
700
891
  actionTailSec: typeof scene.actionTailSec === "number" && scene.actionTailSec > 0 ? scene.actionTailSec : 0,
701
892
  actionLeadSec: typeof scene.actionLeadSec === "number" && scene.actionLeadSec > 0 ? scene.actionLeadSec : 0,
893
+ sourceShot: Number.isInteger(scene.sourceShot) && scene.sourceShot > 0 ? scene.sourceShot : null,
702
894
  broll:
703
895
  hasText(scene.broll?.covers) && Number.isFinite(scene.broll?.plannedSec) && scene.broll.plannedSec > 0
704
896
  ? { covers: scene.broll.covers, plannedSec: scene.broll.plannedSec }
@@ -877,12 +1069,16 @@ export function brollWindow(seg, counts, cueLineId, texts, broll) {
877
1069
  * one marked with actionLeadSec keeps that long before its first word, so the
878
1070
  * action it opens on (the dog stepping onto the mat, #3272) is seen.
879
1071
  */
880
- function trimJoins(spine, reactionCues, scriptLines) {
1072
+ function trimJoins(spine, reactionCues, scriptLines, gapMax = null) {
1073
+ // cutPace: the beat before someone new answers shrinks so the join holds at most gapMax of air.
1074
+ const newSpeakerLead = gapMax === null
1075
+ ? JOIN_LEAD_NEW_SPEAKER_SEC
1076
+ : Math.min(JOIN_LEAD_NEW_SPEAKER_SEC, roundMs(gapMax - JOIN_TAIL_SEC));
881
1077
  spine.forEach((seg, k) => {
882
1078
  if (seg.source !== "clip" || seg.audio !== "clip" || !seg.words?.length) return;
883
1079
  const prev = spine[k - 1];
884
1080
  const sameSpeaker = Boolean(prev?.lastSpeaker) && prev.lastSpeaker === seg.firstSpeaker;
885
- const lead = sameSpeaker || !prev ? JOIN_LEAD_SAME_SPEAKER_SEC : JOIN_LEAD_NEW_SPEAKER_SEC;
1081
+ const lead = sameSpeaker || !prev ? JOIN_LEAD_SAME_SPEAKER_SEC : newSpeakerLead;
886
1082
  const span = scriptedSpan(seg.words, seg.lineIds.map((id) => scriptLines.get(id)?.text));
887
1083
  const line = span ? seg.words.slice(span.first, span.last + 1) : seg.words;
888
1084
  const before = span ? seg.words.slice(0, span.first) : [];
@@ -1022,6 +1218,47 @@ function markPunchIns(spine, sameShot) {
1022
1218
 
1023
1219
  const sameSpeaker = (prev, seg) => Boolean(prev.lastSpeaker && seg.firstSpeaker === prev.lastSpeaker);
1024
1220
 
1221
+ /** cutPace: two pieces of one take, back to back. */
1222
+ const samePiece = (prev, seg) =>
1223
+ Boolean(prev.piece && seg.piece && prev.file === seg.file && prev.piece.take === seg.piece.take);
1224
+
1225
+ /**
1226
+ * cutPace: split each take at the pauses tightenSentenceGaps marked, where one
1227
+ * line ends and the next begins, so the pieces join as a jump cut. Each piece
1228
+ * plays its own lines, on whole frames, from the same file.
1229
+ */
1230
+ function splitAtPauses(spine) {
1231
+ for (let k = spine.length - 1; k >= 0; k--) {
1232
+ const seg = spine[k];
1233
+ const marks = seg.splits ?? [];
1234
+ delete seg.splits;
1235
+ const at = marks
1236
+ .map((m) => ({ t: Math.round((m.at - seg.inSec) * FPS) / FPS, line: m.line }))
1237
+ .filter((m) => m.t > 0 && m.t < seg.seconds);
1238
+ if (at.length === 0) continue;
1239
+ const edges = [0, ...at.map((m) => m.t), seg.seconds];
1240
+ const lineEdges = [0, ...at.map((m) => m.line), seg.lineIds.length];
1241
+ const of = edges.length - 1;
1242
+ const pieces = edges.slice(0, -1).map((from, j) => {
1243
+ const to = edges[j + 1];
1244
+ const piece = {
1245
+ ...seg,
1246
+ inSec: seg.inSec + from,
1247
+ seconds: to - from,
1248
+ lineIds: seg.lineIds.slice(lineEdges[j], lineEdges[j + 1]),
1249
+ words: seg.words
1250
+ ? seg.words.filter((w) => w.s >= from && w.s < to).map((w) => ({ ...w, s: w.s - from, e: w.e - from }))
1251
+ : null,
1252
+ piece: { take: seg.sceneIndex, n: j + 1, of },
1253
+ note: `${seg.note}, piece ${j + 1} of ${of}`,
1254
+ };
1255
+ piece.holdSec = holdFor(piece);
1256
+ return piece;
1257
+ });
1258
+ spine.splice(k, 1, ...pieces);
1259
+ }
1260
+ }
1261
+
1025
1262
  /**
1026
1263
  * The pacings whose edit punches in, each with when a scene plays on in the
1027
1264
  * shot of the one before it. Jump cuts film every scene in one framing. A
@@ -1051,8 +1288,129 @@ export function scenesWithNoClip({ manifest, storyboard, skip, exists = Boolean
1051
1288
  .sort((a, b) => a - b);
1052
1289
  }
1053
1290
 
1291
+ /**
1292
+ * A kind's songAsVoice: the voice is one track, a song, laid whole under the
1293
+ * picture and never cut, and each narrated scene lasts its own stretch of it
1294
+ * (the pulled narration.json's cuts, which run on from one another to the
1295
+ * song's end). Null, and the cut is today's, without the manifest's
1296
+ * `wholeVoice` or without the pulled narration.
1297
+ */
1298
+ export function wholeVoiceOf(manifest, readJson, exists) {
1299
+ if (manifest?.wholeVoice !== true || !exists(manifest.narration?.file)) return null;
1300
+ const cuts = readJson(manifest.narration.timing)?.cuts;
1301
+ if (!Array.isArray(cuts) || cuts.length === 0) return null;
1302
+ return {
1303
+ file: manifest.narration.file,
1304
+ seconds: Math.max(...cuts.map((c) => c.startSec + c.durationSec)),
1305
+ starts: new Map(cuts.map((c) => [c.sceneIndex, c.startSec])),
1306
+ };
1307
+ }
1308
+
1309
+ const roundFrame = (t) => Math.round(t * FPS) / FPS;
1310
+
1311
+ /**
1312
+ * Lays the spine on the whole voice. Each scene with a stretch of the voice
1313
+ * runs from its stretch's start to the next one's, the last to the voice's
1314
+ * end, on whole frames, its clip trimmed or its last frame held to fit. A
1315
+ * scene with no stretch (no line) between two that have one keeps its clip
1316
+ * but takes its time from the scene before it, never past that scene's last
1317
+ * word, so every later line still lands on its words; one with no room left
1318
+ * is dropped. Scenes before the first stretch play first and the voice starts
1319
+ * after them. Returns where the voice starts and what was dropped.
1320
+ */
1321
+ export function fitWholeVoice(spine, whole) {
1322
+ const on = spine.flatMap((seg, k) => (whole.starts.has(seg.sceneIndex) ? [k] : []));
1323
+ if (on.length === 0) return null;
1324
+ const startSec = spine.slice(0, on[0]).reduce((t, seg) => t + seg.seconds, 0);
1325
+ const gone = new Set();
1326
+ on.forEach((k, j) => {
1327
+ const seg = spine[k];
1328
+ const next = on[j + 1];
1329
+ const from = j === 0 ? 0 : roundFrame(whole.starts.get(seg.sceneIndex));
1330
+ const to = next === undefined ? ceilFrame(whole.seconds) : roundFrame(whole.starts.get(spine[next].sceneIndex));
1331
+ const spoken = Math.max(1 / FPS, ceilFrame(Math.max(0, ...(seg.words ?? []).map((w) => w.e))));
1332
+ let room = Math.max(0, to - from - spoken);
1333
+ let given = 0;
1334
+ for (const b of next === undefined ? [] : spine.slice(k + 1, next)) {
1335
+ const seconds = Math.min(b.seconds, floorFrame(room));
1336
+ if (seconds < 1 / FPS) {
1337
+ gone.add(b);
1338
+ continue;
1339
+ }
1340
+ if (seconds < b.seconds) b.note = withHow(b.note, [`cut to ${seconds.toFixed(2)}s to keep the voice on its lines`]);
1341
+ b.seconds = seconds;
1342
+ b.holdSec = holdFor(b);
1343
+ room -= seconds;
1344
+ given += seconds;
1345
+ }
1346
+ seg.seconds = to - from - given;
1347
+ seg.holdSec = holdFor(seg);
1348
+ seg.wholeVoice = true;
1349
+ seg.note = withHow(seg.note, [`on the whole voice from ${from.toFixed(2)}s to ${to.toFixed(2)}s`]);
1350
+ });
1351
+ const dropped = spine
1352
+ .filter((seg) => gone.has(seg))
1353
+ .map((seg) => ({ n: seg.sceneIndex, reason: `scene ${seg.sceneIndex}: no room left between two lines of the voice` }));
1354
+ for (let k = spine.length - 1; k >= 0; k--) if (gone.has(spine[k])) spine.splice(k, 1);
1355
+ return { startSec, dropped };
1356
+ }
1357
+
1358
+ /**
1359
+ * The manifest's `sourceAd` (a kind's shotCopy with a source record), or null:
1360
+ * the record's shot times, its caption style, hook box and card boxes. The
1361
+ * worker writes it; a member's own pull has none, so their cut is today's.
1362
+ */
1363
+ export function sourceAdOf(manifest) {
1364
+ const a = manifest?.sourceAd;
1365
+ if (!a || typeof a !== "object" || !(a.seconds > 0) || !Array.isArray(a.shots) || a.shots.length === 0) return null;
1366
+ return {
1367
+ ...a,
1368
+ cards: Array.isArray(a.cards) ? a.cards : [],
1369
+ pictures: Array.isArray(a.pictures) ? a.pictures : [],
1370
+ };
1371
+ }
1372
+
1373
+ /**
1374
+ * The copied shots laid on the cut. Each scene names the shot it copies (the
1375
+ * storyboard's sourceShot). A spine scene and the shots after it that no spine
1376
+ * scene copies (its cutaways) make one group, the source's voice running over
1377
+ * them as ours does; shots before the first spine scene join its group. Each
1378
+ * group's stretch of the source maps evenly onto its stretch of the cut, so a
1379
+ * cutaway lands at its shot's own place in the group. Returns
1380
+ * [{ shots: [n...], src: [t0, t1], cut: [c0, c1] }], in order.
1381
+ */
1382
+ export function sourceGroups(spine, sourceAd, shotOf) {
1383
+ const entries = [...sourceAd.shots].sort((a, b) => a.n - b.n);
1384
+ const onSpine = new Map();
1385
+ for (const seg of spine) {
1386
+ const n = shotOf(seg.sceneIndex);
1387
+ if (n === null || !entries.some((e) => e.n === n)) continue;
1388
+ const at = onSpine.get(n) ?? { c0: Infinity, c1: -Infinity };
1389
+ onSpine.set(n, { c0: Math.min(at.c0, seg.startSec), c1: Math.max(at.c1, seg.startSec + seg.seconds) });
1390
+ }
1391
+ const heads = entries.filter((e) => onSpine.has(e.n));
1392
+ return heads.map((head, i) => {
1393
+ const next = heads[i + 1];
1394
+ const shots = entries.filter((e) => (i === 0 || e.n >= head.n) && (!next || e.n < next.n));
1395
+ const src0 = i === 0 ? Math.min(head.t0, shots[0].t0) : head.t0;
1396
+ const src1 = next ? next.t0 : Math.max(...shots.map((e) => e.t1));
1397
+ const cut1 = next ? onSpine.get(next.n).c0 : onSpine.get(head.n).c1;
1398
+ return { shots: shots.map((e) => e.n), src: [src0, src1], cut: [roundMs(onSpine.get(head.n).c0), roundMs(cut1)] };
1399
+ });
1400
+ }
1401
+
1402
+ /** A time of the source ad on the cut, through its group (the first or last group's ends past them). */
1403
+ export function mapSourceTime(groups, t) {
1404
+ if (groups.length === 0) return t;
1405
+ const g = groups.find((x) => t < x.src[1]) ?? groups.at(-1);
1406
+ const span = g.src[1] - g.src[0];
1407
+ const k = span > 0 ? (g.cut[1] - g.cut[0]) / span : 1;
1408
+ return Math.min(g.cut[1], Math.max(g.cut[0], g.cut[0] + (t - g.src[0]) * k));
1409
+ }
1410
+
1054
1411
  export function buildTimeline({
1055
- manifest, storyboard, skip, probe, exists, readWords, readLevels = () => null, stillFill = false,
1412
+ manifest, storyboard, skip, probe, exists, readWords, readLevels = () => null, stillFill = false, finish = null,
1413
+ wholeVoice = null, sourceAd = null,
1056
1414
  }) {
1057
1415
  const plans = scenePlans(storyboard);
1058
1416
  const counts = lineWordCounts(storyboard);
@@ -1193,6 +1551,9 @@ export function buildTimeline({
1193
1551
  })));
1194
1552
  }
1195
1553
 
1554
+ const whole = wholeVoice ? fitWholeVoice(spine, wholeVoice) : null;
1555
+ if (whole) dropped.push(...whole.dropped);
1556
+
1196
1557
  const sameShot = PUNCH_IN_RUNS.get(manifest?.shots?.pacing);
1197
1558
  if (sameShot) markPunchIns(spine, sameShot);
1198
1559
 
@@ -1205,10 +1566,23 @@ export function buildTimeline({
1205
1566
  const scriptLines = new Map((storyboard?.script ?? []).map((line) => [line?.lineId, line]));
1206
1567
  const reactionCues = new Set(waiting.filter((w) => w.reaction).map((w) => w.cueLineId));
1207
1568
  const last = spine.at(-1);
1208
- if (refits.has(last) && !reactionCues.has(last.lineIds.at(-1))) Object.assign(last, refits.get(last)(true));
1209
- tightenSentenceGaps(spine, reactionCues, scriptLines, readLevels);
1210
- trimJoins(spine, reactionCues, scriptLines);
1569
+ if (!whole && refits.has(last) && !reactionCues.has(last.lineIds.at(-1))) Object.assign(last, refits.get(last)(true));
1570
+ const pace = finish?.cutPace === true;
1571
+ const lineGap = pace ? { max: PACE_SPLIT_PAUSE_SEC, keep: PACE_GAP_MAX_SEC } : null;
1572
+ tightenSentenceGaps(spine, reactionCues, scriptLines, readLevels, lineGap);
1573
+ trimJoins(spine, reactionCues, scriptLines, pace ? PACE_GAP_MAX_SEC : null);
1574
+ if (pace) {
1575
+ // The pieces of one take alternate between the plain frame and the
1576
+ // punch-in, and a jump-cut run's alternation carries on across them.
1577
+ splitAtPauses(spine);
1578
+ for (const seg of spine) delete seg.punchIn;
1579
+ markPunchIns(spine, (prev, seg) => samePiece(prev, seg) || Boolean(sameShot?.(prev, seg)));
1580
+ }
1211
1581
  for (const c of waiting.filter((w) => w.reaction)) {
1582
+ if (whole) {
1583
+ dropped.push({ n: c.sceneIndex, reason: `cutaway ${c.sceneIndex}: a reaction beat would stop the voice laid whole` });
1584
+ continue;
1585
+ }
1212
1586
  if (!c.cueLineId || placeReaction(spine, counts, c) === null) {
1213
1587
  dropped.push({ n: c.sceneIndex, reason: `cutaway ${c.sceneIndex}: ${noCue(c)}` });
1214
1588
  }
@@ -1220,6 +1594,21 @@ export function buildTimeline({
1220
1594
  else seg.fadeOutSec = Math.min(REACTION_FADE_OUT_SEC, seg.seconds);
1221
1595
  seg.fadeInSec = quietStart(readLevels(seg.file), seg.seconds);
1222
1596
  });
1597
+ // A copied shot no voice runs in lasts its shot's length, its clip trimmed or its last frame held.
1598
+ const shotOf = (n) => plans.get(n)?.sourceShot ?? null;
1599
+ const shotSec = (n) => {
1600
+ const e = sourceAd?.shots.find((x) => x.n === shotOf(n));
1601
+ return e ? e.t1 - e.t0 : null;
1602
+ };
1603
+ if (sourceAd) {
1604
+ for (const seg of spine) {
1605
+ const len = seg.audio === "silence" && !seg.reaction ? shotSec(seg.sceneIndex) : null;
1606
+ if (len === null) continue;
1607
+ seg.seconds = ceilFrame(len);
1608
+ seg.holdSec = holdFor(seg);
1609
+ seg.note = withHow(seg.note, [`its source shot's ${len.toFixed(2)}s`]);
1610
+ }
1611
+ }
1223
1612
  let total = 0;
1224
1613
  for (const seg of spine) {
1225
1614
  seg.startSec = total;
@@ -1228,10 +1617,22 @@ export function buildTimeline({
1228
1617
  spine.forEach((seg, k) => {
1229
1618
  if (seg.reaction?.tail) seg.reaction.tail.atSec = spine[k + 1].startSec;
1230
1619
  });
1620
+ const groups = sourceAd ? sourceGroups(spine, sourceAd, shotOf) : null;
1231
1621
 
1232
1622
  const lineTexts = new Map([...scriptLines].map(([id, line]) => [id, line?.text]));
1233
1623
  const beats = spine.filter((seg) => seg.reaction);
1234
1624
  for (const c of waiting.filter((w) => !w.reaction)) {
1625
+ // A copied shot lands where its shot sits in the source, through its group.
1626
+ const copied = groups?.length ? sourceAd.shots.find((e) => e.n === shotOf(c.sceneIndex)) : null;
1627
+ if (copied) {
1628
+ const startSec = roundMs(mapSourceTime(groups, copied.t0));
1629
+ const span = roundMs(mapSourceTime(groups, copied.t1) - startSec);
1630
+ cutaways.push({
1631
+ ...c, startSec, method: "source", sourceShot: copied.n,
1632
+ seconds: c.source === "clip" ? Math.min(span, c.seconds) : span,
1633
+ });
1634
+ continue;
1635
+ }
1235
1636
  const cue = c.cueLineId ? cueOffsetOnSpine(spine, counts, c.cueLineId) : null;
1236
1637
  if (!cue) {
1237
1638
  dropped.push({ n: c.sceneIndex, reason: `cutaway ${c.sceneIndex}: ${noCue(c)}` });
@@ -1272,6 +1673,8 @@ export function buildTimeline({
1272
1673
  spine,
1273
1674
  cutaways,
1274
1675
  left: dropped.sort((a, b) => a.n - b.n).map((d) => d.reason),
1676
+ ...(whole ? { wholeVoice: { file: wholeVoice.file, startSec: whole.startSec } } : {}),
1677
+ ...(groups ? { sourceGroups: groups } : {}),
1275
1678
  };
1276
1679
  }
1277
1680
 
@@ -1293,7 +1696,15 @@ function sec(n) {
1293
1696
  return (Number.isFinite(n) ? Math.max(0, n) : 0).toFixed(3);
1294
1697
  }
1295
1698
 
1296
- export function buildFfmpegArgs(timeline, { resolve, music, out }) {
1699
+ /**
1700
+ * The render. `finish` (null: today's render) carries the finishing settings'
1701
+ * part of it: levelSegments false leaves each segment's sound as it is for the
1702
+ * whole mix's loudness pass, `ass` names the captions and cards file burned in
1703
+ * (read from the folder ffmpeg runs in), and `speed` over 1 speeds the whole
1704
+ * cut, picture and sound, after the burn.
1705
+ */
1706
+ export function buildFfmpegArgs(timeline, { resolve, music, out, finish = null, sound = null }) {
1707
+ const level = finish && finish.levelSegments === false ? "anull" : LOUDNORM;
1297
1708
  const inputs = [];
1298
1709
  let count = 0;
1299
1710
  const addInput = (...args) => {
@@ -1350,8 +1761,10 @@ export function buildFfmpegArgs(timeline, { resolve, music, out }) {
1350
1761
  const fitLength =
1351
1762
  `apad,atrim=0:${sec(seg.seconds)},afade=t=in:d=${sec(fadeIn)}${rise},` +
1352
1763
  `afade=t=out:st=${sec(seg.seconds - fadeOut)}:d=${sec(fadeOut)}`;
1353
- const fitSound = `${LOUDNORM},${fitLength}`;
1354
- if (seg.audio === "narration") {
1764
+ const fitSound = `${level},${fitLength}`;
1765
+ if (seg.wholeVoice) {
1766
+ filters.push(`anullsrc=r=48000:cl=stereo,atrim=0:${sec(seg.seconds)}[a${i}]`);
1767
+ } else if (seg.audio === "narration") {
1355
1768
  const v = addInput("-i", resolve(seg.voice));
1356
1769
  filters.push(`[${v}:a]${stereo},${skipIn}${fitSound}[a${i}]`);
1357
1770
  } else if (seg.audio === "clip" && seg.reaction?.tail) {
@@ -1359,7 +1772,7 @@ export function buildFfmpegArgs(timeline, { resolve, music, out }) {
1359
1772
  // ducked, faded out, and laid in at the join; the next line's own sound
1360
1773
  // plays at full level over it (#2406).
1361
1774
  const t = seg.reaction.tail;
1362
- filters.push(`${heard}${skipIn}${LOUDNORM},asplit=2[as${i}][at${i}]`);
1775
+ filters.push(`${heard}${skipIn}${level},asplit=2[as${i}][at${i}]`);
1363
1776
  filters.push(`[as${i}]${fitLength}[a${i}]`);
1364
1777
  filters.push(
1365
1778
  `[at${i}]atrim=start=${sec(t.fromSec - inSec)}:duration=${sec(t.seconds)},asetpts=PTS-STARTPTS,` +
@@ -1380,7 +1793,18 @@ export function buildFfmpegArgs(timeline, { resolve, music, out }) {
1380
1793
  // ends on its own. No -shortest: ffmpeg 8 held the whole ad's frames for it,
1381
1794
  // and Debian's ffmpeg 5.1 (the worker's) never ended at all (#2988).
1382
1795
  const pictureSec = timeline.spine.reduce((sum, seg) => sum + seg.seconds, 0);
1383
- filters.push(`[voicecat]apad,atrim=end=${pictureSec.toFixed(6)}[voice]`);
1796
+ if (timeline.wholeVoice) {
1797
+ // The voice laid whole from where its first scene starts, under the spine's own sound.
1798
+ const k = addInput("-i", resolve(timeline.wholeVoice.file));
1799
+ filters.push(`[voicecat]apad,atrim=end=${pictureSec.toFixed(6)}[spoken]`);
1800
+ filters.push(
1801
+ `[${k}:a]${stereo},${level},adelay=delays=${Math.round(timeline.wholeVoice.startSec * 1000)}:all=1,` +
1802
+ `apad,atrim=end=${pictureSec.toFixed(6)}[whole]`,
1803
+ );
1804
+ filters.push("[spoken][whole]amix=inputs=2:duration=first:dropout_transition=0:normalize=0[voice]");
1805
+ } else {
1806
+ filters.push(`[voicecat]apad,atrim=end=${pictureSec.toFixed(6)}[voice]`);
1807
+ }
1384
1808
 
1385
1809
  let videoLabel = "vcat";
1386
1810
  timeline.cutaways.forEach((c, j) => {
@@ -1399,17 +1823,73 @@ export function buildFfmpegArgs(timeline, { resolve, music, out }) {
1399
1823
  videoLabel = `ov${j}`;
1400
1824
  });
1401
1825
 
1402
- filters.push(`${ROOM_TONE}[room]`);
1403
- const beds = ["[room]", ...tails];
1826
+ // A copied ad's product cards: the product sheet picture fitted into each source box, for its time.
1827
+ (finish?.pictures ?? []).forEach((p, j) => {
1828
+ const k = addInput("-loop", "1", "-t", sec(p.endSec), "-i", resolve(p.file));
1829
+ const w = Math.round((p.box[2] - p.box[0]) * WIDTH);
1830
+ const h = Math.round((p.box[3] - p.box[1]) * HEIGHT);
1831
+ filters.push(`[${k}:v]scale=${w}:${h}:force_original_aspect_ratio=decrease,format=rgba[pic${j}]`);
1832
+ filters.push(
1833
+ `[${videoLabel}][pic${j}]overlay=x=${Math.round(p.box[0] * WIDTH)}+(${w}-overlay_w)/2:y=${Math.round(p.box[1] * HEIGHT)}+(${h}-overlay_h)/2` +
1834
+ `:enable=between(t\\,${sec(p.startSec)}\\,${sec(p.endSec)}):eof_action=pass[pin${j}]`,
1835
+ );
1836
+ videoLabel = `pin${j}`;
1837
+ });
1838
+ if (finish?.ass) {
1839
+ filters.push(`[${videoLabel}]subtitles=filename=${finish.ass}[burned]`);
1840
+ videoLabel = "burned";
1841
+ }
1842
+ const speed = finish?.speed > 1 ? finish.speed : null;
1843
+ if (speed) {
1844
+ filters.push(`[${videoLabel}]setpts=PTS/${speed}[faster]`);
1845
+ videoLabel = "faster";
1846
+ }
1847
+
1848
+ const beds = [];
1849
+ if (sound?.beds?.length) {
1850
+ sound.beds.forEach((b, j) => {
1851
+ const k = addInput("-stream_loop", "-1", "-i", resolve(b.file));
1852
+ filters.push(
1853
+ `[${k}:a]${stereo},atrim=0:${sec(b.seconds)},asetpts=PTS-STARTPTS,volume=${b.gainDb}dB,` +
1854
+ `afade=t=in:d=${BED_FADE_SEC},afade=t=out:st=${sec(Math.max(0, b.seconds - BED_FADE_SEC))}:d=${BED_FADE_SEC},` +
1855
+ `adelay=delays=${Math.round(b.startSec * 1000)}:all=1[set${j}]`,
1856
+ );
1857
+ beds.push(`[set${j}]`);
1858
+ });
1859
+ } else {
1860
+ filters.push(`${ROOM_TONE}[room]`);
1861
+ beds.push("[room]");
1862
+ }
1863
+ beds.push(...tails);
1864
+ (sound?.effects ?? []).forEach((e, j) => {
1865
+ const k = addInput("-i", resolve(e.file));
1866
+ filters.push(`[${k}:a]${stereo},volume=${e.gainDb}dB,adelay=delays=${Math.round(e.atSec * 1000)}:all=1[sfx${j}]`);
1867
+ beds.push(`[sfx${j}]`);
1868
+ });
1869
+ let voice = "[voice]";
1404
1870
  if (music) {
1405
1871
  const m = addInput("-stream_loop", "-1", "-i", music);
1406
- filters.push(`[${m}:a]${stereo},volume=${MUSIC_VOLUME}[bed]`);
1872
+ const duck = sound?.music?.duck;
1873
+ if (duck) {
1874
+ filters.push("[voice]asplit=2[voiced][duckkey]");
1875
+ filters.push(`[${m}:a]${stereo},atrim=0:${pictureSec.toFixed(6)},asetpts=PTS-STARTPTS,volume=${sound.music.gainDb}dB[bedraw]`);
1876
+ filters.push(
1877
+ `[bedraw][duckkey]sidechaincompress=threshold=${duck.threshold}:ratio=${duck.ratio}:attack=${duck.attack}:release=${duck.release}[bed]`,
1878
+ );
1879
+ voice = "[voiced]";
1880
+ } else {
1881
+ filters.push(`[${m}:a]${stereo},volume=${MUSIC_VOLUME}[bed]`);
1882
+ }
1407
1883
  beds.push("[bed]");
1408
1884
  }
1409
1885
  filters.push(
1410
- `[voice]${beds.join("")}amix=inputs=${beds.length + 1}:duration=first:dropout_transition=0:normalize=0[mix]`,
1886
+ `${voice}${beds.join("")}amix=inputs=${beds.length + 1}:duration=first:dropout_transition=0:normalize=0[mix]`,
1411
1887
  );
1412
- const audioLabel = "mix";
1888
+ let audioLabel = "mix";
1889
+ if (speed) {
1890
+ filters.push(`[mix]atempo=${speed}[mixfaster]`);
1891
+ audioLabel = "mixfaster";
1892
+ }
1413
1893
 
1414
1894
  return [
1415
1895
  "-y", "-hide_banner", "-loglevel", "error",
@@ -1673,6 +2153,290 @@ export function noWordTimes(timeline) {
1673
2153
  return rows;
1674
2154
  }
1675
2155
 
2156
+ // ---- finishing: captions and cards drawn by code, the pace report, the loudness pass ----
2157
+
2158
+ const pad2 = (n) => String(n).padStart(2, "0");
2159
+ const round2 = (n) => Math.round(n * 100) / 100;
2160
+ const assTime = (t) => {
2161
+ const cs = Math.round(Math.max(0, t) * 100);
2162
+ return `${Math.floor(cs / 360000)}:${pad2(Math.floor(cs / 6000) % 60)}:${pad2(Math.floor(cs / 100) % 60)}.${pad2(cs % 100)}`;
2163
+ };
2164
+ const assText = (s) => String(s).replace(/[{}\\]/g, "").replace(/\s*[\r\n]+\s*/g, " ").trim();
2165
+
2166
+ /** A caption screen never holds words across a pause longer than this, so a silent beat shows none. */
2167
+ export const CAPTION_BREAK_SEC = 0.8;
2168
+
2169
+ /**
2170
+ * Caption screens from the cut's words: a few words a screen (`cap`, a source
2171
+ * record's caption style, else 3), never across a pause, each up until 0.25 s
2172
+ * after its last word or the next screen. `words`: [{ text, start, end }].
2173
+ */
2174
+ export function captionScreens(words, cap = null) {
2175
+ const per = Math.max(1, Math.round(cap?.wordsPerLine?.median ?? 3));
2176
+ const caps = /all-caps|capitals|upper/i.test(cap?.style ?? "");
2177
+ const chunks = [];
2178
+ let cur = [];
2179
+ for (const w of words) {
2180
+ if (cur.length && (cur.length >= per || w.start - cur.at(-1).end > CAPTION_BREAK_SEC)) {
2181
+ chunks.push(cur);
2182
+ cur = [];
2183
+ }
2184
+ cur.push(w);
2185
+ }
2186
+ if (cur.length) chunks.push(cur);
2187
+ return chunks.map((ws, k) => {
2188
+ const next = chunks[k + 1]?.[0];
2189
+ const text = ws.map((w) => assText(w.text)).join(" ").replace(/[.,;:]+$/, "");
2190
+ return {
2191
+ text: caps ? text.toUpperCase() : text,
2192
+ s: ws[0].start,
2193
+ e: roundMs(Math.min(ws.at(-1).end + 0.25, next?.start ?? Infinity)),
2194
+ };
2195
+ });
2196
+ }
2197
+
2198
+ /**
2199
+ * One ASS file for the burn: the caption screens in their band, the cards in
2200
+ * their boxes, the hook box and the end card. Boxes are [x0, y0, x1, y1] as
2201
+ * shares of the frame. Every word on screen is the script's or the storyboard's.
2202
+ */
2203
+ export function editAss({ captions = [], cap = null, cards = [], hook = null, endCard = null }, W = WIDTH, H = HEIGHT) {
2204
+ const thick = /thick black outline/i.test(cap?.style ?? "");
2205
+ const y = cap?.place ? Math.round(((cap.place[0] + cap.place[1]) / 2) * H) : Math.round(H * 0.7);
2206
+ const capSize = Math.round(H * (thick ? 0.05 : 0.034));
2207
+ const box = (b) => `\\pos(${Math.round(((b[0] + b[2]) / 2) * W)},${Math.round(((b[1] + b[3]) / 2) * H)})`;
2208
+ return [
2209
+ "[Script Info]", "ScriptType: v4.00+", `PlayResX: ${W}`, `PlayResY: ${H}`, "WrapStyle: 0", "ScaledBorderAndShadow: yes", "",
2210
+ "[V4+ Styles]",
2211
+ "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding",
2212
+ `Style: Cap,Helvetica,${capSize},&H00FFFFFF,&H00FFFFFF,&H00000000,&H90000000,-1,0,0,0,100,100,0,0,1,${thick ? 7 : 1.5},${thick ? 4 : 2},5,60,60,0,1`,
2213
+ "Style: Card,Helvetica,60,&H00141414,&H00141414,&H00FFFFFF,&H50000000,-1,0,0,0,100,100,0,0,3,18,4,5,0,0,0,1",
2214
+ `Style: End,Helvetica,${Math.round(H * 0.045)},&H00FFFFFF,&H00FFFFFF,&H20101010,&H00000000,-1,0,0,0,100,100,2,0,3,40,0,5,0,0,0,1`, "",
2215
+ "[Events]", "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text",
2216
+ ...captions.map((c) => `Dialogue: 1,${assTime(c.s)},${assTime(c.e)},Cap,,0,0,0,,{\\pos(${W / 2},${y})}${c.text}`),
2217
+ ...cards.filter((c) => c.label).map((c) => `Dialogue: 2,${assTime(c.in)},${assTime(c.out)},Card,,0,0,0,,{${box(c.box)}\\fs${Math.round(H * 0.036)}\\fad(60,0)}${assText(c.label)}`),
2218
+ ...(hook?.text ? [`Dialogue: 3,${assTime(hook.in)},${assTime(hook.out)},Card,,0,0,0,,{${box(hook.box)}\\fs${Math.round(H * 0.026)}}${assText(hook.text)}`] : []),
2219
+ ...(endCard ? [`Dialogue: 4,${assTime(endCard.in)},${assTime(endCard.out)},End,,0,0,0,,{\\pos(${W / 2},${H / 2})\\fad(120,0)}${assText(endCard.text)}`] : []),
2220
+ ].join("\n") + "\n";
2221
+ }
2222
+
2223
+ /**
2224
+ * Where a card sits when the storyboard says it only in words (`placement`).
2225
+ * A source record's own boxes take their place once a run can carry one.
2226
+ */
2227
+ const CARD_BOXES = [
2228
+ [/full|centre|center|middle/i, [0.08, 0.44, 0.92, 0.56]],
2229
+ [/lower|bottom/i, [0.08, 0.56, 0.92, 0.64]],
2230
+ ];
2231
+ const CARD_BOX_TOP = [0.08, 0.14, 0.92, 0.24];
2232
+ export const cardBox = (placement) => CARD_BOXES.find(([words]) => words.test(placement ?? ""))?.[1] ?? CARD_BOX_TOP;
2233
+ /** Overlays that are pictures, not words: no file for them reaches the cut, so none is drawn. */
2234
+ const PICTURE_OVERLAY = /screenshot|chart/i;
2235
+ /** The end card holds the ad's last this many seconds. */
2236
+ export const END_CARD_SEC = 2;
2237
+
2238
+ /**
2239
+ * The storyboard's on-screen words (its overlays) as cards: each lands
2240
+ * CARD_LEAD_SEC before the first word of the line it is cued to and holds
2241
+ * through that line, at least BROLL_MIN_HOLD_SEC. The CTA's own overlay is
2242
+ * the end card instead (endCardOf). An overlay cued to a line no segment
2243
+ * speaks is left out.
2244
+ */
2245
+ export function storyboardCards(storyboard, timeline) {
2246
+ const counts = lineWordCounts(storyboard);
2247
+ const ctaId = storyboard?.cta?.overlayId;
2248
+ const cards = [];
2249
+ for (const o of storyboard?.overlays ?? []) {
2250
+ if (!hasText(o?.text) || !hasText(o?.cueLineId) || o.overlayId === ctaId || PICTURE_OVERLAY.test(o.type ?? "")) continue;
2251
+ const seg = timeline.spine.find((s) => !s.reaction && s.lineIds.includes(o.cueLineId));
2252
+ const cue = seg ? cueOffsetOnSpine(timeline.spine, counts, o.cueLineId) : null;
2253
+ if (!cue) continue;
2254
+ const at = roundMs(Math.max(0, cue.startSec - CARD_LEAD_SEC));
2255
+ const lineEnd = seg.startSec + (cueEndInSegment(seg, counts, o.cueLineId) ?? seg.seconds);
2256
+ cards.push({
2257
+ n: cards.length + 1, label: o.text.trim(), box: cardBox(o.placement),
2258
+ in: at, out: roundMs(Math.max(lineEnd, at + BROLL_MIN_HOLD_SEC)), on: `line ${o.cueLineId}`,
2259
+ });
2260
+ }
2261
+ return cards;
2262
+ }
2263
+
2264
+ /**
2265
+ * A copied ad's on-screen words in the source's own places: the storyboard's
2266
+ * overlays, in script order, land in the record's card boxes one for one, at
2267
+ * the card's times on the cut; the overlay placed "hook" goes in the record's
2268
+ * hook box. Cards with no overlay of ours, and overlays with no card, are
2269
+ * left out and noted.
2270
+ */
2271
+ export function sourceAdEdit(storyboard, sourceAd, groups) {
2272
+ const at = (t) => roundMs(mapSourceTime(groups, t));
2273
+ const order = new Map((storyboard?.script ?? []).map((line, i) => [line?.lineId, i]));
2274
+ const ctaId = storyboard?.cta?.overlayId;
2275
+ const words = (storyboard?.overlays ?? []).filter(
2276
+ (o) => hasText(o?.text) && o.overlayId !== ctaId && !PICTURE_OVERLAY.test(o.type ?? ""),
2277
+ );
2278
+ const hookOverlay = words.find((o) => /hook/i.test(o.placement ?? "")) ?? null;
2279
+ const texts = words
2280
+ .filter((o) => o !== hookOverlay)
2281
+ .sort((a, b) => (order.get(a.cueLineId) ?? Infinity) - (order.get(b.cueLineId) ?? Infinity));
2282
+ const cards = sourceAd.cards.flatMap((c, k) => {
2283
+ const o = texts[k];
2284
+ if (!o) return [];
2285
+ const from = at(c.tIn);
2286
+ return [{ n: k + 1, label: o.text.trim(), box: c.box, in: from, out: Math.max(at(c.tOut), roundMs(from + BROLL_MIN_HOLD_SEC)), on: `source card ${c.n}` }];
2287
+ });
2288
+ const notes = [];
2289
+ if (sourceAd.cards.length > texts.length) notes.push(`${sourceAd.cards.length - texts.length} of the source's cards had no overlay of ours, so they were not drawn`);
2290
+ if (texts.length > sourceAd.cards.length) notes.push(`${texts.length - sourceAd.cards.length} overlays had no source card to land in, so they were not drawn`);
2291
+ const hook = sourceAd.hook && hookOverlay
2292
+ ? { text: hookOverlay.text.trim(), box: sourceAd.hook.box, in: at(sourceAd.hook.tIn), out: at(sourceAd.hook.tOut) }
2293
+ : null;
2294
+ if (sourceAd.hook && !hookOverlay) notes.push("the source has a hook box and the storyboard no overlay placed \"hook\", so none was drawn");
2295
+ return { cards, hook, notes };
2296
+ }
2297
+
2298
+ /** The CTA overlay's words over the ad's last END_CARD_SEC, or null when the storyboard has none. */
2299
+ export function endCardOf(storyboard, seconds) {
2300
+ const ctaId = storyboard?.cta?.overlayId;
2301
+ const overlay = ctaId ? (storyboard?.overlays ?? []).find((o) => o?.overlayId === ctaId) : null;
2302
+ if (!hasText(overlay?.text)) return null;
2303
+ return { text: overlay.text.trim(), in: roundMs(Math.max(0, seconds - END_CARD_SEC)), out: roundMs(seconds + 1) };
2304
+ }
2305
+
2306
+ /**
2307
+ * The pace report: cuts a minute, mean and longest shot, shots over
2308
+ * LONG_SHOT_SEC, words a minute and pauses over 0.3 s, against a source ad's
2309
+ * when there is one (each source figure null when not). Shots are
2310
+ * [{ start, end }] or [{ t0, t1 }]; silences [{ s, e }].
2311
+ */
2312
+ export function paceReport({ ourShots, srcShots = null, ourSec, srcSec = null, ourWords = 0, srcWords = 0, ourSilences = [], speed = 1 }) {
2313
+ const lens = (xs) => xs.map((s) => round2((s.end ?? s.t1) - (s.start ?? s.t0)));
2314
+ const o = lens(ourShots);
2315
+ const sr = srcShots && srcSec ? lens(srcShots) : null;
2316
+ const mean = (a) => (a.length ? round2(a.reduce((x, y) => x + y, 0) / a.length) : null);
2317
+ const long = (a) => a.filter((x) => x > LONG_SHOT_SEC + 0.05).length;
2318
+ return {
2319
+ seconds: { ours: round2(ourSec), source: sr ? round2(srcSec) : null, ratio: sr ? roundMs(ourSec / srcSec) : null },
2320
+ speed,
2321
+ cutsPerMin: { ours: round2(((ourShots.length - 1) / ourSec) * 60), source: sr ? round2(((srcShots.length - 1) / srcSec) * 60) : null },
2322
+ meanShotSec: { ours: mean(o), source: sr ? mean(sr) : null },
2323
+ longestShotSec: { ours: Math.max(0, ...o), source: sr ? Math.max(0, ...sr) : null },
2324
+ over4s: { ours: long(o), source: sr ? long(sr) : null },
2325
+ wordsPerMin: { ours: ourWords ? round2((ourWords / ourSec) * 60) : null, source: srcWords && srcSec ? round2((srcWords / srcSec) * 60) : null },
2326
+ deadAir: {
2327
+ over03s: ourSilences.filter((x) => x.e - x.s > 0.3).length,
2328
+ longestSec: round2(Math.max(0, ...ourSilences.map((x) => x.e - x.s))),
2329
+ },
2330
+ };
2331
+ }
2332
+
2333
+ /** The pauses in the cut's voice, from its word times: every gap of PACE_PAUSE_SEC or more between two words. */
2334
+ export function voicePauses(rows) {
2335
+ const pauses = [];
2336
+ for (let i = 1; i < rows.length; i++) {
2337
+ if (rows[i].start - rows[i - 1].end >= PACE_PAUSE_SEC) pauses.push({ s: rows[i - 1].end, e: rows[i].start });
2338
+ }
2339
+ return pauses;
2340
+ }
2341
+
2342
+ const LOUDNORM_TARGET = `I=${FINISH_LUFS}:TP=${FINISH_TRUE_PEAK}:LRA=${FINISH_LRA}`;
2343
+
2344
+ /** finishLoudness, pass one: measure a file's sound. Its numbers come back on stderr as JSON. */
2345
+ export function loudnessMeasureArgs(file) {
2346
+ return ["-hide_banner", "-nostats", "-i", file, "-map", "0:a:0", "-af", `loudnorm=${LOUDNORM_TARGET}:print_format=json`, "-f", "null", "-"];
2347
+ }
2348
+
2349
+ /** loudnorm's measured numbers out of its printed JSON, or null when any is missing or not a number (a silent file). */
2350
+ export function readLoudness(stderr) {
2351
+ const block = String(stderr ?? "").match(/\{[^{}]*"input_i"[^{}]*\}/);
2352
+ if (!block) return null;
2353
+ let raw;
2354
+ try {
2355
+ raw = JSON.parse(block[0]);
2356
+ } catch {
2357
+ return null;
2358
+ }
2359
+ const measured = {
2360
+ i: Number(raw.input_i), tp: Number(raw.input_tp), lra: Number(raw.input_lra),
2361
+ thresh: Number(raw.input_thresh), offset: Number(raw.target_offset),
2362
+ };
2363
+ return Object.values(measured).every(Number.isFinite) ? measured : null;
2364
+ }
2365
+
2366
+ /**
2367
+ * finishLoudness, pass two: the whole mix normalized with pass one's numbers,
2368
+ * the picture copied as it is. Without them (pass one could not read the
2369
+ * sound) loudnorm runs on its own, in one pass.
2370
+ */
2371
+ export function loudnessApplyArgs(mix, measured, out) {
2372
+ const second = measured
2373
+ ? `:measured_I=${measured.i}:measured_TP=${measured.tp}:measured_LRA=${measured.lra}` +
2374
+ `:measured_thresh=${measured.thresh}:offset=${measured.offset}:linear=true`
2375
+ : "";
2376
+ return [
2377
+ "-y", "-hide_banner", "-loglevel", "error", "-i", mix,
2378
+ "-map", "0:v:0", "-map", "0:a:0", "-c:v", "copy",
2379
+ "-af", `loudnorm=${LOUDNORM_TARGET}${second},aresample=48000`,
2380
+ "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart", out,
2381
+ ];
2382
+ }
2383
+
2384
+ /** Why a finished cut is refused for its loudness, or null when its true peak is at or under TRUE_PEAK_MAX_DB. */
2385
+ export function truePeakRefusal(measured) {
2386
+ if (!measured) return "The cut's loudness could not be measured after the loudness pass, so it was not attached";
2387
+ if (measured.tp <= TRUE_PEAK_MAX_DB) return null;
2388
+ return `The cut's true peak is ${measured.tp.toFixed(2)} dBTP after the loudness pass, over the ${TRUE_PEAK_MAX_DB} dBTP limit, so it was not attached`;
2389
+ }
2390
+
2391
+ function measureLoudness(file) {
2392
+ const res = spawnSync("ffmpeg", loudnessMeasureArgs(file), { encoding: "utf8", maxBuffer: 1 << 26 });
2393
+ return res.status === 0 ? readLoudness(res.stderr) : null;
2394
+ }
2395
+
2396
+ /** A sound file's loudness pass one, padded to 3 s as LOUDNORM is, so a short sound still measures. */
2397
+ export function soundLevelArgs(file) {
2398
+ return ["-hide_banner", "-nostats", "-i", file, "-map", "0:a:0", "-af", `apad=whole_dur=3,loudnorm=${LOUDNORM_TARGET}:print_format=json`, "-f", "null", "-"];
2399
+ }
2400
+
2401
+ function measureLufs(file) {
2402
+ const res = spawnSync("ffmpeg", soundLevelArgs(file), { encoding: "utf8", maxBuffer: 1 << 26 });
2403
+ const measured = res.status === 0 ? readLoudness(res.stderr) : null;
2404
+ return measured ? round2(measured.i) : null;
2405
+ }
2406
+
2407
+ /** Both passes on the rendered mix, then the finished cut measured again; throws LoudnessRefusal over the limit. */
2408
+ export function finishLoudnessPass(mix, out) {
2409
+ const measured = measureLoudness(mix);
2410
+ const args = loudnessApplyArgs(mix, measured, out);
2411
+ const res = spawnSync("ffmpeg", args, { stdio: "inherit" });
2412
+ if (res.status !== 0) {
2413
+ const quoted = args.map((a) => (/[\s\[\];]/.test(a) ? `'${a}'` : a)).join(" ");
2414
+ throw new FirstCutError(1, `ffmpeg failed on the loudness pass. The command it ran:\n ffmpeg ${quoted}`);
2415
+ }
2416
+ const finished = measureLoudness(out);
2417
+ const refusal = truePeakRefusal(finished);
2418
+ if (refusal) throw new LoudnessRefusal(refusal, finished);
2419
+ return { lufs: round2(finished.i), truePeak: round2(finished.tp), passes: measured ? 2 : 1 };
2420
+ }
2421
+
2422
+ /** Whether this ffmpeg has a filter (the captions and cards need `subtitles`, which needs libass). */
2423
+ function hasFilter(name) {
2424
+ const listed = spawnSync("ffmpeg", ["-hide_banner", "-filters"]).stdout?.toString() ?? "";
2425
+ return new RegExp(`^\\s*\\S+\\s+${name}\\s`, "m").test(listed);
2426
+ }
2427
+
2428
+ /** The files the finishing settings write beside the cut: the pace record, and the mix before the loudness pass. */
2429
+ export function finishPaths(outPath) {
2430
+ const dir = path.dirname(outPath);
2431
+ const base = path.basename(outPath, path.extname(outPath));
2432
+ return {
2433
+ pace: path.join(dir, `${base}.pace.json`),
2434
+ mix: path.join(dir, `${base}.mix${path.extname(outPath)}`),
2435
+ sound: path.join(dir, `${base}.sound.json`),
2436
+ master: path.join(dir, `${base}.music-only${path.extname(outPath)}`),
2437
+ };
2438
+ }
2439
+
1676
2440
  function fail(code, message) {
1677
2441
  console.error(message);
1678
2442
  process.exit(code);
@@ -1687,6 +2451,15 @@ export class FirstCutError extends Error {
1687
2451
  }
1688
2452
  }
1689
2453
 
2454
+ /** finishLoudness: the finished cut's true peak is over TRUE_PEAK_MAX_DB, or could not be measured. */
2455
+ export class LoudnessRefusal extends FirstCutError {
2456
+ constructor(message, measured) {
2457
+ super(1, message);
2458
+ this.name = "LoudnessRefusal";
2459
+ this.measured = measured ?? null;
2460
+ }
2461
+ }
2462
+
1690
2463
  function parseArgs(argv) {
1691
2464
  const opts = { dir: undefined, out: undefined, skip: new Set(), music: true, stillFill: false };
1692
2465
  for (let i = 0; i < argv.length; i++) {
@@ -1755,6 +2528,12 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
1755
2528
  if (!storyboard) {
1756
2529
  console.error(`Warning: ${manifest.storyboard ?? "storyboard.json"} is missing or unreadable, so every scene is cut as A-roll and no cutaway is placed.`);
1757
2530
  }
2531
+ const finish = finishingOf(manifest);
2532
+ const sourceAd = sourceAdOf(manifest);
2533
+ const wholeVoice = wholeVoiceOf(manifest, readJson, exists);
2534
+ if (manifest.wholeVoice === true && !wholeVoice) {
2535
+ console.error("Warning: the manifest asks for the voice laid whole, but the narration track or its narration.json is missing, so each scene's voice is laid on its own.");
2536
+ }
1758
2537
  const timeline = buildTimeline({
1759
2538
  manifest,
1760
2539
  storyboard,
@@ -1767,24 +2546,104 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
1767
2546
  return Array.isArray(rows) && rows.length > 0 ? rows : null;
1768
2547
  },
1769
2548
  readLevels: (file) => clipLevels(here(file)),
2549
+ ...(finish ? { finish } : {}),
2550
+ ...(wholeVoice ? { wholeVoice } : {}),
2551
+ ...(sourceAd ? { sourceAd } : {}),
1770
2552
  });
1771
2553
  if (timeline.spine.length === 0) {
1772
2554
  throw new FirstCutError(1, `No scene has a clip or a keyframe. Check: exodus video status ${manifest.runId}`);
1773
2555
  }
1774
2556
 
2557
+ // The finishing settings' part of the render: the speed, the burned
2558
+ // captions and cards, and where the mix goes before its loudness pass.
2559
+ const notes = [];
2560
+ const pictureSec = timeline.spine.reduce((sum, seg) => sum + seg.seconds, 0);
2561
+ // A voice laid whole is a song: it is never sped up.
2562
+ const speed = finish?.cutPace && !timeline.wholeVoice ? speedFor(pictureSec, finish.targetSeconds) : 1;
2563
+ let ass = null;
2564
+ // A copied ad: its words in the record's boxes and its product photos from the product sheet.
2565
+ const copied = sourceAd && finish?.cards ? sourceAdEdit(storyboard, sourceAd, timeline.sourceGroups ?? []) : null;
2566
+ if (copied) notes.push(...copied.notes);
2567
+ let pictures = [];
2568
+ if (sourceAd && finish?.cards && sourceAd.pictures.length > 0) {
2569
+ if (exists(manifest.productSheet)) {
2570
+ pictures = sourceAd.pictures.map((p) => ({
2571
+ file: manifest.productSheet, box: p.box,
2572
+ startSec: roundMs(mapSourceTime(timeline.sourceGroups ?? [], p.tIn)),
2573
+ endSec: roundMs(mapSourceTime(timeline.sourceGroups ?? [], p.tOut)),
2574
+ }));
2575
+ } else {
2576
+ notes.push(`the pull has no product sheet picture, so the source's ${sourceAd.pictures.length} product card(s) were not drawn`);
2577
+ }
2578
+ }
2579
+ if (finish && (finish.captions || finish.cards)) {
2580
+ if (hasFilter("subtitles")) {
2581
+ ass = "edit.ass";
2582
+ const cap = sourceAd?.captions ?? null;
2583
+ writeFileSync(here(ass), editAss({
2584
+ captions: finish.captions ? captionScreens(wordRows(timeline), cap) : [],
2585
+ cap,
2586
+ cards: finish.cards ? (copied ? copied.cards : storyboardCards(storyboard, timeline)) : [],
2587
+ hook: copied?.hook ?? null,
2588
+ // A copied ad has an end card only where its source does.
2589
+ endCard: finish.cards && (!sourceAd || sourceAd.endCard) ? endCardOf(storyboard, pictureSec) : null,
2590
+ }));
2591
+ } else {
2592
+ notes.push("this ffmpeg has no subtitles filter (libass), so the captions and cards were not burned in");
2593
+ }
2594
+ }
2595
+ const paths = finishPaths(out);
2596
+ const rendered = finish?.finishLoudness ? paths.mix : out;
2597
+ // The burn reads its file from the folder ffmpeg runs in, so every path is absolute then.
2598
+ const at = ass ? (file) => path.resolve(dir, file) : here;
2599
+
1775
2600
  const music = musicOn && exists(manifest.music) ? manifest.music : null;
2601
+ const sound = soundOf(manifest);
2602
+ let mixSound = null;
2603
+ const levels = new Map();
2604
+ const measure = (file) => {
2605
+ if (!levels.has(file)) levels.set(file, exists(file) ? measureLufs(here(file)) : null);
2606
+ return levels.get(file);
2607
+ };
2608
+ if (sound) {
2609
+ const missing = [...sound.effects, ...sound.beds].filter((x) => !exists(x.file)).map((x) => x.file);
2610
+ const plan = soundPlan(
2611
+ { ...sound, effects: sound.effects.filter((x) => exists(x.file)), beds: sound.beds.filter((x) => exists(x.file)) },
2612
+ timeline, storyboard, { levelled: !finish?.finishLoudness, measure },
2613
+ );
2614
+ const ducked = sound.duck && music ? { gainDb: duckedMusicGain(plan.voiceLufs, measure(music)), duck: DUCK } : null;
2615
+ mixSound = {
2616
+ ...plan, music: ducked,
2617
+ notes: [...missing.map((f) => `${f} is not in the folder, so it is left out`), ...plan.notes],
2618
+ };
2619
+ }
1776
2620
  const ffmpegArgs = buildFfmpegArgs(timeline, {
1777
- resolve: here,
1778
- music: music ? here(music) : null,
1779
- out,
2621
+ resolve: at,
2622
+ music: music ? at(music) : null,
2623
+ out: rendered,
2624
+ ...(finish ? { finish: { levelSegments: !finish.finishLoudness, ass, speed, ...(pictures.length ? { pictures } : {}) } } : {}),
2625
+ ...(mixSound ? { sound: mixSound } : {}),
1780
2626
  });
1781
- const res = spawnSync("ffmpeg", ffmpegArgs, { stdio: "inherit" });
2627
+ const res = spawnSync("ffmpeg", ffmpegArgs, { stdio: "inherit", ...(ass ? { cwd: path.resolve(dir) } : {}) });
1782
2628
  if (res.status !== 0) {
1783
2629
  const quoted = ffmpegArgs.map((a) => (/[\s\[\];]/.test(a) ? `'${a}'` : a)).join(" ");
1784
2630
  throw new FirstCutError(1, `ffmpeg failed. The command it ran:\n ffmpeg ${quoted}`);
1785
2631
  }
2632
+ let loudness = null;
2633
+ if (finish?.finishLoudness) {
2634
+ try {
2635
+ loudness = finishLoudnessPass(rendered, out);
2636
+ } finally {
2637
+ rmSync(rendered, { force: true });
2638
+ }
2639
+ }
1786
2640
 
1787
- const rows = wordRows(timeline);
2641
+ // With the cut sped up, the times written beside it are the sped-up ones.
2642
+ const faster = (t) => (speed === 1 ? t : roundMs(t / speed));
2643
+ const rows = wordRows(timeline).map((r) => (speed === 1 ? r : { ...r, start: faster(r.start), end: faster(r.end) }));
2644
+ const scenes = sceneTimes(timeline).map((p) =>
2645
+ speed === 1 ? p : { ...p, startSec: faster(p.startSec), seconds: faster(p.seconds) },
2646
+ );
1788
2647
  const sidecars = sidecarPaths(out);
1789
2648
  if (rows.length > 0) {
1790
2649
  writeFileSync(sidecars.words, `${JSON.stringify(rows, null, 2)}\n`);
@@ -1793,12 +2652,57 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
1793
2652
  rmSync(sidecars.words, { force: true });
1794
2653
  rmSync(sidecars.srt, { force: true });
1795
2654
  }
1796
- writeFileSync(sidecars.scenes, `${JSON.stringify(sceneTimes(timeline), null, 2)}\n`);
2655
+ writeFileSync(sidecars.scenes, `${JSON.stringify(scenes, null, 2)}\n`);
1797
2656
 
1798
2657
  const duration = execFileSync("ffprobe", [
1799
2658
  "-v", "error", "-show_entries", "format=duration", "-of", "default=nw=1:nk=1", out,
1800
2659
  ]).toString().trim();
1801
- return { out, durationSec: Number(duration), manifest, timeline, music, rows, sidecars };
2660
+ const durationSec = Number(duration);
2661
+
2662
+ // The sound record beside the cut, and the music-only master the kind asked for.
2663
+ let soundRecord;
2664
+ if (mixSound) {
2665
+ let master = null;
2666
+ if (sound.musicOnlyMaster) {
2667
+ const lufs = music ? measure(music) : null;
2668
+ if (!music) mixSound.notes.push("the cut has no music, so no music-only master was made");
2669
+ else if (!Number.isFinite(lufs)) mixSound.notes.push("the music has no loudness to measure, so no music-only master was made");
2670
+ else {
2671
+ const args = musicOnlyMasterArgs(out, here(music), durationSec, round2(FINISH_LUFS - lufs), paths.master);
2672
+ if (spawnSync("ffmpeg", args, { stdio: "inherit" }).status === 0) master = paths.master;
2673
+ else mixSound.notes.push("ffmpeg could not make the music-only master");
2674
+ }
2675
+ }
2676
+ soundRecord = {
2677
+ voiceLufs: mixSound.voiceLufs,
2678
+ effects: mixSound.effects,
2679
+ beds: mixSound.beds,
2680
+ ...(mixSound.music ? { music: mixSound.music } : {}),
2681
+ ...(master ? { master: path.basename(master) } : {}),
2682
+ notes: mixSound.notes,
2683
+ };
2684
+ writeFileSync(paths.sound, `${JSON.stringify(soundRecord, null, 2)}\n`);
2685
+ }
2686
+ const withSound = (cut) => (soundRecord ? { ...cut, sound: soundRecord, ...(soundRecord.master ? { musicOnlyMaster: paths.master } : {}) } : cut);
2687
+ if (!finish) return withSound({ out, durationSec, manifest, timeline, music, rows, sidecars });
2688
+
2689
+ // The pace record: the cut's own numbers, the settings it was cut with (so
2690
+ // a redo of the run matches), and its loudness once the pass ran.
2691
+ const pace = {
2692
+ ...paceReport({
2693
+ ourShots: scenes.map((p) => ({ start: p.startSec, end: roundMs(p.startSec + p.seconds) })),
2694
+ ...(sourceAd ? { srcShots: sourceAd.shots, srcSec: sourceAd.seconds } : {}),
2695
+ ourSec: durationSec,
2696
+ ourWords: rows.length,
2697
+ ourSilences: voicePauses(rows).filter((x) => x.s > 0.3 && x.e < durationSec - 0.3),
2698
+ speed,
2699
+ }),
2700
+ finishing: finish,
2701
+ ...(loudness ? { loudness } : {}),
2702
+ ...(notes.length > 0 ? { notes } : {}),
2703
+ };
2704
+ writeFileSync(paths.pace, `${JSON.stringify(pace, null, 2)}\n`);
2705
+ return withSound({ out, durationSec, manifest, timeline, music, rows, sidecars, pace });
1802
2706
  }
1803
2707
 
1804
2708
  function main() {
@@ -1840,6 +2744,15 @@ function main() {
1840
2744
  for (const line of edges) console.log(" " + line);
1841
2745
  }
1842
2746
  console.log(`Room tone: under the whole ad. Music bed: ${music ?? "none"}`);
2747
+ if (cut.pace) {
2748
+ const { pace } = cut;
2749
+ const loud = pace.loudness ? `, ${pace.loudness.lufs} LUFS, true peak ${pace.loudness.truePeak} dBTP` : "";
2750
+ console.log(
2751
+ `Pace: ${pace.cutsPerMin.ours} cuts a minute, ${pace.over4s.ours} shots over ${LONG_SHOT_SEC}s, ` +
2752
+ `${pace.deadAir.over03s} pauses over 0.3s, speed ${pace.speed}${loud} -> ${path.basename(finishPaths(out).pace)}`,
2753
+ );
2754
+ for (const note of pace.notes ?? []) console.log(` ${note}`);
2755
+ }
1843
2756
  console.log(`Upload it: exodus video upload ${manifest.runId} --file ${out}`);
1844
2757
  }
1845
2758