// How many effects a clip carries, and how many of them wait on a cut-out. Six or seven a minute reads as busy without // crowding; quick talk and shot cuts carry more, one long sentence fewer. The cut-outs go to HeyGen all at once and come // back together (two in 31 s on case2), so where they fall in the clip does not matter; how many go at once is capped. import { type JsonObject } from "./json.js"; import type { Effect } from "./library.js"; import type { Word } from "./words.js"; const A_MINUTE = { least: 5, usual: 6.5, most: 10 } as const; const QUICK_WORDS_A_SECOND = 2.5; const SENTENCE_PAUSE = 0.8; const LONG_SENTENCE = 8; const CUTS_A_MINUTE = 4; const SWING = 0.75; /** The most effects of one run that each wait on their own cut-out from HeyGen, all asked at once. */ export const MOST_CUTOUTS = 4; const PUNCH = "punch-in"; const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v)); const forSeconds = (rate: number, seconds: number) => Math.max(1, Math.round((rate * seconds) / 60)); /** The clip's sentences as spans: a sentence closes on its own stop or on a pause. */ function sentencesOf(words: Word[]): { start: number; end: number }[] { const out: { start: number; end: number }[] = []; let from: number | null = null; let last = 0; for (const word of words) { if (from !== null && word.start - last >= SENTENCE_PAUSE) { out.push({ start: from, end: last }); from = null; } from ??= word.start; last = word.end; if (/[.?!。?!]$/.test(word.text.trimEnd())) { out.push({ start: from, end: last }); from = null; } } if (from !== null) out.push({ start: from, end: last }); return out; } function rateFor(words: Word[], shots: number[], seconds: number): number { if (seconds <= 0) return A_MINUTE.usual; const quick = clamp( (words.length / seconds - QUICK_WORDS_A_SECOND) / QUICK_WORDS_A_SECOND, -1, 1, ); const cuts = Math.min(1, (shots.length * 60) / seconds / CUTS_A_MINUTE); const long = sentencesOf(words) .map((s) => s.end - s.start) .filter((d) => d > LONG_SENTENCE) .reduce((a, b) => a + b, 0) / seconds; return clamp( A_MINUTE.usual + SWING * quick + SWING * cuts - 2 * SWING * long, A_MINUTE.least, A_MINUTE.most, ); } export const countFor = (words: Word[], shots: number[], seconds: number): number => forSeconds(rateFor(words, shots, seconds), seconds); export const mostFor = (seconds: number): number => forSeconds(A_MINUTE.most, seconds); export const effectsCounted = (rows: { series?: unknown }[]): number => rows.filter((r) => !r.series).length + new Set(rows.map((r) => r.series).filter(Boolean)).size; export const leastFor = (seconds: number): number => forSeconds(A_MINUTE.least, seconds); export const needsCutout = (effect: Effect | undefined): boolean => !!effect?.slots.includes("main_matte"); /** What a row is worth keeping when there are too many: on a line the video is about, then striking (a cut-out). */ export const worthIn = (allowed: Map) => (row: JsonObject): number => (row.point === true ? 2 : 0) + (row.effect !== PUNCH && needsCutout(allowed.get(String(row.effect))) ? 1 : 0); /** The rows with at most `MOST_CUTOUTS` effects needing a cut-out, the most worth kept; what goes is returned, to fill. */ export function cutoutsSpaced( rows: JsonObject[], allowed: Map, ): { rows: JsonObject[]; gone: JsonObject[]; said: string[] } { const ranked = rows .filter((r) => needsCutout(allowed.get(String(r.effect)))) .sort((a, b) => Number(a.start) - Number(b.start)); const worth = worthIn(allowed); const kept = new Set( [...ranked] .sort( (a, b) => Number(a.effect === PUNCH) - Number(b.effect === PUNCH) || worth(b) - worth(a), ) .slice(0, MOST_CUTOUTS), ); const gone = ranked.filter((r) => !kept.has(r)); return { rows: rows.filter((r) => !gone.includes(r)), gone, said: gone.map( (r) => `${String(r.effect)} at ${String(r.start)} s taken out: ${MOST_CUTOUTS} effects already wait on a cut-out`, ), }; } export type FillKind = "title" | "punch"; /** The kind a fill at `at` should be: never the kind of the fill or effect next to it, a title first. */ export function fillKindAt( around: { effect: string; start: number; title: boolean }[], at: number, ): FillKind { const sorted = [...around].sort((a, b) => a.start - b.start); const before = sorted.filter((r) => r.start < at).at(-1); const after = sorted.find((r) => r.start >= at); const kindOf = (r?: { effect: string; title: boolean }): FillKind | null => !r ? null : r.effect === PUNCH ? "punch" : r.title ? "title" : null; const near = [kindOf(before), kindOf(after)]; if (near.includes("title") && !near.includes("punch")) return "punch"; return "title"; }