Skip to content

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

test(task): cover audio_duration file-vs-SubMaker priority and fallback

Yusuf Selim Akdemirbfe98673922f
diff --git a/app/services/task.py b/app/services/task.py
index 1b068dbd..0197f391 100644
--- a/app/services/task.py
+++ b/app/services/task.py
@@ -535,15 +535,13 @@ def generate_audio(
"failed to synthesize audio; verify the selected voice and TTS connectivity",
)
return None, None, None
- # Measure the real written audio_file, not sub_maker.cues[-1].end.
- # SubMaker duration is the last WORD BOUNDARY's end time; TTS engines
- # commonly leave a short tail beyond that (silence/breath room), so
- # the two differ. Using the word-boundary figure as "the" audio
- # duration under-counts - verified with a 2-sentence test TTS call
- # where SubMaker measured 5.39s vs 7s for the actual file, a 1.6s
- # gap. Anything sized off the under-counted duration (e.g. video
- # clips trimmed to match narration length) can end up shorter than
- # the real audio, silently truncating the end of the narration.
+ # Measure the real written audio_file, not sub_maker.cues[-1].end:
+ # the latter is the last WORD BOUNDARY, and TTS leaves a fixed tail
+ # past it (Edge TTS: ~0.88s at any length - 19% of a 7-word clip but
+ # 1.4% of a 153-word one, so short scripts suffer most). The
+ # under-count sizes paid generate_bgm() calls, is reported as
+ # audio_duration to the API/WebUI, and under-sources
+ # download_videos() material, scaled by video_count.
file_duration = voice.get_audio_duration(audio_file)
audio_duration = math.ceil(
file_duration if file_duration > 0 else voice.get_audio_duration(sub_maker)
diff --git a/test/services/test_task.py b/test/services/test_task.py
index c201fb1f..1f5a5151 100644
--- a/test/services/test_task.py
+++ b/test/services/test_task.py
@@ -831,6 +831,144 @@ class TestTaskService(unittest.TestCase):
self.assertEqual(failed_task["failed_stage"], "audio")
self.assertIn("does not exist", failed_task["error"])
+ def test_generate_audio_prefers_file_duration_over_sub_maker(self):
+ # Every fixture deliberately makes the file duration and the SubMaker
+ # duration ceil to DIFFERENT integers. If someone "simplifies" them to
+ # values that share a ceil, this test can no longer tell which source
+ # the implementation used - it stops discriminating, silently.
+ cases = (
+ # The maintainer's own reproduction numbers from the PR discussion.
+ (8.4, 7.8375, 9),
+ # An exact-integer file duration: proves math.ceil() is really
+ # used and rules out int()+1 style code that adds a spurious
+ # second. The SubMaker value ceils to 7, so 8 can only come
+ # from the file.
+ (8.0, 6.2, 8),
+ # File duration shorter than the SubMaker value: the only case
+ # where this change makes audio_duration smaller than before (the
+ # old code returned 8). The contract is "the file wins", not
+ # "the larger value wins".
+ (5.0, 7.8375, 5),
+ )
+
+ for file_duration, sub_maker_duration, expected in cases:
+ with self.subTest(file_duration=file_duration):
+ task_id = f"test-tts-audio-priority-{uuid4().hex}"
+ task_dir = utils.task_dir(task_id)
+ audio_path = os.path.join(task_dir, "audio.mp3")
+ params = VideoParams(
+ video_subject="tts audio",
+ video_script="",
+ voice_name="test-voice",
+ )
+ sub_maker = MagicMock()
+
+ def fake_duration(target, _file=file_duration, _sub=sub_maker_duration):
+ # Dispatch on argument type, never on call order: a
+ # sequence side_effect would still pass against an
+ # implementation that measured the SubMaker first, which
+ # is exactly the regression this test exists to catch.
+ return _file if isinstance(target, str) else _sub
+
+ try:
+ with (
+ patch.object(tm.voice, "tts", return_value=sub_maker) as tts,
+ patch.object(
+ tm.voice, "get_audio_duration", side_effect=fake_duration
+ ) as get_duration,
+ ):
+ audio_file, audio_duration, result_sub_maker = tm.generate_audio(
+ task_id, params, "script"
+ )
+ finally:
+ shutil.rmtree(task_dir, ignore_errors=True)
+
+ self.assertEqual(audio_file, audio_path)
+ self.assertEqual(audio_duration, expected)
+ # Asserting the value alone would still pass an
+ # implementation returning 9.0; the type assertion pins the
+ # other side of the rounding contract, so a refactor cannot
+ # drop math.ceil() and pass the float straight through.
+ self.assertIsInstance(audio_duration, int)
+ self.assertIs(result_sub_maker, sub_maker)
+ tts.assert_called_once()
+ # When file measurement succeeds the SubMaker must not be
+ # measured at all: exactly one call, and that call's argument
+ # is the audio file path. Both assertions together are what
+ # prove the priority order.
+ self.assertEqual(len(get_duration.call_args_list), 1)
+ self.assertEqual(get_duration.call_args_list[0].args[0], audio_path)
+
+ def test_generate_audio_falls_back_to_sub_maker_when_file_duration_is_zero(self):
+ task_id = "test-tts-audio-fallback"
+ task_dir = utils.task_dir(task_id)
+ audio_path = os.path.join(task_dir, "audio.mp3")
+ params = VideoParams(
+ video_subject="tts audio",
+ video_script="",
+ voice_name="test-voice",
+ )
+ sub_maker = MagicMock()
+
+ def fake_duration(target):
+ # voice.get_audio_duration() returns 0.0 when file measurement
+ # fails (missing file or decode error); only then may the
+ # SubMaker word-boundary duration be used.
+ return 0.0 if isinstance(target, str) else 7.8375
+
+ try:
+ with (
+ patch.object(tm.voice, "tts", return_value=sub_maker),
+ patch.object(
+ tm.voice, "get_audio_duration", side_effect=fake_duration
+ ) as get_duration,
+ ):
+ audio_file, audio_duration, result_sub_maker = tm.generate_audio(
+ task_id, params, "script"
+ )
+ finally:
+ shutil.rmtree(task_dir, ignore_errors=True)
+
+ self.assertEqual(audio_file, audio_path)
+ self.assertEqual(audio_duration, 8)
+ self.assertIsInstance(audio_duration, int)
+ self.assertIs(result_sub_maker, sub_maker)
+ self.assertEqual(len(get_duration.call_args_list), 2)
+ self.assertEqual(get_duration.call_args_list[0].args[0], audio_path)
+ self.assertIs(get_duration.call_args_list[1].args[0], sub_maker)
+
+ def test_generate_audio_fails_when_file_and_sub_maker_durations_are_zero(self):
+ # This change replaces the source of audio_duration, so the
+ # pre-existing zero-duration guard must be proven to still fire
+ # rather than be bypassed by the new file-measurement branch.
+ task_id = "test-tts-audio-zero-duration"
+ task_dir = utils.task_dir(task_id)
+ params = VideoParams(
+ video_subject="tts audio",
+ video_script="",
+ voice_name="test-voice",
+ )
+ sub_maker = MagicMock()
+
+ try:
+ with (
+ patch.object(tm.voice, "tts", return_value=sub_maker),
+ patch.object(tm.voice, "get_audio_duration", return_value=0.0),
+ patch.object(tm, "_mark_task_failed") as mark_task_failed,
+ ):
+ audio_file, audio_duration, result_sub_maker = tm.generate_audio(
+ task_id, params, "script"
+ )
+ finally:
+ shutil.rmtree(task_dir, ignore_errors=True)
+
+ self.assertIsNone(audio_file)
+ self.assertIsNone(audio_duration)
+ self.assertIsNone(result_sub_maker)
+ mark_task_failed.assert_called_once_with(
+ task_id, "audio", "generated audio duration is zero"
+ )
+
def test_generate_subtitle_uses_whisper_for_custom_audio_without_sub_maker(self):
"""
自定义音频不会经过 TTS,所以没有 sub_maker。