Make SubFox production-ready with parallel translation and UI controls
This commit is contained in:
parent
c40b8bed2b
commit
2b1d05f02c
6046 changed files with 798327 additions and 0 deletions
41
app/srt_parser.py
Normal file
41
app/srt_parser.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# app/srt_parser.py
|
||||
import re
|
||||
from typing import List, Dict
|
||||
|
||||
|
||||
SRT_BLOCK_RE = re.compile(
|
||||
r"(\d+)\s*\n"
|
||||
r"(\d{2}:\d{2}:\d{2},\d{3})\s-->\s(\d{2}:\d{2}:\d{2},\d{3})\s*\n"
|
||||
r"(.*?)(?=\n{2,}|\Z)",
|
||||
re.DOTALL,
|
||||
)
|
||||
|
||||
|
||||
def parse_srt(content: str) -> List[Dict[str, str]]:
|
||||
normalized = content.replace("\r\n", "\n").replace("\r", "\n").strip()
|
||||
|
||||
blocks = []
|
||||
for match in SRT_BLOCK_RE.finditer(normalized):
|
||||
index, start, end, text = match.groups()
|
||||
blocks.append(
|
||||
{
|
||||
"index": int(index),
|
||||
"start": start,
|
||||
"end": end,
|
||||
"text": text.strip(),
|
||||
}
|
||||
)
|
||||
|
||||
return blocks
|
||||
|
||||
|
||||
def build_srt(blocks: List[Dict[str, str]]) -> str:
|
||||
output = []
|
||||
|
||||
for i, block in enumerate(blocks, start=1):
|
||||
output.append(str(i))
|
||||
output.append(f"{block['start']} --> {block['end']}")
|
||||
output.append(block["text"])
|
||||
output.append("")
|
||||
|
||||
return "\n".join(output).strip() + "\n"
|
||||
Loading…
Add table
Add a link
Reference in a new issue