Skip to content

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

fix(voice): ignore symbol-only script lines when aligning subtitles

Rudy Celekli0b8af70b7c18
diff --git a/app/services/voice.py b/app/services/voice.py
index 688e14bf..fefa6f44 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -3693,7 +3693,14 @@ def create_subtitle(
_write_subtitle_items(sub_items, subtitle_file)
return
- script_lines = utils.split_string_by_punctuations(text)
+ # 只有符号的行(如 `* * *`、`———`、`• • •` 场景分隔)不会被朗读,
+ # 也不会得到任何词级时间轴;保留它们会让行数永远对不上,整段字幕
+ # 都无法生成。
+ script_lines = [
+ line
+ for line in utils.split_string_by_punctuations(text)
+ if re.sub(r"[_\W]+", "", line)
+ ]
if hasattr(sub_maker, "cues") and sub_maker.cues:
sub_items = _build_subtitle_items_from_edge_cues(sub_maker, script_lines)
else:
diff --git a/test/services/test_voice_unspeakable_script_lines.py b/test/services/test_voice_unspeakable_script_lines.py
new file mode 100644
index 00000000..36b6a7eb
--- /dev/null
+++ b/test/services/test_voice_unspeakable_script_lines.py
@@ -0,0 +1,42 @@
+from datetime import timedelta
+
+import pytest
+from edge_tts import SubMaker
+from edge_tts.srt_composer import Subtitle
+
+from app.services import subtitle, voice
+
+WORDS = ["Welcome", "to", "the", "show", "Let", "us", "begin"]
+
+
+def _edge_sub_maker():
+ sub_maker = SubMaker()
+ start = 0.0
+ for index, word in enumerate(WORDS, 1):
+ sub_maker.cues.append(
+ Subtitle(
+ index=index,
+ start=timedelta(seconds=start),
+ end=timedelta(seconds=start + 0.4),
+ content=word,
+ )
+ )
+ start += 0.5
+ return sub_maker
+
+
[email protected]("separator", ["* * *", "———", "• • •", "= = ="])
+def test_symbol_only_script_lines_do_not_drop_edge_subtitles(tmp_path, separator):
+ # Scene-break lines made only of symbols are never spoken, so Edge emits
+ # no word boundary for them. They must not block every other caption.
+ subtitle_file = tmp_path / "subtitle.srt"
+
+ voice.create_subtitle(
+ _edge_sub_maker(),
+ f"Welcome to the show.\n{separator}\nLet us begin.",
+ str(subtitle_file),
+ )
+
+ items = subtitle.file_to_subtitles(str(subtitle_file))
+ assert [item[2] for item in items] == ["Welcome to the show", "Let us begin"]
+ assert items[1][1] == "00:00:02,000 --> 00:00:03,400"