Skip to content

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

fix(tts): reject Edge streams with no audio payload

Rudy Celekliee38e0b594d5
diff --git a/app/services/voice.py b/app/services/voice.py
index 496b00f7..69e73a6c 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -1360,6 +1360,13 @@ def azure_tts_v1(
communicate, _handle_chunk, timeout_seconds=timeout_seconds
)
+ # Edge can finish a stream with timing events but no audio payload.
+ # Those events produce a nonempty SRT, yet the MP3 is unplayable.
+ if os.path.getsize(voice_file) == 0:
+ logger.warning("failed, edge tts stream contained no audio")
+ os.remove(voice_file)
+ continue
+
if not sub_maker.get_srt():
logger.warning("failed, sub_maker.get_srt() is empty")
continue
diff --git a/test/services/test_voice.py b/test/services/test_voice.py
index ebd078cd..aa258e5c 100644
--- a/test/services/test_voice.py
+++ b/test/services/test_voice.py
@@ -316,6 +316,45 @@ class TestVoiceService(unittest.TestCase):
self.assertEqual(len(sub_maker.events), 1)
self.assertEqual(sub_maker.events[0]["type"], "WordBoundary")
+ def test_azure_tts_v1_rejects_boundary_only_stream(self):
+ """Subtitle events without audio must not produce a successful TTS result."""
+
+ class _BoundaryOnlyCommunicate:
+ def __init__(self, text, voice, rate="+0%", boundary=None):
+ pass
+
+ def stream_sync(self):
+ yield {
+ "type": "WordBoundary",
+ "offset": 0,
+ "duration": 10000000,
+ "text": "hello",
+ }
+
+ class _FakeSubMaker:
+ def __init__(self):
+ self.events = []
+
+ def feed(self, chunk):
+ self.events.append(chunk)
+
+ def get_srt(self):
+ return "1\n00:00:00,000 --> 00:00:01,000\nhello\n" if self.events else ""
+
+ with tempfile.TemporaryDirectory() as tmp_dir, patch.object(
+ vs.edge_tts, "Communicate", _BoundaryOnlyCommunicate
+ ), patch.object(vs.edge_tts, "SubMaker", _FakeSubMaker):
+ voice_file = Path(tmp_dir) / "boundary-only.mp3"
+ result = vs.azure_tts_v1(
+ text="hello",
+ voice_name="en-US-AriaNeural-Female",
+ voice_file=str(voice_file),
+ voice_rate=1.0,
+ )
+
+ self.assertIsNone(result)
+ self.assertFalse(voice_file.exists())
+
def test_azure_tts_v1_times_out_hanging_stream_sync(self):
"""
验证 Azure TTS V1 在 edge_tts 同步流卡住时能够快速失败。