eldouma-graphics / src /lib /script.ts
Moeeldouma's picture
Deploy Eldouma Graphics — Docker Space, bring-your-own-key demo
39d98a0 verified
Raw
History Blame Contribute Delete
1 kB
// Turn a written script into estimated word timings (~2.7 words/sec + punctuation
// pauses) so a script-only project (no recording yet) can still drive the timeline,
// captions and preview. Real timings replace these once you record/transcribe or TTS it.
// cap so a runaway paste can't create a multi-hour timeline (and a giant render job)
export const MAX_SCRIPT_WORDS = 4000;
export function scriptToWords(script: string): { text: string; startMs: number; endMs: number }[] {
const tokens = String(script || "").replace(/\s+/g, " ").trim().split(" ").filter(Boolean).slice(0, MAX_SCRIPT_WORDS);
const out: { text: string; startMs: number; endMs: number }[] = [];
let t = 0;
for (const tok of tokens) {
const letters = tok.replace(/[^\p{L}\p{N}]/gu, "").length || 1;
const dur = Math.max(180, Math.min(620, 70 * letters + 110));
out.push({ text: tok, startMs: t, endMs: t + dur });
t += dur + (/[.!?]$/.test(tok) ? 320 : /[,;:]$/.test(tok) ? 160 : 50);
}
return out;
}