// KoeOkoshi — pure SRT / plain-text builder. // // No dependencies, no DOM, no network: given timestamped segments it returns an // SRT string and a plain-text transcript. Lives apart from the app so it can be // unit-tested in Node (see src/srt.test.mjs). // // Segment shape (as emitted by transformers.js Whisper `out.chunks`): // { text: string, timestamp: [start, end] } // seconds; `end` may be null // // Design choices, all covered by the tests: // - timestamps render as SRT `HH:MM:SS,mmm` (comma, 3-digit ms, zero-padded); // - segments are ordered by start time; // - empty / whitespace-only segments are dropped; // - a null / missing / non-increasing `end` is resolved to the next segment's // start, else to a provided `fallbackEnd` (e.g. audio duration), else +2 s. /** Format a time in seconds as an SRT timestamp `HH:MM:SS,mmm`. */ export function formatTimestamp(totalSeconds) { let s = Number(totalSeconds); if (!Number.isFinite(s) || s < 0) s = 0; const totalMs = Math.round(s * 1000); const hours = Math.floor(totalMs / 3_600_000); const minutes = Math.floor((totalMs % 3_600_000) / 60_000); const seconds = Math.floor((totalMs % 60_000) / 1000); const millis = totalMs % 1000; const p = (n, width = 2) => String(n).padStart(width, '0'); return `${p(hours)}:${p(minutes)}:${p(seconds)},${p(millis, 3)}`; } /** * Clean, order, and fill in end times for a raw segment list. * @returns {{text:string,start:number,end:number}[]} */ export function normalizeSegments(segments, { fallbackEnd } = {}) { if (!Array.isArray(segments)) return []; const cleaned = []; for (const seg of segments) { if (!seg) continue; const text = String(seg.text ?? '').trim(); if (!text) continue; // drop empty / whitespace-only const ts = seg.timestamp || seg.timestamps || []; let start = Number(ts[0]); if (!Number.isFinite(start) || start < 0) start = 0; let end = ts[1] == null ? null : Number(ts[1]); if (end != null && !Number.isFinite(end)) end = null; cleaned.push({ text, start, end }); } // Stable order by start time. cleaned.sort((a, b) => a.start - b.start); // Resolve missing / non-increasing end times. const hasFallback = Number.isFinite(fallbackEnd); for (let i = 0; i < cleaned.length; i++) { const cur = cleaned[i]; if (cur.end == null || cur.end <= cur.start) { const next = cleaned[i + 1]; if (next && next.start > cur.start) { cur.end = next.start; } else if (hasFallback && fallbackEnd > cur.start) { cur.end = fallbackEnd; } else { cur.end = cur.start + 2; // last-resort minimum duration } } } return cleaned; } /** * Build an SRT subtitle document from segments. * Returns '' when there are no non-empty segments. */ export function segmentsToSrt(segments, opts = {}) { const segs = normalizeSegments(segments, opts); if (segs.length === 0) return ''; const blocks = segs.map((s, i) => `${i + 1}\n${formatTimestamp(s.start)} --> ${formatTimestamp(s.end)}\n${s.text}` ); // Trailing newline so the file ends cleanly. return blocks.join('\n\n') + '\n'; } /** * Build a plain-text transcript: one cleaned segment per line, in order. */ export function segmentsToText(segments) { return normalizeSegments(segments).map((s) => s.text).join('\n'); }