"""Formatting helpers for timestamped Whisper output (no ML dependencies).""" from __future__ import annotations from dataclasses import dataclass import json import math import re import textwrap from collections import Counter from typing import Any, Iterable @dataclass(frozen=True) class TimedWord: text: str start: float end: float space_before: bool = True @dataclass(frozen=True) class Cue: start: float end: float text: str def _safe_time(value: Any) -> float | None: if value is None: return None try: number = float(value) except (TypeError, ValueError): return None return number if math.isfinite(number) and number >= 0 else None def _time_pair(value: Any) -> tuple[float | None, float | None]: if not isinstance(value, (list, tuple)) or len(value) < 2: return None, None return _safe_time(value[0]), _safe_time(value[1]) def _split_text_with_times(text: str, start: float, end: float, max_chars: int) -> list[TimedWord]: """Split segment text into approximate word/character timings. Whisper's word timestamp mode already produces word chunks. This also handles segment-level timestamps and languages that do not use spaces (e.g. Chinese). Segment-to-word timings are proportional estimates, not forced alignments. """ if not text or not text.strip(): return [] leading_space = text[:1].isspace() stripped = text.strip() if re.search(r"\s", stripped): raw_parts = re.findall(r"\S+", stripped) raw_space_flags = [leading_space] + [True] * max(0, len(raw_parts) - 1) else: raw_parts = [stripped] raw_space_flags = [leading_space] # Break oversized single tokens too (for example long URLs), otherwise one # unbreakable token could exceed the two-line limit and lose text in export. parts: list[str] = [] separator_flags: list[bool] = [] group_size = max(1, max_chars // 2) for part, space_flag in zip(raw_parts, raw_space_flags): if len(part) > max_chars: pieces = [part[i : i + group_size] for i in range(0, len(part), group_size)] parts.extend(pieces) separator_flags.extend([space_flag] + [False] * max(0, len(pieces) - 1)) else: parts.append(part) separator_flags.append(space_flag) weights = [max(1, len(part)) for part in parts] total_weight = sum(weights) or 1 span = max(0.0, end - start) cursor = start result: list[TimedWord] = [] for index, (part, weight) in enumerate(zip(parts, weights)): part_start = cursor cursor = end if index == len(parts) - 1 else cursor + span * (weight / total_weight) part_end = max(part_start, cursor) result.append(TimedWord(part, part_start, part_end, separator_flags[index])) return result def chunks_to_words( chunks: Any, full_text: str, duration: float, max_chars: int = 42, ) -> list[TimedWord]: """Convert model chunks to timed words without silently dropping text. Whisper can return text chunks with missing/partial timestamps. The former implementation skipped those chunks whenever at least one other chunk had a timestamp, which could silently remove words from an SRT. Missing spans are now interpolated between adjacent valid timestamps. If no timestamps are usable, the full transcript is kept and given approximate timings. """ records: list[dict[str, Any]] = [] if isinstance(chunks, list): for chunk in chunks: if not isinstance(chunk, dict): continue text = str(chunk.get("text") or "") if not text.strip(): continue start, end = _time_pair(chunk.get("timestamp")) if start is not None and end is None: end = min(max(duration, 0.5), start + 0.45) elif end is not None and start is None: start = max(0.0, end - 0.45) if start is not None and end is not None: start = max(0.0, start) end = max(start, min(max(duration, 0.5), end)) records.append({"text": text, "start": start, "end": end}) known_indices = [i for i, item in enumerate(records) if item["start"] is not None and item["end"] is not None] if not known_indices: fallback_text = full_text.strip() or " ".join(item["text"].strip() for item in records) return _split_text_with_times(fallback_text, 0.0, max(duration, 0.5), max_chars) if fallback_text else [] # Fill each run of missing timestamps proportionally within the time span # bounded by the nearest known chunk on either side. cursor = 0 while cursor < len(records): if records[cursor]["start"] is not None: cursor += 1 continue run_start = cursor while cursor < len(records) and records[cursor]["start"] is None: cursor += 1 run_end = cursor previous_end = records[run_start - 1]["end"] if run_start > 0 else 0.0 next_start = records[run_end]["start"] if run_end < len(records) else max(duration, 0.5) left = max(0.0, float(previous_end or 0.0)) right = max(left, float(next_start if next_start is not None else max(duration, 0.5))) group = records[run_start:run_end] weights = [max(1, len(re.sub(r"\s+", "", item["text"]))) for item in group] total_weight = sum(weights) or 1 position = left span = right - left for offset, (item, weight) in enumerate(zip(group, weights)): item_start = position position = right if offset == len(group) - 1 else position + span * (weight / total_weight) item["start"] = item_start item["end"] = max(item_start, position) words: list[TimedWord] = [] for item in records: start = float(item["start"] or 0.0) end = float(item["end"] if item["end"] is not None else start) words.extend(_split_text_with_times(item["text"], start, end, max_chars)) # The caller compares these timed words with full_text. If the model's # chunk text itself is incomplete, the app can retry segment alignment and # finally preserve full_text with explicitly approximate timings. return words def timestamp_chunk_coverage(chunks: Any) -> float: """Return the fraction of non-empty chunks that carry at least one time.""" if not isinstance(chunks, list): return 0.0 useful = [chunk for chunk in chunks if isinstance(chunk, dict) and str(chunk.get("text") or "").strip()] if not useful: return 0.0 timed = 0 for chunk in useful: start, end = _time_pair(chunk.get("timestamp")) if start is not None or end is not None: timed += 1 return timed / len(useful) def timed_words_to_text(words: Iterable[TimedWord]) -> str: """Rebuild readable text from timed words for transcript-integrity checks.""" return _join_words(words) def transcript_coverage(reference: str, candidate: str) -> float: """Character-multiset coverage used to detect missing transcript text. It ignores spaces and punctuation, supports Unicode scripts, and is a completeness guard rather than a measure of transcription correctness. """ expected = "".join(char.casefold() for char in str(reference) if char.isalnum()) actual = "".join(char.casefold() for char in str(candidate) if char.isalnum()) if not expected: return 1.0 if not actual: return 0.0 available = Counter(actual) matched = sum(min(count, available.get(char, 0)) for char, count in Counter(expected).items()) return matched / len(expected) def _join_words(words: Iterable[TimedWord]) -> str: out = "" for word in words: if out and word.space_before: out += " " out += word.text return re.sub(r"\s+([,.;:!?।؟,。!?])", r"\1", out).strip() def _wrap_caption(text: str, max_chars: int) -> list[str]: text = re.sub(r"\s+", " ", text).strip() if not text: return [] return textwrap.wrap( text, width=max(1, int(max_chars)), break_long_words=True, break_on_hyphens=False, replace_whitespace=True, drop_whitespace=True, ) or [text] def build_cues( words: list[TimedWord], max_chars: int = 42, max_lines: int = 2, max_duration: float = 5.5, max_gap: float = 0.85, ) -> list[Cue]: """Create readable subtitle cues from timestamped words.""" if not words: return [] cues: list[Cue] = [] current: list[TimedWord] = [] def flush() -> None: nonlocal current if not current: return text = _join_words(current) lines = _wrap_caption(text, max_chars) if text and lines: start = max(0.0, current[0].start) end = max(start + 0.25, current[-1].end) if cues and start < cues[-1].end: start = cues[-1].end + 0.02 end = max(start + 0.2, end) cues.append(Cue(start, end, "\n".join(lines))) current = [] for word in words: candidate = current + [word] candidate_text = _join_words(candidate) candidate_lines = _wrap_caption(candidate_text, max_chars) gap = word.start - current[-1].end if current else 0.0 elapsed = word.end - current[0].start if current else 0.0 punctuation_break = bool(current) and bool(re.search(r"[.!?。!?।؟]$", current[-1].text)) and elapsed >= 1.25 should_flush = bool(current) and ( gap > max_gap or elapsed > max_duration or len(candidate_lines) > max_lines or punctuation_break ) if should_flush: flush() current.append(word) flush() return cues def _format_timestamp(seconds: float, separator: str) -> str: milliseconds = max(0, int(round(seconds * 1000))) hours, remainder = divmod(milliseconds, 3_600_000) minutes, remainder = divmod(remainder, 60_000) secs, millis = divmod(remainder, 1000) return f"{hours:02d}:{minutes:02d}:{secs:02d}{separator}{millis:03d}" def to_srt(cues: list[Cue]) -> str: blocks = [] for index, cue in enumerate(cues, start=1): blocks.append( f"{index}\n{_format_timestamp(cue.start, ',')} --> {_format_timestamp(cue.end, ',')}\n{cue.text}" ) return "\n\n".join(blocks) + ("\n" if blocks else "") def to_vtt(cues: list[Cue]) -> str: blocks = ["WEBVTT"] for cue in cues: blocks.append( f"{_format_timestamp(cue.start, '.')} --> {_format_timestamp(cue.end, '.')}\n{cue.text}" ) return "\n\n".join(blocks) + "\n" def to_json(cues: list[Cue]) -> str: payload = [ {"start": round(cue.start, 3), "end": round(cue.end, 3), "text": cue.text} for cue in cues ] return json.dumps(payload, ensure_ascii=False, indent=2) + "\n" def format_duration(seconds: float) -> str: total = max(0, int(round(seconds))) hours, remainder = divmod(total, 3600) minutes, secs = divmod(remainder, 60) if hours: return f"{hours}:{minutes:02d}:{secs:02d}" return f"{minutes}:{secs:02d}"