Skip to content

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

fix: keep Gemini TTS subtitles on edge provider

Harry1c1db66000ae
diff --git a/app/services/voice.py b/app/services/voice.py
index 8a2e291b..26ff8fcd 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -1219,8 +1219,9 @@ def populate_legacy_submaker_with_full_text(
1. edge_tts 7.x 的 `SubMaker` 不再提供旧版本里的 `create_sub()`;
2. 项目里 Gemini、SiliconFlow 等非 edge 路径依然需要返回一个
带 `subs/offset` 的对象,供后续统一计算音频时长和生成字幕;
- 3. 对于拿不到逐词边界的 TTS 服务,最稳妥的做法是先把整段文本作为
- 一个完整字幕片段写入,保证视频生成流程不中断。
+ 3. 对于拿不到逐词边界的 TTS 服务,需要至少按脚本断句切成多个片段,
+ 这样后续 `subtitle_provider=edge` 的聚合逻辑才能继续工作,而不是
+ 因为整段文本无法和脚本断句逐行匹配而回退 Whisper。
Args:
sub_maker: 需要写入兼容字段的字幕对象
@@ -1240,10 +1241,43 @@ def populate_legacy_submaker_with_full_text(
if not normalized_text:
return sub_maker
- # 统一转成 edge_tts 旧逻辑使用的 100ns 单位。
audio_duration_100ns = max(int(audio_duration_seconds * 10000000), 1)
- sub_maker.subs.append(normalized_text)
- sub_maker.offset.append((0, audio_duration_100ns))
+
+ # Gemini / SiliconFlow 这类路径拿不到逐词边界时,仍然尽量沿用项目
+ # 原来的“按标点断句 + 按字符数比例分配时长”的策略。这样既能让
+ # create_subtitle() 匹配脚本断句,也能避免再次回退 Whisper。
+ sentences = utils.split_string_by_punctuations(normalized_text)
+ if not sentences:
+ sentences = [normalized_text]
+
+ total_chars = sum(len(sentence) for sentence in sentences)
+ if total_chars <= 0:
+ sub_maker.subs.append(normalized_text)
+ sub_maker.offset.append((0, audio_duration_100ns))
+ return sub_maker
+
+ current_offset = 0
+ for index, sentence in enumerate(sentences):
+ cleaned_sentence = sentence.strip()
+ if not cleaned_sentence:
+ continue
+
+ # 前面的句子按字符数比例分配时长,最后一句兜底吃掉剩余时长,
+ # 避免整数取整导致总时长丢失或字幕结束时间短于音频。
+ if index == len(sentences) - 1:
+ sentence_end = audio_duration_100ns
+ else:
+ sentence_chars = len(cleaned_sentence)
+ sentence_duration = max(
+ int(audio_duration_100ns * (sentence_chars / total_chars)),
+ 1,
+ )
+ sentence_end = min(current_offset + sentence_duration, audio_duration_100ns)
+
+ sub_maker.subs.append(cleaned_sentence)
+ sub_maker.offset.append((current_offset, sentence_end))
+ current_offset = sentence_end
+
return sub_maker
diff --git a/test/services/test_voice.py b/test/services/test_voice.py
index 6fff51ee..4f2f175e 100644
--- a/test/services/test_voice.py
+++ b/test/services/test_voice.py
@@ -2,6 +2,7 @@ import asyncio
import unittest
import os
import sys
+import tempfile
from pathlib import Path
from unittest.mock import patch
@@ -10,6 +11,7 @@ sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from app.utils import utils
from app.services import voice as vs
+from app.services import task as task_service
from pydub import AudioSegment
temp_dir = utils.storage_dir("temp")
@@ -106,7 +108,8 @@ class TestVoiceService(unittest.TestCase):
def test_gemini_tts_uses_legacy_submaker_fields(self):
"""
验证 Gemini TTS 在 edge_tts 7.x 环境下仍会返回项目兼容的字幕结构,
- 避免再次调用不存在的 create_sub() 导致整条生成链路失败。
+ 并且可以被 `subtitle_provider=edge` 的字幕生成链路直接消费,
+ 避免再次回退 Whisper。
"""
class _InlineData:
@@ -144,7 +147,7 @@ class TestVoiceService(unittest.TestCase):
voice_file = f"{temp_dir}/tts-gemini-Zephyr.mp3"
subtitle_file = f"{temp_dir}/tts-gemini-Zephyr.srt"
- text = "Gemini subtitle generation should work now."
+ text = "Gemini subtitle generation should work now. Testing multiple lines."
with patch("google.generativeai.configure"), patch(
"google.generativeai.GenerativeModel", _FakeModel
@@ -157,12 +160,55 @@ class TestVoiceService(unittest.TestCase):
)
self.assertIsNotNone(sub_maker)
- self.assertEqual(getattr(sub_maker, "subs", []), [text])
- self.assertEqual(len(getattr(sub_maker, "offset", [])), 1)
+ self.assertEqual(
+ getattr(sub_maker, "subs", []),
+ ["Gemini subtitle generation should work now", "Testing multiple lines"],
+ )
+ self.assertEqual(len(getattr(sub_maker, "offset", [])), 2)
+ self.assertEqual(sub_maker.offset[0][0], 0)
+ self.assertLess(sub_maker.offset[0][1], sub_maker.offset[1][1])
vs.create_subtitle(sub_maker=sub_maker, text=text, subtitle_file=subtitle_file)
subtitle_content = Path(subtitle_file).read_text(encoding="utf-8")
self.assertIn("Gemini subtitle generation should work now", subtitle_content)
+ self.assertIn("Testing multiple lines", subtitle_content)
+
+ def test_generate_subtitle_keeps_edge_provider_for_gemini_legacy_submaker(self):
+ """
+ 验证 Gemini TTS 返回的 legacy 字幕结构在 edge provider 下可以直接产出
+ SRT,不会因为匹配失败而回退到 Whisper。
+ """
+ script = "Gemini subtitle generation should work now. Testing multiple lines."
+ sub_maker = vs.populate_legacy_submaker_with_full_text(
+ vs.ensure_legacy_submaker_fields(vs.SubMaker()),
+ script,
+ 2.4,
+ )
+
+ with tempfile.TemporaryDirectory() as tmp_dir, patch.object(
+ task_service.config,
+ "app",
+ dict(task_service.config.app, subtitle_provider="edge"),
+ ), patch("app.services.subtitle.create") as whisper_create, patch(
+ "app.utils.utils.task_dir",
+ lambda tid="": str(Path(tmp_dir) / tid) if tid else str(Path(tmp_dir)),
+ ):
+ task_id = "gemini-subtitle-edge-task"
+ Path(tmp_dir, task_id).mkdir(parents=True, exist_ok=True)
+ subtitle_path = task_service.generate_subtitle(
+ task_id=task_id,
+ params=type("Params", (), {"subtitle_enabled": True})(),
+ video_script=script,
+ sub_maker=sub_maker,
+ audio_file="",
+ )
+
+ self.assertTrue(subtitle_path.endswith("subtitle.srt"))
+ self.assertTrue(Path(subtitle_path).exists())
+ self.assertFalse(whisper_create.called)
+ subtitle_content = Path(subtitle_path).read_text(encoding="utf-8")
+ self.assertIn("Gemini subtitle generation should work now", subtitle_content)
+ self.assertIn("Testing multiple lines", subtitle_content)
if __name__ == "__main__":
# python -m unittest test.services.test_voice.TestVoiceService.test_azure_tts_v1