Repository Public
1/** WebVTT and transcript helpers (pure). */2export interface Cue { start: number; end: number; text: string }34const TS = /(?:(\d+):)?(\d{2}):(\d{2})[.,](\d{3})/;5const toSeconds = (m: RegExpExecArray) => Number(m[1] ?? 0) * 3600 + Number(m[2]) * 60 + Number(m[3]) + Number(m[4]) / 1000;67export interface VttReport { cues: Cue[]; errors: string[] }89/** Parses a WebVTT file. Validation errors are reported, never thrown: the caller decides. */10export function parseVtt(source: string): VttReport {11 const errors: string[] = [], cues: Cue[] = [];12 const text = source.replace(/^/, "").replace(/\r\n?/g, "\n");13 if (!/^WEBVTT(?:[ \t\n]|$)/.test(text)) errors.push("en-tête WEBVTT manquant");14 for (const block of text.split(/\n{2,}/).slice(1)) {15 const lines = block.split("\n").filter((l) => l !== "");16 if (!lines.length || /^(NOTE|STYLE|REGION)\b/.test(lines[0]!)) continue;17 const at = lines.findIndex((l) => l.includes("-->"));18 if (at < 0) { errors.push(`bloc sans repère temporel : « ${lines[0]} »`); continue; }19 const [a, b] = lines[at]!.split("-->");20 const s = TS.exec(a ?? ""), e = TS.exec(b ?? "");21 if (!s || !e) { errors.push(`repère temporel invalide : « ${lines[at]} »`); continue; }22 const start = toSeconds(s), end = toSeconds(e);23 if (end <= start) errors.push(`fin avant le début à ${lines[at]}`);24 if (cues.length && start < cues[cues.length - 1]!.start) errors.push(`répliques dans le désordre à ${lines[at]}`);25 const body = lines.slice(at + 1).join(" ").replace(/<[^>]+>/g, "").trim();26 if (!body) { errors.push(`réplique vide à ${lines[at]}`); continue; }27 cues.push({ start, end, text: body });28 }29 return { cues, errors };30}3132const pad = (n: number, w = 2) => String(n).padStart(w, "0");33export function vttTime(t: number): string {34 const ms = Math.round(t * 1000), h = Math.floor(ms / 3_600_000), m = Math.floor((ms % 3_600_000) / 60_000), s = Math.floor((ms % 60_000) / 1000);35 return `${pad(h)}:${pad(m)}:${pad(s)}.${pad(ms % 1000, 3)}`;36}3738export function toVtt(cues: Cue[], note?: string): string {39 return ["WEBVTT", ...(note ? ["", `NOTE ${note}`] : []), ...cues.map((c, i) => `\n${i + 1}\n${vttTime(c.start)} --> ${vttTime(c.end)}\n${c.text}`), ""].join("\n");40}4142/** Index of the segment being spoken at time t, or -1. Segments are sorted by start. */43export function activeSegment(segments: readonly { start: number; end: number }[], t: number): number {44 let lo = 0, hi = segments.length - 1, found = -1;45 while (lo <= hi) {46 const mid = (lo + hi) >> 1;47 if (segments[mid]!.start <= t) { found = mid; lo = mid + 1; } else hi = mid - 1;48 }49 return found >= 0 && t < segments[found]!.end ? found : -1;50}5152/** Index of the chapter containing time t (the last one that has started). */53export function activeChapter(chapters: readonly { start: number }[], t: number): number {54 let found = -1;55 chapters.forEach((c, i) => { if (c.start <= t + 0.001) found = i; });56 return found;57}5859const clock = (t: number) => { const s = Math.floor(t), h = Math.floor(s / 3600), m = Math.floor((s % 3600) / 60); return h ? `${h}:${pad(m)}:${pad(s % 60)}` : `${m}:${pad(s % 60)}`; };6061/** Plain-text transcript with timestamps, for an accessible export. */62export function transcriptToText(title: string, segments: readonly { start: number; text: string }[]): string {63 return [title, "=".repeat(Math.min(title.length, 80)), "", ...segments.map((s) => `[${clock(s.start)}] ${s.text}`), ""].join("\n");64}65