利用 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(tts): avoid resubmitting accepted audio (#1516)
Rudy Mizrahi Celeklidff4a0a107eb
diff --git a/app/services/voice.py b/app/services/voice.pyindex 6e0d91b4..31fd6c0c 100644--- a/app/services/voice.py+++ b/app/services/voice.py@@ -1577,6 +1577,7 @@ def siliconflow_tts(for i in range(3): # 尝试3次temporary_audio = None+ response_accepted = Falsetry:logger.info(f"start siliconflow tts, model: {model}, voice: {voice}, try: {i + 1}"@@ -1590,6 +1591,7 @@ def siliconflow_tts()if response.status_code == 200:+ response_accepted = Trueif not response.content:logger.error("siliconflow tts returned empty audio")return None@@ -1649,6 +1651,10 @@ def siliconflow_tts(return Noneexcept Exception as e:logger.error(f"siliconflow tts failed: {str(e)}")+ if response_accepted:+ # Retrying local processing cannot recover this accepted response+ # and would submit another synthesis request.+ return Nonefinally:if temporary_audio and os.path.exists(temporary_audio):try:@@ -2492,6 +2498,7 @@ def _openai_compatible_tts(for i in range(3):temporary_audio = None+ response_accepted = Falsetry:logger.info(f"start {provider} tts, voice: {voice}, try: {i + 1}")ensure_file_path_exists(voice_file)@@ -2503,6 +2510,7 @@ def _openai_compatible_tts()continue+ response_accepted = Trueif not response.content:raise ValueError(f"{provider} returned empty audio")@@ -2533,6 +2541,10 @@ def _openai_compatible_tts()except Exception as e:logger.error(f"{provider} tts failed: {str(e)}")+ if response_accepted:+ # Retrying local processing cannot recover this accepted response+ # and would submit another synthesis request.+ return Nonefinally:if temporary_audio and os.path.exists(temporary_audio):try:diff --git a/test/services/test_kokoro.py b/test/services/test_kokoro.pyindex f3a086a4..a0298ed2 100644--- a/test/services/test_kokoro.py+++ b/test/services/test_kokoro.py@@ -118,9 +118,10 @@ def test_failed_audio_never_overwrites_output(monkeypatch, tmp_path, provider, f) is Noneassert output.read_bytes() == b"previous audio"assert list(tmp_path.iterdir()) == [output]- assert post.call_count == 3+ expected_attempts = 3 if failure in {"http", "timeout"} else 1+ assert post.call_count == expected_attemptsif failure in {"zero", "nan", "replace"}:- assert clip.close.call_count == 3+ clip.close.assert_called_once()def test_transient_error_retries_successfully(monkeypatch, tmp_path):diff --git a/test/services/test_media_accepted_audio_retries.py b/test/services/test_media_accepted_audio_retries.pynew file mode 100644index 00000000..25d04021--- /dev/null+++ b/test/services/test_media_accepted_audio_retries.py@@ -0,0 +1,56 @@+from unittest.mock import Mock++from pydub import AudioSegment+import pytest+import requests++from app.services import voice+++def accepted_audio():+ response = requests.Response()+ response.status_code = 200+ with AudioSegment.silent(duration=200).export(format='mp3') as encoded:+ response._content = encoded.read()+ return response+++def invoke(provider, output):+ if provider == 'siliconflow':+ return voice.siliconflow_tts('Hello world.', 'test-model', 'test-voice', 1.0, str(output))+ return voice._openai_compatible_tts(+ 'test-provider', 'http://127.0.0.1:9999/v1', '', 'test-model', 'test-voice',+ 'Hello world.', 1.0, str(output),+ )++[email protected]('provider', ['siliconflow', 'compatible'])+def test_accepted_audio_publication_failure_does_not_repeat_post(tmp_path, monkeypatch, accepted_audio, provider):+ output = tmp_path / 'voice.mp3'+ output.write_bytes(b'previous complete narration')+ post = Mock(return_value=accepted_audio)+ monkeypatch.setattr(voice.requests, 'post', post)+ monkeypatch.setitem(voice.config.siliconflow, 'api_key', 'test-local-key')++ def cannot_publish(*args):+ raise OSError('destination is unavailable')++ monkeypatch.setattr(voice.os, 'replace', cannot_publish)+ assert invoke(provider, output) is None+ assert post.call_count == 1+ assert output.read_bytes() == b'previous complete narration'+ assert list(tmp_path.iterdir()) == [output]++[email protected]('provider', ['siliconflow', 'compatible'])+def test_connect_timeout_keeps_existing_retry_and_success_policy(tmp_path, monkeypatch, accepted_audio, provider):+ output = tmp_path / 'voice.mp3'+ post = Mock(side_effect=[requests.ConnectTimeout('before connection'), accepted_audio])+ monkeypatch.setattr(voice.requests, 'post', post)+ monkeypatch.setitem(voice.config.siliconflow, 'api_key', 'test-local-key')++ assert invoke(provider, output) is not None+ assert post.call_count == 2+ assert voice.get_audio_duration(str(output)) > 0+ assert list(tmp_path.iterdir()) == [output]