Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(subtitles): require timed headers before publication

Rudy Celekli4daab8d482ec
diff --git a/app/utils/subtitle_writer.py b/app/utils/subtitle_writer.py
index 8345937b..4f158bd3 100644
--- a/app/utils/subtitle_writer.py
+++ b/app/utils/subtitle_writer.py
@@ -1,6 +1,7 @@
"""Stage subtitle artifacts beside their destination before publication."""
from contextlib import contextmanager
import os
+import re
import tempfile
from loguru import logger
@@ -27,11 +28,15 @@ def staged_subtitle_file(destination):
def has_subtitle_cue(content) -> bool:
"""Return True when the body carries at least one timed SRT cue.
- An SRT body without a single ``-->`` timing line holds no caption any player
+ An SRT body without a complete timestamp timing line holds no caption any player
can display, so a publication built from it is a failed write rather than a
valid empty result.
"""
- return any("-->" in line for line in str(content or "").splitlines())
+ timestamp = r"[0-9]{2,}:[0-5][0-9]:[0-5][0-9],[0-9]{3}"
+ return any(
+ re.fullmatch(rf"\s*{timestamp}\s+-->\s+{timestamp}(?:\s+.*)?\s*", line)
+ for line in str(content or "").splitlines()
+ )
def write_subtitle_file(destination, content) -> bool:
diff --git a/test/services/test_subtitle_publication_headers.py b/test/services/test_subtitle_publication_headers.py
new file mode 100644
index 00000000..97a0770c
--- /dev/null
+++ b/test/services/test_subtitle_publication_headers.py
@@ -0,0 +1,20 @@
+import pytest
+
+from app.utils.subtitle_writer import write_subtitle_file
+
+
[email protected]("content", ["Transcription failed --> retry later", "1\nstart --> end\nA caption\n"])
+def test_arrow_prose_cannot_replace_existing_captions(tmp_path, content):
+ destination = tmp_path / "captions.srt"
+ original = "1\n00:00:00,000 --> 00:00:01,000\nExisting caption\n"
+ destination.write_text(original, encoding="utf-8")
+ assert write_subtitle_file(str(destination), content) is False
+ assert destination.read_text(encoding="utf-8") == original
+ assert list(tmp_path.iterdir()) == [destination]
+
+
+def test_real_timing_header_can_publish_caption_text_containing_arrows(tmp_path):
+ destination = tmp_path / "captions.srt"
+ content = "1\n00:00:00,000 --> 00:00:01,000\nleft --> right\n"
+ assert write_subtitle_file(str(destination), content) is True
+ assert destination.read_text(encoding="utf-8") == content