feat: add DeepSeek subtitle AI workflow
This commit is contained in:
1 parent
bcebeaef82
commit
72072c884c
15 files changed
+710
No files matched your search
@@ -0,0 +1,104 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import re
|
||||
|
||||
|
||||
TIME_RE = re.compile(
|
||||
r"(?P<start>\d{1,2}:\d{2}:\d{2}[\.,]\d{3})\s*-->\s*"
|
||||
r"(?P<end>\d{1,2}:\d{2}:\d{2}[\.,]\d{3})"
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class SubtitleCue:
|
||||
start: str
|
||||
end: str
|
||||
text: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class CleanSubtitleResult:
|
||||
cues: list[SubtitleCue]
|
||||
original_count: int
|
||||
cleaned_count: int
|
||||
duplicate_count: int
|
||||
empty_count: int
|
||||
|
||||
|
||||
def normalize_time(value: str) -> str:
|
||||
return value.replace(",", ".")
|
||||
|
||||
|
||||
def has_visible_text(rows: list[str]) -> bool:
|
||||
text = "".join(rows).replace(" ", "").replace("\u00a0", "").strip()
|
||||
return bool(text)
|
||||
|
||||
|
||||
def parse_subtitle(content: str) -> list[SubtitleCue]:
|
||||
cues: list[SubtitleCue] = []
|
||||
current_time: tuple[str, str] | None = None
|
||||
text_rows: list[str] = []
|
||||
|
||||
def flush() -> None:
|
||||
nonlocal current_time, text_rows
|
||||
if current_time and text_rows:
|
||||
cues.append(SubtitleCue(current_time[0], current_time[1], "\n".join(text_rows).strip()))
|
||||
current_time = None
|
||||
text_rows = []
|
||||
|
||||
for raw_line in str(content or "").replace("\r", "").replace("\ufeff", "").split("\n"):
|
||||
row = raw_line.strip()
|
||||
if not row:
|
||||
flush()
|
||||
continue
|
||||
if row.upper().startswith("WEBVTT") or row.isdigit():
|
||||
continue
|
||||
match = TIME_RE.search(row)
|
||||
if match:
|
||||
flush()
|
||||
current_time = (normalize_time(match.group("start")), normalize_time(match.group("end")))
|
||||
continue
|
||||
if current_time:
|
||||
text_rows.append(row)
|
||||
|
||||
flush()
|
||||
return cues
|
||||
|
||||
|
||||
def clean_english_subtitle(content: str) -> CleanSubtitleResult:
|
||||
parsed = parse_subtitle(content)
|
||||
cleaned: list[SubtitleCue] = []
|
||||
seen: set[tuple[str, str, str]] = set()
|
||||
duplicate_count = 0
|
||||
empty_count = 0
|
||||
|
||||
for cue in parsed:
|
||||
rows = cue.text.splitlines()
|
||||
if not has_visible_text(rows):
|
||||
empty_count += 1
|
||||
continue
|
||||
key = (cue.start, cue.end, cue.text.strip())
|
||||
if key in seen:
|
||||
duplicate_count += 1
|
||||
continue
|
||||
seen.add(key)
|
||||
cleaned.append(cue)
|
||||
|
||||
return CleanSubtitleResult(
|
||||
cues=cleaned,
|
||||
original_count=len(parsed),
|
||||
cleaned_count=len(cleaned),
|
||||
duplicate_count=duplicate_count,
|
||||
empty_count=empty_count,
|
||||
)
|
||||
|
||||
|
||||
def build_vtt(cues: list[SubtitleCue] | list[dict[str, str]]) -> str:
|
||||
blocks: list[str] = []
|
||||
for index, cue in enumerate(cues, 1):
|
||||
start = cue.start if isinstance(cue, SubtitleCue) else cue["start"]
|
||||
end = cue.end if isinstance(cue, SubtitleCue) else cue["end"]
|
||||
text = cue.text if isinstance(cue, SubtitleCue) else cue["text"]
|
||||
blocks.append(f"{index}\n{start} --> {end}\n{text.strip()}")
|
||||
return "WEBVTT\n\n" + "\n\n".join(blocks).strip() + "\n"
|
||||
Reference in new issue
Block a user