subfox/app/services/srt_parser.py

35 lines
813 B
Python

import re
def parse_srt(content: str):
content = content.replace("\r\n", "\n").replace("\r", "\n").strip()
parts = re.split(r"\n\s*\n", content)
blocks = []
for part in parts:
lines = [line.rstrip() for line in part.split("\n") if line.strip() != ""]
if len(lines) < 3:
continue
try:
index = int(lines[0].strip())
times = lines[1].strip()
if " --> " not in times:
continue
start, end = times.split(" --> ", 1)
text = "\n".join(lines[2:]).strip()
blocks.append({
"index": index,
"start": start,
"end": end,
"text": text
})
except Exception:
continue
return blocks