// A plan that leaves more than ten seconds with no effect is filled in code, not sent back to the model: a punch-in or a // title the plan model was shown, on the stretch's key word, never the kind of the effect beside it. import { cutoutsSpaced, fillKindAt } from "./count.js"; import { defaultsOf } from "./defaults.js"; import { OPENING_S } from "./cutouts.js"; import { onGrid } from "./grid.js"; import { nextPicks } from "./nextPick.js"; import type { Moment } from "./pick.js"; import { isObject, type JsonObject, listOf } from "./json.js"; import { type Effect, isKind, TAKEOVERS, TITLES } from "./library.js"; import { AGAIN_SECONDS, EMPTY_SECONDS, emptied, holesIn } from "./plan.js"; import { ridesFingers } from "./said.js"; import { CARD_MOST, coversPerson } from "./scene.js"; import { shotsOf } from "./shots.js"; import type { Word } from "./words.js"; const PUNCH = "punch-in"; const PUNCH_SECONDS = 2.2; const LEAD = 0.3; // The opening is filled from its first seconds, as a plan must open by then. const OPENS_BY = 1.5; const FILLS = 12; const TITLE_WORDS = 4; /** The word to land on: the first key word said in the stretch, else its longest word. */ export function keyWordIn(words: Word[], key: number[][], from: number, to: number): Word | null { const inside = words .map((w, k) => ({ w, k })) .filter(({ w }) => w.start >= from + LEAD && w.start <= to - 1); const keyed = inside.find(({ k }) => key.some(([a = -1, b = a]) => k >= a && k <= b)); if (keyed) return keyed.w; return inside.reduce( (best, { w }) => (!best || w.text.length > best.text.length ? w : best), null, ); } /** The effect ids a plan ask lists, in its order. */ export const shownIn = (text: string): string[] => [...text.matchAll(/^- ([\w-]+) \(/gm)].map((m) => m[1] ?? ""); export interface Fill { allowed: Map; words: Word[]; seconds: number; shots: number[]; /** The effects in the order a title is looked for: those the plan model was shown first. */ shown: string[]; moments?: Moment[]; /** False once the cut-outs are counted: a fill then needs none beyond the opening's own. */ newCutouts?: boolean; } /** Whether `id` on `start` to `end` keeps 60 s from each other row of it, as the plan check counts. */ export const apartFrom = (rows: JsonObject[], id: string, start: number, end: number): boolean => rows.every( (r) => r.effect !== id || Number(r.start) - end >= AGAIN_SECONDS - 1e-3 || start - Number(r.end) >= AGAIN_SECONDS - 1e-3, ); const cutOutAllowed = (effect: Effect, end: number, fill: Fill): boolean => fill.newCutouts !== false || !effect.slots.includes("main_matte") || end <= OPENING_S + 1e-3; /** A title shorter than this is gone before it is read. */ const READ_LEAST = 1.5; const spans = (rows: JsonObject[]) => rows.map((r) => ({ start: Number(r.start), end: Number(r.end) })); export function holeIn(rows: JsonObject[], seconds: number): [number, number] | null { const first = Math.min(...rows.map((r) => Number(r.start))); if (rows.length && first > OPENS_BY + 1e-6) return [0, first]; return emptied(spans(rows), seconds, EMPTY_SECONDS); } /** `start` to as near `end` as no shot cut comes between, or null when that leaves less than `min`. */ function beforeCut(start: number, end: number, min: number, shots: number[]): number | null { const cut = shots.find((t) => t > start + 0.1 && t < end - 0.1); const to = onGrid(cut ?? end); return to - start >= min - 1e-6 ? to : null; } function titleFor( start: number, to: number, rows: JsonObject[], fill: Fill, ): { effect: Effect; end: number } | null { const used = new Set(rows.map((r) => String(r.effect))); for (const id of fill.shown) { const effect = fill.allowed.get(id); if (!effect || used.has(id) || !isKind(id, TITLES) || effect.max < READ_LEAST) continue; const end = beforeCut( start, Math.min(to, start + Math.max(effect.usual, READ_LEAST)), Math.max(effect.min, READ_LEAST), fill.shots, ); if (end !== null) return { effect, end }; } return null; } /** One row for the empty stretch `from` to `to`, and why; null when nothing fits it. */ // fallow-ignore-next-line complexity function rowFor( from: number, to: number, rows: JsonObject[], key: number[][], fill: Fill, ): { row: JsonObject; said: string } | null { const opening = from < 1e-3; const picked = pickedFor(from, to, rows, fill, fill.newCutouts !== false); if (picked) return picked; const word = keyWordIn(fill.words, key, from, opening ? Math.min(to, OPENS_BY + 2) : to) ?? (opening ? (fill.words.find((w) => w.start < to) ?? null) : null); if (!word) return standIn(from, to, rows, fill); const start = onGrid( Math.max(from, opening ? Math.min(word.start - LEAD, OPENS_BY) : word.start - LEAD), ); const gap = `${from} to ${to} s had no effect`; const punch = fill.allowed.get(PUNCH); const punchEnd = () => punch && beforeCut(start, Math.min(to, start + PUNCH_SECONDS), punch.min, fill.shots); const punched = () => { const end = punchEnd(); return end && punch && apartFrom(rows, PUNCH, start, end) && cutOutAllowed(punch, end, fill) ? { row: { effect: PUNCH, start, end, slots: {} }, said: `${gap}: a punch-in on "${word.text}" at ${start} s`, } : null; }; const titled = () => { const title = titleFor(start, to, rows, fill); return title ? { row: { effect: title.effect.id, start, end: title.end, slots: {} }, said: `${gap}: the ${title.effect.id} title on "${word.text}" at ${start} s`, } : null; }; return ( (fillKindAt(besides(rows), start) === "punch" ? (punched() ?? titled()) : (titled() ?? punched())) ?? standIn(from, to, rows, fill) ); } const textSlotsOf = (effect: Effect): string[] => listOf(effect.meta.text_slots).map(String); /** An effect a fill may put anywhere: no fingers, no takeover, at most one line of text of its own. */ function standsAlone(effect: Effect): boolean { const slots = textSlotsOf(effect); const own = slots[0] === undefined ? null : defaultsOf(effect.template)?.[slots[0]]; return ( !ridesFingers(effect) && !isKind(effect.id, TAKEOVERS) && slots.length <= 1 && (own === undefined || own === null || typeof own === "string") ); } const framed = (t: number, from: number, to: number): number => { const at = onGrid(t); return at < from - 1e-6 ? onGrid(at + 1 / 30) : at > to + 1e-6 ? onGrid(at - 1 / 30) : at; }; /** The last fill: the first effect that fits whole inside one shot of the stretch, its one line the words said there; * at the opening, in a shot that starts by then. */ function standIn( from: number, to: number, rows: JsonObject[], fill: Fill, ): { row: JsonObject; said: string } | null { const used = new Set(rows.map((r) => String(r.effect))); const latest = from < 1e-3 ? OPENS_BY : Math.min(to, from + EMPTY_SECONDS); const effects = [...new Set([...fill.shown, ...fill.allowed.keys()])] .flatMap((id) => fill.allowed.get(id) ?? []) .filter((e) => !used.has(e.id) && standsAlone(e)); const inStretch = shotsOf(fill.shots, fill.seconds) .map(([first, last]) => [Math.max(first, from), Math.min(last, to)] as const) .filter(([a, b]) => b > a && a <= latest + 1e-6) .reverse(); for (const [a, b] of inStretch) { for (const effect of effects) { const longest = coversPerson(effect) ? Math.min(effect.max, CARD_MOST) : effect.max; const run = Math.min(Math.max(effect.usual, effect.min, READ_LEAST), longest, b - a); const [start, end] = [framed(a, a, b), framed(a + run, a, b)]; const long = end - start >= Math.max(effect.min, READ_LEAST) - 1e-3; const slots = long && slotsFor(effect, fill.words, start, end); if (!slots || !apartFrom(rows, effect.id, start, end) || !cutOutAllowed(effect, end, fill)) continue; return { row: { effect: effect.id, start, end, slots }, said: `${from} to ${to} s had no effect: ${effect.id}, which fits the shot at ${start} s`, }; } } return null; } function slotsFor( effect: Effect, words: Word[], start: number, end: number, ): Record | null { const [slot] = textSlotsOf(effect); if (slot === undefined) return {}; const word = words.find((w) => w.end > start && w.start < end); const line = word ? lineFrom(words, word, end) : ""; return line ? { [slot]: line } : null; } /** Whether the fill finds a row for the empty stretch: the plan check asks for one only there. */ export const holeFillable = ( rows: JsonObject[], hole: [number, number], key: number[][], fill: Fill, ): boolean => rowFor(hole[0], hole[1], rows, key, fill) !== null; /** The first of the pick's effects for `from` to `to` that fits there, ahead of anything code makes; at the opening, * one that starts by then. */ function pickedFor( from: number, to: number, rows: JsonObject[], fill: Fill, cutouts: boolean, ): { row: JsonObject; said: string } | null { const used = new Set(rows.map((r) => String(r.effect))); const [row] = nextPicks( { start: from, end: to }, { start: from, end: to }, fill.moments ?? [], fill.allowed, (r, e) => !used.has(e.id) && !ridesFingers(e) && !isKind(e.id, TAKEOVERS) && (cutouts || !e.slots.includes("main_matte")) && !(from < 1e-3 && Number(r.start) > OPENS_BY) && beforeCut(Number(r.start), Number(r.end), e.min, fill.shots) === Number(r.end), ); return row ? { row, said: `${from} to ${to} s had no effect: ${String(row.effect)}, picked for these seconds`, } : null; } /** Rows as the fill's alternation reads them: which are punch-ins, which titles. */ const besides = (rows: JsonObject[]) => rows.map((r) => ({ effect: String(r.effect), start: Number(r.start), title: isKind(String(r.effect), TITLES), })); /** The plan with each stretch over ten seconds that has no effect filled, and what was added. */ export function filledPlan(given: JsonObject, fill: Fill): { given: JsonObject; said: string[] } { const key = keyOf(given); let rows = listOf(given.placements).filter(isObject); const said: string[] = []; const unfilled = new Set(); for (let n = 0; n < FILLS; n += 1) { const hole = holesIn(spans(rows), fill.seconds).find((h) => !unfilled.has(String(h))); if (!hole) break; const made = rowFor(hole[0], hole[1], rows, key, fill); if (!made) { unfilled.add(String(hole)); continue; } rows = [...rows, made.row].sort((a, b) => Number(a.start) - Number(b.start)); said.push(made.said); } return { given: { ...given, placements: rows }, said }; } export const keyOf = (given: JsonObject): number[][] => listOf(given.key).map((k) => listOf(k).map(Number)); /** The plan with each card that hides the person cut to its longest; one that cannot be that short is taken out, for * the empty stretch to be filled. */ export function cardsCapped( given: JsonObject, allowed: Map, ): { given: JsonObject; said: string[] } { const said: string[] = []; const rows = listOf(given.placements) .filter(isObject) .flatMap((row) => { const effect = allowed.get(String(row.effect)); const [start, end] = [Number(row.start), Number(row.end)]; if (!effect || !coversPerson(effect) || end - start <= CARD_MOST + 1e-6) return [row]; if (effect.min > CARD_MOST) { said.push(`${effect.id} hides the person for ${effect.min} s at least: taken out`); return []; } said.push(`${effect.id} hides the person: cut from ${end - start} s to ${CARD_MOST} s`); return [{ ...row, end: onGrid(start + CARD_MOST) }]; }); return { given: { ...given, placements: rows }, said }; } /** The words said from `word` on, up to `end`, as one line of a title. */ function lineFrom(words: Word[], word: Word, end: number): string { const from = words.indexOf(word); return words .slice(Math.max(0, from), Math.max(0, from) + TITLE_WORDS) .filter((w, k) => k === 0 || w.start < end) .map((w) => w.text) .join(" ") .replace(/[\s,.;:!?…-]+$/u, ""); } /** A title of one text line for the stretch, the ones the plan was shown first, none already on the video. */ export function titleRow( from: number, to: number, word: Word, words: Word[], effects: Effect[], used: Set, ): JsonObject | null { const start = onGrid(Math.max(from, word.start - LEAD)); for (const effect of effects) { const slots = listOf(effect.meta.text_slots).map(String); if ( !isKind(effect.id, TITLES) || used.has(effect.id) || slots.length !== 1 || effect.max < READ_LEAST ) continue; // A fill asks HeyGen for nothing: a title over a cut-out would play over an empty one, a black frame. if (effect.slots.includes("main_matte")) continue; const ownLine = defaultsOf(effect.template)?.[slots[0]!]; if (ownLine !== undefined && ownLine !== null && typeof ownLine !== "string") continue; const end = onGrid(Math.min(to, start + Math.max(effect.usual, READ_LEAST))); if (end - start < Math.max(effect.min, READ_LEAST) - 1e-6) continue; const line = lineFrom(words, word, end); if (line) return { effect: effect.id, start, end, slots: { [slots[0]!]: line } }; } return null; } function punchIn( fill: Fill, from: number, to: number, word: Word, rows: JsonObject[], ): JsonObject | null { if (!fill.allowed.get(PUNCH)) return null; const at = Math.max(from, word.start - LEAD); const end = onGrid(Math.min(to, at + PUNCH_SECONDS)); return end && end <= OPENING_S + 1e-3 && apartFrom(rows, PUNCH, onGrid(at), end) ? { effect: PUNCH, start: onGrid(at), end, slots: {} } : null; } function freeUntil(from: number, rows: JsonObject[], fill: Fill): number { const next = rows.find((r) => Number(r.start) > from); const cut = fill.shots.find((t) => t > from + 0.1); return Math.min(Number(next?.start ?? fill.seconds), cut ?? fill.seconds); } const wordIn = (fill: Fill, key: number[][], from: number, to: number): Word | undefined => keyWordIn(fill.words, key, from, to) ?? fill.words.find((w) => w.start >= from && w.start < to); function pickedInTime( from: number, to: number, rows: JsonObject[], fill: Fill, ): { row: JsonObject; said: string } | null { const picked = pickedFor(from, to, rows, fill, false); const lastEnd = Math.max( 0, ...rows.filter((r) => Number(r.end) <= from + 1e-3).map((r) => Number(r.end)), ); return picked && Number(picked.row.start) - lastEnd <= EMPTY_SECONDS ? picked : null; } function putBack( gone: JsonObject, rows: JsonObject[], fill: Fill, effects: Effect[], key: number[][], ): { row: JsonObject | null; said: string } { const from = Number(gone.start); const to = freeUntil(from, rows, fill); if (rows.some((r) => Number(r.start) < to && Number(r.end) > from)) return { row: null, said: "" }; const picked = pickedInTime(from, to, rows, fill); if (picked) return { row: picked.row, said: `${from} to ${to} s: ${String(picked.row.effect)}, picked for these seconds, in its place`, }; const word = wordIn(fill, key, from, to); if (!word) return { row: null, said: `${from} to ${to} s left empty: nothing is said there` }; const used = new Set(rows.map((r) => String(r.effect))); const punched = punchIn(fill, from, to, word, rows); const titled = () => titleRow(from, to, word, fill.words, effects, used); const row = fillKindAt(besides(rows), from) === "punch" ? (punched ?? titled()) : (titled() ?? punched); if (!row) return { row: null, said: `${from} to ${to} s left empty: no title fits` }; return { row, said: `${from} to ${to} s: ${String(row.effect)} on "${word.text}" in its place` }; } /** The plan with at most `MOST_CUTOUTS` effects waiting on a cut-out, each taken out put back as a title on * its seconds' own words or a punch-in the opening's cut-out can carry, the kinds taking turns. */ export function cutoutsPaced(given: JsonObject, fill: Fill): { given: JsonObject; said: string[] } { const key = listOf(given.key).map((k) => listOf(k).map(Number)); // An effect inside the opening is cut from the opening's own cut-out: it asks HeyGen for nothing more. const own = (r: JsonObject) => Number(r.end) <= OPENING_S + 1e-3; const all = listOf(given.placements).filter(isObject); const spaced = cutoutsSpaced( all.filter((r) => !own(r)), fill.allowed, ); let rows = all.filter((r) => own(r) || spaced.rows.includes(r)); const said = [...spaced.said]; const effects = fill.shown.flatMap((id) => fill.allowed.get(id) ?? []); for (const gone of spaced.gone) { const put = putBack(gone, rows, fill, effects, key); if (put.said) said.push(put.said); if (put.row) rows = [...rows, put.row].sort((x, y) => Number(x.start) - Number(y.start)); } return { given: { ...given, placements: rows }, said }; }