NINA / subtitles.py
Malina
Claude Opus 4.8
NINA Phase 3a: embedded subtitle fallback (no new deps)
f1e4c89
Raw History Blame
3.77 kB
# ─────────────────────────────────────────────
# NINA β€” embedded subtitle extraction
# ─────────────────────────────────────────────
# When the audio is too garbled for reliable ASR, a clean human-authored subtitle
# track (if the upload carries one) is far better than anything we can transcribe.
# This reads text-based subtitle streams straight out of the container with ffmpeg
# β€” no new dependencies. Bitmap subtitles (PGS/VobSub) and burned-in captions are
# NOT handled here; those need OCR (a separate, heavier step).
#
# Best-effort: returns [] on anything unexpected, so the pipeline simply falls
# back to the ASR transcript.
import json
import os
import re
import shutil
import subprocess
import tempfile
from config import SUBTITLE_TEXT_CODECS
_SRT_TIME = re.compile(
r"(\d+):(\d+):(\d+)[,.](\d+)\s*-->\s*(\d+):(\d+):(\d+)[,.](\d+)"
)
def _subtitle_stream_index(video_path: str):
"""Return the 0-based index (among subtitle streams) of the first text-based
subtitle track, or None."""
try:
out = subprocess.run(
["ffprobe", "-v", "error", "-select_streams", "s",
"-show_entries", "stream=index,codec_name", "-of", "json", video_path],
capture_output=True, text=True, timeout=30,
)
streams = (json.loads(out.stdout or "{}")).get("streams", [])
for i, s in enumerate(streams):
if s.get("codec_name", "").lower() in SUBTITLE_TEXT_CODECS:
return i
except Exception:
pass
return None
def _parse_srt(text: str) -> list[dict]:
segments: list[dict] = []
for block in re.split(r"\n\s*\n", text.strip()):
m = _SRT_TIME.search(block)
if not m:
continue
h1, m1, s1, ms1, h2, m2, s2, ms2 = map(int, m.groups())
start = h1 * 3600 + m1 * 60 + s1 + ms1 / 1000.0
end = h2 * 3600 + m2 * 60 + s2 + ms2 / 1000.0
lines = block.splitlines()
ti = next((i for i, ln in enumerate(lines) if "-->" in ln), 0)
txt = " ".join(ln.strip() for ln in lines[ti + 1:] if ln.strip())
txt = re.sub(r"<[^>]+>", "", txt) # strip styling tags
txt = re.sub(r"\{[^}]+\}", "", txt) # strip ASS override blocks
txt = txt.strip()
if txt:
segments.append({"text": txt, "start": round(start, 2), "end": round(end, 2)})
return segments
def extract_embedded_subs(video_path: str) -> list[dict]:
"""Extract the first text-based subtitle track as [{text,start,end}], or []."""
try:
if not (shutil.which("ffmpeg") and shutil.which("ffprobe")):
return []
idx = _subtitle_stream_index(video_path)
if idx is None:
return []
fd, srt = tempfile.mkstemp(prefix="nina_subs_", suffix=".srt")
os.close(fd)
try:
r = subprocess.run(
["ffmpeg", "-y", "-i", video_path, "-map", f"0:s:{idx}",
"-f", "srt", srt, "-loglevel", "error"],
capture_output=True, timeout=120,
)
if r.returncode != 0:
return []
with open(srt, encoding="utf-8", errors="ignore") as f:
segs = _parse_srt(f.read())
if segs:
print(f"[NINA] Embedded subtitle track found β€” {len(segs)} cues.")
return segs
finally:
try:
os.remove(srt)
except OSError:
pass
except Exception as e:
print(f"[NINA] Subtitle extraction failed: {e}")
return []