@aicopycoders/exodus 2026.11.700-beta.1 → 2026.11.1000-beta.2

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.
@@ -38,6 +38,10 @@
38
38
  // described above, byte for byte. The same holds for its sound settings, which
39
39
  // reach this file as the manifest's `sound` (see soundOf), and for a voice
40
40
  // that is one song laid whole, the manifest's `wholeVoice` (see wholeVoiceOf).
41
+ // Trusted template songs without a separate underlay meter the voice alone
42
+ // (bedLufs=null), without inventing a bed for cutChecks. Other doors are unchanged.
43
+ // Trusted unmapped narration requires actual moving coverage instead of the
44
+ // legacy cloned tail. Explicit still-fill and selected mapped holds stay distinct.
41
45
  import { createHash } from "node:crypto";
42
46
  import { execFileSync, spawnSync } from "node:child_process";
43
47
  import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
@@ -166,6 +170,17 @@ export const TRUE_PEAK_MAX_DB = -1;
166
170
  */
167
171
  export const PEAK_LIMITER = "aresample=192000,aformat=channel_layouts=stereo,alimiter=limit=0.8:level=false:attack=2:release=60";
168
172
 
173
+ /** Plan 14's on/off cut checks, each read only with cutChecks; paceCheck also carries its numbers. */
174
+ const PLAN_14_CHECKS = ["floorCheck", "flashCheck", "levelStepCheck", "voFitCheck", "dashGapCheck", "nameCheck"];
175
+ const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
176
+ /** paceCheck's numbers: PACE_BARS', each replaced by the manifest's own where it gives a number above 0 and at most 10. */
177
+ function paceBarsOf(raw) {
178
+ const own = isObject(raw) ? raw : {};
179
+ return Object.fromEntries(
180
+ PACE_BAR_KEYS.map((key) => [key, Number.isFinite(own[key]) && own[key] > 0 && own[key] <= 10 ? own[key] : PACE_BARS[key]]),
181
+ );
182
+ }
183
+
169
184
  /**
170
185
  * The manifest's finishing settings, or null when none is on. The worker
171
186
  * writes them from the run's frozen kind settings, so a redo of the run cuts
@@ -188,6 +203,13 @@ export function finishingOf(manifest) {
188
203
  ...(Number.isFinite(f.brollMinSec) && f.brollMinSec > 0 ? { brollMinSec: f.brollMinSec } : {}),
189
204
  ...(f.speedUpSlowBroll === true ? { speedUpSlowBroll: true } : {}),
190
205
  ...(f.cutChecks === true ? { cutChecks: true } : {}),
206
+ ...(f.cutChecks === true && f.blipCheck === true ? { blipCheck: true } : {}),
207
+ ...(f.cutChecks === true && f.freezeCheck === true ? { freezeCheck: true } : {}),
208
+ ...Object.fromEntries(PLAN_14_CHECKS.filter((key) => f.cutChecks === true && f[key] === true).map((key) => [key, true])),
209
+ ...(f.cutChecks === true && (f.paceCheck === true || isObject(f.paceCheck)) ? { paceCheck: paceBarsOf(f.paceCheck) } : {}),
210
+ // Plan 14: the end card from the pack photo, and the record of whether the brand reached the ad (brandOf).
211
+ ...(f.endCardLayers === true ? { endCardLayers: true } : {}),
212
+ ...(f.brandCheck === true ? { brandCheck: true } : {}),
191
213
  };
192
214
  if (!Object.values(on).some(Boolean) && Object.keys(added).length === 0) return null;
193
215
  const target = f.targetSeconds;
@@ -278,22 +300,35 @@ export function soundPlan(sound, timeline, storyboard, { levelled, measure }) {
278
300
  const counts = lineWordCounts(storyboard);
279
301
  const effects = [];
280
302
  for (const e of sound.effects) {
281
- const cue = cueOffsetOnSpine(spine, counts, e.cueLineId);
303
+ const timed = timeline.narratedShotList === true || [e.offsetSec, e.durationSec, e.volumeDb].some(Number.isFinite);
304
+ const heard = timed ? wordRows(timeline).filter((word) => word.lineId === e.cueLineId) : [];
305
+ const cue = timed
306
+ ? heard.length ? { startSec: Math.min(...heard.map((word) => word.start)) + (e.offsetSec ?? 0) } : null
307
+ : cueOffsetOnSpine(spine, counts, e.cueLineId);
282
308
  if (!cue) {
309
+ if (timed) throw new SourceMapRefusal("A timed sound has no actual canonical cue word", [
310
+ { severity: "refuse", code: "sound-cue-unmeasured", scene: null, detail: e.cueLineId },
311
+ ]);
283
312
  notes.push(`the sound cued to ${e.cueLineId} is left out: the cut does not speak that line`);
284
313
  continue;
285
314
  }
286
315
  const k = spine.findIndex((seg) => cue.startSec < seg.startSec + seg.seconds);
316
+ if (timed && (cue.startSec < 0 || cue.startSec >= pictureSec ||
317
+ (Number.isFinite(e.durationSec) && (!(e.durationSec > 0) || cue.startSec + e.durationSec > pictureSec + 1e-7)))) {
318
+ throw new SourceMapRefusal("An authored sound window lies outside the complete audio/picture clock", [
319
+ { severity: "refuse", code: "sound-cue-window", scene: null, detail: e.cueLineId },
320
+ ]);
321
+ }
287
322
  const under = Number.isFinite(e.underDb) ? e.underDb : SOUND_UNDER_DB;
288
323
  effects.push({
289
- ...(timeline.mapped ? e : {}),
324
+ ...(timeline.mapped || timed ? e : {}),
290
325
  file: e.file, cueLineId: e.cueLineId,
291
326
  // A mapped cut keeps the intended heard cue independent from the actual file placement.
292
- ...(timeline.mapped || e.source ? { plannedOnsetSec: cue.startSec } : {}),
293
- atSec: timeline.mapped
327
+ ...(timeline.mapped || timed || e.source ? { plannedOnsetSec: r6(cue.startSec) } : {}),
328
+ atSec: timeline.mapped || timed
294
329
  ? (Math.round(cue.startSec * EDIT_SAMPLE_RATE) - Math.round((e.source?.eventWindow?.fromSec ?? 0) * EDIT_SAMPLE_RATE)) / EDIT_SAMPLE_RATE
295
330
  : roundMs(Math.max(0, cue.startSec - SOUND_LEAD_SEC)),
296
- gainDb: gainUnder(segLufs[k] ?? voiceLufs, under, measure(e.file), SOUND_FALLBACK_DB),
331
+ gainDb: Number.isFinite(e.volumeDb) ? e.volumeDb : gainUnder(segLufs[k] ?? voiceLufs, under, measure(e.file), SOUND_FALLBACK_DB),
297
332
  });
298
333
  }
299
334
  const bedRef = heardVoice(voiceLufs) ? voiceLufs : FINISH_LUFS;
@@ -926,7 +961,42 @@ function finalWordTailScene(storyboard, beat) {
926
961
  return scenes.length === 1 ? scenes[0] : null;
927
962
  }
928
963
 
929
- export function scenePlans(storyboard, planningBudget = null) {
964
+ /**
965
+ * Flexible beats are quote capacity, not mandatory pictures. P2 validates the
966
+ * approved program's required jobs/prerequisites/order before persisting the
967
+ * board. Bind only its actual contiguous slots here; reservations are planning
968
+ * minima, never measured source or final clocks.
969
+ */
970
+ function selectedPictureJobs(manifest, board, budget = manifest?.planningBudget) {
971
+ if (manifest?.templateMotion?.enabled !== true || !budget?.pictureRange) return null;
972
+ const groups = new Map();
973
+ let seconds = 0;
974
+ for (const scene of board?.scenes ?? []) {
975
+ if (scene.broll || scene.kind === "cutaway") continue;
976
+ const n = scene.planPicture;
977
+ if (!Number.isInteger(n) || n < 1 || n > budget.pictureRange.max ||
978
+ !Number.isFinite(scene.planSeconds) || scene.planSeconds <= 0 ||
979
+ !(scene.durationSec >= scene.planSeconds)) {
980
+ throw new FirstCutError(1, `Scene ${scene.sceneIndex} has no valid selected picture reservation; quote capacity is not an edit plan`);
981
+ }
982
+ const owners = groups.get(n) ?? [];
983
+ owners.push(scene);
984
+ groups.set(n, owners);
985
+ seconds += scene.planSeconds;
986
+ }
987
+ const count = groups.size;
988
+ if (count < budget.pictureRange.min || count > budget.pictureRange.max ||
989
+ Math.abs(seconds - budget.targetSeconds) > 1e-6 ||
990
+ [...groups].some(([n, owners]) => n > count || owners.length > 2)) {
991
+ throw new FirstCutError(1, "The selected picture reservations must form the approved contiguous range and sum to its requested planning budget");
992
+ }
993
+ return Array.from({ length: count }, (_, i) => ({
994
+ id: budget.beats[i].id, owners: groups.get(i + 1),
995
+ reservedSeconds: groups.get(i + 1).reduce((sum, scene) => sum + scene.planSeconds, 0),
996
+ }));
997
+ }
998
+
999
+ export function scenePlans(storyboard, planningBudget = null, selectedJobs = null) {
930
1000
  const plans = new Map();
931
1001
  const suffixes = new Map();
932
1002
  for (const beat of planningBudget?.beats ?? []) {
@@ -958,7 +1028,12 @@ export function scenePlans(storyboard, planningBudget = null) {
958
1028
  suffixes.get(scene.sceneIndex) ?? 0,
959
1029
  ),
960
1030
  actionLeadSec: typeof scene.actionLeadSec === "number" && scene.actionLeadSec > 0 ? scene.actionLeadSec : 0,
961
- ...(planningBudget ? { editSeconds: reservation || null, overlay } : {}),
1031
+ ...(planningBudget ? {
1032
+ editSeconds: selectedJobs
1033
+ ? selectedJobs.find((job) => job.owners.includes(scene)) ? scene.planSeconds : null
1034
+ : reservation || null,
1035
+ overlay,
1036
+ } : {}),
962
1037
  sourceShot: Number.isInteger(scene.sourceShot) && scene.sourceShot > 0 ? scene.sourceShot : null,
963
1038
  broll:
964
1039
  hasText(scene.broll?.covers) && Number.isFinite(scene.broll?.plannedSec) && scene.broll.plannedSec > 0
@@ -1203,6 +1278,16 @@ function narrationEnd(voiceSeconds, voiceWords, endsAd, measured = false) {
1203
1278
  const holdFor = (seg) =>
1204
1279
  seg.source === "clip" ? Math.max(0, seg.seconds - Math.max(0, seg.clipSeconds - seg.inSec)) : 0;
1205
1280
 
1281
+ function refusePictureHolds(short) {
1282
+ if (short.length) throw new SourceMapRefusal(
1283
+ "The selected voice/action clock outlasts its available picture; supply real footage rather than frozen fill",
1284
+ short.map((seg) => ({
1285
+ severity: "refuse", code: "picture-hold", scene: seg.sceneIndex,
1286
+ detail: `${seg.file} needs ${seg.seconds}s, but only ${seg.seconds - seg.holdSec}s of retained source remains`,
1287
+ })),
1288
+ );
1289
+ }
1290
+
1206
1291
  /**
1207
1292
  * Put a reaction beat into the spine right after its cue line ends, splitting
1208
1293
  * the segment that speaks it when more lines follow in that segment. Returns
@@ -1356,11 +1441,15 @@ function selectedEditorialHold(manifest, storyboard, sceneIndex) {
1356
1441
  const scenes = storyboard?.scenes ?? [];
1357
1442
  const scene = scenes.at(-1);
1358
1443
  const hold = scene?.editorialHold;
1444
+ const selected = selectedPictureJobs(manifest, storyboard)?.at(-1);
1359
1445
  const beat = manifest.planningBudget?.beats?.at(-1);
1446
+ const bound = selected
1447
+ ? selected.owners.at(-1) === scene && scene.planSeconds === hold?.seconds
1448
+ : scene?.planningBeatIds?.includes(beat?.id) && beat?.picture?.seconds === hold?.seconds;
1360
1449
  if (scene?.sceneIndex !== sceneIndex || hold?.boundary !== "retained-end" ||
1361
1450
  !Number.isInteger(hold.sourceSceneIndex) || scenes.at(-2)?.sceneIndex !== hold.sourceSceneIndex ||
1362
1451
  !(hold.seconds > 0) || !Number.isFinite(hold.seconds) ||
1363
- !scene.planningBeatIds?.includes(beat?.id) || beat?.picture?.seconds !== hold.seconds) return null;
1452
+ !bound) return null;
1364
1453
  return hold;
1365
1454
  }
1366
1455
 
@@ -1403,6 +1492,51 @@ export function wholeVoiceOf(manifest, readJson, exists) {
1403
1492
  };
1404
1493
  }
1405
1494
 
1495
+ /** Read the existing pulled master/scene words before any finishing sound order.
1496
+ * This is the same clock consumed by firstCut, not a planned timing substitute. */
1497
+ export function narrationFinishClock({ dir, manifest, storyboard }) {
1498
+ const exists = (file) => typeof file === "string" && existsSync(path.join(dir, file));
1499
+ const readJson = (file) => {
1500
+ if (!exists(file)) return null;
1501
+ try { return JSON.parse(readFileSync(path.join(dir, file), "utf8")); } catch { return null; }
1502
+ };
1503
+ const refuse = (detail) => { throw new SourceMapRefusal("Approved continuous narration requires its actual canonical clock", [
1504
+ { severity: "refuse", code: "narration-clock-unmeasured", scene: null, detail },
1505
+ ]); };
1506
+ const whole = wholeVoiceOf(manifest, readJson, exists);
1507
+ if (!whole || manifest.sourceMap !== true || !storyboard) refuse("the approved whole narration, timing and measured cut are required");
1508
+ const cuts = readJson(manifest.narration.timing)?.cuts;
1509
+ const scenes = storyboard.scenes ?? [];
1510
+ if (!Array.isArray(cuts) || cuts.length !== scenes.length) refuse("every planned scene needs exactly one actual narration interval");
1511
+ const words = [];
1512
+ let end = 0;
1513
+ for (const [index, scene] of scenes.entries()) {
1514
+ const take = manifest.scenes.find((entry) => entry.sceneIndex === scene.sceneIndex);
1515
+ const interval = cuts[index];
1516
+ const local = readJson(take?.words);
1517
+ if (interval.sceneIndex !== scene.sceneIndex || !Number.isFinite(interval.startSec) ||
1518
+ interval.startSec < end - 1e-6 || !Number.isFinite(interval.durationSec) || interval.durationSec <= 0 ||
1519
+ take?.wordsFrom !== "voice" || !exists(take.voice) || !Array.isArray(local) || !local.length) {
1520
+ refuse(`scene ${scene.sceneIndex} lacks its ordered actual audio/word interval`);
1521
+ }
1522
+ const ids = [];
1523
+ let lastEnd = 0;
1524
+ for (const word of local) {
1525
+ if (!hasText(word.w) || !hasText(word.lineId) || !scene.lineIds?.includes(word.lineId) ||
1526
+ !Number.isFinite(word.s) || !Number.isFinite(word.e) || word.s < lastEnd ||
1527
+ word.e <= word.s || word.e > interval.durationSec + 1e-6) refuse(`scene ${scene.sceneIndex} has an invalid or untagged actual word`);
1528
+ if (ids.at(-1) !== word.lineId) ids.push(word.lineId);
1529
+ lastEnd = word.e;
1530
+ words.push({ ...word, s: r6(word.s + interval.startSec), e: r6(word.e + interval.startSec) });
1531
+ }
1532
+ if (ids.length !== scene.lineIds.length || ids.some((id, i) => id !== scene.lineIds[i])) refuse(`scene ${scene.sceneIndex} is missing canonical line coverage`);
1533
+ end = interval.startSec + interval.durationSec;
1534
+ }
1535
+ const audio = clockOf(measureMedia(path.join(dir, whole.file))).audio;
1536
+ if (!audio || end > audio.decodedSec + 1 / EDIT_SAMPLE_RATE) refuse("narration intervals exceed the decoded approved master EOF");
1537
+ return { durationSec: audio.decodedSec, words, cuts };
1538
+ }
1539
+
1406
1540
  const roundFrame = (t) => Math.round(t * FPS) / FPS;
1407
1541
 
1408
1542
  /**
@@ -1453,6 +1587,15 @@ export function fitWholeVoice(spine, whole) {
1453
1587
  seg.seconds = to - from - given;
1454
1588
  seg.holdSec = holdFor(seg);
1455
1589
  seg.wholeVoice = true;
1590
+ if (!whole.measured) {
1591
+ // Canonical tagged words belong to the actual song-cut start, not the
1592
+ // rounded picture boundary. Leave legacy unowned word records unchanged.
1593
+ const shift = whole.starts.get(seg.sceneIndex) - from;
1594
+ if (shift && seg.words?.some((word) => hasText(word.lineId))) {
1595
+ seg.words = seg.words.map((word) => hasText(word.lineId)
1596
+ ? { ...word, s: word.s + shift, e: word.e + shift } : word);
1597
+ }
1598
+ }
1456
1599
  seg.note = withHow(seg.note, [`on the whole voice from ${from.toFixed(2)}s to ${to.toFixed(2)}s`]);
1457
1600
  if (whole.measured) {
1458
1601
  const at = whole.starts.get(seg.sceneIndex);
@@ -1778,7 +1921,8 @@ function snapToFrames(spine) {
1778
1921
  }
1779
1922
  const words = seg.words ?? [];
1780
1923
  const lastEnd = words.length ? Math.max(...words.map(wordEnd)) : 0;
1781
- const needed = lastEnd > 0 ? Math.ceil(lastEnd * FPS - 1e-6) : 0;
1924
+ // A song word may continue over a picture cut; the master is never split.
1925
+ const needed = !seg.songOnly && lastEnd > 0 ? Math.ceil(lastEnd * FPS - 1e-6) : 0;
1782
1926
  let end = Math.max(Math.round(exact * FPS), frame + needed, frame + 1);
1783
1927
  // A stretch of a whole voice has the song's length, not the clip's: short picture there is a hold, refused.
1784
1928
  const fixed = seg.wholeVoice || seg.fixedLength;
@@ -1989,10 +2133,15 @@ export function buildSourceMap({ spine, cutaways, scriptLines, wholeVoice = null
1989
2133
  }
1990
2134
  }
1991
2135
  }
2136
+ // Song captions belong to the continuous master's clock, not the picture
2137
+ // piece they happen to accompany; still refuse words past that audio EOF.
2138
+ const wordLimit = seg.songOnly && wholeVoice?.clock?.audio
2139
+ ? wholeVoice.startFrame + Math.ceil(wholeVoice.clock.audio.decodedSec * FPS - 1e-6)
2140
+ : startFrame + frames;
1992
2141
  for (const p of spoken) {
1993
2142
  if (p.local.startSec < -1e-6) flag("refuse", "word-before-piece", seg, `${p.part} starts ${r6(-p.local.startSec)}s before its piece`);
1994
- if (p.edit.lastFrame >= startFrame + frames) {
1995
- flag("refuse", "word-past-piece", seg, `${p.part}'s last word ends on frame ${p.edit.lastFrame}, after its piece's ${startFrame + frames - 1}`);
2143
+ if (p.edit.lastFrame >= wordLimit) {
2144
+ flag("refuse", seg.songOnly ? "word-past-audio-eof" : "word-past-piece", seg, `${p.part}'s last word ends on frame ${p.edit.lastFrame}, after its ${seg.songOnly ? "song master" : "piece"}'s ${wordLimit - 1}`);
1996
2145
  }
1997
2146
  }
1998
2147
  const ev = seg.actionEvidence ?? null;
@@ -2202,13 +2351,26 @@ export function buildTimeline({
2202
2351
  const heard = readWords;
2203
2352
  readWords = (file) => withPhonemeEnds(heard(file));
2204
2353
  }
2205
- const plans = scenePlans(storyboard, measure ? manifest.planningBudget : null);
2354
+ const selectedJobs = selectedPictureJobs(manifest, storyboard);
2355
+ // Narrated template reservations own picture time, not the short master read.
2356
+ // Reuse the pulled per-scene slices of that same approved take, placing each
2357
+ // at its picture start and leaving the remaining picture air to the music bed.
2358
+ const reservedNarration = Boolean(wholeVoice && selectedJobs && manifest?.templateMotion?.narratedShotList === true);
2359
+ const nativeTextBudget = !measure && manifest?.templateMotion?.enabled === true &&
2360
+ (storyboard?.audio?.captionStyle || storyboard?.overlays?.some((overlay) => overlay.render))
2361
+ ? manifest.planningBudget : null;
2362
+ const plans = scenePlans(storyboard, measure || selectedJobs ? manifest.planningBudget : nativeTextBudget, selectedJobs);
2206
2363
  const counts = lineWordCounts(storyboard);
2207
2364
  const spine = [];
2208
2365
  const cutaways = [];
2209
2366
  const waiting = [];
2210
2367
  const dropped = [];
2211
2368
  const missing = new Set(scenesWithNoClip({ manifest, storyboard, skip, exists }));
2369
+ // Frozen template-video audio ownership, supplied by the worker from the
2370
+ // run's voiceMode/songAsVoice. Discard applies to EVERY picture, including
2371
+ // wordless inserts and reactions; a stray voice file cannot replace native dialogue.
2372
+ const discardClipAudio = manifest?.clipAudio === "discard";
2373
+ const keepClipAudio = manifest?.clipAudio === "keep";
2212
2374
  const filled = new Set();
2213
2375
  // A narrated segment is first fitted as a join; the one that ends the ad is
2214
2376
  // refitted once the spine is known.
@@ -2232,7 +2394,7 @@ export function buildTimeline({
2232
2394
  "The selected editorial hold has no measured preceding picture", [
2233
2395
  { severity: "refuse", code: "editorial-hold-source-unproven", scene: n, detail: "hold binding cannot use an absent, skipped or unrelated source" },
2234
2396
  ]);
2235
- const voice = exists(scene.voice) ? scene.voice : null;
2397
+ const voice = !keepClipAudio && exists(scene.voice) ? scene.voice : null;
2236
2398
  const vm = voice ? clockOf(measure(voice)) : null;
2237
2399
  spine.push({
2238
2400
  sceneIndex: n, source: "clip", file: source.file, clipSeconds: editorialHold.seconds, inSec: 0,
@@ -2255,7 +2417,7 @@ export function buildTimeline({
2255
2417
  const m = measure ? clockOf(measure(scene.clip)) : null;
2256
2418
  waiting.push({
2257
2419
  sceneIndex: n, source: "clip", file: scene.clip,
2258
- seconds: m ? (m.picture ? m.picture.endSlot / FPS : 0) : clip.seconds, hasAudio: clip.hasAudio, ...cue,
2420
+ seconds: m ? (m.picture ? m.picture.endSlot / FPS : 0) : clip.seconds, hasAudio: clip.hasAudio && !discardClipAudio, ...cue,
2259
2421
  ...(m ? { map: m } : {}),
2260
2422
  });
2261
2423
  } else if (stillFill && exists(scene.keyframe)) {
@@ -2270,8 +2432,8 @@ export function buildTimeline({
2270
2432
  continue;
2271
2433
  }
2272
2434
 
2273
- const narrated = plan.narrated || scene.wordsFrom === "voice";
2274
- const voice = exists(scene.voice) ? scene.voice : null;
2435
+ const narrated = !keepClipAudio && (discardClipAudio || plan.narrated || scene.wordsFrom === "voice");
2436
+ const voice = !keepClipAudio && exists(scene.voice) ? scene.voice : null;
2275
2437
  const flag = scene.flagged ? " (flagged)" : "";
2276
2438
  if (measure && scene.words && scene.wordsDescribe === "original-performance") {
2277
2439
  throw new SourceMapRefusal("The word observation describes a replaced performance, not the selected audible bytes", [
@@ -2385,11 +2547,38 @@ export function buildTimeline({
2385
2547
  const laid = wholeClock
2386
2548
  ? { ...wholeVoice, declaredSec: wholeVoice.seconds, seconds: wholeClock.audio ? wholeClock.audio.decodedSec : wholeVoice.seconds, measured: true }
2387
2549
  : wholeVoice;
2388
- const whole = laid ? fitWholeVoice(spine, laid) : null;
2550
+ const whole = laid && !reservedNarration ? fitWholeVoice(spine, laid) : null;
2389
2551
  if (whole) dropped.push(...whole.dropped);
2552
+ if (whole && (manifest?.templateMotion?.narratedShotList === true ||
2553
+ (manifest?.templateMotion?.enabled === true && laid.measured))) {
2554
+ // Picture cuts (including the song's 150ms lead-in) round to frames;
2555
+ // captions remain on the untouched master word clock, not that rounded cut.
2556
+ for (const seg of spine) {
2557
+ if (!seg.wholeVoice) continue;
2558
+ const shift = wholeVoice.starts.get(seg.sceneIndex) - whole.intervals.find((interval) => interval.scene === seg.sceneIndex).fromFrame / FPS;
2559
+ if (seg.words) seg.words = seg.words.map((word) => ({ ...word, s: r6(word.s + shift), e: r6(word.e + shift) }));
2560
+ if (manifest?.templateMotion?.enabled === true && manifest.templateMotion.narratedShotList !== true) seg.songOnly = true;
2561
+ }
2562
+ }
2563
+ // Keeping a trusted song's real EOF never authorizes invented terminal
2564
+ // frames. The mapped route already refuses unavailable picture; refuse the
2565
+ // same shortage before fitWholeVoice's legacy holdSec can become tpad.
2566
+ if (whole && !measure && manifest?.templateMotion?.enabled === true) {
2567
+ const short = spine.filter((seg) => seg.wholeVoice && seg.holdSec > 1e-6);
2568
+ if (short.length) throw new SourceMapRefusal(
2569
+ "The whole song outlasts its available picture; supply enough real footage without shortening the song or freezing its ending",
2570
+ short.map((seg) => ({
2571
+ severity: "refuse", code: "whole-voice-picture-short", scene: seg.sceneIndex,
2572
+ detail: `${seg.file} needs ${seg.seconds}s to retain the whole song, but only ${seg.seconds - seg.holdSec}s of real picture remains`,
2573
+ })),
2574
+ );
2575
+ }
2390
2576
 
2391
2577
  const sameShot = PUNCH_IN_RUNS.get(manifest?.shots?.pacing);
2392
- if (sameShot) markPunchIns(spine, sameShot);
2578
+ // Trusted selected capture owns framing. Temporal gap cuts may still run,
2579
+ // but cannot invent a spatial reframe of the approved source.
2580
+ const preserveCapture = manifest?.templateMotion?.enabled === true && manifest.templateMotion.selectedCapture === true;
2581
+ if (sameShot && !preserveCapture) markPunchIns(spine, sameShot);
2393
2582
 
2394
2583
  const noCue = (c) =>
2395
2584
  c.cueLineId ? `${c.cueLineId} is spoken by no scene in the cut` : "the storyboard names no cue line";
@@ -2401,6 +2590,25 @@ export function buildTimeline({
2401
2590
  const reactionCues = new Set(waiting.filter((w) => w.reaction).map((w) => w.cueLineId));
2402
2591
  const last = spine.at(-1);
2403
2592
  if (!whole && refits.has(last) && !reactionCues.has(last.lineIds.at(-1))) Object.assign(last, refits.get(last)(true));
2593
+ if (manifest?.templateMotion?.enabled === true && (measure || selectedJobs || nativeTextBudget)) {
2594
+ // Check the original native clock before join edits can discard a word
2595
+ // wholly beyond EOF. Planning seconds may shrink; spoken/action proof may not.
2596
+ for (const seg of spine) {
2597
+ if (seg.source !== "clip" || seg.audio !== "clip" || seg.wholeVoice ||
2598
+ !(plans.get(seg.sceneIndex)?.editSeconds > 0)) continue;
2599
+ const required = Math.max(0, ...(seg.words ?? []).map(wordEnd)) + (seg.actionTailSec ?? 0);
2600
+ const holdSec = Math.max(0, required - seg.clipSeconds);
2601
+ if (holdSec > 0) refusePictureHolds([{ ...seg, seconds: required, holdSec }]);
2602
+ }
2603
+ }
2604
+ if (nativeTextBudget) {
2605
+ // A selected physical scene reservation is not expendable dialogue air.
2606
+ // Keep its source intact before gap/join edits; the reservation pass below
2607
+ // fits retained footage and still refuses a source-short/frozen result.
2608
+ for (const seg of spine) {
2609
+ if (seg.audio === "clip" && !seg.wholeVoice && plans.get(seg.sceneIndex)?.editSeconds > 0) seg.fixedLength = true;
2610
+ }
2611
+ }
2404
2612
  const pace = finish?.cutPace === true;
2405
2613
  const lineGap = pace ? { max: PACE_SPLIT_PAUSE_SEC, keep: PACE_GAP_MAX_SEC } : null;
2406
2614
  tightenSentenceGaps(spine, reactionCues, scriptLines, readLevels, lineGap);
@@ -2409,8 +2617,10 @@ export function buildTimeline({
2409
2617
  // The pieces of one take alternate between the plain frame and the
2410
2618
  // punch-in, and a jump-cut run's alternation carries on across them.
2411
2619
  splitAtPauses(spine);
2412
- for (const seg of spine) delete seg.punchIn;
2413
- markPunchIns(spine, (prev, seg) => samePiece(prev, seg) || Boolean(sameShot?.(prev, seg)));
2620
+ if (!preserveCapture) {
2621
+ for (const seg of spine) delete seg.punchIn;
2622
+ markPunchIns(spine, (prev, seg) => samePiece(prev, seg) || Boolean(sameShot?.(prev, seg)));
2623
+ }
2414
2624
  }
2415
2625
  for (const c of waiting.filter((w) => w.reaction)) {
2416
2626
  if (whole) {
@@ -2443,7 +2653,7 @@ export function buildTimeline({
2443
2653
  seg.note = withHow(seg.note, [`its source shot's ${len.toFixed(2)}s`]);
2444
2654
  }
2445
2655
  }
2446
- if (measure) {
2656
+ if (measure || selectedJobs || nativeTextBudget) {
2447
2657
  const takes = new Map();
2448
2658
  for (const seg of spine) {
2449
2659
  const same = takes.get(seg.sceneIndex) ?? [];
@@ -2460,14 +2670,50 @@ export function buildTimeline({
2460
2670
  // Reserve once per take. Earlier pieces must stop at their split: extending
2461
2671
  // one would play source speech that the resumed piece will play again.
2462
2672
  const before = pieces.slice(0, -1).reduce((sum, piece) => sum + piece.seconds, 0);
2673
+ if (manifest?.templateMotion?.enabled === true && seg.source === "clip" && seg.audio === "clip") {
2674
+ // A full-picture reservation can need the real pre-speech frames
2675
+ // trimJoins removed. Restore those before asking for frozen fill,
2676
+ // without replaying source already used by an earlier split piece.
2677
+ const previous = pieces.at(-2);
2678
+ const earliest = previous ? previous.inSec + previous.seconds : 0;
2679
+ const inSec = Math.min(seg.inSec, Math.max(earliest, floorFrame(seg.clipSeconds - (ceilFrame(reserved) - before))));
2680
+ const shift = seg.inSec - inSec;
2681
+ if (shift > 0) {
2682
+ seg.inSec = inSec;
2683
+ seg.words = seg.words?.map((word) => ({ ...word, s: word.s + shift, e: word.e + shift }));
2684
+ seg.note = withHow(seg.note, [`pre-speech ${shift.toFixed(2)}s restored for the reservation`]);
2685
+ }
2686
+ }
2463
2687
  const lastWord = Math.max(0, ...(seg.words ?? []).map(wordEnd));
2464
- seg.seconds = Math.max(pieces.length > 1 ? seg.seconds : 0,
2465
- ceilFrame(reserved) - before, ceilFrame(lastWord + (seg.actionTailSec ?? 0)));
2688
+ const nativePicture = manifest?.templateMotion?.enabled === true && seg.source === "clip" && seg.audio === "clip";
2689
+ const available = Math.max(0, seg.clipSeconds - seg.inSec);
2690
+ const pictureReservation = nativePicture
2691
+ ? Math.min(ceilFrame(reserved) - before, available)
2692
+ : reservedNarration && seg.source === "clip" && seg.audio === "narration"
2693
+ ? Math.min(ceilFrame(reserved) - before, floorFrame(available))
2694
+ : ceilFrame(reserved) - before;
2695
+ const required = lastWord + (seg.actionTailSec ?? 0);
2696
+ // Frame rounding is editorial air, not missing speech/action. Retain an
2697
+ // in-bounds native ending even when EOF falls between edit frames.
2698
+ const proofEnd = nativePicture && required <= available
2699
+ ? Math.min(ceilFrame(required), available) : ceilFrame(required);
2700
+ const narrationAir = reservedNarration && seg.audio === "narration"
2701
+ ? Math.min(seg.seconds, floorFrame(Math.max(0, seg.clipSeconds - seg.inSec))) : 0;
2702
+ seg.seconds = Math.max(pieces.length > 1 ? seg.seconds : narrationAir,
2703
+ pictureReservation, proofEnd);
2466
2704
  seg.fixedLength = true;
2467
2705
  seg.holdSec = holdFor(seg); // the measured map refuses source-short/frozen coverage
2468
2706
  }
2469
2707
  }
2470
2708
  }
2709
+ // Trusted narration may not manufacture frozen coverage, even without a
2710
+ // selected picture reservation. Explicit stills and mapped editorial holds
2711
+ // keep their own routes; whole-song shortages were checked above.
2712
+ if (!measure && manifest?.templateMotion?.enabled === true) {
2713
+ const short = spine.filter((seg) =>
2714
+ seg.source === "clip" && !seg.wholeVoice && !seg.editorialHold && seg.holdSec > 0);
2715
+ refusePictureHolds(short);
2716
+ }
2471
2717
  if (measure) snapToFrames(spine);
2472
2718
  let total = 0;
2473
2719
  for (const seg of spine) {
@@ -2588,12 +2834,110 @@ export function buildTimeline({
2588
2834
  spine,
2589
2835
  cutaways,
2590
2836
  left: dropped.sort((a, b) => a.n - b.n).map((d) => d.reason),
2591
- ...(whole ? { wholeVoice: { file: wholeVoice.file, startSec: wholeStartSec, ...(wholeClock ? { clock: wholeClock } : {}) } } : {}),
2837
+ ...(whole ? { wholeVoice: {
2838
+ file: wholeVoice.file, startSec: wholeStartSec, ...(wholeClock ? { clock: wholeClock } : {}),
2839
+ ...(manifest?.templateMotion?.enabled === true && manifest.templateMotion.narratedShotList !== true ? { songOnly: true } : {}),
2840
+ } } : {}),
2592
2841
  ...(groups ? { sourceGroups: groups } : {}),
2842
+ ...(manifest?.templateMotion?.narratedShotList === true ? { narratedShotList: true } : {}),
2843
+ ...(reservedNarration ? { reservedNarration: true } : {}),
2593
2844
  ...(sourceMap ? { mapped: true, sourceMap } : {}),
2594
2845
  };
2595
2846
  }
2596
2847
 
2848
+ /**
2849
+ * Enabled only for a server-stamped template launch. These selected obligations
2850
+ * require preservation, not proof that an action succeeded or permission to
2851
+ * freeze contact, completion or a living tail.
2852
+ */
2853
+ function protectTemplateMotion(timeline, manifest, board) {
2854
+ if (manifest.templateMotion?.enabled !== true) return;
2855
+ const protection = new Map();
2856
+ const selectedJobs = selectedPictureJobs(manifest, board);
2857
+ for (const scene of board?.scenes ?? []) {
2858
+ const evidence = {};
2859
+ const phases = (scene.actionPhases ?? []).filter((p) => p.required === true && hasText(p.id) &&
2860
+ ["contact", "completion", "interior-hold", "living-tail"].includes(p.kind) && p.window);
2861
+ if (phases.length) evidence.actionPhases = phases;
2862
+ if (scene.actionLeadSec > 0) evidence.actionLeadSec = scene.actionLeadSec;
2863
+ if (scene.actionTailSec > 0) evidence.actionTailSec = scene.actionTailSec;
2864
+ const beats = (manifest.planningBudget?.beats ?? []).filter((b) => scene.planningBeatIds?.includes(b.id) &&
2865
+ (b.picture || b.silent));
2866
+ if (beats.length) evidence.planningBeats = beats;
2867
+ const selected = selectedJobs?.find((job) => job.owners.includes(scene));
2868
+ if (selected) evidence.selectedPicture = {
2869
+ id: selected.id, job: scene.planJob ?? null, seconds: scene.planSeconds,
2870
+ action: scene.subjectAction || scene.videoPrompt,
2871
+ };
2872
+ const facts = (board.protectedVisualConstraints ?? []).find((c) => c.sceneIndex === scene.sceneIndex)?.facts
2873
+ ?.filter((f) => f.subject === "action" && f.kind === "fact" && hasText(f.quote) && f.source);
2874
+ if (facts?.length) evidence.approvedActions = facts;
2875
+ const obligations = board.mechanismPlan?.kind === "required" ? board.mechanismPlan.obligations : [];
2876
+ const mechanism = (scene.mechanismCoverage ?? []).flatMap((coverage) => {
2877
+ const obligation = obligations.find((o) => o.id === coverage.obligationId);
2878
+ return obligation && coverage.phases?.length ? [{ coverage, obligation }] : [];
2879
+ });
2880
+ if (mechanism.length) evidence.mechanism = mechanism;
2881
+ const listener = manifest.templateMotion.listeners?.find((s) => s.sceneIndex === scene.sceneIndex);
2882
+ if (listener) evidence.listener = listener;
2883
+ if (scene.cutawayType === "reaction") evidence.reaction = { cueLineId: scene.cueLineId };
2884
+ const terminal = manifest.planningBudget?.beats?.at(-1);
2885
+ if (manifest.outputPermission?.ending?.mode === "native" &&
2886
+ (selectedJobs ? selectedJobs.at(-1)?.owners.at(-1) === scene
2887
+ : terminal ? scene.planningBeatIds?.includes(terminal.id) : scene === board.scenes.at(-1))) {
2888
+ evidence.ending = manifest.outputPermission.ending;
2889
+ }
2890
+ if (Object.keys(evidence).length) protection.set(scene.sceneIndex, evidence);
2891
+ }
2892
+ for (const segment of [...timeline.spine, ...timeline.cutaways]) {
2893
+ const evidence = protection.get(segment.sceneIndex);
2894
+ if (evidence) segment.motionProtection = evidence;
2895
+ }
2896
+ }
2897
+
2898
+ /** Final-clock locations for the existing probe, with only localized stillness
2899
+ * permissions. A quiet picture cannot excuse missing audible words underneath. */
2900
+ function templateMotionReport(timeline, board, speed) {
2901
+ const segments = [...timeline.cutaways, ...timeline.spine];
2902
+ const shots = sceneTimes(timeline).map((shot) => {
2903
+ const segment = segments.find((s) =>
2904
+ s.sceneIndex === shot.scene && s.startSec <= shot.startSec + 0.001 &&
2905
+ s.startSec + s.seconds >= shot.startSec + shot.seconds - 0.001);
2906
+ return {
2907
+ sceneIndex: shot.scene, startSec: shot.startSec / speed, seconds: shot.seconds / speed,
2908
+ file: segment?.file ?? null, speed: (segment?.speed ?? 1) * speed,
2909
+ ...(segment?.motionProtection ? { protection: segment.motionProtection } : {}),
2910
+ };
2911
+ });
2912
+ const holds = [];
2913
+ const addHold = (sceneIndex, startSec, endSec, basis) => {
2914
+ for (const shot of shots.filter((s) => s.sceneIndex === sceneIndex)) {
2915
+ const start = Math.max(startSec / speed, shot.startSec);
2916
+ const end = Math.min(endSec / speed, shot.startSec + shot.seconds);
2917
+ if (!(end > start)) continue;
2918
+
2919
+ for (const seg of timeline.spine) {
2920
+ const from = Math.max(start, seg.startSec / speed);
2921
+ const to = Math.min(end, (seg.startSec + seg.seconds) / speed);
2922
+ if (to > from) holds.push({ sceneIndex, startSec: from, seconds: to - from, basis,
2923
+ silent: !timeline.wholeVoice && seg.audio === "silence" });
2924
+ }
2925
+ }
2926
+ };
2927
+ for (const seg of timeline.spine) {
2928
+ if (seg.editorialHold?.sourceFrame !== undefined) {
2929
+ addHold(seg.sceneIndex, seg.startSec, seg.startSec + seg.seconds, "selected-retained-frame");
2930
+ }
2931
+ }
2932
+ const temporal = resolveTemporalRequirements({ timeline }, board ?? {});
2933
+ for (const requirement of temporal.requirements) {
2934
+ if (requirement.kind !== "interior-hold") continue;
2935
+ const owner = temporal.owners.find((o) => o.id === requirement.id);
2936
+ if (owner) addHold(owner.sceneIndex, requirement.startSec, requirement.endSec, requirement.id);
2937
+ }
2938
+ return { shots, holds };
2939
+ }
2940
+
2597
2941
  /**
2598
2942
  * brollMinSec: a cutaway on screen for less than `minSec` of the finished ad
2599
2943
  * (its time in the cut over `speed`) is left out, and the shot before it plays
@@ -2606,13 +2950,13 @@ export function holdBroll(timeline, minSec, speed, clipSeconds) {
2606
2950
  const left = [];
2607
2951
  for (const c of timeline.cutaways) {
2608
2952
  const shown = c.seconds / speed;
2609
- if (shown >= minSec - 1e-6) {
2953
+ if (c.motionProtection || shown >= minSec - 1e-6) {
2610
2954
  kept.push(c);
2611
2955
  continue;
2612
2956
  }
2613
2957
  const why = `cutaway ${c.sceneIndex}: ${shown.toFixed(2)}s on screen, under the ${minSec}s B-roll minimum, so it is left out and`;
2614
2958
  const before = kept.at(-1);
2615
- if (before && Math.abs(before.startSec + before.seconds - c.startSec) < 0.5 / FPS) {
2959
+ if (before && !before.motionProtection && Math.abs(before.startSec + before.seconds - c.startSec) < 0.5 / FPS) {
2616
2960
  before.seconds = roundMs(c.startSec + c.seconds - before.startSec);
2617
2961
  if (c.cutShortBy) before.cutShortBy = c.cutShortBy;
2618
2962
  else delete before.cutShortBy;
@@ -2650,7 +2994,7 @@ export function speedUpSlowBroll(timeline, motionOf, clipSeconds) {
2650
2994
  const notes = [];
2651
2995
  for (const slow of slowShots(measured)) {
2652
2996
  const c = slow.cutaway;
2653
- if (!c) continue;
2997
+ if (!c || c.motionProtection) continue;
2654
2998
  const have = clipSeconds(c.file);
2655
2999
  if (!(have > c.seconds + 0.05)) {
2656
3000
  notes.push(`cutaway ${c.sceneIndex} barely moves, but its clip (${have.toFixed(2)}s) is no longer than its time on screen (${c.seconds.toFixed(2)}s), so it plays as it is`);
@@ -2926,6 +3270,27 @@ export function buildFfmpegArgs(timeline, { resolve, music, out, finish = null,
2926
3270
  );
2927
3271
  videoLabel = `pin${j}`;
2928
3272
  });
3273
+ // endCardLayers: the brand's ground over the whole frame, then the pack photo in its box, pushing in slowly.
3274
+ if (finish?.endCard) {
3275
+ const e = finish.endCard;
3276
+ const box = END_CARD_PHOTO_BOX;
3277
+ const w = even((box[2] - box[0]) * WIDTH);
3278
+ const h = even((box[3] - box[1]) * HEIGHT);
3279
+ const frames = Math.max(1, Math.round(e.seconds * FPS));
3280
+ const k = addInput("-loop", "1", "-t", sec(e.seconds), "-i", resolve(e.file));
3281
+ filters.push(`color=c=0x${e.colors.background}:s=${WIDTH}x${HEIGHT}:r=${FPS}:d=${sec(e.out)},format=yuv420p[ecground]`);
3282
+ filters.push(`[${videoLabel}][ecground]overlay=enable=between(t\\,${sec(e.in)}\\,${sec(e.out)}):eof_action=pass[ecg]`);
3283
+ filters.push(
3284
+ `[${k}:v]fps=${FPS},scale=${w}:${h}:force_original_aspect_ratio=decrease,` +
3285
+ `pad=${w}:${h}:(ow-iw)/2:(oh-ih)/2:color=0x${e.colors.background},setsar=1,` +
3286
+ `zoompan=z=1+${END_CARD_PUSH_IN}*on/${frames}:x=iw/2-(iw/zoom/2):y=ih/2-(ih/zoom/2):d=1:s=${w}x${h}:fps=${FPS},` +
3287
+ `format=yuv420p,fade=t=in:st=0:d=${END_CARD_PHOTO_FADE_SEC},setpts=PTS-STARTPTS+${sec(e.in)}/TB[ecpic]`,
3288
+ );
3289
+ filters.push(
3290
+ `[ecg][ecpic]overlay=x=${Math.round(box[0] * WIDTH)}:y=${Math.round(box[1] * HEIGHT)}:enable=between(t\\,${sec(e.in)}\\,${sec(e.out)}):eof_action=pass[ecard]`,
3291
+ );
3292
+ videoLabel = "ecard";
3293
+ }
2929
3294
  if (finish?.ass) {
2930
3295
  filters.push(`[${videoLabel}]subtitles=filename=${finish.ass}[burned]`);
2931
3296
  videoLabel = "burned";
@@ -2952,7 +3317,7 @@ export function buildFfmpegArgs(timeline, { resolve, music, out, finish = null,
2952
3317
  );
2953
3318
  beds.push(`[set${j}]`);
2954
3319
  });
2955
- } else if (!timeline.mapped || !timeline.wholeVoice) {
3320
+ } else if ((!timeline.mapped || !timeline.wholeVoice) && !timeline.wholeVoice?.songOnly) {
2956
3321
  filters.push(timeline.mapped
2957
3322
  ? `${ROOM_TONE},atrim=end_sample=${Math.round(pictureSec * EDIT_SAMPLE_RATE)}[room]`
2958
3323
  : `${ROOM_TONE}[room]`);
@@ -2964,8 +3329,9 @@ export function buildFfmpegArgs(timeline, { resolve, music, out, finish = null,
2964
3329
  const atSample = Math.round(e.atSec * EDIT_SAMPLE_RATE);
2965
3330
  const skip = Math.max(0, -atSample);
2966
3331
  const delay = Math.max(0, atSample);
2967
- filters.push(timeline.mapped
2968
- ? `[${k}:a]${stereo},asetpts=PTS-STARTPTS,atrim=start_sample=${skip + (e.sourcePrimingSamples ?? 0)},asetpts=PTS-STARTPTS,volume=${e.gainDb}dB,adelay=delays=${delay}S:all=1[sfx${j}]`
3332
+ const endSample = Number.isFinite(e.durationSec) ? skip + (e.sourcePrimingSamples ?? 0) + Math.round(e.durationSec * EDIT_SAMPLE_RATE) : null;
3333
+ filters.push(timeline.mapped || Number.isFinite(e.durationSec) || Number.isFinite(e.offsetSec)
3334
+ ? `[${k}:a]${stereo},asetpts=PTS-STARTPTS,atrim=start_sample=${skip + (e.sourcePrimingSamples ?? 0)}${endSample === null ? "" : `:end_sample=${endSample}`},asetpts=PTS-STARTPTS,volume=${e.gainDb}dB,adelay=delays=${delay}S:all=1[sfx${j}]`
2969
3335
  : `[${k}:a]${stereo},volume=${e.gainDb}dB,adelay=delays=${Math.round(e.atSec * 1000)}:all=1[sfx${j}]`);
2970
3336
  beds.push(`[sfx${j}]`);
2971
3337
  });
@@ -2974,11 +3340,13 @@ export function buildFfmpegArgs(timeline, { resolve, music, out, finish = null,
2974
3340
  if (music) {
2975
3341
  const m = addInput("-stream_loop", "-1", "-i", music);
2976
3342
  const duck = clean ? null : sound?.music?.duck;
3343
+ const fades = timeline.narratedShotList
3344
+ ? `,afade=t=in:d=${Math.min(1, pictureSec)},afade=t=out:st=${sec(Math.max(0, pictureSec - 3))}:d=${Math.min(3, pictureSec)}` : "";
2977
3345
  if (duck) {
2978
3346
  filters.push("[voice]asplit=2[voiced][duckkey]");
2979
3347
  filters.push(timeline.mapped
2980
- ? `[${m}:a]${stereo},asetpts=PTS-STARTPTS,atrim=start_sample=${timeline.musicPrimingSamples ?? 0}:end_sample=${Math.round(pictureSec * EDIT_SAMPLE_RATE) + (timeline.musicPrimingSamples ?? 0)},asetpts=PTS-STARTPTS,volume=${sound.music.gainDb}dB[bedraw]`
2981
- : `[${m}:a]${stereo},atrim=0:${pictureSec.toFixed(6)},asetpts=PTS-STARTPTS,volume=${sound.music.gainDb}dB[bedraw]`);
3348
+ ? `[${m}:a]${stereo},asetpts=PTS-STARTPTS,atrim=start_sample=${timeline.musicPrimingSamples ?? 0}:end_sample=${Math.round(pictureSec * EDIT_SAMPLE_RATE) + (timeline.musicPrimingSamples ?? 0)},asetpts=PTS-STARTPTS,volume=${sound.music.gainDb}dB${fades}[bedraw]`
3349
+ : `[${m}:a]${stereo},atrim=0:${pictureSec.toFixed(6)},asetpts=PTS-STARTPTS,volume=${sound.music.gainDb}dB${fades}[bedraw]`);
2982
3350
  filters.push(
2983
3351
  `[bedraw][duckkey]sidechaincompress=threshold=${duck.threshold}:ratio=${duck.ratio}:attack=${duck.attack}:release=${duck.release}[bed]`,
2984
3352
  );
@@ -2987,7 +3355,7 @@ export function buildFfmpegArgs(timeline, { resolve, music, out, finish = null,
2987
3355
  filters.push(`[${m}:a]${stereo},atrim=0:${pictureSec.toFixed(6)},asetpts=PTS-STARTPTS,volume=${sound.music.gainDb}dB[bed]`);
2988
3356
  } else {
2989
3357
  filters.push(timeline.mapped
2990
- ? `[${m}:a]${stereo},asetpts=PTS-STARTPTS,atrim=start_sample=${timeline.musicPrimingSamples ?? 0}:end_sample=${Math.round(pictureSec * EDIT_SAMPLE_RATE) + (timeline.musicPrimingSamples ?? 0)},asetpts=PTS-STARTPTS,volume=${MUSIC_VOLUME}[bed]`
3358
+ ? `[${m}:a]${stereo},asetpts=PTS-STARTPTS,atrim=start_sample=${timeline.musicPrimingSamples ?? 0}:end_sample=${Math.round(pictureSec * EDIT_SAMPLE_RATE) + (timeline.musicPrimingSamples ?? 0)},asetpts=PTS-STARTPTS,volume=${timeline.narratedShotList ? "-17dB" : MUSIC_VOLUME}${fades}[bed]`
2991
3359
  : `[${m}:a]${stereo},volume=${MUSIC_VOLUME}[bed]`);
2992
3360
  }
2993
3361
  beds.push("[bed]");
@@ -3015,7 +3383,9 @@ export function buildFfmpegArgs(timeline, { resolve, music, out, finish = null,
3015
3383
  filters.push(`${label}asplit=2[mu${j}][mb${j}]`, `[mb${j}]atrim=end=${pictureSec.toFixed(6)}[mt${j}]`);
3016
3384
  return `[mu${j}]`;
3017
3385
  });
3018
- filters.push(
3386
+ // Trusted template songs may have no separate underlay. Meter only real
3387
+ // streams there; preserve the existing unmigrated graph for other doors.
3388
+ if (under.length || finish.meters.allowAbsentBed !== true) filters.push(
3019
3389
  `${under.map((_, j) => `[mt${j}]`).join("")}amix=inputs=${under.length}:duration=longest:dropout_transition=0:normalize=0[bedmeter]`,
3020
3390
  );
3021
3391
  }
@@ -3038,7 +3408,8 @@ export function buildFfmpegArgs(timeline, { resolve, music, out, finish = null,
3038
3408
  "-movflags", "+faststart",
3039
3409
  out,
3040
3410
  ...(finish?.meters
3041
- ? ["-map", "[vmeter]", "-c:a", "pcm_s16le", finish.meters.voice, "-map", "[bedmeter]", "-c:a", "pcm_s16le", finish.meters.bed]
3411
+ ? ["-map", "[vmeter]", "-c:a", "pcm_s16le", finish.meters.voice,
3412
+ ...(under.length || finish.meters.allowAbsentBed !== true ? ["-map", "[bedmeter]", "-c:a", "pcm_s16le", finish.meters.bed] : [])]
3042
3413
  : []),
3043
3414
  ];
3044
3415
  }
@@ -3051,7 +3422,7 @@ const roundMs = (n) => Math.round(n * 1000) / 1000;
3051
3422
  * there and its placement splits around it. A scene split around a reaction
3052
3423
  * beat, or shown twice, has one entry per stretch.
3053
3424
  */
3054
- export function sceneTimes(timeline) {
3425
+ export function sceneTimes(timeline, exact = false) {
3055
3426
  const covers = timeline.cutaways
3056
3427
  .filter((c) => c.seconds > 0)
3057
3428
  .map((c) => ({ from: c.startSec, to: c.startSec + c.seconds }));
@@ -3067,7 +3438,7 @@ export function sceneTimes(timeline) {
3067
3438
  return pieces.map((p) => ({ sceneIndex: seg.sceneIndex, startSec: p.from, seconds: p.to - p.from }));
3068
3439
  });
3069
3440
  return [...visible, ...timeline.cutaways]
3070
- .map((p) => timeline.mapped
3441
+ .map((p) => timeline.mapped || exact
3071
3442
  ? { scene: p.sceneIndex, startSec: p.startSec, seconds: p.seconds }
3072
3443
  : { scene: p.sceneIndex, startSec: roundMs(p.startSec), seconds: roundMs(p.seconds) })
3073
3444
  .filter((p) => p.seconds > 0)
@@ -3087,10 +3458,10 @@ export function wordRows(timeline) {
3087
3458
  const owner = parts.find((p) => p.measured && index >= p.firstWord && index <= p.lastWord);
3088
3459
  rows.push(timeline.mapped
3089
3460
  ? {
3090
- text, start, end, scene: seg.sceneIndex, lineId: owner?.lineId ?? null,
3461
+ text, start: r6(start), end: r6(end), scene: seg.sceneIndex, lineId: w.lineId ?? owner?.lineId ?? null,
3091
3462
  partId: owner?.sourceSpan?.partId ?? owner?.part ?? null,
3092
3463
  }
3093
- : { text, start: roundMs(start), end: roundMs(end), scene: seg.sceneIndex });
3464
+ : { text, start: roundMs(start), end: roundMs(end), scene: seg.sceneIndex, ...(hasText(w.lineId) ? { lineId: w.lineId } : {}) });
3094
3465
  }
3095
3466
  }
3096
3467
  }
@@ -3449,7 +3820,7 @@ export function lineWords(timeline, storyboard) {
3449
3820
  * their boxes, the hook box and the end card. Boxes are [x0, y0, x1, y1] as
3450
3821
  * shares of the frame. Every word on screen is the script's or the storyboard's.
3451
3822
  */
3452
- export function editAss({ captions = [], cap = null, cards = [], hook = null, endCard = null, capMaxShare = null }, W = WIDTH, H = HEIGHT) {
3823
+ export function editAss({ captions = [], cap = null, cards = [], hook = null, endCard = null, endLayers = null, capMaxShare = null }, W = WIDTH, H = HEIGHT) {
3453
3824
  const thick = /thick black outline/i.test(cap?.style ?? "");
3454
3825
  const y = cap?.place ? Math.round(((cap.place[0] + cap.place[1]) / 2) * H) : Math.round(H * 0.7);
3455
3826
  const capSize = Math.min(Math.round(H * (thick ? 0.05 : 0.034)), capMaxShare ? Math.round(H * capMaxShare) : Infinity);
@@ -3460,12 +3831,14 @@ export function editAss({ captions = [], cap = null, cards = [], hook = null, en
3460
3831
  "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding",
3461
3832
  `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`,
3462
3833
  "Style: Card,Helvetica,60,&H00141414,&H00141414,&H00FFFFFF,&H50000000,-1,0,0,0,100,100,0,0,3,18,4,5,0,0,0,1",
3463
- `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`, "",
3834
+ `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`,
3835
+ ...(endLayers ? endCardStyles(endLayers.colors, H) : []), "",
3464
3836
  "[Events]", "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text",
3465
3837
  ...captions.map((c) => `Dialogue: 1,${assTime(c.s)},${assTime(c.e)},Cap,,0,0,0,,{\\pos(${W / 2},${y})}${c.text}`),
3466
3838
  ...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)}`),
3467
3839
  ...(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)}`] : []),
3468
3840
  ...(endCard ? [`Dialogue: 4,${assTime(endCard.in)},${assTime(endCard.out)},End,,0,0,0,,{\\pos(${W / 2},${H / 2})\\fad(120,0)}${assText(endCard.text)}`] : []),
3841
+ ...(endLayers ? endCardDialogues(endLayers, W, H) : []),
3469
3842
  ].join("\n") + "\n";
3470
3843
  }
3471
3844
 
@@ -3561,6 +3934,158 @@ export function sourceAdPaceShots(sourceAd) {
3561
3934
  return Array.isArray(sourceAd?.adShots) && sourceAd.adShots.length > 0 ? sourceAd.adShots : sourceAd.shots;
3562
3935
  }
3563
3936
 
3937
+ /**
3938
+ * endCardLayers and brandCheck (Plan 14): the manifest's `brand`, written by
3939
+ * the worker beside `finishing`: the product's pack photo it downloaded
3940
+ * (`packPhoto`, a file in the folder), the product's name, the scenes whose
3941
+ * picture the pull's rule draws from that photo (`photoScenes`), the end
3942
+ * card's length and the brand's colours (hex), each optional. Null when the
3943
+ * manifest has none, so a cut without it is the one it always was.
3944
+ */
3945
+ export const END_CARD_LAYERS_SEC = 6;
3946
+ export const END_CARD_MIN_SEC = 5;
3947
+ export const END_CARD_MAX_SEC = 8;
3948
+ /** The end card's colours when the brand gives none: a near-black ground, white words, a white call-to-action box. */
3949
+ export const END_CARD_COLORS = { background: "111111", text: "FFFFFF", accent: "FFFFFF" };
3950
+ /** Where the pack photo sits on the end card, as shares of the frame, and how far it pushes in across the card. */
3951
+ export const END_CARD_PHOTO_BOX = [0.1, 0.1, 0.9, 0.56];
3952
+ export const END_CARD_PUSH_IN = 0.05;
3953
+ /** The words build in one after another, this far apart, after the photo has faded in. */
3954
+ const END_CARD_WORD_STEP_SEC = 0.6;
3955
+ const END_CARD_PHOTO_FADE_SEC = 0.4;
3956
+ /** A spoken call to action longer than this many words is too long to letter; the card then shows none. */
3957
+ const END_CARD_CTA_MAX_WORDS = 8;
3958
+
3959
+ const hexColour = (value) => (typeof value === "string" && /^#?[0-9a-f]{6}$/i.test(value.trim()) ? value.trim().replace(/^#/, "").toUpperCase() : null);
3960
+
3961
+ export function brandOf(manifest) {
3962
+ const b = manifest?.brand;
3963
+ if (!b || typeof b !== "object") return null;
3964
+ const text = (v) => (hasText(v) ? v.trim() : null);
3965
+ const colors = b.colors && typeof b.colors === "object" ? b.colors : {};
3966
+ const seconds = Number.isFinite(b.endCardSec) ? Math.min(END_CARD_MAX_SEC, Math.max(END_CARD_MIN_SEC, b.endCardSec)) : END_CARD_LAYERS_SEC;
3967
+ return {
3968
+ packPhoto: text(b.packPhoto),
3969
+ productName: text(b.productName),
3970
+ photoScenes: Array.isArray(b.photoScenes) ? b.photoScenes.filter((n) => Number.isInteger(n)) : [],
3971
+ endCardSec: seconds,
3972
+ colors: {
3973
+ background: hexColour(colors.background) ?? END_CARD_COLORS.background,
3974
+ text: hexColour(colors.text) ?? END_CARD_COLORS.text,
3975
+ accent: hexColour(colors.accent) ?? END_CARD_COLORS.accent,
3976
+ },
3977
+ };
3978
+ }
3979
+
3980
+ /**
3981
+ * The end card's words, each the storyboard's own or null: the headline is the
3982
+ * product's name, the offer an overlay the storyboard types or places as an
3983
+ * offer, price, deal or guarantee, and the call to action the CTA's overlay,
3984
+ * else its spoken line when that is short enough to letter.
3985
+ */
3986
+ export function endCardWords(storyboard, productName = null) {
3987
+ const overlays = storyboard?.overlays ?? [];
3988
+ const ctaId = storyboard?.cta?.overlayId;
3989
+ const ctaOverlay = ctaId ? overlays.find((o) => o?.overlayId === ctaId) : null;
3990
+ const spokenId = storyboard?.cta?.spokenLineId;
3991
+ const spoken = spokenId ? (storyboard?.script ?? []).find((l) => l?.lineId === spokenId)?.text : null;
3992
+ const offer = overlays.find(
3993
+ (o) => o?.overlayId !== ctaId && hasText(o?.text) && /offer|discount|price|deal|guarantee|promo/i.test(`${o.type ?? ""} ${o.placement ?? ""}`),
3994
+ );
3995
+ const headline = hasText(storyboard?.product?.name) ? storyboard.product.name.trim() : hasText(productName) ? productName.trim() : null;
3996
+ const shortSpoken = hasText(spoken) && spoken.trim().split(/\s+/).length <= END_CARD_CTA_MAX_WORDS ? spoken.trim() : null;
3997
+ return {
3998
+ headline,
3999
+ offer: hasText(offer?.text) ? offer.text.trim() : null,
4000
+ cta: hasText(ctaOverlay?.text) ? ctaOverlay.text.trim() : shortSpoken,
4001
+ };
4002
+ }
4003
+
4004
+ /**
4005
+ * endCardLayers: the ad's last `endCardSec` seconds of screen time (`speed`
4006
+ * is the cut's speed-up, applied after) become the end card: the brand's
4007
+ * ground colour, the pack photo pushing in slowly, and the words drawn by
4008
+ * code over it. The sound runs on under it as cut, so the call to action is
4009
+ * heard over the card. Never more than half the ad. Null without a pack photo.
4010
+ */
4011
+ export function endCardLayersOf(brand, storyboard, pictureSec, speed = 1) {
4012
+ if (!brand?.packPhoto) return null;
4013
+ const seconds = Math.min(brand.endCardSec * speed, pictureSec / 2);
4014
+ if (!(seconds > 0)) return null;
4015
+ const at = roundMs(Math.max(0, pictureSec - seconds));
4016
+ return {
4017
+ file: brand.packPhoto,
4018
+ in: at,
4019
+ out: roundMs(pictureSec + 1),
4020
+ seconds: roundMs(pictureSec - at),
4021
+ words: endCardWords(storyboard, brand.productName),
4022
+ colors: brand.colors,
4023
+ };
4024
+ }
4025
+
4026
+ /** An ASS colour (&HAABBGGRR) from a hex RRGGBB. */
4027
+ const assColour = (hex, alpha = "00") => `&H${alpha}${hex.slice(4, 6)}${hex.slice(2, 4)}${hex.slice(0, 2)}`;
4028
+
4029
+ /** The end card's three word styles, in the brand's colours. */
4030
+ function endCardStyles(colors, H) {
4031
+ const text = assColour(colors.text);
4032
+ return [
4033
+ `Style: EndHead,Helvetica,${Math.round(H * 0.05)},${text},${text},&H00000000,&H00000000,-1,0,0,0,100,100,1,0,1,0,0,5,60,60,0,1`,
4034
+ `Style: EndOffer,Helvetica,${Math.round(H * 0.034)},${text},${text},&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,0,0,5,60,60,0,1`,
4035
+ // The call to action sits in a box of the accent colour, its words in the ground colour.
4036
+ `Style: EndCta,Helvetica,${Math.round(H * 0.036)},${assColour(colors.background)},${assColour(colors.background)},${assColour(colors.accent)},${assColour(colors.accent)},-1,0,0,0,100,100,1,0,3,28,0,5,60,60,0,1`,
4037
+ ];
4038
+ }
4039
+
4040
+ /** The end card's words, building in one after another after the photo. */
4041
+ function endCardDialogues(e, W, H) {
4042
+ const rows = [
4043
+ ["EndHead", e.words.headline, 0.64],
4044
+ ["EndOffer", e.words.offer, 0.71],
4045
+ ["EndCta", e.words.cta, 0.82],
4046
+ ].filter(([, words]) => hasText(words));
4047
+ return rows.map(([style, words, y], k) => {
4048
+ const from = roundMs(e.in + END_CARD_PHOTO_FADE_SEC + k * END_CARD_WORD_STEP_SEC);
4049
+ return `Dialogue: 5,${assTime(from)},${assTime(e.out)},${style},,0,0,0,,{\\pos(${W / 2},${Math.round(H * y)})\\fad(250,0)}${assText(words)}`;
4050
+ });
4051
+ }
4052
+
4053
+ /** Caption screens stop where the end card starts: its words are the card's, not the caption band's. */
4054
+ export function captionsBeforeEndCard(screens, e) {
4055
+ if (!e) return screens;
4056
+ return screens.filter((c) => c.s < e.in).map((c) => (c.e > e.in ? { ...c, e: e.in } : c));
4057
+ }
4058
+
4059
+ /** Whether every word of `name`, in order, is among the heard words (case, accents and punctuation aside). */
4060
+ export function nameHeard(rows, name) {
4061
+ const fold = (t) => String(t).normalize("NFKD").replace(/\p{M}/gu, "").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, " ").trim();
4062
+ const want = fold(name).split(" ").filter(Boolean);
4063
+ if (want.length === 0) return null;
4064
+ const heard = rows.flatMap((r) => fold(r.text).split(" ").filter(Boolean));
4065
+ for (let i = 0; i + want.length <= heard.length; i++) {
4066
+ if (want.every((w, k) => heard[i + k] === w)) return true;
4067
+ }
4068
+ return false;
4069
+ }
4070
+
4071
+ /**
4072
+ * brandCheck: what the cut records about the brand, and only records: the
4073
+ * pack photo reached the folder; the scenes whose picture the pull's rule
4074
+ * draws from it; whether the end card carries it; whether the product's name
4075
+ * is among the heard words (null with no name to look for).
4076
+ */
4077
+ export function brandRecord({ brand, packPhoto, endLayers, rows }) {
4078
+ const name = brand?.productName ?? null;
4079
+ return {
4080
+ packPhoto,
4081
+ photoScenes: packPhoto ? (brand?.photoScenes ?? []) : [],
4082
+ photoReachedScene: packPhoto && (brand?.photoScenes ?? []).length > 0,
4083
+ endCardPicture: Boolean(endLayers),
4084
+ productName: name,
4085
+ productNameHeard: name ? nameHeard(rows, name) : null,
4086
+ };
4087
+ }
4088
+
3564
4089
  /**
3565
4090
  * The pace report: cuts a minute, mean and longest shot, shots over
3566
4091
  * LONG_SHOT_SEC, words a minute and pauses over 0.3 s, against a source ad's
@@ -3602,8 +4127,11 @@ export function voicePauses(rows) {
3602
4127
  /**
3603
4128
  * The bars a checked cut is held to. voiceOverBedDb and the slow-shot ratio
3604
4129
  * are guesses, to be tuned against the graders' notes on the cuts they mark.
4130
+ * blipSec (blipCheck), freezeSec and freezeYavg (freezeCheck) are guesses too:
4131
+ * a freeze is picture whose frame-to-frame change (motionArgs' YAVG) stays
4132
+ * under freezeYavg for freezeSec or longer.
3605
4133
  */
3606
- export const CHECK_BARS = { voiceOverBedDb: 10, minBrollSec: 1.2, truePeak: -1.0, lufs: -14 };
4134
+ export const CHECK_BARS = { voiceOverBedDb: 10, minBrollSec: 1.2, truePeak: -1.0, lufs: -14, blipSec: 1.0, freezeSec: 1.0, freezeYavg: 0.2 };
3607
4135
  /** A shot is slow when it moves under this share of the cut's median motion, or under SLOW_SHOT_FLOOR. */
3608
4136
  const SLOW_SHOT_RATIO = 0.35;
3609
4137
  const SLOW_SHOT_FLOOR = 0.6;
@@ -3648,6 +4176,18 @@ export function shortShots(shots, minSec = CHECK_BARS.minBrollSec) {
3648
4176
  .map((s) => ({ n: s.n, role: s.role, at: s.start, sec: roundMs(s.end - s.start) }));
3649
4177
  }
3650
4178
 
4179
+ /**
4180
+ * blipCheck: shots of any role on screen for less than `minSec` of the cut,
4181
+ * unless the source ad cut its own shot as short. `shots`: [{ n, role, start,
4182
+ * end, srcSec }], srcSec the length of the source shot the scene copies, or
4183
+ * null when there is none to compare with (then every short shot is named).
4184
+ */
4185
+ export function blips(shots, minSec = CHECK_BARS.blipSec) {
4186
+ return shots
4187
+ .filter((s) => s.end - s.start < minSec && (s.srcSec == null || s.srcSec >= minSec))
4188
+ .map((s) => ({ n: s.n, role: s.role, at: s.start, sec: roundMs(s.end - s.start), srcSec: s.srcSec == null ? null : roundMs(s.srcSec) }));
4189
+ }
4190
+
3651
4191
  /**
3652
4192
  * Shots whose motion is far under the cut's median. A shot of someone talking
3653
4193
  * to camera (a-roll) sits still at natural speed, so it is never slow here.
@@ -3675,6 +4215,20 @@ export function checkVerdict(c, bars = CHECK_BARS) {
3675
4215
  if (c.shortShots?.length) fails.push(`${c.shortShots.length} b-roll shot(s) under ${bars.minBrollSec} s`);
3676
4216
  if (c.slowShots === null) fails.push("shot motion could not be measured");
3677
4217
  else if (c.slowShots?.length) fails.push(`${c.slowShots.length} slow shot(s)`);
4218
+ if (c.blips?.length) fails.push(`${c.blips.length} blip(s) under ${bars.blipSec} s`);
4219
+ if (c.freezes?.length) fails.push(`${c.freezes.length} freeze(s) of ${bars.freezeSec} s or more`);
4220
+ if (c.floors?.cards.length) fails.push(`${c.floors.cards.length} card(s) on screen outside ${FLOOR_BARS.cardMinSec} to ${FLOOR_BARS.cardMaxSec} s`);
4221
+ if (c.floors?.leads.length) fails.push(`${c.floors.leads.length} shot(s) after a card under ${FLOOR_BARS.leadSec} s before the first word`);
4222
+ if (c.floors?.dialogue.length) fails.push(`${c.floors.dialogue.length} dialogue take(s) under ${FLOOR_BARS.dialogueMinSec} s`);
4223
+ if (c.floors?.shortTakes.length) fails.push(`${c.floors.shortTakes.length} take(s) over ${FLOOR_BARS.shortTakeShare * 100}% short of plan`);
4224
+ if (c.flashes?.length) fails.push(`${c.flashes.length} flash(es) under ${FLASH_BARS.maxSec} s`);
4225
+ if (c.levelSteps === null) fails.push("the cut's sound levels could not be read");
4226
+ else if (c.levelSteps?.length) fails.push(`${c.levelSteps.length} level step(s) over ${LEVEL_STEP_BARS.maxStepDb} dB at a cut`);
4227
+ if (c.voFits?.length) fails.push(`${c.voFits.length} voice-over fit(s) broken`);
4228
+ if (c.dashGaps?.length) fails.push(`${c.dashGaps.length} dash gap(s) over ${DASH_BARS.maxGapSec} s`);
4229
+ if (c.paceBreaks?.lines.length) fails.push(`${c.paceBreaks.lines.length} line(s) outside the pace band`);
4230
+ if (c.paceBreaks?.gaps.length) fails.push(`${c.paceBreaks.gaps.length} gap(s) between lines outside their band`);
4231
+ if (c.names?.missing.length) fails.push(`${c.names.missing.length} name(s) not heard`);
3678
4232
  if (c.truePeak != null && c.truePeak > bars.truePeak) fails.push(`true peak ${c.truePeak} dBTP (bar ${bars.truePeak})`);
3679
4233
  if (c.lufs != null && Math.abs(c.lufs - bars.lufs) > 1) fails.push(`loudness ${c.lufs} LUFS (bar ${bars.lufs} ± 1)`);
3680
4234
  return { pass: !fails.length, fails };
@@ -3715,6 +4269,364 @@ export function motionByShot(rows, shots) {
3715
4269
  });
3716
4270
  }
3717
4271
 
4272
+ /**
4273
+ * freezeCheck: each stretch of consecutive frames whose change (readMotion's
4274
+ * rows, [{ t, y }]) stays under `still` for `minSec` or longer. A stretch runs
4275
+ * from its first still frame to the next frame that moves, or to one frame past
4276
+ * the last when the cut ends still.
4277
+ */
4278
+ export function freezes(rows, { minSec = CHECK_BARS.freezeSec, still = CHECK_BARS.freezeYavg } = {}) {
4279
+ const out = [];
4280
+ for (let i = 0; i < rows.length; i++) {
4281
+ if (!(rows[i].y < still)) continue;
4282
+ let j = i;
4283
+ while (j + 1 < rows.length && rows[j + 1].y < still) j++;
4284
+ const step = j > 0 ? rows[j].t - rows[j - 1].t : 0;
4285
+ const end = j + 1 < rows.length ? rows[j + 1].t : rows[j].t + step;
4286
+ if (end - rows[i].t >= minSec - 1e-9) out.push({ at: roundMs(rows[i].t), sec: roundMs(end - rows[i].t) });
4287
+ i = j;
4288
+ }
4289
+ return out;
4290
+ }
4291
+
4292
+ /** The length of the source ad's shot `n`, or null when there is no source record or no such shot. */
4293
+ const sourceShotSec = (sourceAd, n) => {
4294
+ const e = n == null ? null : sourceAd?.shots.find((x) => x.n === n);
4295
+ return e ? e.t1 - e.t0 : null;
4296
+ };
4297
+
4298
+ // ---- Plan 14: record-only cut checks from Luke's rulebook, each under cutChecks ----
4299
+
4300
+ /**
4301
+ * The bars Plan 14's cut checks hold a cut to. The numbers are Luke's
4302
+ * (microdrama rulebook RB5.8, P-7, RB9.4, RB1.6, RB5.4, RB5.1 to RB5.3); how
4303
+ * each maps onto this cut, where a card is an overlay on a line and a take is
4304
+ * a scene, is a guess until a graded cut tunes it. spikeYavg and
4305
+ * spikeOverMedian (flashCheck) are guesses of our own.
4306
+ */
4307
+ export const FLOOR_BARS = { cardMinSec: 2.5, cardMaxSec: 3.0, leadSec: 1.0, dialogueMinSec: 8, shortTakeShare: 0.25 };
4308
+ export const FLASH_BARS = { maxSec: 0.2, spikeYavg: 12, spikeOverMedian: 4 };
4309
+ export const LEVEL_STEP_BARS = { maxStepDb: 3, sideSec: 0.5, minQuietSec: 0.1, wordClearSec: 0.05 };
4310
+ export const VO_FIT_BARS = { leadSec: 0.3 };
4311
+ export const DASH_BARS = { maxGapSec: 0.15, sameTakeMaxSec: 0.3 };
4312
+ /** paceCheck's numbers; a format may set any of the first six (settings.ts paceCheck). */
4313
+ export const PACE_BARS = { minWps: 2.3, maxWps: 2.7, minGapSec: 0.3, maxGapSec: 0.6, beatMaxSec: 1.2, hookMaxGapSec: 1.5, minLineWords: 4 };
4314
+ export const PACE_BAR_KEYS = ["minWps", "maxWps", "minGapSec", "maxGapSec", "beatMaxSec", "hookMaxGapSec"];
4315
+
4316
+ /**
4317
+ * floorCheck (RB5.8): a card on screen outside cardMinSec to cardMaxSec; the
4318
+ * first shot after a card whose first word comes under leadSec into it; a
4319
+ * dialogue take under dialogueMinSec that does not end on a reaction beat; a
4320
+ * take more than shortTakeShare under its planned length. All times are the
4321
+ * finished cut's. `cards`: [{ n, label, in, out }]; `shots`: [{ n, start, end }];
4322
+ * `rows`: the cut's words [{ start }]; `takes`: takesOf's [{ n, at, sec,
4323
+ * takeSec, planSec, dialogue, endsOnReaction }].
4324
+ */
4325
+ export function floors({ cards, shots, rows, takes }, bars = FLOOR_BARS) {
4326
+ const cardsOut = cards
4327
+ .map((c) => ({ n: c.n, label: c.label, at: roundMs(c.in), sec: roundMs(c.out - c.in) }))
4328
+ .filter((c) => c.sec < bars.cardMinSec - 1e-9 || c.sec > bars.cardMaxSec + 1e-9);
4329
+ const leads = [];
4330
+ for (const c of cards) {
4331
+ const shot = shots.find((s) => s.start >= c.out - 1e-3);
4332
+ const word = shot ? rows.find((r) => r.start >= shot.start - 1e-3) : null;
4333
+ if (!word) continue;
4334
+ const lead = roundMs(word.start - shot.start);
4335
+ if (lead < bars.leadSec) leads.push({ card: c.n, n: shot.n, at: roundMs(shot.start), leadSec: lead });
4336
+ }
4337
+ const dialogue = takes
4338
+ .filter((t) => t.dialogue && !t.endsOnReaction && t.sec < bars.dialogueMinSec)
4339
+ .map((t) => ({ n: t.n, at: roundMs(t.at), sec: roundMs(t.sec) }));
4340
+ const shortTakes = takes
4341
+ .filter((t) => t.planSec > 0 && t.takeSec < t.planSec * (1 - bars.shortTakeShare))
4342
+ .map((t) => ({ n: t.n, sec: roundMs(t.takeSec), planSec: roundMs(t.planSec) }));
4343
+ return { cards: cardsOut, leads, dialogue, shortTakes };
4344
+ }
4345
+
4346
+ /**
4347
+ * floorCheck's takes from the spine: each run of segments of one scene (a take
4348
+ * split at its pauses is one take), whether it is the scene's own dialogue,
4349
+ * and whether a reaction beat follows it; then each cutaway. `sec` is its time
4350
+ * in the finished cut; `takeSec` its own length before any speed-up, which is
4351
+ * what its plan (the storyboard's durationSec) is read against.
4352
+ */
4353
+ export function takesOf(timeline, plans, speed = 1) {
4354
+ const takes = [];
4355
+ timeline.spine.forEach((seg, i) => {
4356
+ if (seg.reaction) return;
4357
+ const last = takes.at(-1);
4358
+ const endsOnReaction = Boolean(timeline.spine[i + 1]?.reaction);
4359
+ if (last && last.n === seg.sceneIndex && last.lastSeg === i - 1) {
4360
+ Object.assign(last, { takeSec: last.takeSec + seg.seconds, lastSeg: i, endsOnReaction });
4361
+ return;
4362
+ }
4363
+ takes.push({
4364
+ n: seg.sceneIndex, start: seg.startSec, takeSec: seg.seconds, dialogue: seg.audio === "clip",
4365
+ planSec: plans.get(seg.sceneIndex)?.durationSec ?? 0, lastSeg: i, endsOnReaction,
4366
+ });
4367
+ });
4368
+ for (const c of timeline.cutaways) {
4369
+ takes.push({ n: c.sceneIndex, start: c.startSec, takeSec: c.seconds, dialogue: false, planSec: plans.get(c.sceneIndex)?.durationSec ?? 0, endsOnReaction: false });
4370
+ }
4371
+ return takes.map(({ lastSeg, start, ...t }) => ({ ...t, at: start / speed, sec: t.takeSec / speed }));
4372
+ }
4373
+
4374
+ /**
4375
+ * flashCheck (P-7, Plan 13's 0.12 s piece and one-frame leaks): every shot on
4376
+ * screen under maxSec (a one-frame piece included), and every single frame of
4377
+ * picture the motion read shows as a flash: a change into it and a change out
4378
+ * of it, both a cut's size (spikeYavg, and spikeOverMedian times the cut's
4379
+ * median change), as when a clip's first frame is another picture. A frame
4380
+ * flash inside a short shot already named is not named twice.
4381
+ */
4382
+ export function flashes(shots, motion, bars = FLASH_BARS) {
4383
+ const out = shots
4384
+ .filter((s) => s.end - s.start < bars.maxSec - 1e-9)
4385
+ .map((s) => ({ n: s.n, at: roundMs(s.start), sec: roundMs(s.end - s.start), from: "shot" }));
4386
+ const values = motion.map((m) => m.y).sort((a, b) => a - b);
4387
+ const median = values.length ? values[Math.floor(values.length / 2)] : 0;
4388
+ const spike = (r) => r && r.y >= bars.spikeYavg && r.y >= median * bars.spikeOverMedian;
4389
+ for (let i = 0; i + 1 < motion.length; i++) {
4390
+ if (!spike(motion[i]) || !spike(motion[i + 1])) continue;
4391
+ const at = motion[i].t;
4392
+ const sec = motion[i + 1].t - at;
4393
+ if (!out.some((f) => at >= f.at - 1 / FPS && at < f.at + f.sec + 1 / FPS)) out.push({ n: null, at: roundMs(at), sec: roundMs(sec), from: "frame" });
4394
+ i++;
4395
+ }
4396
+ return out.sort((a, b) => a.at - b.at);
4397
+ }
4398
+
4399
+ /** The power mean of some loudness windows, in dB. */
4400
+ const meanDb = (db) => 10 * Math.log10(db.reduce((sum, d) => sum + 10 ** (d / 10), 0) / db.length);
4401
+
4402
+ /**
4403
+ * levelStepCheck (RB9.4, V-5): at each cut, the sound with no word in it just
4404
+ * before and just after (sideSec each side, clear of every word by
4405
+ * wordClearSec), and the step between them; a step over maxStepDb is named. A
4406
+ * cut with under minQuietSec of such sound on either side cannot be read and
4407
+ * is left out. `levels`: the finished cut's { windowSec, db }; `cuts`: [{ n, at }].
4408
+ */
4409
+ export function levelSteps(levels, cuts, rows, bars = LEVEL_STEP_BARS) {
4410
+ const { windowSec, db } = levels;
4411
+ const busy = (t) => rows.some((r) => t >= r.start - bars.wordClearSec && t <= r.end + bars.wordClearSec);
4412
+ const side = (from, to) => {
4413
+ const kept = [];
4414
+ for (let k = Math.max(0, Math.floor(from / windowSec)); k < Math.min(db.length, Math.ceil(to / windowSec)); k++) {
4415
+ const mid = (k + 0.5) * windowSec;
4416
+ if (mid >= from && mid < to && !busy(mid)) kept.push(db[k]);
4417
+ }
4418
+ return kept.length * windowSec >= bars.minQuietSec - 1e-9 ? meanDb(kept) : null;
4419
+ };
4420
+ const out = [];
4421
+ for (const c of cuts) {
4422
+ const before = side(c.at - bars.sideSec, c.at);
4423
+ const after = side(c.at, c.at + bars.sideSec);
4424
+ if (before === null || after === null) continue;
4425
+ const step = round2(after - before);
4426
+ if (Math.abs(step) > bars.maxStepDb) out.push({ n: c.n, at: roundMs(c.at), stepDb: step, beforeDb: round2(before), afterDb: round2(after) });
4427
+ }
4428
+ return out;
4429
+ }
4430
+
4431
+ /**
4432
+ * voFitCheck (RB1.6): each voice-over take's first word starts leadSec or more
4433
+ * after the last word heard before it, and its last word ends inside its take.
4434
+ * `takes`: [{ n, start, end }] in cut time; `rows`: the cut's words [{ start, end, scene }].
4435
+ */
4436
+ export function voFits(takes, rows, bars = VO_FIT_BARS) {
4437
+ const out = [];
4438
+ for (const t of takes) {
4439
+ const own = rows.filter((r) => r.scene === t.n && r.start >= t.start - 1e-3 && r.start < t.end);
4440
+ if (own.length === 0) continue;
4441
+ const first = own[0];
4442
+ const last = own.at(-1);
4443
+ const before = rows.filter((r) => !own.includes(r) && r.start < first.start).at(-1);
4444
+ if (before && first.start - before.end < bars.leadSec - 1e-9) {
4445
+ out.push({ n: t.n, at: roundMs(first.start), early: roundMs(first.start - before.end) });
4446
+ }
4447
+ if (last.end > t.end + 1e-3) out.push({ n: t.n, at: roundMs(last.end), late: roundMs(last.end - t.end) });
4448
+ }
4449
+ return out;
4450
+ }
4451
+
4452
+ /** A line the script ends on a dash: the next speaker cuts in. */
4453
+ const ENDS_ON_DASH = /(?:[—–]|-{1,2})["'”’)\]]*\s*$/;
4454
+ /** A line the script ends on an ellipsis: a beat after it. */
4455
+ const ENDS_ON_ELLIPSIS = /(?:…|\.{3})["'”’)\]]*\s*$/;
4456
+
4457
+ /**
4458
+ * Each heard script line in the cut, in time order: its first and last word,
4459
+ * its word count, and the take it was heard in. `words`: lineWords' rows in
4460
+ * cut time, [{ text, start, end, line }]; `takeOf`: line id to scene.
4461
+ */
4462
+ export function heardLines(words, script, takeOf = () => null) {
4463
+ const byLine = new Map();
4464
+ for (const w of words) {
4465
+ if (!byLine.has(w.line)) byLine.set(w.line, []);
4466
+ byLine.get(w.line).push(w);
4467
+ }
4468
+ const scripted = new Map((script ?? []).filter((l) => typeof l?.lineId === "string").map((l) => [l.lineId, l]));
4469
+ return [...byLine.entries()]
4470
+ .filter(([id]) => scripted.has(id))
4471
+ .map(([id, ws]) => ({
4472
+ line: scripted.get(id), id, start: ws[0].start, end: Math.max(...ws.map((w) => w.end)), words: ws.length, take: takeOf(id),
4473
+ }))
4474
+ .sort((a, b) => a.start - b.start);
4475
+ }
4476
+
4477
+ /**
4478
+ * dashGapCheck (RB5.4, RB5.9): a line the script ends on a dash, and the
4479
+ * next heard line another speaker says: the gap from the dashed line's last
4480
+ * word to the cut-in's first, named when over maxGapSec. `sameTake` marks a
4481
+ * gap inside one take, the one Luke's pace judge rejects over sameTakeMaxSec.
4482
+ */
4483
+ export function dashGaps(lines, bars = DASH_BARS) {
4484
+ const out = [];
4485
+ lines.forEach((l, i) => {
4486
+ const next = lines[i + 1];
4487
+ if (!next || !ENDS_ON_DASH.test(l.line.text ?? "") || next.line.speakerId === l.line.speakerId) return;
4488
+ const gap = roundMs(next.start - l.end);
4489
+ if (gap <= bars.maxGapSec + 1e-9) return;
4490
+ const sameTake = l.take != null && l.take === next.take;
4491
+ out.push({ line: l.id, next: next.id, at: roundMs(l.end), gapSec: gap, sameTake, ...(sameTake && gap > bars.sameTakeMaxSec ? { rolled: true } : {}) });
4492
+ });
4493
+ return out;
4494
+ }
4495
+
4496
+ /**
4497
+ * paceCheck (RB5.1 to RB5.3): each heard line of minLineWords or more spoken
4498
+ * at minWps to maxWps words a second, first word to last; the gap between two
4499
+ * heard lines from minGapSec to maxGapSec, up to beatMaxSec where the script
4500
+ * asks for a beat (a direction, or a line ending on an ellipsis), and under
4501
+ * hookMaxGapSec after the first line. A gap after a dash is dashGapCheck's.
4502
+ */
4503
+ export function paceBreaks(lines, bars = PACE_BARS) {
4504
+ const words = lines
4505
+ .filter((l) => l.words >= bars.minLineWords && l.end > l.start)
4506
+ .map((l) => ({ line: l.id, at: roundMs(l.start), wps: round2(l.words / (l.end - l.start)) }))
4507
+ .filter((l) => l.wps < bars.minWps || l.wps > bars.maxWps);
4508
+ const gaps = [];
4509
+ lines.forEach((l, i) => {
4510
+ const next = lines[i + 1];
4511
+ if (!next || ENDS_ON_DASH.test(l.line.text ?? "")) return;
4512
+ const gap = roundMs(next.start - l.end);
4513
+ if (i === 0) {
4514
+ if (gap > bars.hookMaxGapSec + 1e-9) gaps.push({ line: l.id, next: next.id, at: roundMs(l.end), gapSec: gap, hook: true });
4515
+ return;
4516
+ }
4517
+ const beat = asksPause(l.line) || asksPause(next.line) || ENDS_ON_ELLIPSIS.test(l.line.text ?? "");
4518
+ const max = beat ? bars.beatMaxSec : bars.maxGapSec;
4519
+ if (gap < bars.minGapSec - 1e-9 || gap > max + 1e-9) gaps.push({ line: l.id, next: next.id, at: roundMs(l.end), gapSec: gap, ...(beat ? { beat: true } : {}) });
4520
+ });
4521
+ return { lines: words, gaps };
4522
+ }
4523
+
4524
+ /** A title before a name ("Mrs. Hughes"): its period ends no sentence. */
4525
+ const TITLE = /^(Mr|Mrs|Ms|Dr|St)\.$/;
4526
+ /** A name as the check compares it: lower case, letters and digits only, no possessive. */
4527
+ const nameToken = (word) => wordToken(String(word ?? "").replace(/['’]s$/i, ""));
4528
+
4529
+ /**
4530
+ * The script's names: every word that reads as one (nameLikeWords: a capital
4531
+ * inside it, or a leading capital where no sentence starts) anywhere in the
4532
+ * script, and each capitalized word of a cast member's name, so the same word
4533
+ * at a line's start counts too.
4534
+ */
4535
+ export function scriptNames(script, cast = []) {
4536
+ const names = new Set();
4537
+ for (const member of cast ?? []) {
4538
+ for (const word of String(member?.name ?? "").split(/\s+/)) {
4539
+ const token = nameToken(word);
4540
+ if (/^[A-Z]/.test(word) && token && !/^\d+$/.test(token)) names.add(token);
4541
+ }
4542
+ }
4543
+ for (const line of script ?? []) {
4544
+ const words = scriptWords(line?.text).map((w) => (TITLE.test(w) ? w.slice(0, -1) : w));
4545
+ nameLikeWords(words).forEach((isName, i) => {
4546
+ const token = nameToken(words[i]);
4547
+ if (isName && token && !/^\d+$/.test(token)) names.add(token);
4548
+ });
4549
+ }
4550
+ return names;
4551
+ }
4552
+
4553
+ /**
4554
+ * nameCheck (RB3.10): every name the script gives a take survives in what
4555
+ * the scribe heard in that take, as asr-name-roundtrip does for the product's
4556
+ * name: the same word, the same sound, one slip on a long name, or written as
4557
+ * two heard words. `takes`: [{ n, lineIds, heard: [word] }], one per scene;
4558
+ * `cast`: the storyboard's [{ name }].
4559
+ */
4560
+ export function namesNotHeard(takes, script, cast = []) {
4561
+ const known = scriptNames(script, cast);
4562
+ const texts = new Map((script ?? []).map((l) => [l?.lineId, l?.text]));
4563
+ let checked = 0;
4564
+ const missing = [];
4565
+ for (const t of takes) {
4566
+ const heard = t.heard.map(nameToken).filter(Boolean);
4567
+ const joined = heard.join("");
4568
+ for (const id of t.lineIds) {
4569
+ for (const word of scriptWords(texts.get(id))) {
4570
+ const name = nameToken(word);
4571
+ if (!known.has(name) || !/^[A-Z]/.test(word.replace(/^[^A-Za-z0-9]+/, ""))) continue;
4572
+ checked++;
4573
+ const found = heard.some((h) => sameWord(name, h, true) || h === `${name}s`) || (name.length >= 5 && joined.includes(name));
4574
+ if (!found) missing.push({ n: t.n, line: id, name: word.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, "") });
4575
+ }
4576
+ }
4577
+ }
4578
+ return { checked, missing };
4579
+ }
4580
+
4581
+ /**
4582
+ * Plan 14's checks a finished cut is put through, each only when `finish`
4583
+ * turns it on, every time in the finished cut's (after `speed`). The script's
4584
+ * lines are read off the clips' scribe words (lineWords), as the captions are.
4585
+ */
4586
+ function plan14Checks({ timeline, storyboard, rows, shots, motion, out, finish, cards, speed }) {
4587
+ const faster = (t) => roundMs(t / speed);
4588
+ const script = storyboard?.script ?? [];
4589
+ const takeOfLine = new Map();
4590
+ for (const seg of timeline.spine) for (const id of seg.lineIds ?? []) if (!takeOfLine.has(id)) takeOfLine.set(id, seg.sceneIndex);
4591
+ const lines = finish?.dashGapCheck || finish?.paceCheck
4592
+ ? heardLines(lineWords(timeline, storyboard).map((w) => ({ ...w, start: faster(w.start), end: faster(w.end) })), script, (id) => takeOfLine.get(id) ?? null)
4593
+ : [];
4594
+ const byScene = new Map();
4595
+ for (const seg of timeline.spine) {
4596
+ if (seg.reaction) continue;
4597
+ const take = byScene.get(seg.sceneIndex) ?? { n: seg.sceneIndex, lineIds: [], heard: [] };
4598
+ take.lineIds.push(...(seg.lineIds ?? []).filter((id) => !take.lineIds.includes(id)));
4599
+ take.heard.push(...(seg.words ?? []).map((w) => w.w));
4600
+ byScene.set(seg.sceneIndex, take);
4601
+ }
4602
+ const levels = finish?.levelStepCheck ? clipLevels(out) : null;
4603
+ return {
4604
+ ...(finish?.floorCheck
4605
+ ? {
4606
+ floors: floors({
4607
+ cards: cards.map((c) => ({ ...c, in: faster(c.in), out: faster(c.out) })),
4608
+ shots, rows, takes: takesOf(timeline, scenePlans(storyboard), speed),
4609
+ }),
4610
+ }
4611
+ : {}),
4612
+ ...(finish?.flashCheck ? { flashes: flashes(shots, motion) } : {}),
4613
+ // null: the cut's sound could not be decoded, so no cut was read.
4614
+ ...(finish?.levelStepCheck ? { levelSteps: levels ? levelSteps(levels, shots.slice(1).map((s) => ({ n: s.n, at: s.start })), rows) : null } : {}),
4615
+ // A song laid whole is one voice-over the length of the ad, so it has no takes to fit.
4616
+ ...(finish?.voFitCheck
4617
+ ? {
4618
+ voFits: timeline.wholeVoice ? [] : voFits(
4619
+ timeline.spine.filter((seg) => seg.audio === "narration").map((seg) => ({ n: seg.sceneIndex, start: faster(seg.startSec), end: faster(seg.startSec + seg.seconds) })),
4620
+ rows,
4621
+ ),
4622
+ }
4623
+ : {}),
4624
+ ...(finish?.dashGapCheck ? { dashGaps: dashGaps(lines) } : {}),
4625
+ ...(finish?.paceCheck ? { paceBreaks: paceBreaks(lines, { ...PACE_BARS, ...finish.paceCheck }) } : {}),
4626
+ ...(finish?.nameCheck ? { names: namesNotHeard([...byScene.values()], script, storyboard?.cast) } : {}),
4627
+ };
4628
+ }
4629
+
3718
4630
  /** The part a spine segment plays on screen, as the checks read shots: a scene speaking its own lines (or a reaction) is a-roll. */
3719
4631
  const roleOf = (seg) => (seg.reaction || seg.audio === "clip" ? "a-roll" : seg.audio === "narration" ? "vo-enactment" : "b-roll");
3720
4632
 
@@ -3724,8 +4636,16 @@ const roleOf = (seg) => (seg.reaction || seg.audio === "clip" ? "a-roll" : seg.a
3724
4636
  * shots as written beside it, `levels` the voice's and the bed's loudness from
3725
4637
  * the render. Caption sync is not checked: it needs a transcription of the
3726
4638
  * finished cut of its own, and the cut's words are the ones its captions came from.
4639
+ * With blipCheck and freezeCheck in `finish`, the checks also name blips (each
4640
+ * shot's source length found through `shotOf`, the storyboard's sourceShot, in
4641
+ * `sourceAd`) and freezes; with them off, their keys are absent. The same holds
4642
+ * for Plan 14's checks, floorCheck to nameCheck: `cards` are the text cards
4643
+ * burned in and `speed` the cut's speed-up, so every time they name is the
4644
+ * finished cut's. `endCard` (endCardLayers) is the pack-photo end card: its
4645
+ * slow push-in is still picture by design, so freezeCheck reads only the
4646
+ * picture before it.
3727
4647
  */
3728
- function checkCut({ timeline, storyboard, screens, rows, scenes, out, loudness, levels }) {
4648
+ function checkCut({ timeline, storyboard, screens, rows, scenes, out, loudness, levels, finish = null, sourceAd = null, shotOf = () => null, cards = [], speed = 1, endCard = null }) {
3729
4649
  const roles = new Map(timeline.spine.map((seg) => [seg.sceneIndex, roleOf(seg)]));
3730
4650
  for (const c of timeline.cutaways) roles.set(c.sceneIndex, "b-roll");
3731
4651
  const shots = scenes.map((p) => ({ n: p.scene, role: roles.get(p.scene) ?? "b-roll", start: p.startSec, end: roundMs(p.startSec + p.seconds) }));
@@ -3742,6 +4662,9 @@ function checkCut({ timeline, storyboard, screens, rows, scenes, out, loudness,
3742
4662
  shortShots: shortShots(shots),
3743
4663
  // null: ffmpeg could not read the motion, so no shot was checked.
3744
4664
  slowShots: motion.length ? slowShots(motionByShot(motion, shots)) : null,
4665
+ ...(finish?.blipCheck ? { blips: blips(shots.map((s) => ({ ...s, srcSec: sourceShotSec(sourceAd, shotOf(s.n)) }))) } : {}),
4666
+ ...(finish?.freezeCheck ? { freezes: freezes(endCard ? motion.filter((r) => r.t < endCard.in / speed) : motion) } : {}),
4667
+ ...plan14Checks({ timeline, storyboard, rows, shots, motion, out, finish, cards, speed }),
3745
4668
  lufs: finished ? round2(finished.i) : null,
3746
4669
  truePeak: finished ? round2(finished.tp) : null,
3747
4670
  };
@@ -3938,6 +4861,12 @@ export class TextFinishRefusal extends FirstCutError {
3938
4861
  }
3939
4862
  }
3940
4863
 
4864
+ // Build Sheet boxes are normalized XYWH; text rendering and sourceAd use XYXY.
4865
+ // Convert only storyboard-owned geometry. Leave invalid values for the renderer
4866
+ // to refuse rather than clamping or replacing an authored box.
4867
+ const storyboardTextBox = (box) => Array.isArray(box) && box.length === 4 && box.every(Number.isFinite)
4868
+ ? [box[0], box[1], box[0] + box[2], box[1] + box[3]] : box;
4869
+
3941
4870
  /** Author-selected geometry is planned layout, not an observation of source pixels. */
3942
4871
  export function textMaterial(cut, board, policy) {
3943
4872
  const words = wordRows(cut.timeline);
@@ -3977,10 +4906,11 @@ export function textMaterial(cut, board, policy) {
3977
4906
  : isHook ? sourceAd?.hook?.box : sourceAd?.cards?.[cardIndex++]?.box;
3978
4907
  const item = {
3979
4908
  id: o.overlayId, kind: isHook ? "question-sticker" : "card", text: o.text,
4909
+ cueLineId: o.cueLineId,
3980
4910
  required: isEnd && !permission?.endCard || isOffer && !permission?.offer
3981
4911
  ? authored.required === true : authored.required !== false,
3982
4912
  provenance: authored.provenance ?? "post",
3983
- ...(authored.box ?? refBox ? { box: authored.box ?? refBox } : {}),
4913
+ ...(authored.box !== undefined ? { box: storyboardTextBox(authored.box) } : refBox !== undefined ? { box: refBox } : {}),
3984
4914
  in: authored.startSec ?? (heard.length ? Math.min(...heard.map((w) => w.start)) : undefined),
3985
4915
  out: authored.endSec ?? (heard.length ? Math.max(...heard.map((w) => w.end)) : undefined),
3986
4916
  ...(authored.opening !== undefined ? { opening: authored.opening } : {}),
@@ -3998,32 +4928,67 @@ export function textMaterial(cut, board, policy) {
3998
4928
  else items.push({ ...item, kind: "offer" }); // helper skips optional output or holds an explicitly required unauthorized offer
3999
4929
  } else items.push(item);
4000
4930
  }
4931
+ // Only trusted template starts migrate to audible-source boundaries.
4932
+ // Unstamped mapped runs retain their existing visual-boundary semantics.
4933
+ const template = cut.manifest?.templateMotion?.enabled === true;
4934
+ const cuts = [];
4935
+ if (template) {
4936
+ // A muted picture insert does not interrupt a word, nor a whole song.
4937
+ for (const seg of cut.timeline.spine) {
4938
+ if (seg.wholeVoice) continue;
4939
+ cuts.push(seg.startSec, seg.startSec + seg.seconds);
4940
+ let removed = 0;
4941
+ for (const gap of seg.cuts ?? []) {
4942
+ const at = gap.s - removed - (seg.inSec ?? 0);
4943
+ if (at > 0 && at < seg.seconds) cuts.push(seg.startSec + at);
4944
+ removed += gap.e - gap.s;
4945
+ }
4946
+ }
4947
+ if (policy.captionsByLine === true) {
4948
+ for (let i = 1; i < words.length; i++) {
4949
+ if (words[i].lineId !== words[i - 1].lineId) cuts.push(words[i].start);
4950
+ }
4951
+ }
4952
+ } else {
4953
+ cuts.push(
4954
+ ...cut.timeline.spine.flatMap((s) => [s.startSec, s.startSec + s.seconds]),
4955
+ ...cut.timeline.cutaways.flatMap((s) => [s.startSec, s.startSec + s.seconds]),
4956
+ );
4957
+ }
4958
+ const captionStyle = {
4959
+ ...(sourceAd?.captions ?? {}), ...(board.audio?.captionStyle ?? {}),
4960
+ ...(board.audio?.captionStyle?.box !== undefined
4961
+ ? { box: storyboardTextBox(board.audio.captionStyle.box), place: undefined } : {}),
4962
+ };
4963
+ if (template && policy.captions === true && captionStyle.box === undefined) {
4964
+ // Source capture already transports the selected caption band as `place`.
4965
+ // Without one, use the Skeleton template setup's 0.70–0.80 speech band
4966
+ // (video/cards/setup/skeleton-timeline-edit-treatment.md), not an LLM box.
4967
+ const band = captionStyle.place ?? [0.70, 0.80];
4968
+ captionStyle.box = [0.08, band[0], 0.92, band[1]];
4969
+ }
4001
4970
  return {
4002
4971
  treatment: policy.treatment, words, items, selection, pictureHolds, skippedPictures,
4003
4972
  captions: {
4004
4973
  on: policy.captions === true,
4005
- cap: {
4006
- ...(sourceAd?.captions ?? {}), ...(board.audio?.captionStyle ?? {}),
4007
- ...(board.audio?.captionStyle?.box ? { place: undefined } : {}),
4008
- },
4974
+ keepNumericLists: cut.manifest?.templateMotion?.enabled === true && cut.timeline.narratedShotList === true,
4975
+ cap: captionStyle,
4009
4976
  names: (board.cast ?? []).map((c) => c.name).filter(hasText),
4010
- ...(cut.timeline.wholeVoice ? { lyricLines: (board.script ?? []).map((line) => line.text) } : {}),
4977
+ ...(cut.timeline.wholeVoice && !cut.timeline.narratedShotList ? { lyricLines: (board.script ?? []).map((line) => line.text) } : {}),
4011
4978
  },
4012
- cuts: [...new Set([
4013
- ...cut.timeline.spine.flatMap((s) => [s.startSec, s.startSec + s.seconds]),
4014
- ...cut.timeline.cutaways.flatMap((s) => [s.startSec, s.startSec + s.seconds]),
4015
- ])].sort((a, b) => a - b),
4016
- W: WIDTH, H: HEIGHT, totalSec: cut.sourceMap.frames / FPS,
4979
+ cuts: [...new Set(cuts)].sort((a, b) => a - b),
4980
+ W: WIDTH, H: HEIGHT, totalSec: (cut.sourceMap?.frames ?? cut.textStage?.frames) / FPS,
4017
4981
  };
4018
4982
  }
4019
4983
 
4020
4984
  /** Resolve authored anchors on the actual visible edit and retained word clock, never planning seconds. */
4021
4985
  export function resolveTemporalRequirements(cut, board) {
4022
4986
  const words = wordRows(cut.timeline);
4023
- const pictures = sceneTimes(cut.timeline);
4987
+ const pictures = sceneTimes(cut.timeline, cut.textStage?.frames !== undefined);
4024
4988
  const requirements = [];
4025
4989
  const owners = [];
4026
4990
  const holds = [];
4991
+ const selectedJobs = selectedPictureJobs(cut.manifest, board);
4027
4992
  const covers = (start, end, spans) => {
4028
4993
  let at = start;
4029
4994
  for (const span of spans) {
@@ -4057,7 +5022,9 @@ export function resolveTemporalRequirements(cut, board) {
4057
5022
  }
4058
5023
  requirements.push({
4059
5024
  id: phase.id, kind: phase.kind, description: phase.description, startSec, endSec,
4060
- ...(scene.planningBeatIds?.length === 1 ? { beatId: scene.planningBeatIds[0] } : {}),
5025
+ ...(selectedJobs
5026
+ ? { beatId: selectedJobs.find((job) => job.owners.includes(scene))?.id }
5027
+ : scene.planningBeatIds?.length === 1 ? { beatId: scene.planningBeatIds[0] } : {}),
4061
5028
  ...(phase.before !== undefined ? { before: phase.before } : {}),
4062
5029
  ...(phase.after !== undefined ? { after: phase.after } : {}),
4063
5030
  ...(phase.afterPhaseId !== undefined ? { afterPhaseId: phase.afterPhaseId } : {}),
@@ -4073,52 +5040,102 @@ function phasePassed(temporal, id) {
4073
5040
  return temporal?.retention?.status === "pass" && temporal.receipt?.phases.some((phase) => phase.id === id && phase.status === "pass");
4074
5041
  }
4075
5042
 
5043
+ /** Bind the already rendered unmapped terminal picture to its decoded EOF.
5044
+ * Semantic completion still requires the exact-output temporal observer. */
5045
+ function unmappedTerminalSource(cut, sceneIndex, endFrame, audible = false) {
5046
+ if (cut.sourceMap || cut.manifest?.templateMotion?.enabled !== true ||
5047
+ !Number.isInteger(endFrame) || cut.textStage?.frames !== endFrame) return null;
5048
+ const seg = [...cut.timeline.spine, ...cut.timeline.cutaways].find((entry) =>
5049
+ entry.sceneIndex === sceneIndex && entry.source === "clip" && entry.file &&
5050
+ !entry.editorialHold && !(entry.holdSec > 0) &&
5051
+ Math.abs(entry.startSec + entry.seconds - endFrame / FPS) < 1e-7);
5052
+ if (!seg || !audible) return seg ?? null;
5053
+ const whole = seg.wholeVoice ? cut.timeline.wholeVoice : null;
5054
+ const file = whole?.file ?? (seg.audio === "clip" ? seg.file : seg.audio === "narration" ? seg.voice : null);
5055
+ if (!file) return null;
5056
+ const audio = clockOf(measureMedia(path.resolve(cut.textStage.dir, file))).audio;
5057
+ const end = whole ? endFrame / FPS - whole.startSec : (seg.inSec ?? 0) + seg.seconds;
5058
+ return audio && audio.decodedSec + 1 / EDIT_SAMPLE_RATE >= end ? seg : null;
5059
+ }
5060
+
4076
5061
  /** Required spoken lines are checked against what the route-owned track retained, not proportional scene durations. */
4077
5062
  export function finishFulfillment(cut, board, policy, temporal = null) {
4078
5063
  const words = wordRows(cut.timeline);
4079
5064
  const holds = [];
4080
5065
  const lines = [];
4081
- const tokens = (text) => spokenTokens(scriptWords(text)).tokens;
4082
5066
  for (const line of board.script ?? []) {
4083
5067
  const kept = words.filter((w) => w.lineId === line.lineId);
4084
- const want = tokens(line.text);
4085
- const got = tokens(kept.map((w) => w.text).join(" "));
4086
- const exact = want.length === got.length && want.every((token, i) => token === got[i]);
5068
+ const scripted = spokenTokens(scriptWords(line.text));
5069
+ const retained = spokenTokens(scriptWords(kept.map((w) => w.text).join(" ")));
5070
+ // Reuse exact split/join normalization, without fuzzy-name or misheard
5071
+ // substitutions. Compare every retained token within this canonical owner:
5072
+ // ownership alone cannot excuse omitted, extra or borrowed words.
5073
+ const heard = joinSplitWords(retained, scripted.tokens);
5074
+ const expected = joinSplitWords(scripted, heard.tokens);
5075
+ const exact = expected.tokens.length === heard.tokens.length &&
5076
+ expected.tokens.every((token, i) => token === heard.tokens[i]);
4087
5077
  lines.push({ lineId: line.lineId, sourceSpan: line.sourceSpan ?? null, retained: kept.map((w) => w.text), exactCanonicalWords: exact });
4088
5078
  if (!exact) holds.push({ code: "required-words-not-retained", id: line.lineId, reason: `the final audible route does not retain the complete approved line: ${line.text}` });
4089
5079
  }
4090
5080
  const beats = [];
4091
- for (const beat of policy.planningBudget?.beats ?? []) {
4092
- const owners = (board.scenes ?? []).filter((s) => s.planningBeatIds?.includes(beat.id));
4093
- const windows = sceneTimes(cut.timeline).filter((s) => owners.some((o) => o.sceneIndex === s.scene))
5081
+ const selectedJobs = selectedPictureJobs(cut.manifest, board, policy.planningBudget);
5082
+ const planned = selectedJobs ?? policy.planningBudget?.beats ?? [];
5083
+ const narratedReservations = cut.timeline.reservedNarration === true;
5084
+ for (const beat of planned) {
5085
+ const owners = selectedJobs ? beat.owners : (board.scenes ?? []).filter((s) => s.planningBeatIds?.includes(beat.id));
5086
+ const windows = sceneTimes(cut.timeline, cut.textStage?.frames !== undefined).filter((s) => owners.some((o) => o.sceneIndex === s.scene))
4094
5087
  .map((s) => ({ start: s.startSec, end: s.startSec + s.seconds }));
4095
5088
  const seconds = windows.reduce((sum, w) => sum + w.end - w.start, 0);
4096
- const reserved = beat.silent?.seconds ?? beat.picture?.seconds ?? null;
4097
- const retained = owners.length > 0 && windows.length > 0 && (reserved === null || seconds * FPS + 1e-6 >= Math.ceil(reserved * FPS - 1e-6));
5089
+ const reserved = selectedJobs ? beat.reservedSeconds : beat.silent?.seconds ?? beat.picture?.seconds ?? null;
5090
+ // The receipt retains its authored reservation. Short sources lower only
5091
+ // the retention floor; shortening available reserved footage still holds.
5092
+ const ownedSegments = cut.timeline.spine.filter((seg) => owners.some((owner) => owner.sceneIndex === seg.sceneIndex));
5093
+ const nativeReservation = cut.manifest?.templateMotion?.enabled === true && ownedSegments.length > 0 &&
5094
+ ownedSegments.every((seg) => seg.source === "clip" && seg.audio === "clip" && !seg.wholeVoice);
5095
+ let footage = narratedReservations ? ownedSegments
5096
+ .reduce((sum, seg) => sum + floorFrame(Math.max(0, (seg.clipSeconds ?? 0) - (seg.inSec ?? 0))), 0) : null;
5097
+ if (nativeReservation) {
5098
+ // Split pieces share one source EOF: count each take's available picture
5099
+ // once, not the same tail again for every resumed piece.
5100
+ const takes = new Map();
5101
+ for (const seg of ownedSegments) {
5102
+ const available = floorFrame(Math.max(0, seg.clipSeconds - seg.inSec));
5103
+ takes.set(seg.sceneIndex, Math.max(takes.get(seg.sceneIndex) ?? 0, available));
5104
+ }
5105
+ footage = [...takes.values()].reduce((sum, seconds) => sum + seconds, 0);
5106
+ }
5107
+ const floor = footage === null ? reserved : Math.min(reserved, footage);
5108
+ const retained = owners.length > 0 && windows.length > 0 && seconds > 0 && (floor === null || seconds * FPS + 1e-6 >= Math.ceil(floor * FPS - 1e-6));
4098
5109
  const pictureAction = beat.picture?.binding === "concrete"
4099
5110
  ? board.pictureBindings?.find((binding) => binding.beatId === beat.id)?.picture?.action
4100
5111
  : beat.picture?.action;
5112
+ const needsAction = selectedJobs
5113
+ ? owners.some((scene) => narratedReservations ? scene.actionPhases?.length > 0 : hasText(scene.subjectAction) || hasText(scene.videoPrompt))
5114
+ : Boolean(beat.picture?.action);
4101
5115
  const observed = owners.length > 0 && owners.every((scene) => {
5116
+ if (narratedReservations && !scene.actionPhases?.length) return true;
5117
+ const actualAction = selectedJobs ? scene.subjectAction || scene.videoPrompt : pictureAction;
4102
5118
  const phases = temporal?.owners?.filter((owner) => owner.sceneIndex === scene.sceneIndex) ?? [];
4103
5119
  return phases.length > 0 && phases.every((phase) => phasePassed(temporal, phase.id)) &&
4104
- (!beat.picture?.action || hasText(pictureAction) && temporal.requirements.some((phase) =>
4105
- phase.description.trim() === pictureAction.trim() && phases.some((owner) => owner.id === phase.id) && phasePassed(temporal, phase.id)));
5120
+ (!needsAction || hasText(actualAction) && temporal.requirements.some((phase) =>
5121
+ phase.description.trim() === actualAction.trim() && phases.some((owner) => owner.id === phase.id) && phasePassed(temporal, phase.id)));
4106
5122
  });
4107
- beats.push({ id: beat.id, windows, seconds, reservedSeconds: reserved, retained, action: beat.picture?.action ? observed ? "observed" : "not-temporally-observed" : "not-asked" });
5123
+ beats.push({ id: beat.id, windows, seconds, reservedSeconds: reserved, retained, action: needsAction ? observed ? "observed" : "not-temporally-observed" : "not-asked" });
4108
5124
  if (!retained) holds.push({ code: "required-beat-not-retained", id: beat.id, reason: "the required picture/action/hold reservation is missing or shortened in the actual edit" });
4109
- if (beat.picture?.action && !observed) holds.push({ code: "required-action-unobserved", id: beat.id, reason: "no exact-source dense observation proves the authored contact/completion/hold/tail phases" });
5125
+ if (needsAction && !observed) holds.push({ code: "required-action-unobserved", id: beat.id, reason: "no exact-source dense observation proves the authored contact/completion/hold/tail phases" });
4110
5126
  if (beat.picture?.finalWordTailSec > 0) {
4111
5127
  const owner = finalWordTailScene(board, beat);
4112
- const endFrame = cut.sourceMap?.frames;
5128
+ const endFrame = cut.sourceMap?.frames ?? cut.textStage?.frames;
4113
5129
  const endSec = endFrame / FPS;
4114
5130
  const finalWord = words.length ? Math.max(...words.map((word) => word.end)) : null;
4115
5131
  const finalPiece = owner && cut.sourceMap?.pieces?.find((piece) =>
4116
5132
  piece.sceneIndex === owner.sceneIndex && piece.edit.startFrame + piece.edit.frames === endFrame &&
4117
5133
  piece.picture && !piece.picture.editorialHold && piece.picture.heldFrames === 0 &&
4118
5134
  piece.sound && piece.sound.tailSilenceSamples === 0 && ["clip", "narration"].includes(piece.route.audio));
5135
+ const finalSource = owner && unmappedTerminalSource(cut, owner.sceneIndex, endFrame, true);
4119
5136
  const finalOwnedWord = finalWord !== null && words.some((word) =>
4120
5137
  word.scene === owner?.sceneIndex && Math.abs(word.end - finalWord) < 1e-7);
4121
- const visibleTail = finalWord !== null && sceneTimes(cut.timeline).some((window) =>
5138
+ const visibleTail = finalWord !== null && sceneTimes(cut.timeline, cut.textStage?.frames !== undefined).some((window) =>
4122
5139
  window.scene === owner?.sceneIndex && window.startSec <= finalWord + 1e-7 &&
4123
5140
  window.startSec + window.seconds >= endSec - 1e-7);
4124
5141
  const livingTail = temporal?.requirements?.some((phase) =>
@@ -4126,8 +5143,8 @@ export function finishFulfillment(cut, board, policy, temporal = null) {
4126
5143
  temporal.owners?.some((binding) => binding.id === phase.id && binding.sceneIndex === owner?.sceneIndex) &&
4127
5144
  finalWord !== null && Math.abs(phase.startSec - finalWord) < 1e-6 &&
4128
5145
  Math.abs(phase.endSec - endSec) < 1e-6 && phasePassed(temporal, phase.id));
4129
- if (!finalPiece || !finalOwnedWord || !visibleTail || !livingTail ||
4130
- finalPiece.edit.startFrame / FPS > finalWord + 1e-7 ||
5146
+ if (!(finalPiece || finalSource) || !finalOwnedWord || !visibleTail || !livingTail ||
5147
+ (finalPiece ? finalPiece.edit.startFrame / FPS : finalSource?.startSec ?? Infinity) > finalWord + 1e-7 ||
4131
5148
  endSec - finalWord + 1e-7 < beat.picture.finalWordTailSec) {
4132
5149
  holds.push({ code: "final-word-tail-unproven", id: beat.id,
4133
5150
  reason: `the selected living suffix requires at least ${beat.picture.finalWordTailSec}s after the complete final heard word inside the same final voiced take, fully visible through EOF with exact-source living-tail observation; no appended silent picture or frozen fill` });
@@ -4136,12 +5153,13 @@ export function finishFulfillment(cut, board, policy, temporal = null) {
4136
5153
  }
4137
5154
  let ending = { status: "unselected" };
4138
5155
  if (policy.outputPermission?.ending?.mode === "native") {
4139
- const terminalBeat = policy.planningBudget?.beats?.at(-1);
4140
- const owners = (board.scenes ?? []).filter((scene) => scene.planningBeatIds?.includes(terminalBeat?.id));
4141
- const owner = owners.length === 1 ? owners[0] : null;
4142
- const visible = sceneTimes(cut.timeline);
5156
+ const terminalBeat = planned.at(-1);
5157
+ const owners = selectedJobs ? terminalBeat?.owners ?? []
5158
+ : (board.scenes ?? []).filter((scene) => scene.planningBeatIds?.includes(terminalBeat?.id));
5159
+ const owner = selectedJobs ? owners.at(-1) ?? null : owners.length === 1 ? owners[0] : null;
5160
+ const visible = sceneTimes(cut.timeline, cut.textStage?.frames !== undefined);
4143
5161
  const last = visible.at(-1);
4144
- const endFrame = cut.sourceMap?.frames;
5162
+ const endFrame = cut.sourceMap?.frames ?? cut.textStage?.frames;
4145
5163
  const endSec = endFrame / FPS;
4146
5164
  const spans = owner ? visible.filter((span) => span.scene === owner.sceneIndex) : [];
4147
5165
  const uninterrupted = spans.length === 1 && last === spans[0] &&
@@ -4164,15 +5182,22 @@ export function finishFulfillment(cut, board, policy, temporal = null) {
4164
5182
  temporal.owners?.some((binding) => binding.id === phase.id && binding.sceneIndex === releaseScene) &&
4165
5183
  phase.startSec >= releaseStart - 1e-7 && Math.abs(phase.endSec - releaseEnd) < 1e-7 &&
4166
5184
  phasePassed(temporal, phase.id));
4167
- const mapped = !!(terminalPiece || terminalOverlay) && (policy.outputPermission.ending.kind === "editorial-hold" ? heldProven : !held);
4168
- const fulfilled = !!terminalBeat && uninterrupted && !!release && !!mapped &&
5185
+ // Real narration boards have no phase contract unless one was authored.
5186
+ // Keep exact-source release proof mandatory for authored phases and every
5187
+ // other route; an absent narration phase is not an unfulfillable promise.
5188
+ const needsRelease = !narratedReservations || (board.scenes ?? []).some((scene) =>
5189
+ scene.sceneIndex === releaseScene && scene.actionPhases?.length > 0);
5190
+ const retainedPicture = cut.sourceMap
5191
+ ? !!(terminalPiece || terminalOverlay) && (policy.outputPermission.ending.kind === "editorial-hold" ? heldProven : !held)
5192
+ : policy.outputPermission.ending.kind !== "editorial-hold" && !!owner && !!unmappedTerminalSource(cut, owner.sceneIndex, endFrame);
5193
+ const fulfilled = !!terminalBeat && uninterrupted && (!needsRelease || !!release) && !!retainedPicture &&
4169
5194
  lines.every((line) => line.exactCanonicalWords);
4170
5195
  ending = { kind: policy.outputPermission.ending.kind, status: fulfilled ? "fulfilled" : "hold",
4171
5196
  beatId: terminalBeat?.id ?? null, sceneIndex: owner?.sceneIndex ?? null,
4172
5197
  startFrame: last ? Math.round(last.startSec * FPS) : null, endFrame: endFrame ?? null,
4173
5198
  phaseId: release?.id ?? null };
4174
5199
  if (!fulfilled) holds.push({ code: "native-ending-unproven", id: terminalBeat?.id ?? "ending",
4175
- reason: "native permission is not fulfillment: the selected terminal job must be the uninterrupted final mapped picture through EOF, with complete approved words and an exact-source observed completion/release" });
5200
+ reason: "native permission is not fulfillment: the selected terminal job must be the uninterrupted final retained picture through decoded EOF, with complete approved words and any required exact-source completion/release proof" });
4176
5201
  }
4177
5202
  for (const phase of temporal?.requirements ?? []) {
4178
5203
  if (!phasePassed(temporal, phase.id)) holds.push({ code: "temporal-observation-unproven", id: phase.id, reason: temporal.receipt?.phases.find((p) => p.id === phase.id)?.reason ?? "the required temporal phase was not observed on this exact source" });
@@ -4206,7 +5231,29 @@ export function finishFulfillment(cut, board, policy, temporal = null) {
4206
5231
  * changes only pixels via ASS and STREAM-COPIES its already finished audio.
4207
5232
  */
4208
5233
  export async function completeFirstCutText(cut, { out, prepareReadoutVerifier, observeTemporalAction, checkTemporalRetention, renderer: suppliedRenderer } = {}) {
4209
- if (!cut.textStage || !cut.sourceMap?.render || !cut.timeline?.mapped) throw new FirstCutError(1, "Text completion requires the prepared mapped pre-text artifact");
5234
+ if (!cut.textStage || (!cut.sourceMap?.render && cut.manifest?.templateMotion?.enabled !== true)) {
5235
+ throw new FirstCutError(1, "Text completion requires the prepared mapped or trusted Template pre-text artifact");
5236
+ }
5237
+ const originalTimeline = cut.timeline;
5238
+ const prepared = cut.sourceMap ? null : renderFindings(null, measureMedia(cut.out));
5239
+ if (prepared?.findings.some((finding) => finding.severity === "refuse")) {
5240
+ throw new SourceMapRefusal("The prepared native/song cut has no usable decoded clock", prepared.findings);
5241
+ }
5242
+ if (prepared) {
5243
+ const speed = cut.textStage.speed ?? 1;
5244
+ const scale = (seg) => ({
5245
+ ...seg, startSec: seg.startSec / speed, seconds: seg.seconds / speed, inSec: (seg.inSec ?? 0) / speed,
5246
+ ...(seg.words ? { words: seg.words.map((word) => ({
5247
+ ...word, s: word.s / speed, e: word.e / speed,
5248
+ ...(Number.isFinite(word.phoneEndAfterSec) ? { phoneEndAfterSec: word.phoneEndAfterSec / speed } : {}),
5249
+ })) } : {}),
5250
+ ...(seg.cuts ? { cuts: seg.cuts.map((gap) => ({ ...gap, s: gap.s / speed, e: gap.e / speed })) } : {}),
5251
+ });
5252
+ cut = {
5253
+ ...cut, textStage: { ...cut.textStage, frames: prepared.render.frames },
5254
+ ...(speed !== 1 ? { timeline: { ...cut.timeline, spine: cut.timeline.spine.map(scale), cutaways: cut.timeline.cutaways.map(scale) } } : {}),
5255
+ };
5256
+ }
4210
5257
  const { dir, board, policy } = cut.textStage;
4211
5258
  if (!board || !Array.isArray(board.scenes)) throw new TextFinishRefusal([{ code: "approved-board-missing", id: "cut", reason: "the approved board is required to retain selected words, actions and text" }]);
4212
5259
  const material = textMaterial(cut, board, policy);
@@ -4291,44 +5338,55 @@ export async function completeFirstCutText(cut, { out, prepareReadoutVerifier, o
4291
5338
  // subsequent transform. The audio stream is copied, not AAC-encoded again.
4292
5339
  if (createHash("sha256").update(readFileSync(cut.out)).digest("hex") !== source.sha256) throw new FirstCutError(1, "The pre-text source changed while its observation was being used");
4293
5340
  const { findings, render, planningTiming } = renderFindings(cut.sourceMap, measureMedia(destination), policy.planningBudget);
4294
- cut.sourceMap.findings.push(...findings);
4295
- cut.sourceMap.render = render;
4296
- if (planningTiming) cut.sourceMap.planningTiming = planningTiming;
4297
- const rasters = [
4298
- ...directives.captions.map((c, i) => ({ ...c, id: `caption:${i}`, kind: "caption", startSec: c.s, endSec: c.e, renderBox: material.captions.cap?.box })),
4299
- ...[...directives.cards, directives.hook, directives.endCard].filter(Boolean).map((c) => ({ ...c, startSec: c.in, endSec: c.out })),
4300
- ].map((entry) => {
4301
- const { lines, ...measurement } = entry.measurement;
4302
- return {
4303
- id: entry.id, kind: entry.kind, startSec: entry.startSec, endSec: entry.endSec,
4304
- renderBox: entry.renderBox, box: entry.box, fs: entry.fs,
4305
- textSha256: createHash("sha256").update(JSON.stringify(entry.lines)).digest("hex"), measurement,
4306
- raster: textFrameWindow(entry.startSec, entry.endSec),
5341
+ if (prepared && (render.frames !== prepared.render.frames || render.samples !== prepared.render.samples ||
5342
+ render.soundStartSamples !== prepared.render.soundStartSamples)) {
5343
+ findings.push({ severity: "refuse", code: "text-render-clock-changed", scene: null, detail: "the native/song text burn changed its prepared picture or copied-audio clock" });
5344
+ }
5345
+ if (!cut.sourceMap && findings.some((finding) => finding.severity === "refuse")) {
5346
+ throw new SourceMapRefusal("The native/song text finish cannot retain its exact prepared clock", findings);
5347
+ }
5348
+ if (cut.sourceMap) {
5349
+ cut.sourceMap.findings.push(...findings);
5350
+ cut.sourceMap.render = render;
5351
+ if (planningTiming) cut.sourceMap.planningTiming = planningTiming;
5352
+ const rasters = [
5353
+ ...directives.captions.map((c, i) => ({ ...c, id: `caption:${i}`, kind: "caption", startSec: c.s, endSec: c.e, renderBox: material.captions.cap?.box })),
5354
+ ...[...directives.cards, directives.hook, directives.endCard].filter(Boolean).map((c) => ({ ...c, startSec: c.in, endSec: c.out })),
5355
+ ].map((entry) => {
5356
+ const { lines, ...measurement } = entry.measurement;
5357
+ return {
5358
+ id: entry.id, kind: entry.kind, startSec: entry.startSec, endSec: entry.endSec,
5359
+ renderBox: entry.renderBox, box: entry.box, fs: entry.fs,
5360
+ textSha256: createHash("sha256").update(JSON.stringify(entry.lines)).digest("hex"), measurement,
5361
+ raster: textFrameWindow(entry.startSec, entry.endSec),
5362
+ };
5363
+ });
5364
+ cut.sourceMap.text = {
5365
+ source, report: path.basename(reportPath), permissions: text.permissions, fulfillment,
5366
+ styleVersion: "literal-ass.v1", assSha256, rasters, readouts: text.readouts,
4307
5367
  };
4308
- });
4309
- cut.sourceMap.text = {
4310
- source, report: path.basename(reportPath), permissions: text.permissions, fulfillment,
4311
- styleVersion: "literal-ass.v1", assSha256, rasters, readouts: text.readouts,
4312
- };
4313
- const planningResolved = (finding) => fulfillment.resolvedPlanningFindings.some((p) => p.scene === finding.scene && p.code === finding.code);
4314
- cut.sourceMap.semanticPlanningFindings = cut.sourceMap.findings.filter(planningResolved);
4315
- cut.sourceMap.findings = cut.sourceMap.findings.filter((finding) => !planningResolved(finding));
4316
- cut.sourceMap.temporal = temporal;
4317
- try {
4318
- cut.sourceMap.final = { sha256: createHash("sha256").update(readFileSync(destination)).digest("hex") };
4319
- } catch (error) {
4320
- if (!planningTiming) throw error;
4321
- delete cut.sourceMap.final;
4322
- planningTiming.status = "unknown";
4323
- planningTiming.decoded = null;
4324
- cut.sourceMap.findings.push({ severity: "refuse", code: "planning-budget-clock-unmeasured", scene: null, detail: "the actual final bytes could not be hashed, so the final decoded timing is not byte-bound" });
5368
+ const planningResolved = (finding) => fulfillment.resolvedPlanningFindings.some((p) => p.scene === finding.scene && p.code === finding.code);
5369
+ cut.sourceMap.semanticPlanningFindings = cut.sourceMap.findings.filter(planningResolved);
5370
+ cut.sourceMap.findings = cut.sourceMap.findings.filter((finding) => !planningResolved(finding));
5371
+ cut.sourceMap.temporal = temporal;
5372
+ try {
5373
+ cut.sourceMap.final = { sha256: createHash("sha256").update(readFileSync(destination)).digest("hex") };
5374
+ } catch (error) {
5375
+ if (!planningTiming) throw error;
5376
+ delete cut.sourceMap.final;
5377
+ planningTiming.status = "unknown";
5378
+ planningTiming.decoded = null;
5379
+ cut.sourceMap.findings.push({ severity: "refuse", code: "planning-budget-clock-unmeasured", scene: null, detail: "the actual final bytes could not be hashed, so the final decoded timing is not byte-bound" });
5380
+ }
5381
+ if (planningTiming) planningTiming.finalSha256 = cut.sourceMap.final?.sha256 ?? null;
5382
+ const mapSidecar = sourceMapPath(destination);
5383
+ writeFileSync(mapSidecar, `${JSON.stringify(cut.sourceMap, null, 2)}\n`);
5384
+ refuseUnholdable(cut.sourceMap);
4325
5385
  }
4326
- if (planningTiming) planningTiming.finalSha256 = cut.sourceMap.final?.sha256 ?? null;
4327
- const sidecars = { ...sidecarPaths(destination), sourceMap: sourceMapPath(destination), text: reportPath };
4328
- writeFileSync(sidecars.sourceMap, `${JSON.stringify(cut.sourceMap, null, 2)}\n`);
4329
- refuseUnholdable(cut.sourceMap);
5386
+ const sidecars = { ...sidecarPaths(destination), ...(cut.sourceMap ? { sourceMap: sourceMapPath(destination) } : {}), text: reportPath };
5387
+ if (!cut.sourceMap && planningTiming) planningTiming.finalSha256 = createHash("sha256").update(readFileSync(destination)).digest("hex");
4330
5388
  writeFileSync(sidecars.words, `${JSON.stringify(material.words, null, 2)}\n`);
4331
- writeFileSync(sidecars.scenes, `${JSON.stringify(sceneTimes(cut.timeline), null, 2)}\n`);
5389
+ writeFileSync(sidecars.scenes, `${JSON.stringify(sceneTimes(cut.timeline, cut.textStage?.frames !== undefined), null, 2)}\n`);
4332
5390
  writeFileSync(sidecars.srt, toSrt(material.words));
4333
5391
  const { textStage, ...finished } = cut;
4334
5392
  const paths = finishPaths(destination);
@@ -4345,7 +5403,13 @@ export async function completeFirstCutText(cut, { out, prepareReadoutVerifier, o
4345
5403
  }
4346
5404
  if (finished.pace) writeFileSync(paths.pace, `${JSON.stringify(finished.pace, null, 2)}\n`);
4347
5405
  if (finished.sound) writeFileSync(paths.sound, `${JSON.stringify(finished.sound, null, 2)}\n`);
4348
- return { ...finished, out: destination, durationSec: cut.sourceMap.frames / FPS, sourceMap: cut.sourceMap, sidecars, text };
5406
+ return {
5407
+ ...finished, timeline: originalTimeline, out: destination,
5408
+ durationSec: (cut.sourceMap?.frames ?? render.frames) / FPS,
5409
+ ...(cut.sourceMap ? { sourceMap: cut.sourceMap } : {}),
5410
+ ...(!cut.sourceMap && planningTiming ? { planningTiming } : {}),
5411
+ sidecars, text,
5412
+ };
4349
5413
  }
4350
5414
 
4351
5415
  function parseArgs(argv) {
@@ -4419,9 +5483,21 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
4419
5483
  const finish = finishingOf(manifest);
4420
5484
  const sourceAd = sourceAdOf(manifest);
4421
5485
  const wholeVoice = wholeVoiceOf(manifest, readJson, exists);
5486
+ // A template song retains its decoded musical release. Audio container
5487
+ // durations can include padding or only estimate VBR/ADTS length; neither is
5488
+ // a sample clock. Reuse the mapped reader without opting this cut into maps.
5489
+ if (manifest.templateMotion?.enabled === true && wholeVoice && manifest.sourceMap !== true) {
5490
+ const clock = clockOf(measureMedia(here(wholeVoice.file)));
5491
+ const problem = clockProblem(clock, "audio");
5492
+ if (problem) throw new SourceMapRefusal("The whole song's decoded EOF could not be measured", [
5493
+ { severity: "refuse", code: "unmeasured-whole-voice", scene: null, detail: `${wholeVoice.file}: ${problem}` },
5494
+ ]);
5495
+ wholeVoice.seconds = clock.audio.decodedSec;
5496
+ }
4422
5497
  if (manifest.wholeVoice === true && !wholeVoice) {
4423
5498
  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.");
4424
5499
  }
5500
+ if (manifest.templateMotion?.narratedShotList === true) narrationFinishClock({ dir, manifest, storyboard });
4425
5501
  const readClocks = manifest.sourceMap === true ? mediaReader(here, exists) : null;
4426
5502
  const timeline = buildTimeline({
4427
5503
  manifest,
@@ -4443,13 +5519,18 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
4443
5519
  if (timeline.spine.length === 0) {
4444
5520
  throw new FirstCutError(1, `No scene has a clip or a keyframe. Check: exodus video status ${manifest.runId}`);
4445
5521
  }
5522
+ protectTemplateMotion(timeline, manifest, storyboard);
5523
+ const stagedText = prepareText && (timeline.mapped || (manifest.templateMotion?.enabled === true &&
5524
+ (storyboard?.audio?.captionStyle || storyboard?.overlays?.some((overlay) => overlay.render))));
4446
5525
 
4447
5526
  // The finishing settings' part of the render: the speed, the burned
4448
5527
  // captions and cards, and where the mix goes before its loudness pass.
4449
5528
  const notes = [];
4450
5529
  const pictureSec = timeline.spine.reduce((sum, seg) => sum + seg.seconds, 0);
4451
- // A voice laid whole is a song: it is never sped up.
4452
- const speed = finish?.cutPace && !timeline.wholeVoice ? speedFor(pictureSec, finish.targetSeconds) : 1;
5530
+ // A song and selected semantic action clocks are never globally accelerated.
5531
+ const protectedMotion = [...timeline.spine, ...timeline.cutaways].some((s) => s.motionProtection);
5532
+ const speed = finish?.cutPace && !timeline.wholeVoice && !protectedMotion ? speedFor(pictureSec, finish.targetSeconds) : 1;
5533
+ if (finish?.cutPace && protectedMotion) notes.push("selected action/listening/ending obligations keep their real-time clock; cutPace does not accelerate the whole cut");
4453
5534
  if (timeline.sourceMap) {
4454
5535
  // The exact bytes every measured clock was read from, for whoever binds an approval to them.
4455
5536
  timeline.sourceMap.sources = readClocks.files().filter(exists).map((file) => {
@@ -4492,8 +5573,9 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
4492
5573
  notes.push(...speedUpSlowBroll(timeline, motionOf, (file) => probe(file).seconds));
4493
5574
  }
4494
5575
  let ass = null;
4495
- // The caption screens burned in, which cutChecks reads.
5576
+ // The caption screens and text cards burned in, which cutChecks reads.
4496
5577
  let burned = null;
5578
+ let burnedCards = [];
4497
5579
  // A copied ad: its words in the record's boxes and its product photos from the product sheet.
4498
5580
  const copied = sourceAd && finish?.cards ? sourceAdEdit(storyboard, sourceAd, timeline.sourceGroups ?? []) : null;
4499
5581
  if (copied) notes.push(...copied.notes);
@@ -4509,27 +5591,41 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
4509
5591
  notes.push(`the pull has no product sheet picture, so the source's ${sourceAd.pictures.length} product card(s) were not drawn`);
4510
5592
  }
4511
5593
  }
4512
- if (finish && (finish.captions || finish.cards) && !(prepareText && timeline.mapped)) {
5594
+ // endCardLayers (Plan 14): the pack photo the worker downloaded, its words drawn by code.
5595
+ const brand = finish?.endCardLayers || finish?.brandCheck ? brandOf(manifest) : null;
5596
+ const packPhoto = Boolean(brand?.packPhoto) && exists(brand.packPhoto);
5597
+ let endLayers = null;
5598
+ if (finish?.endCardLayers) {
5599
+ endLayers = packPhoto ? endCardLayersOf(brand, storyboard, pictureSec, speed) : null;
5600
+ if (!endLayers) notes.push("the pull has no pack photo of the product, so the end card was not drawn");
5601
+ }
5602
+ if (finish && (finish.captions || finish.cards || endLayers) && !stagedText) {
4513
5603
  if (hasFilter("subtitles")) {
4514
5604
  ass = "edit.ass";
4515
5605
  const cap = sourceAd?.captions ?? null;
4516
- const screens = !finish.captions
5606
+ const screens = captionsBeforeEndCard(!finish.captions
4517
5607
  ? []
4518
5608
  : finish.captionsByLine
4519
5609
  ? lineCaptionScreens(lineWords(timeline, storyboard), cap)
4520
- : captionScreens(wordRows(timeline), cap);
5610
+ : captionScreens(wordRows(timeline), cap), endLayers);
4521
5611
  if (finish.captions) burned = screens;
5612
+ burnedCards = finish.cards ? (copied ? copied.cards : storyboardCards(storyboard, timeline)) : [];
4522
5613
  writeFileSync(here(ass), editAss({
4523
5614
  captions: screens,
4524
5615
  cap,
4525
5616
  ...(finish.captionsByLine ? { capMaxShare: CAPTION_MAX_SHARE } : {}),
4526
- cards: finish.cards ? (copied ? copied.cards : storyboardCards(storyboard, timeline)) : [],
5617
+ cards: burnedCards,
4527
5618
  hook: copied?.hook ?? null,
4528
- // A copied ad has an end card only where its source does.
4529
- endCard: finish.cards && (!sourceAd || sourceAd.endCard) ? endCardOf(storyboard, pictureSec) : null,
5619
+ // A copied ad has an end card only where its source does. The pack-photo end card takes the CTA words' place.
5620
+ endCard: !endLayers && finish.cards && (!sourceAd || sourceAd.endCard) ? endCardOf(storyboard, pictureSec) : null,
5621
+ endLayers,
4530
5622
  }));
4531
5623
  } else {
4532
- notes.push("this ffmpeg has no subtitles filter (libass), so the captions and cards were not burned in");
5624
+ notes.push(
5625
+ endLayers
5626
+ ? "this ffmpeg has no subtitles filter (libass), so the captions, cards and the end card's words were not burned in"
5627
+ : "this ffmpeg has no subtitles filter (libass), so the captions and cards were not burned in",
5628
+ );
4533
5629
  }
4534
5630
  }
4535
5631
  // cleanVoice, where this ffmpeg has the filters. A voice laid whole is a song, kept as it is.
@@ -4547,7 +5643,10 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
4547
5643
  }
4548
5644
  const paths = finishPaths(out);
4549
5645
  const rendered = finish?.finishLoudness ? paths.mix : out;
4550
- const meters = finish?.cutChecks ? { voice: paths.voice, bed: paths.bed } : null;
5646
+ const meters = finish?.cutChecks ? {
5647
+ voice: paths.voice, bed: paths.bed,
5648
+ ...(manifest.templateMotion?.enabled === true ? { allowAbsentBed: true } : {}),
5649
+ } : null;
4551
5650
  // The burn reads its file from the folder ffmpeg runs in, so every path is absolute then.
4552
5651
  const at = ass ? (file) => path.resolve(dir, file) : here;
4553
5652
 
@@ -4594,6 +5693,12 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
4594
5693
  mixSound.effects = mixSound.effects.map((e) => {
4595
5694
  const decoded = record(e.file, e.source);
4596
5695
  const event = e.source?.kind === "library" ? e.source.eventWindow : null;
5696
+ if (Number.isFinite(e.durationSec) && Math.round(e.durationSec * EDIT_SAMPLE_RATE) >
5697
+ decoded.sourceSamples - decoded.sourcePrimingSamples) {
5698
+ throw new SourceMapRefusal("An authored sound duration exceeds its actual decoded asset", [
5699
+ { severity: "refuse", code: "sound-duration-eof", scene: null, detail: e.file },
5700
+ ]);
5701
+ }
4597
5702
  if (event && e.atSec + event.toSec > timeline.sourceMap.frames / FPS + 1 / EDIT_SAMPLE_RATE) {
4598
5703
  throw new SourceMapRefusal("A required registered sound event would be truncated by the final cut", [
4599
5704
  { severity: "refuse", code: "sound-event-cutoff", scene: e.fromScene ?? null, detail: e.file },
@@ -4625,11 +5730,16 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
4625
5730
  finish: {
4626
5731
  levelSegments: !finish.finishLoudness, ass, speed,
4627
5732
  ...(pictures.length ? { pictures } : {}), ...(clean ? { clean } : {}), ...(meters ? { meters } : {}),
5733
+ ...(endLayers ? { endCard: endLayers } : {}),
4628
5734
  },
4629
5735
  }
4630
5736
  : {}),
4631
5737
  ...(mixSound ? { sound: mixSound } : {}),
4632
5738
  });
5739
+ if (meters?.allowAbsentBed) {
5740
+ rmSync(meters.voice, { force: true });
5741
+ rmSync(meters.bed, { force: true });
5742
+ }
4633
5743
  const res = spawnSync("ffmpeg", ffmpegArgs, { stdio: "inherit", ...(ass ? { cwd: path.resolve(dir) } : {}) });
4634
5744
  if (res.status !== 0) {
4635
5745
  const quoted = ffmpegArgs.map((a) => (/[\s\[\];]/.test(a) ? `'${a}'` : a)).join(" ");
@@ -4637,7 +5747,7 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
4637
5747
  }
4638
5748
  let meterLevels = null;
4639
5749
  if (meters) {
4640
- meterLevels = { voiceLufs: measureLufs(meters.voice), bedLufs: measureLufs(meters.bed) };
5750
+ meterLevels = { voiceLufs: measureLufs(meters.voice), bedLufs: meters.allowAbsentBed && !existsSync(meters.bed) ? null : measureLufs(meters.bed) };
4641
5751
  rmSync(meters.voice, { force: true });
4642
5752
  rmSync(meters.bed, { force: true });
4643
5753
  }
@@ -4711,14 +5821,19 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
4711
5821
  const withSound = (cut0) => {
4712
5822
  const cut = {
4713
5823
  ...cut0, ...(timeline.sourceMap ? { sourceMap: timeline.sourceMap } : {}),
4714
- ...(prepareText && timeline.mapped ? { textStage: { dir: path.resolve(dir), board: storyboard, policy: { ...manifest.textFinish, outputPermission: manifest.outputPermission, planningBudget: manifest.planningBudget } } } : {}),
5824
+ ...(manifest.templateMotion?.enabled === true ? { motion: templateMotionReport(timeline, storyboard, speed) } : {}),
5825
+ ...(stagedText ? { textStage: { dir: path.resolve(dir), board: storyboard, speed, policy: { ...manifest.textFinish, ...(manifest.templateMotion?.enabled === true ? { captionsByLine: manifest.textFinish?.captionsByLine ?? finish?.captionsByLine === true } : {}), outputPermission: manifest.outputPermission, planningBudget: manifest.planningBudget } } } : {}),
4715
5826
  };
4716
5827
  return soundRecord ? { ...cut, sound: soundRecord, ...(soundRecord.master ? { musicOnlyMaster: paths.master } : {}) } : cut;
4717
5828
  };
4718
5829
  if (!finish) return withSound({ out, durationSec, manifest, timeline, music, rows, sidecars });
4719
5830
 
5831
+ const plans = finish.blipCheck ? scenePlans(storyboard) : null;
4720
5832
  const checks = finish.cutChecks
4721
- ? checkCut({ timeline, storyboard, screens: burned, rows, scenes, out, loudness, levels: meterLevels })
5833
+ ? checkCut({
5834
+ timeline, storyboard, screens: burned, rows, scenes, out, loudness, levels: meterLevels,
5835
+ finish, sourceAd, shotOf: (n) => plans?.get(n)?.sourceShot ?? null, cards: burnedCards, speed, endCard: endLayers,
5836
+ })
4722
5837
  : null;
4723
5838
  // The pace record: the cut's own numbers, the settings it was cut with (so
4724
5839
  // a redo of the run matches), its loudness once the pass ran, and its checks.
@@ -4734,6 +5849,7 @@ export function firstCut({ dir, out: outOpt, skip = new Set(), music: musicOn =
4734
5849
  finishing: finish,
4735
5850
  ...(loudness ? { loudness } : {}),
4736
5851
  ...(checks ? { checks } : {}),
5852
+ ...(finish.brandCheck ? { brand: brandRecord({ brand, packPhoto, endLayers, rows }) } : {}),
4737
5853
  ...(notes.length > 0 ? { notes } : {}),
4738
5854
  };
4739
5855
  writeFileSync(paths.pace, `${JSON.stringify(pace, null, 2)}\n`);