Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(video-learning): preserve multiline WebVTT cues (#1091)

Bingxi Zhao (Frank)364b9cf59c8d
diff --git a/deeptutor/video_learning/service.py b/deeptutor/video_learning/service.py
index 670a59a92..777dc4b00 100644
--- a/deeptutor/video_learning/service.py
+++ b/deeptutor/video_learning/service.py
@@ -28,6 +28,10 @@ MAX_SEGMENT_SECONDS = 90
YOUTUBE_HOSTS = {"youtube.com", "www.youtube.com", "m.youtube.com", "music.youtube.com"}
VIDEO_ID_RE = re.compile(r"^[A-Za-z0-9_-]{11}$")
MATERIAL_ID_RE = re.compile(r"^[0-9a-f]{16,64}$")
+WEBVTT_TIMING_RE = re.compile(
+ r"^\s*(?P<start>\d{2}:\d{2}(?::\d{2})?[.,]\d{3})\s+-->\s+"
+ r"(?P<end>\d{2}:\d{2}(?::\d{2})?[.,]\d{3})(?:[ \t]+.*)?$"
+)
class TimedMediaError(RuntimeError):
@@ -235,14 +239,37 @@ def build_segments(cues: list[dict[str, Any]]) -> list[dict[str, Any]]:
def parse_webvtt(text: str) -> list[dict[str, Any]]:
- pattern = re.compile(
- r"(?m)^(?P<start>\d{2}:\d{2}(?::\d{2})?[.,]\d{3})\s+-->\s+"
- r"(?P<end>\d{2}:\d{2}(?::\d{2})?[.,]\d{3}).*\n(?P<body>.*?)(?=\n\s*\n|\Z)",
- re.DOTALL,
- )
+ """Parse Invidious WebVTT while tolerating malformed leading blanks.
+
+ Some caption endpoints insert a whitespace-only line between a cue timing
+ line and its payload. Treat those as leading padding, then collect all
+ consecutive payload lines until the next blank separator or timing line.
+ """
+ lines = text.replace("\r\n", "\n").replace("\r", "\n").split("\n")
result: list[dict[str, Any]] = []
- for match in pattern.finditer(text):
- body = re.sub(r"<[^>]+>", "", match.group("body")).strip().replace("\n", " ")
+ index = 0
+ while index < len(lines):
+ match = WEBVTT_TIMING_RE.match(lines[index])
+ if match is None:
+ index += 1
+ continue
+
+ index += 1
+ body_lines: list[str] = []
+ while index < len(lines):
+ line = lines[index]
+ if WEBVTT_TIMING_RE.match(line):
+ break
+ if not line.strip():
+ index += 1
+ if body_lines:
+ break
+ continue
+ body_lines.append(line)
+ index += 1
+
+ body = re.sub(r"<[^>]+>", "", "\n".join(body_lines))
+ body = re.sub(r"\s+", " ", body).strip()
if body:
result.append(
{
diff --git a/tests/video_learning/test_service.py b/tests/video_learning/test_service.py
index 28258fc2f..5dd382e8f 100644
--- a/tests/video_learning/test_service.py
+++ b/tests/video_learning/test_service.py
@@ -79,6 +79,39 @@ def test_transcript_normalization_enforces_the_storage_budget(monkeypatch) -> No
assert [cue["text"] for cue in cues] == ["1234", "5678"]
+def test_webvtt_preserves_caption_after_leading_blank_and_inline_tags() -> None:
+ cues = service.parse_webvtt(
+ "WEBVTT\n\n00:00:00.000 --> 00:00:02.000\n \nOpening <c>idea</c>\nand continuation\n"
+ )
+
+ assert cues == [
+ {
+ "start": 0.0,
+ "end": 2.0,
+ "text": "Opening idea and continuation",
+ }
+ ]
+
+
+def test_webvtt_keeps_ordinary_cues_and_accepts_timing_settings() -> None:
+ cues = service.parse_webvtt(
+ """WEBVTT
+
+first-cue
+00:01.000 --> 00:02.500 line:90% position:50% align:middle
+Ordinary caption
+
+00:03.000 --> 00:04.000
+Second line
+"""
+ )
+
+ assert cues == [
+ {"start": 1.0, "end": 2.5, "text": "Ordinary caption"},
+ {"start": 3.0, "end": 4.0, "text": "Second line"},
+ ]
+
+
def test_invidious_caption_choice_accepts_the_real_snake_case_schema() -> None:
captions = [
{"label": "English (auto-generated)", "language_code": "en", "auto_generated": True},