Skip to content

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

fix(subtitle): allow Whisper subtitles for custom audio

harry0703832bd9e80b4b
diff --git a/app/models/schema.py b/app/models/schema.py
index 33443f39..d2871344 100644
--- a/app/models/schema.py
+++ b/app/models/schema.py
@@ -85,7 +85,7 @@ class VideoParams(BaseModel):
None # Materials used to generate the video
)
- custom_audio_file: Optional[str] = None # Custom audio file path, will ignore video_script and disable subtitle
+ custom_audio_file: Optional[str] = None # Custom audio file path, will ignore TTS and can still use Whisper subtitles
video_language: Optional[str] = "" # auto detect
voice_name: Optional[str] = ""
diff --git a/app/services/task.py b/app/services/task.py
index 9e583467..8a38e6a5 100644
--- a/app/services/task.py
+++ b/app/services/task.py
@@ -194,13 +194,23 @@ def generate_subtitle(task_id, params, video_script, sub_maker, audio_file):
- subtitle_path: path to the generated subtitle file
'''
logger.info("\n\n## generating subtitle")
- if not params.subtitle_enabled or sub_maker is None:
+ if not params.subtitle_enabled:
return ""
subtitle_path = path.join(utils.task_dir(task_id), "subtitle.srt")
subtitle_provider = config.app.get("subtitle_provider", "edge").strip().lower()
logger.info(f"\n\n## generating subtitle, provider: {subtitle_provider}")
+ if sub_maker is None and subtitle_provider != "whisper":
+ # 自定义音频不会经过 TTS,因此没有 Edge/Azure 等 TTS 返回的
+ # sub_maker 时间轴。只有 Whisper 可以直接从音频文件转写字幕;
+ # 其他字幕提供方继续保持原有行为,避免生成错误的空时间轴。
+ logger.warning(
+ "subtitle maker is missing, skip subtitle generation for provider: "
+ f"{subtitle_provider}"
+ )
+ return ""
+
subtitle_fallback = False
if subtitle_provider == "edge":
voice.create_subtitle(
diff --git a/test/services/test_task.py b/test/services/test_task.py
index 13cd27d1..624c5bfd 100644
--- a/test/services/test_task.py
+++ b/test/services/test_task.py
@@ -163,6 +163,94 @@ class TestTaskService(unittest.TestCase):
tts.assert_not_called()
update_task.assert_called_with(task_id, state=tm.const.TASK_STATE_FAILED)
+ def test_generate_subtitle_uses_whisper_for_custom_audio_without_sub_maker(self):
+ """
+ 自定义音频不会经过 TTS,所以没有 sub_maker。
+ Whisper 可以直接从音频文件转写,此时不能被 sub_maker 为空的保护逻辑提前跳过。
+ """
+ task_id = "test-custom-audio-whisper-subtitle"
+ task_dir = utils.task_dir(task_id)
+ audio_file = os.path.join(task_dir, "custom-audio.mp3")
+ Path(audio_file).write_bytes(b"fake audio")
+ params = VideoParams(
+ video_subject="custom audio",
+ video_script="Hello world.",
+ subtitle_enabled=True,
+ )
+
+ def fake_whisper_create(audio_file, subtitle_file):
+ Path(subtitle_file).write_text(
+ "1\n00:00:00,000 --> 00:00:01,000\nHello world.\n\n",
+ encoding="utf-8",
+ )
+
+ try:
+ with (
+ patch.object(
+ tm.config,
+ "app",
+ dict(tm.config.app, subtitle_provider="whisper"),
+ ),
+ patch.object(
+ tm.subtitle, "create", side_effect=fake_whisper_create
+ ) as create,
+ patch.object(tm.subtitle, "correct") as correct,
+ ):
+ subtitle_path = tm.generate_subtitle(
+ task_id=task_id,
+ params=params,
+ video_script="Hello world.",
+ sub_maker=None,
+ audio_file=audio_file,
+ )
+ finally:
+ shutil.rmtree(task_dir, ignore_errors=True)
+
+ self.assertTrue(subtitle_path.endswith("subtitle.srt"))
+ create.assert_called_once_with(audio_file=audio_file, subtitle_file=subtitle_path)
+ correct.assert_called_once_with(
+ subtitle_file=subtitle_path, video_script="Hello world."
+ )
+
+ def test_generate_subtitle_skips_edge_provider_without_sub_maker(self):
+ """
+ Edge 字幕依赖 TTS 返回的 sub_maker 时间轴。
+ 自定义音频缺少该对象时应继续跳过,避免产生不可信的字幕时间轴。
+ """
+ task_id = "test-custom-audio-edge-no-submaker"
+ task_dir = utils.task_dir(task_id)
+ audio_file = os.path.join(task_dir, "custom-audio.mp3")
+ Path(audio_file).write_bytes(b"fake audio")
+ params = VideoParams(
+ video_subject="custom audio",
+ video_script="Hello world.",
+ subtitle_enabled=True,
+ )
+
+ 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,
+ ):
+ subtitle_path = tm.generate_subtitle(
+ task_id=task_id,
+ params=params,
+ video_script="Hello world.",
+ sub_maker=None,
+ audio_file=audio_file,
+ )
+ finally:
+ shutil.rmtree(task_dir, ignore_errors=True)
+
+ self.assertEqual(subtitle_path, "")
+ create_subtitle.assert_not_called()
+ whisper_create.assert_not_called()
+
@unittest.skipUnless(
RUN_INTEGRATION_TESTS,
"MPT_RUN_INTEGRATION_TESTS not set",