利用 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(voice): stage Azure speech output (#1580)
Rudy Mizrahi Celeklib3abbeb40cd1
diff --git a/app/services/voice.py b/app/services/voice.pyindex dd654072..a9612ee4 100644--- a/app/services/voice.py+++ b/app/services/voice.py@@ -1790,6 +1790,9 @@ def azure_tts_v2(return 0for i in range(3):+ temporary_audio = None+ audio_config = None+ speech_synthesizer = Nonetry:logger.info(f"start, voice name: {voice_name}, rate: {voice_rate}, try: {i + 1}"@@ -1820,9 +1823,15 @@ def azure_tts_v2(logger.error("Azure speech key or region is not set")return None- audio_config = speechsdk.audio.AudioOutputConfig(- filename=voice_file, use_default_speaker=True+ # File and default-speaker output are mutually exclusive SDK modes.+ # Let each attempt own a private file, never the last good export.+ ensure_file_path_exists(voice_file)+ descriptor, temporary_audio = tempfile.mkstemp(+ prefix=".azure-tts-", suffix=".mp3",+ dir=os.path.dirname(os.path.abspath(voice_file)),)+ os.close(descriptor)+ audio_config = speechsdk.audio.AudioOutputConfig(filename=temporary_audio)speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)@@ -1848,6 +1857,15 @@ def azure_tts_v2(# 正式生成都会按 WebUI/API 传入的 voice_rate 调整语速。result = speech_synthesizer.speak_ssml_async(ssml).get()if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:+ if os.path.getsize(temporary_audio) <= 0:+ logger.error("Azure completed without audio; preserve the previous export")+ return None+ # Release SDK file-output owners before publication, including+ # on Windows where an open handle can prevent replacement.+ speech_synthesizer = None+ audio_config = None+ os.replace(temporary_audio, voice_file)+ temporary_audio = Nonelogger.success(f"azure v2 speech synthesis succeeded: {voice_file}")return sub_makerelif result.reason == speechsdk.ResultReason.Canceled:@@ -1862,6 +1880,18 @@ def azure_tts_v2(logger.info(f"completed, output file: {voice_file}")except Exception as e:logger.error(f"failed, error: {str(e)}")+ finally:+ # Exceptions/cancellation can also leave the SDK output owner alive.+ # Drop all file owners before attempting to remove its staged file.+ speech_synthesizer = None+ audio_config = None+ if temporary_audio is not None:+ try:+ os.remove(temporary_audio)+ except FileNotFoundError:+ pass+ except OSError as exc:+ logger.warning(f"failed to remove staged Azure audio: {exc}")return Nonediff --git a/test/services/test_azure_boundary_duration.py b/test/services/test_azure_boundary_duration.pyindex 50ca6e5d..cd864355 100644--- a/test/services/test_azure_boundary_duration.py+++ b/test/services/test_azure_boundary_duration.py@@ -1,4 +1,6 @@from datetime import timedelta+from pathlib import Path+from tempfile import TemporaryDirectoryfrom types import SimpleNamespaceimport unittestfrom unittest.mock import Mock, patch@@ -13,11 +15,20 @@ class TestAzureBoundaryDuration(unittest.TestCase):captured = {}synthesizer = Mock()synthesizer.synthesis_word_boundary.connect.side_effect = lambda callback: captured.update(callback=callback)+ def configure_output(*, filename):+ captured["filename"] = filename+ return Mock()+def speak(ssml):+ # This duration-only SDK fixture must also fulfill the+ # completed file-output contract before publication.+ Path(captured["filename"]).write_bytes(b"fixture-audio")captured["callback"](SimpleNamespace(duration=duration, audio_offset=123, text="word"))return SimpleNamespace(get=lambda: SimpleNamespace(reason=sdk.ResultReason.SynthesizingAudioCompleted))synthesizer.speak_ssml_async.side_effect = speak- with patch.dict(voice.config.azure, {"speech_key": "test-key", "speech_region": "test-region"}), patch.object(sdk.audio, "AudioOutputConfig"), patch.object(sdk, "SpeechConfig"), patch.object(sdk, "SpeechSynthesizer", return_value=synthesizer):- result = voice.azure_tts_v2("Word.", "en-US-AriaNeural-V2-Female", "unused.mp3")+ with TemporaryDirectory() as directory, patch.dict(voice.config.azure, {"speech_key": "test-key", "speech_region": "test-region"}), patch.object(sdk.audio, "AudioOutputConfig", side_effect=configure_output), patch.object(sdk, "SpeechConfig"), patch.object(sdk, "SpeechSynthesizer", return_value=synthesizer):+ output = Path(directory) / "narration.mp3"+ result = voice.azure_tts_v2("Word.", "en-US-AriaNeural-V2-Female", str(output))+ self.assertEqual(output.read_bytes(), b"fixture-audio")self.assertIsNotNone(result)self.assertEqual(result.offset, [(123, 123 + ticks)])diff --git a/test/services/test_azure_file_publication.py b/test/services/test_azure_file_publication.pynew file mode 100644index 00000000..c9f4f5f0--- /dev/null+++ b/test/services/test_azure_file_publication.py@@ -0,0 +1,108 @@+import shutil+import subprocess+import sys+import weakref+from datetime import timedelta+from pathlib import Path+from types import ModuleType, SimpleNamespace+from unittest.mock import patch++import pytest++from app.config import config+from app.services import voice++[email protected]("outcome,strict,nested", [("completed", True, False), ("completed", True, True), ("completed", False, False), ("cancelled", False, False), ("error", False, False), ("empty", False, False)])+def test_public_azure_file_output_preserves_last_success(tmp_path, outcome, strict, nested):+ """Controlled documented SDK file/callback boundary; no real Azure call."""+ if not shutil.which("ffmpeg"):+ pytest.skip("native FFmpeg required")+ complete = subprocess.run(["ffmpeg", "-nostdin", "-v", "error", "-f", "lavfi", "-i", "sine=frequency=880:duration=0.2", "-f", "mp3", "-"], capture_output=True, check=True).stdout+ output = tmp_path / "nested" / "narration.mp3" if nested else tmp_path / "narration.mp3"+ previous = b"previous-successful-export"+ if not nested:+ output.write_bytes(previous)+ configured = []+ open_outputs = set()+ real_remove = voice.os.remove++ def remove_after_sdk_release(path):+ assert str(path) not in open_outputs, "SDK still owns the staged output"+ return real_remove(path)+ requests = []++ class AudioOutput:+ def __init__(self, filename=None, use_default_speaker=False):+ if strict and filename and use_default_speaker:+ raise ValueError("only one output argument may be selected")+ if not Path(filename).parent.is_dir():+ raise OSError("audio output parent does not exist")+ self.filename = filename+ self.output = open(filename, "wb")+ open_outputs.add(str(filename))+ configured.append(filename)++ def __del__(self):+ if hasattr(self, "output"):+ self.output.close()+ open_outputs.discard(str(self.filename))++ class SpeechConfig:+ def __init__(self, **_kwargs):+ pass++ def set_property(self, *_args, **_kwargs):+ pass++ def set_speech_synthesis_output_format(self, *_args):+ pass++ class Synthesizer:+ def __init__(self, audio_config, speech_config):+ self.audio_config = audio_config+ self.destination = Path(audio_config.filename)+ self.callback = None+ owner = weakref.ref(self)+ self.synthesis_word_boundary = SimpleNamespace(connect=lambda callback: setattr(owner(), "callback", callback))++ def speak_ssml_async(self, text):+ requests.append(text)++ def get():+ self.audio_config.output.write(complete if outcome == "completed" else b"" if outcome == "empty" else complete[:50])+ self.audio_config.output.flush()+ if outcome == "error":+ raise OSError("controlled SDK failure after writing partial bytes")+ if self.callback and outcome == "completed":+ self.callback(SimpleNamespace(text="Hello", duration=timedelta(seconds=0.2), audio_offset=0))+ return SimpleNamespace(reason="cancelled" if outcome == "cancelled" else "completed", cancellation_details=SimpleNamespace(reason="error", error_details="fixture"))+ return SimpleNamespace(get=get)++ sdk = ModuleType("azure.cognitiveservices.speech")+ sdk.audio = SimpleNamespace(AudioOutputConfig=AudioOutput)+ sdk.SpeechConfig = SpeechConfig+ sdk.SpeechSynthesizer = Synthesizer+ sdk.SessionEventArgs = object+ sdk.PropertyId = SimpleNamespace(SpeechServiceResponse_RequestWordBoundary="word")+ sdk.SpeechSynthesisOutputFormat = SimpleNamespace(Audio48Khz192KBitRateMonoMp3="mp3")+ sdk.ResultReason = SimpleNamespace(SynthesizingAudioCompleted="completed", Canceled="cancelled")+ sdk.CancellationReason = SimpleNamespace(Error="error")+ azure = ModuleType("azure")+ cognitive = ModuleType("azure.cognitiveservices")+ azure.cognitiveservices = cognitive+ cognitive.speech = sdk+ with patch.object(voice.os, "remove", side_effect=remove_after_sdk_release), patch.dict(sys.modules, {"azure": azure, "azure.cognitiveservices": cognitive, "azure.cognitiveservices.speech": sdk}), patch.dict(config.azure, {"speech_key": "fixture", "speech_region": "fixture"}):+ result = voice.azure_tts_v2("Hello", "en-US-AriaNeural-V2", str(output))+ if outcome == "completed":+ assert result is not None+ assert result.subs == ["Hello"]+ assert output.read_bytes() == complete+ subprocess.run(["ffmpeg", "-nostdin", "-v", "error", "-xerror", "-i", str(output), "-f", "null", "-"], capture_output=True, check=True)+ else:+ assert result is None+ assert output.read_bytes() == previous+ assert not open_outputs+ assert all(Path(path) != output for path in configured)+ assert list(output.parent.iterdir()) == [output]+ assert len(requests) == (3 if outcome in {"cancelled", "error"} else 1)