Skip to content

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

fix(voice): validate remote speech audio (#1579)

Rudy Mizrahi Celekli37436d49938b
diff --git a/app/services/voice.py b/app/services/voice.py
index f6e865e6..dd654072 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -29,6 +29,7 @@ from moviepy.audio.io.AudioFileClip import AudioFileClip
from openai import OpenAI
from app.config import config
+from app.services import bgm
from app.utils import utils
from app.utils.subtitle_writer import staged_subtitle_file
@@ -1681,6 +1682,7 @@ def siliconflow_tts(
sub_maker = ensure_legacy_submaker_fields(SubMaker())
try:
+ _validate_remote_tts_audio(temporary_audio)
audio_clip = AudioFileClip(temporary_audio)
try:
audio_duration = audio_clip.duration
@@ -2257,6 +2259,19 @@ def get_minimax_voice_catalog(
return catalog
+def _validate_remote_tts_audio(file_path: str) -> None:
+ """Decode accepted audio fully before replacing a successful narration.
+
+ MoviePy's duration/initial frame probe does not validate later MP3 frames.
+ Reuse the bounded, self-contained FFmpeg audio validator without following
+ playlists or resubmitting an already accepted speech request.
+ """
+ try:
+ bgm.validate_audio_file(file_path, timeout_seconds=120)
+ except (bgm.BgmUploadError, bgm.BgmServiceError) as exc:
+ raise ValueError("TTS returned audio that could not be fully decoded") from exc
+
+
def _write_validated_minimax_audio(audio_bytes: bytes, voice_file: str) -> float:
"""
将 MiniMax 音频原子写入目标路径,并返回时长。
@@ -2276,6 +2291,7 @@ def _write_validated_minimax_audio(audio_bytes: bytes, voice_file: str) -> float
with open(temp_path, "wb") as output:
output.write(audio_bytes)
+ _validate_remote_tts_audio(temp_path)
audio_clip = AudioFileClip(temp_path)
try:
audio_duration = float(audio_clip.duration)
@@ -2494,6 +2510,7 @@ def elevenlabs_tts(
logger.error("ElevenLabs TTS returned no audio data")
return None
+ _validate_remote_tts_audio(temp_path)
audio_clip = AudioFileClip(temp_path)
try:
audio_duration = float(audio_clip.duration)
@@ -2604,6 +2621,7 @@ def _openai_compatible_tts(
temporary_audio = f.name
f.write(response.content)
+ _validate_remote_tts_audio(temporary_audio)
audio_clip = AudioFileClip(temporary_audio)
try:
audio_duration = audio_clip.duration
@@ -2878,6 +2896,7 @@ def fish_audio_tts(
temporary_audio = f.name
f.write(response.content)
+ _validate_remote_tts_audio(temporary_audio)
audio_clip = AudioFileClip(temporary_audio)
try:
audio_duration = audio_clip.duration
diff --git a/test/services/test_fish_audio.py b/test/services/test_fish_audio.py
index aa16a018..70675d85 100644
--- a/test/services/test_fish_audio.py
+++ b/test/services/test_fish_audio.py
@@ -140,6 +140,7 @@ class TestFishAudioDispatch(unittest.TestCase):
class TestFishAudioTTSRequest(unittest.TestCase):
"""fish_audio_tts sends correct request payloads."""
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def _call_with_capture(self, voice_rate=1.0, voice_volume=1.0,
reference_id=None, model="s2.1-pro-free",
response=None):
@@ -222,6 +223,7 @@ class TestFishAudioTTSRequest(unittest.TestCase):
class TestFishAudioErrorHandling(unittest.TestCase):
"""Error responses are handled gracefully."""
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def _call_with_status(self, status_code, text="error"):
resp = _FakeResponse(status_code=status_code, content=b"", text=text)
@@ -256,6 +258,7 @@ class TestFishAudioErrorHandling(unittest.TestCase):
result = vs.fish_audio_tts("Test.", voice_file)
self.assertIsNone(result)
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_invalid_success_preserves_existing_audio_without_repeat_charge(self):
response = _FakeResponse(content=b"invalid audio" * 20)
with (
@@ -273,6 +276,7 @@ class TestFishAudioErrorHandling(unittest.TestCase):
self.assertEqual(list(Path(tmp_dir).iterdir()), [voice_file])
post.assert_called_once()
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_valid_audio_replaces_existing_file_after_decoding(self):
response = _FakeResponse(content=b"new mp3" * 30)
with tempfile.TemporaryDirectory() as tmp_dir:
diff --git a/test/services/test_kokoro.py b/test/services/test_kokoro.py
index fc870e97..fc0d6b6e 100644
--- a/test/services/test_kokoro.py
+++ b/test/services/test_kokoro.py
@@ -1,7 +1,7 @@
"""Kokoro 协议兼容和共享音频传输回归,不依赖外部服务。"""
from types import SimpleNamespace
-from unittest.mock import Mock
+from unittest.mock import Mock, patch
import pytest
import requests
@@ -72,6 +72,7 @@ def test_unspeakable_text_does_not_make_requests(monkeypatch, kokoro_config, tmp
post.assert_not_called()
+@patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
@pytest.mark.parametrize("provider", ["kokoro", "chatterbox"])
@pytest.mark.parametrize("rate, expected", [(0.1, 0.25), (1.2, 1.2), (5, 4.0)])
def test_transport_closes_audio_and_preserves_contract(monkeypatch, tmp_path, provider, rate, expected):
@@ -94,6 +95,7 @@ def test_transport_closes_audio_and_preserves_contract(monkeypatch, tmp_path, pr
assert post.call_args.kwargs["headers"]["Authorization"] == "Bearer key"
+@patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
@pytest.mark.parametrize("provider", ["kokoro", "chatterbox"])
@pytest.mark.parametrize("failure", ["empty", "decode", "zero", "nan", "replace", "http", "timeout", "connect_timeout"])
def test_failed_audio_never_overwrites_output(monkeypatch, tmp_path, provider, failure):
@@ -128,6 +130,7 @@ def test_failed_audio_never_overwrites_output(monkeypatch, tmp_path, provider, f
clip.close.assert_called_once()
+@patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_preconnection_error_retries_successfully(monkeypatch, tmp_path):
responses = [requests.ConnectTimeout(), SimpleNamespace(status_code=200, content=b"audio", text="")]
post = Mock(side_effect=responses)
diff --git a/test/services/test_remote_speech_complete_decode.py b/test/services/test_remote_speech_complete_decode.py
new file mode 100644
index 00000000..61a43c69
--- /dev/null
+++ b/test/services/test_remote_speech_complete_decode.py
@@ -0,0 +1,78 @@
+import json
+import shutil
+import subprocess
+import threading
+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+from unittest.mock import patch
+
+import pytest
+import requests
+from moviepy.audio.io.AudioFileClip import AudioFileClip
+
+from app.config import config
+from app.services import voice
+
+
[email protected]("provider", ["siliconflow", "minimax", "elevenlabs", "openai", "fish"])
[email protected]("corrupt", [True, False])
+def test_accepted_audio_requires_full_decode_and_preserves_previous_export(tmp_path, provider, corrupt):
+ if not shutil.which("ffmpeg"):
+ pytest.skip("native FFmpeg required")
+ complete = subprocess.run(["ffmpeg", "-nostdin", "-v", "error", "-f", "lavfi", "-i", "sine=frequency=440:duration=2", "-f", "mp3", "-"], capture_output=True, check=True).stdout
+ damaged = complete[:len(complete)*2//5] + bytes(len(complete)//5) + complete[len(complete)*3//5:]
+ payload = damaged if corrupt else complete
+ probe = tmp_path / "probe.mp3"
+ probe.write_bytes(payload)
+ with AudioFileClip(str(probe)) as clip:
+ assert clip.duration > 0
+ decoded = subprocess.run(["ffmpeg", "-nostdin", "-v", "error", "-xerror", "-i", str(probe), "-f", "null", "-"], capture_output=True)
+ assert (decoded.returncode != 0) == corrupt
+ seen = []
+
+ class Handler(BaseHTTPRequestHandler):
+ def log_message(self, *_args):
+ pass
+
+ def do_POST(self):
+ seen.append(json.loads(self.rfile.read(int(self.headers["Content-Length"]))))
+ data = json.dumps({"base_resp": {"status_code": 0}, "data": {"status": 2, "audio": payload.hex()}}).encode() if provider == "minimax" else payload
+ self.send_response(200)
+ self.send_header("Content-Type", "application/json" if provider == "minimax" else "audio/mpeg")
+ self.send_header("Content-Length", str(len(data)))
+ self.end_headers()
+ self.wfile.write(data)
+
+ server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
+ thread = threading.Thread(target=server.serve_forever)
+ thread.start()
+ real_post = requests.post
+
+ def post_to_fixture(url, **kwargs):
+ assert url.startswith("https://")
+ return real_post(f"http://127.0.0.1:{server.server_port}/speech", **kwargs)
+
+ output = tmp_path / "narration.mp3"
+ output.write_bytes(complete)
+ try:
+ with patch.dict(config.siliconflow, {"api_key": "fixture"}), patch.dict(config.minimax_tts, {"api_key": "fixture"}), patch.dict(config.elevenlabs, {"api_key": "fixture"}), patch.object(voice, "get_fish_audio_api_key", return_value="fixture"), patch.object(voice.requests, "post", side_effect=post_to_fixture):
+ calls = {
+ "siliconflow": lambda: voice.siliconflow_tts("hello", "model", "voice", 1.0, str(output)),
+ "minimax": lambda: voice.minimax_tts("hello", "voice", 1.0, str(output)),
+ "elevenlabs": lambda: voice.elevenlabs_tts("hello", "voice", str(output)),
+ "openai": lambda: voice._openai_compatible_tts("fixture", "https://fixture.invalid/v1", "fixture", "model", "voice", "hello", 1.0, str(output)),
+ "fish": lambda: voice.fish_audio_tts("hello", str(output)),
+ }
+ result = calls[provider]()
+ assert len(seen) == 1, "accepted audio must not trigger another speech POST"
+ if corrupt:
+ assert result is None
+ assert output.read_bytes() == complete
+ else:
+ assert result is not None
+ assert output.read_bytes() == complete
+ assert sorted(path.name for path in tmp_path.iterdir()) == ["narration.mp3", "probe.mp3"]
+ finally:
+ server.shutdown()
+ server.server_close()
+ thread.join(5)
+ assert not thread.is_alive()
diff --git a/test/services/test_voice.py b/test/services/test_voice.py
index a84663a6..118b4e3e 100644
--- a/test/services/test_voice.py
+++ b/test/services/test_voice.py
@@ -700,6 +700,7 @@ class TestVoiceService(unittest.TestCase):
self.assertEqual(getattr(sub_maker, "subs", []), ["小米语音合成测试", "第二句话"])
self.assertEqual(len(getattr(sub_maker, "offset", [])), 2)
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_minimax_tts_uses_regional_endpoint_and_hex_audio(self):
class _Response:
status_code, text = 200, ""
@@ -743,6 +744,7 @@ class TestVoiceService(unittest.TestCase):
self.assertEqual(captured["json"]["voice_setting"]["voice_id"], "male-qn-qingse")
self.assertEqual(captured["json"]["audio_setting"]["format"], "mp3")
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_minimax_tts_reuses_cn_llm_key_and_endpoint(self):
"""TTS 未单独配置时,应复用同区域的 MiniMax LLM 凭证和地址。"""
class _Response:
@@ -861,6 +863,7 @@ class TestVoiceService(unittest.TestCase):
with self.assertRaisesRegex(RuntimeError, "invalid api key"):
vs.get_minimax_voice_catalog(api_key="invalid-key")
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_minimax_tts_does_not_leave_invalid_audio_output(self):
"""响应音频无法解析时,不应覆盖已有文件或留下临时文件。"""
class _Response:
@@ -928,6 +931,7 @@ class TestVoiceService(unittest.TestCase):
with patch.object(vs.config, "chatterbox", {}):
self.assertEqual(vs.get_chatterbox_voices(), ["chatterbox:default-Female"])
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_chatterbox_tts_posts_to_openai_compatible_endpoint(self):
"""Success path: POST /audio/speech, write audio, return legacy SubMaker."""
@@ -1033,6 +1037,7 @@ class TestVoiceService(unittest.TestCase):
return _BrokenClip
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_elevenlabs_tts_audio_clip_closed_on_duration_error(self):
"""AudioFileClip.close() must be called even when reading .duration raises."""
close_calls: list = []
@@ -1069,6 +1074,7 @@ class TestVoiceService(unittest.TestCase):
self.assertIsNone(result)
self.assertTrue(close_calls, "AudioFileClip.close() was never called")
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_chatterbox_tts_audio_clip_closed_on_duration_error(self):
"""AudioFileClip.close() must be called even when reading .duration raises."""
close_calls: list = []
@@ -1099,6 +1105,7 @@ class TestVoiceService(unittest.TestCase):
self.assertIsNone(result)
self.assertTrue(close_calls, "AudioFileClip.close() was never called")
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_fish_audio_tts_audio_clip_closed_on_duration_error(self):
"""AudioFileClip.close() must be called even when reading .duration raises."""
close_calls: list = []
@@ -1502,6 +1509,7 @@ class TestElevenLabsVoice(unittest.TestCase):
result = vs.get_elevenlabs_voices("fake-key")
self.assertEqual(result, [])
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
@patch("app.services.voice.requests.post")
@patch("app.services.voice.AudioFileClip")
@patch("app.services.voice.config")
@@ -1525,6 +1533,7 @@ class TestElevenLabsVoice(unittest.TestCase):
if os.path.exists(out_path):
os.remove(out_path)
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_elevenlabs_tts_rejects_oversize_audio_without_replacing_existing_file(self):
"""A paid 200 response can be much larger than usable speech."""
response = SimpleNamespace(
@@ -1550,6 +1559,7 @@ class TestElevenLabsVoice(unittest.TestCase):
self.assertEqual(post.call_count, 1)
self.assertEqual(sorted(path.name for path in Path(tmp_dir).iterdir()), ["voice.mp3"])
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_elevenlabs_tts_decode_failure_preserves_existing_file(self):
"""A corrupt successful response must not publish a partial final MP3."""
response = SimpleNamespace(
@@ -1644,6 +1654,7 @@ class TestElevenLabsVoice(unittest.TestCase):
)
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_siliconflow_subtitle_spans_full_audio_duration(self):
"""Last subtitle entry must end at the actual audio end, not truncated early.
@@ -1758,6 +1769,7 @@ class TestElevenLabsVoice(unittest.TestCase):
self.assertIsNone(result)
post.assert_called_once()
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_siliconflow_tts_rejects_invalid_success_audio(self):
"""HTTP 200 with corrupt audio must not become a fake 10-second success."""
fake_response = SimpleNamespace(status_code=200, content=b"invalid mp3")
@@ -1780,6 +1792,7 @@ class TestElevenLabsVoice(unittest.TestCase):
self.assertIsNone(result)
post.assert_called_once()
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_siliconflow_tts_invalid_audio_preserves_existing_narration(self):
fake_response = SimpleNamespace(status_code=200, content=b"invalid mp3")
with (
@@ -1802,6 +1815,7 @@ class TestElevenLabsVoice(unittest.TestCase):
self.assertEqual(voice_file.read_bytes(), b"previous valid narration")
self.assertEqual(list(Path(temp_dir).iterdir()), [voice_file])
+ @patch("app.services.voice._validate_remote_tts_audio", new=lambda _path: None)
def test_siliconflow_tts_publishes_only_after_audio_validation(self):
fake_response = SimpleNamespace(status_code=200, content=b"new mp3")
with tempfile.TemporaryDirectory() as temp_dir: