LUNA / subtitles.py
Malina
Claude Opus 5 (1M context)
Transkrypcja byla jedyna trescia, ktora szla do modelu bez ograniczenia
eac204b
Raw History Blame Contribute Delete
4.39 kB
# Embedded subtitle extraction.
#
# An uploaded MKV or MP4 often carries a text subtitle track. Reading it costs a
# subprocess call, while Whisper costs CPU seconds we do not have: transcription is
# the heaviest item in a build the website caps at eight minutes. A human-authored
# track is also more accurate than anything we would transcribe, and it keeps proper
# names and numbers spelled correctly.
#
# Adapted from NINA's subtitles.py. Text-based codecs only: bitmap subtitles (PGS,
# VobSub) and captions burned into the picture are not handled here - the vision model
# reads those off the frames anyway.
#
# Best-effort: returns "" on anything unexpected and the caller falls back to Whisper.
import json
import os
import re
import shutil
import subprocess
import tempfile
from config import TRANSCRIBE_MAX_SECONDS
TEXT_CODECS = {"subrip", "srt", "ass", "ssa", "mov_text", "webvtt", "text", "eia_608", "subviewer"}
_SRT_TIME = re.compile(r"(\d+):(\d+):(\d+)[,.](\d+)\s*-->\s*(\d+):(\d+):(\d+)[,.](\d+)")
def _first_text_track(path: str):
"""Index of the first text-based subtitle stream, counted among subtitle streams."""
try:
out = subprocess.run(
["ffprobe", "-v", "error", "-select_streams", "s",
"-show_entries", "stream=index,codec_name", "-of", "json", path],
capture_output=True, text=True, timeout=30,
)
streams = (json.loads(out.stdout or "{}")).get("streams", [])
for i, st in enumerate(streams):
if st.get("codec_name", "").lower() in TEXT_CODECS:
return i
except Exception:
pass
return None
def _start_seconds(match) -> float:
h, m, sec, frac = match.group(1), match.group(2), match.group(3), match.group(4)
return int(h) * 3600 + int(m) * 60 + int(sec) + int(frac) / (10 ** len(frac))
def _parse_srt(text: str) -> list[str]:
"""Cues as plain lines, in order.
Two things the raw track does that the transcript should not inherit. Rolling
captions (the eia_608 family especially) repeat the same line across consecutive
cues as the text scrolls, which would pad the transcript with duplicates and pay
tokens for them. And Whisper stops at TRANSCRIBE_MAX_SECONDS, while a subtitle
track had no cap at all: a three hour film handed over its whole script, which is
a different rule for the same job depending only on how the words were obtained.
"""
lines: list[str] = []
for block in re.split(r"\n\s*\n", text.strip()):
m = _SRT_TIME.search(block)
if not m:
continue
if _start_seconds(m) > TRANSCRIBE_MAX_SECONDS:
break
rows = block.splitlines()
ti = next((i for i, ln in enumerate(rows) if "-->" in ln), 0)
txt = " ".join(ln.strip() for ln in rows[ti + 1:] if ln.strip())
txt = re.sub(r"<[^>]+>", "", txt) # styling tags
txt = re.sub(r"\{[^}]+\}", "", txt) # ASS override blocks
txt = txt.strip()
if txt and txt != (lines[-1] if lines else None):
lines.append(txt)
return lines
def embedded_transcript(path: str) -> str:
"""The first text subtitle track as one transcript string, or "" if there is none."""
try:
if not (shutil.which("ffmpeg") and shutil.which("ffprobe")):
return ""
idx = _first_text_track(path)
if idx is None:
return ""
fd, srt = tempfile.mkstemp(prefix="luna_subs_", suffix=".srt")
os.close(fd)
try:
r = subprocess.run(
["ffmpeg", "-y", "-i", 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:
cues = _parse_srt(f.read())
if cues:
print(f"[LUNA] subtitles: {len(cues)} cue(s) read from the container, "
f"Whisper not needed for {os.path.basename(path)}")
return " ".join(cues).strip()
finally:
try:
os.remove(srt)
except OSError:
pass
except Exception as e:
print(f"[LUNA] subtitles: extraction failed ({type(e).__name__}: {e})")
return ""