Skip to content

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

fix: keep final SRT subtitle block

Andrew Barnes423d87c5d0eb
diff --git a/app/services/subtitle.py b/app/services/subtitle.py
index 1e78ea68..772a04d3 100644
--- a/app/services/subtitle.py
+++ b/app/services/subtitle.py
@@ -161,6 +161,13 @@ def file_to_subtitles(filename):
current_times, current_text = None, ""
elif current_times:
current_text += line
+
+ # Flush the final block. SRT files whose last subtitle is not followed by a
+ # trailing blank line never hit the blank-line branch above, so without this
+ # the last subtitle would be silently dropped.
+ if current_times:
+ index += 1
+ times_texts.append((index, current_times.strip(), current_text.strip()))
return times_texts
diff --git a/test/services/test_subtitle.py b/test/services/test_subtitle.py
index f68a9198..c95a0d4a 100644
--- a/test/services/test_subtitle.py
+++ b/test/services/test_subtitle.py
@@ -43,6 +43,50 @@ class TestSubtitleService(unittest.TestCase):
self.assertNotIn("---", corrected_srt)
self.assertNotIn("00:00:00,000 --> 00:00:00,000", corrected_srt)
+ def test_file_to_subtitles_keeps_last_block_without_trailing_newline(self):
+ """
+ The final subtitle must be parsed even when the SRT file does not end
+ with a trailing blank line. Many tools omit it, and previously the last
+ block was silently dropped because only a blank line flushed a block.
+ """
+ srt_without_trailing_blank = (
+ "1\n"
+ "00:00:00,000 --> 00:00:01,000\n"
+ "Hello\n\n"
+ "2\n"
+ "00:00:01,000 --> 00:00:02,000\n"
+ "World"
+ )
+
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ subtitle_file = Path(tmp_dir) / "subtitle.srt"
+ subtitle_file.write_text(srt_without_trailing_blank, encoding="utf-8")
+
+ items = subtitle.file_to_subtitles(str(subtitle_file))
+
+ self.assertEqual(len(items), 2)
+ self.assertEqual(items[0][2], "Hello")
+ self.assertEqual(items[1][2], "World")
+
+ def test_file_to_subtitles_parses_blocks_with_trailing_newline(self):
+ """A normal SRT ending in a blank line still parses all blocks."""
+ srt_with_trailing_blank = (
+ "1\n"
+ "00:00:00,000 --> 00:00:01,000\n"
+ "Hello\n\n"
+ "2\n"
+ "00:00:01,000 --> 00:00:02,000\n"
+ "World\n\n"
+ )
+
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ subtitle_file = Path(tmp_dir) / "subtitle.srt"
+ subtitle_file.write_text(srt_with_trailing_blank, encoding="utf-8")
+
+ items = subtitle.file_to_subtitles(str(subtitle_file))
+
+ self.assertEqual([item[2] for item in items], ["Hello", "World"])
+
if __name__ == "__main__":
unittest.main()