Files
hpr-intro-stripper/hpr_stripper/srt.py
T
Lee Hanken 9917320640 hpr-intro-stripper: layered intro/outro cut-point detection and trimming
Fetches HPR's own metadata, audio, and official transcripts live; falls
back to local speech recognition (tiny model, matching HPR's own
hpr-get-and-transcribe.bash tooling) only when needed, and to
correlation against vendored reference clips or sibling episodes
otherwise. License-aware, resumable, and never guesses -- flags
anything it isn't confident about for manual review rather than
producing a plausible-looking wrong cut.
2026-11-22 10:36:07 +00:00

32 lines
983 B
Python

"""Minimal SRT parsing -- stdlib only, no subtitle library dependency."""
import re
_BLOCK_RE = re.compile(
r"(\d+)\s*\n"
r"(\d{2}):(\d{2}):(\d{2})[,.](\d{3})\s*-->\s*"
r"(\d{2}):(\d{2}):(\d{2})[,.](\d{3})\s*\n"
r"(.*?)(?=\n\s*\n|\Z)",
re.DOTALL,
)
def _to_seconds(h, m, s, ms):
return int(h) * 3600 + int(m) * 60 + int(s) + int(ms) / 1000.0
def parse(srt_text):
"""Returns a list of (text, start_seconds, end_seconds), in order."""
segments = []
for match in _BLOCK_RE.finditer(srt_text.replace("\r\n", "\n")):
h1, m1, s1, ms1, h2, m2, s2, ms2, text = match.groups()[1:]
start = _to_seconds(h1, m1, s1, ms1)
end = _to_seconds(h2, m2, s2, ms2)
clean_text = " ".join(line.strip() for line in text.strip().splitlines())
segments.append((clean_text, start, end))
return segments
def parse_file(path):
with open(path, "r", encoding="utf-8", errors="replace") as f:
return parse(f.read())