Skip to content

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

fix(subtitle): preserve timestamps inside SRT cue text

Chen Quan / 陈全a76b61da5969
diff --git a/app/services/subtitle.py b/app/services/subtitle.py
index 04386705..60dd7332 100644
--- a/app/services/subtitle.py
+++ b/app/services/subtitle.py
@@ -171,7 +171,9 @@ def file_to_subtitles(filename):
with open(filename, "r", encoding="utf-8") as f:
for line in f:
times = re.findall("([0-9]*:[0-9]*:[0-9]*,[0-9]*)", line)
- if times:
+ # Once a cue has started, timestamps belong to its text until the
+ # blank-line separator; they must not overwrite the cue's timing.
+ if times and current_times is None:
current_times = line
elif line.strip() == "" and current_times:
index += 1
diff --git a/test/services/test_subtitle.py b/test/services/test_subtitle.py
index 7b4f5012..37de427b 100644
--- a/test/services/test_subtitle.py
+++ b/test/services/test_subtitle.py
@@ -292,6 +292,36 @@ class TestSubtitleService(unittest.TestCase):
self.assertEqual([item[2] for item in items], ["Hello", "World"])
+ def test_file_to_subtitles_preserves_timestamps_in_cue_text(self):
+ """Timestamp examples in narration must not replace a cue's timing."""
+ for text in (
+ "Jump to 00:01:23,456 in the recording.",
+ "00:01:23,456",
+ "00:01:23,456 --> 00:01:24,000",
+ ):
+ with self.subTest(text=text), tempfile.TemporaryDirectory() as tmp_dir:
+ subtitle_file = Path(tmp_dir) / "subtitle.srt"
+ subtitle_file.write_text(
+ "1\n00:00:00,000 --> 00:00:02,000\n"
+ f"Timestamp example:\n{text}\nContinue watching.\n\n"
+ "2\n00:00:02,000 --> 00:00:03,000\nNext cue",
+ encoding="utf-8",
+ )
+
+ items = subtitle.file_to_subtitles(str(subtitle_file))
+
+ self.assertEqual(
+ items,
+ [
+ (
+ 1,
+ "00:00:00,000 --> 00:00:02,000",
+ f"Timestamp example:\n{text}\nContinue watching.",
+ ),
+ (2, "00:00:02,000 --> 00:00:03,000", "Next cue"),
+ ],
+ )
+
if __name__ == "__main__":
unittest.main()