Skip to content

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

fix(tts): convert Azure boundary durations without string parsing

Rudy Celekli012e3d60d1ba
diff --git a/app/services/voice.py b/app/services/voice.py
index d1766b37..49ae5dd6 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -14,7 +14,7 @@ import threading
import time
import unicodedata
import wave
-from datetime import datetime, timedelta
+from datetime import timedelta
from typing import Union
from urllib.parse import urlparse
from xml.sax.saxutils import escape, unescape
@@ -1699,19 +1699,13 @@ def azure_tts_v2(
ssml = _build_azure_v2_ssml(text, voice_name, voice_rate)
def _format_duration_to_offset(duration) -> int:
- if isinstance(duration, str):
- time_obj = datetime.strptime(duration, "%H:%M:%S.%f")
- milliseconds = (
- (time_obj.hour * 3600000)
- + (time_obj.minute * 60000)
- + (time_obj.second * 1000)
- + (time_obj.microsecond // 1000)
- )
- return milliseconds * 10000
-
+ if isinstance(duration, timedelta):
+ # Speech SDK durations are timedeltas; integer arithmetic retains
+ # all microseconds and also handles zero/exact-second durations.
+ return ((duration.days * 86400 + duration.seconds) * 1000000
+ + duration.microseconds) * 10
if isinstance(duration, int):
return duration
-
return 0
for i in range(3):
@@ -1733,7 +1727,7 @@ def azure_tts_v2(
# print('\tTextOffset: {}'.format(evt.text_offset))
# print('\tWordLength: {}'.format(evt.word_length))
- duration = _format_duration_to_offset(str(evt.duration))
+ duration = _format_duration_to_offset(evt.duration)
offset = _format_duration_to_offset(evt.audio_offset)
sub_maker.subs.append(evt.text)
sub_maker.offset.append((offset, offset + duration))
diff --git a/test/services/test_azure_boundary_duration.py b/test/services/test_azure_boundary_duration.py
new file mode 100644
index 00000000..50ca6e5d
--- /dev/null
+++ b/test/services/test_azure_boundary_duration.py
@@ -0,0 +1,23 @@
+from datetime import timedelta
+from types import SimpleNamespace
+import unittest
+from unittest.mock import Mock, patch
+import azure.cognitiveservices.speech as sdk
+from app.services import voice
+
+
+class TestAzureBoundaryDuration(unittest.TestCase):
+ def test_sdk_timedeltas_preserve_exact_seconds_and_submillisecond_ticks(self):
+ for duration, ticks in [(timedelta(0), 0), (timedelta(seconds=1), 10000000), (timedelta(microseconds=123456), 1234560)]:
+ with self.subTest(duration=duration):
+ captured = {}
+ synthesizer = Mock()
+ synthesizer.synthesis_word_boundary.connect.side_effect = lambda callback: captured.update(callback=callback)
+ def speak(ssml):
+ 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")
+ self.assertIsNotNone(result)
+ self.assertEqual(result.offset, [(123, 123 + ticks)])