Skip to content

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

Fix subtitle matching for markdown script markers

harry07035dd294c84acd
diff --git a/app/services/subtitle.py b/app/services/subtitle.py
index 9c39a8c1..1e78ea68 100644
--- a/app/services/subtitle.py
+++ b/app/services/subtitle.py
@@ -192,7 +192,8 @@ def similarity(a, b):
def correct(subtitle_file, video_script):
subtitle_items = file_to_subtitles(subtitle_file)
- script_lines = utils.split_string_by_punctuations(video_script)
+ normalized_script = utils.normalize_script_for_subtitle_matching(video_script)
+ script_lines = utils.split_string_by_punctuations(normalized_script)
corrected = False
new_subtitle_items = []
diff --git a/app/services/voice.py b/app/services/voice.py
index 2afbcf19..11d14fc6 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -1035,15 +1035,22 @@ def mimo_tts(
def _format_text(text: str) -> str:
- # text = text.replace("\n", " ")
+ """
+ 清理字幕对齐前的脚本文本。
+
+ 这里不能只在 LLM 生成阶段处理,因为用户也可能手动粘贴脚本,或通过
+ API 直接传入包含 Markdown 标记的文本。TTS 通常不会朗读 `---`、
+ `___`、`***` 这类分隔符行,也不会朗读 `_` 这种强调标记;如果字幕
+ 对齐仍保留这些字符,`create_subtitle()` 会一直等待不存在的 cue,
+ 最终导致字幕文件缺失并在 Whisper fallback 校正时补出全 0 时间轴。
+ """
text = text.replace("[", " ")
text = text.replace("]", " ")
text = text.replace("(", " ")
text = text.replace(")", " ")
text = text.replace("{", " ")
text = text.replace("}", " ")
- text = text.strip()
- return text
+ return utils.normalize_script_for_subtitle_matching(text)
def _build_subtitle_formatter():
@@ -1093,8 +1100,8 @@ def _match_script_line(script_lines: list[str], current_text: str, sub_index: in
这里复用了项目原有的“按标点拆脚本,再逐段比对”的思路:
1. 优先精确匹配;
- 2. 再做一次去常规标点后的匹配;
- 3. 最后做一次更激进的非单词字符清洗匹配。
+ 2. 再做一次去标点和 Markdown `_` 格式符后的匹配;
+ 3. 最后做一次阿拉伯语字符形态归一化匹配。
这样可以兼容:
- TTS 返回里可能缺失或单独拆分的标点;
@@ -1107,20 +1114,15 @@ def _match_script_line(script_lines: list[str], current_text: str, sub_index: in
if current_text == target_line:
return target_line.strip()
- current_text_normalized = re.sub(r"[^\w\s]", "", current_text)
- target_line_normalized = re.sub(r"[^\w\s]", "", target_line)
- if current_text_normalized == target_line_normalized:
- return target_line.strip()
-
- current_text_normalized = re.sub(r"\W+", "", current_text)
- target_line_normalized = re.sub(r"\W+", "", target_line)
+ current_text_normalized = re.sub(r"[_\W]+", "", current_text)
+ target_line_normalized = re.sub(r"[_\W]+", "", target_line)
if current_text_normalized == target_line_normalized:
return target_line.strip()
# 最后一层阿拉伯语容错:edge-tts 返回的字母形态、变音符号或 Tatweel
# 可能和脚本不同。只在常规匹配失败后归一化比较,非阿拉伯语文本不会受影响。
- current_ar = re.sub(r"\W+", "", _normalize_arabic(current_text))
- target_ar = re.sub(r"\W+", "", _normalize_arabic(target_line))
+ current_ar = re.sub(r"[_\W]+", "", _normalize_arabic(current_text))
+ target_ar = re.sub(r"[_\W]+", "", _normalize_arabic(target_line))
if current_ar and current_ar == target_ar:
return target_line.strip()
diff --git a/app/utils/utils.py b/app/utils/utils.py
index cfead12c..69d46010 100644
--- a/app/utils/utils.py
+++ b/app/utils/utils.py
@@ -1,6 +1,7 @@
import json
import locale
import os
+import re
from functools import lru_cache
from pathlib import Path
import threading
@@ -205,6 +206,39 @@ def split_string_by_punctuations(s):
return result
+def normalize_script_for_subtitle_matching(video_script: str) -> str:
+ """
+ 清理字幕匹配前的脚本文本。
+
+ 用户可能手动输入 Markdown 分隔符、标题强调或 `_` 这类格式符号。
+ 这些字符通常不会出现在 TTS/Whisper 的识别结果里;如果继续参与
+ 字幕逐行匹配,脚本行数量会大于真实字幕行数量,最终可能补出
+ `00:00:00,000 --> 00:00:00,000`,导致剪辑软件无法导入 SRT。
+ """
+ video_script = video_script or ""
+ underscore_count = video_script.count("_")
+ video_script = video_script.replace("_", "")
+ cleaned_lines = []
+ removed_separator_lines = 0
+ for line in video_script.splitlines():
+ line = line.strip()
+ # Markdown 分隔符或强调符号单独成行时不会被 TTS 朗读,必须从
+ # 脚本行里移除,避免字幕聚合卡在这类“不可发声”的目标行上。
+ if re.fullmatch(r"[-*_]{3,}", line):
+ removed_separator_lines += 1
+ continue
+ cleaned_lines.append(line)
+
+ normalized_script = "\n".join(cleaned_lines).strip()
+ if underscore_count or removed_separator_lines:
+ logger.debug(
+ "normalized script for subtitle matching, "
+ f"removed underscores: {underscore_count}, "
+ f"removed markdown separator lines: {removed_separator_lines}"
+ )
+ return normalized_script
+
+
def md5(text):
import hashlib
diff --git a/test/services/test_subtitle.py b/test/services/test_subtitle.py
new file mode 100644
index 00000000..f68a9198
--- /dev/null
+++ b/test/services/test_subtitle.py
@@ -0,0 +1,48 @@
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+# 测试文件直接运行时,也能从仓库根目录导入 app 包。
+sys.path.insert(0, str(Path(__file__).parent.parent.parent))
+
+from app.services import subtitle
+
+
+class TestSubtitleService(unittest.TestCase):
+ def test_correct_ignores_markdown_separator_lines(self):
+ """
+ Whisper fallback 校正阶段也必须忽略 `---` 这类不可发声脚本行。
+
+ 如果这里继续保留 Markdown 分隔符,`correct()` 会认为脚本行数多于
+ 字幕行数,并补出 `00:00:00,000 --> 00:00:00,000`,剪辑软件会把
+ 生成的 SRT 判定为不可导入。
+ """
+ original_srt = (
+ "1\n"
+ "00:00:00,100 --> 00:00:01,000\n"
+ "第一段\n\n"
+ "2\n"
+ "00:00:01,100 --> 00:00:02,000\n"
+ "第二段\n\n"
+ )
+
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ subtitle_file = Path(tmp_dir) / "subtitle.srt"
+ subtitle_file.write_text(original_srt, encoding="utf-8")
+
+ subtitle.correct(
+ subtitle_file=str(subtitle_file),
+ video_script="第一段\n---\n第二段",
+ )
+
+ corrected_srt = subtitle_file.read_text(encoding="utf-8")
+
+ self.assertIn("第一段", corrected_srt)
+ self.assertIn("第二段", corrected_srt)
+ self.assertNotIn("---", corrected_srt)
+ self.assertNotIn("00:00:00,000 --> 00:00:00,000", corrected_srt)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/test/services/test_voice.py b/test/services/test_voice.py
index bcbbd9a4..0c6f6649 100644
--- a/test/services/test_voice.py
+++ b/test/services/test_voice.py
@@ -546,6 +546,73 @@ class TestVoiceService(unittest.TestCase):
self.assertIn("أهلاً وسهلاً بك في المدرسة", sub_items[0])
self.assertIn("شكراً لك", sub_items[-1])
+ def test_create_subtitle_ignores_markdown_separator_lines(self):
+ """
+ 用户手动脚本可能包含 `---` 这类 Markdown 分隔符。TTS 不会朗读
+ 这些符号行,字幕聚合也不应把它们当成目标字幕行,否则后续真实
+ 字幕会卡住并回退到 Whisper。
+ """
+ text = "第一段\n---\n第二段"
+ sub_maker = SimpleNamespace(
+ cues=[
+ SimpleNamespace(
+ content="第一段",
+ start=timedelta(seconds=0),
+ end=timedelta(seconds=0.8),
+ ),
+ SimpleNamespace(
+ content="第二段",
+ start=timedelta(seconds=1),
+ end=timedelta(seconds=1.8),
+ ),
+ ]
+ )
+
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ subtitle_file = Path(tmp_dir) / "subtitle.srt"
+ vs.create_subtitle(
+ sub_maker=sub_maker,
+ text=text,
+ subtitle_file=str(subtitle_file),
+ )
+
+ subtitle_content = subtitle_file.read_text(encoding="utf-8")
+
+ self.assertIn("第一段", subtitle_content)
+ self.assertIn("第二段", subtitle_content)
+ self.assertNotIn("---", subtitle_content)
+ self.assertNotIn("00:00:00,000 --> 00:00:00,000", subtitle_content)
+
+ def test_create_subtitle_ignores_markdown_underscore_marks(self):
+ """
+ `_` 常被用户用作 Markdown 强调标记,但 TTS 返回的 cue 通常不包含
+ 这些格式符。匹配时应忽略 `_`,避免生成空字幕或回退到 Whisper。
+ """
+ text = "这是_a_测试。"
+ sub_maker = SimpleNamespace(
+ cues=[
+ SimpleNamespace(
+ content="这是a测试",
+ start=timedelta(seconds=0),
+ end=timedelta(seconds=0.8),
+ ),
+ ]
+ )
+
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ subtitle_file = Path(tmp_dir) / "subtitle.srt"
+ vs.create_subtitle(
+ sub_maker=sub_maker,
+ text=text,
+ subtitle_file=str(subtitle_file),
+ )
+
+ subtitle_content = subtitle_file.read_text(encoding="utf-8")
+
+ self.assertIn("这是a测试", subtitle_content)
+ self.assertNotIn("这是_a_测试", subtitle_content)
+ self.assertNotIn("00:00:00,000 --> 00:00:00,000", subtitle_content)
+
def test_convert_rate_to_percent_signs_zero_rate(self):
# Rates near but not exactly 1.0 round to 0 percent. edge-tts rejects
# an unsigned "0%" (ValueError: Invalid rate '0%'), so the helper must