利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(subtitle): prevent unexpected Whisper fallback
harry07031d5fa132480e
diff --git a/app/services/task.py b/app/services/task.pyindex 7ed4ea39..210b1909 100644--- a/app/services/task.py+++ b/app/services/task.py@@ -203,6 +203,10 @@ def generate_subtitle(task_id, params, video_script, sub_maker, audio_file):subtitle_provider = config.app.get("subtitle_provider", "edge").strip().lower()logger.info(f"\n\n## generating subtitle, provider: {subtitle_provider}")+ if not subtitle_provider:+ logger.info("subtitle provider is empty, skip subtitle generation")+ return ""+if sub_maker is None and subtitle_provider != "whisper":# 自定义音频不会经过 TTS,因此没有 Edge/Azure 等 TTS 返回的# sub_maker 时间轴。只有 Whisper 可以直接从音频文件转写字幕;@@ -213,16 +217,22 @@ def generate_subtitle(task_id, params, video_script, sub_maker, audio_file):)return ""- subtitle_fallback = Falseif subtitle_provider == "edge":voice.create_subtitle(text=video_script, sub_maker=sub_maker, subtitle_file=subtitle_path)if not os.path.exists(subtitle_path):- subtitle_fallback = True- logger.warning("subtitle file not found, fallback to whisper")+ # Edge 字幕偶尔会因为时间轴与文案无法匹配而没有产出文件。这里不能+ # 自动切换到 Whisper,否则首次失败会在用户不知情的情况下下载数 GB+ # 的模型。只有显式配置 Whisper 时才允许加载模型,Edge 失败则保留+ # 无字幕视频并记录原因,避免意外的网络和磁盘开销。+ logger.warning(+ "edge subtitle generation did not produce a subtitle file; "+ "skip subtitles without falling back to whisper"+ )+ return ""- if subtitle_provider == "whisper" or subtitle_fallback:+ if subtitle_provider == "whisper":subtitle.create(audio_file=audio_file, subtitle_file=subtitle_path)logger.info("\n\n## correcting subtitle")subtitle.correct(subtitle_file=subtitle_path, video_script=video_script)diff --git a/config.example.toml b/config.example.tomlindex 59f6a963..763fbe40 100644--- a/config.example.toml+++ b/config.example.toml@@ -209,7 +209,9 @@ pollinations_model_name = ""mimo_tts_model_name = "mimo-v2.5-tts"mimo_tts_style_prompt = "请用自然、清晰、适合短视频旁白的语气朗读。"-# Subtitle provider: "edge" or "whisper". Leave empty to skip generation.+# Subtitle provider: "edge" or "whisper". Whisper is loaded only when selected+# explicitly; Edge failures do not trigger an automatic Whisper model download.+# Leave empty to skip generation.subtitle_provider = "edge"# FFmpeg is normally downloaded and detected automatically. If detection fails,diff --git a/test/services/test_task.py b/test/services/test_task.pyindex e8c25ed2..af477d4a 100644--- a/test/services/test_task.py+++ b/test/services/test_task.py@@ -470,6 +470,48 @@ class TestTaskService(unittest.TestCase):create_subtitle.assert_not_called()whisper_create.assert_not_called()+ def test_generate_subtitle_does_not_fallback_to_whisper_when_edge_fails(self):+ """+ Edge 没有生成字幕文件时应保留无字幕结果,不能自动下载 Whisper 模型。++ 该场景可能由 TTS 时间轴与原始文案无法匹配触发。自动回退会让未选择+ Whisper 的用户意外下载数 GB 模型,因此必须验证 Whisper 完全不会被调用。+ """+ task_id = "test-edge-subtitle-without-output"+ task_dir = utils.task_dir(task_id)+ params = VideoParams(+ video_subject="edge subtitle",+ video_script="Hello world.",+ subtitle_enabled=True,+ )+ sub_maker = object()++ try:+ with (+ patch.object(+ tm.config,+ "app",+ dict(tm.config.app, subtitle_provider="edge"),+ ),+ patch.object(tm.voice, "create_subtitle") as create_subtitle,+ patch.object(tm.subtitle, "create") as whisper_create,+ patch.object(tm.subtitle, "correct") as whisper_correct,+ ):+ subtitle_path = tm.generate_subtitle(+ task_id=task_id,+ params=params,+ video_script="Hello world.",+ sub_maker=sub_maker,+ audio_file=os.path.join(task_dir, "audio.mp3"),+ )+ finally:+ shutil.rmtree(task_dir, ignore_errors=True)++ self.assertEqual(subtitle_path, "")+ create_subtitle.assert_called_once()+ whisper_create.assert_not_called()+ whisper_correct.assert_not_called()+def test_start_returns_each_intermediate_result(self):"""API 的 script、terms、audio、subtitle 和 materials 模式共用同一条任务