Skip to content

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

fix(tts): preserve Gemini narration when MP3 export fails

Rudy Celekli4a53c8d8596f
diff --git a/app/services/voice.py b/app/services/voice.py
index c1f1f757..40cc6a92 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -1817,6 +1817,7 @@ def gemini_tts(
from google.genai import types
_configure_pydub_ffmpeg(AudioSegment)
+ temporary_audio = None
try:
api_key = config.app.get("gemini_api_key", "")
if not api_key:
@@ -1892,21 +1893,29 @@ def gemini_tts(
# pydub 会返回打开的输出文件对象。批量生成时若不主动关闭,文件描述符
# 会持续累积,并在 Windows 上增加后续覆盖或删除音频文件失败的概率。
- exported_audio = audio_segment.export(voice_file, format="mp3")
+ if len(audio_segment) <= 0:
+ raise ValueError("Gemini returned empty PCM audio")
+ temp_fd, temporary_audio = tempfile.mkstemp(
+ prefix=".gemini-tts-", suffix=".mp3",
+ dir=os.path.dirname(os.path.abspath(voice_file)),
+ )
+ os.close(temp_fd)
+ exported_audio = audio_segment.export(temporary_audio, format="mp3")
exported_audio.close()
- logger.info(f"completed, output file: {voice_file}")
-
# Gemini 拿不到 edge_tts 那种逐词边界事件,因此这里退回到
# 项目原有的 `subs/offset` 兼容结构,至少保证后续字幕与时长
# 计算链路可继续工作。
sub_maker = ensure_legacy_submaker_fields(SubMaker())
audio_duration = len(audio_segment) / 1000.0 # 转换为秒
- return populate_legacy_submaker_with_full_text(
+ sub_maker = populate_legacy_submaker_with_full_text(
sub_maker=sub_maker,
text=text,
audio_duration_seconds=audio_duration,
)
+ os.replace(temporary_audio, voice_file)
+ logger.info(f"completed, output file: {voice_file}")
+ return sub_maker
except ImportError as e:
logger.error(f"Missing required package for Gemini TTS: {str(e)}. Please install: pip install pydub")
@@ -1915,6 +1924,13 @@ def gemini_tts(
logger.error(f"Gemini TTS failed, error: {str(e)}")
return None
+ finally:
+ if temporary_audio and os.path.exists(temporary_audio):
+ try:
+ os.unlink(temporary_audio)
+ except OSError as cleanup_error:
+ logger.warning(f"could not remove Gemini staging audio: {cleanup_error}")
+
def mimo_tts(
text: str,
diff --git a/test/services/test_gemini_atomic_export.py b/test/services/test_gemini_atomic_export.py
new file mode 100644
index 00000000..e9f1b66f
--- /dev/null
+++ b/test/services/test_gemini_atomic_export.py
@@ -0,0 +1,31 @@
+import tempfile
+import unittest
+from pathlib import Path
+from types import SimpleNamespace
+from unittest.mock import patch
+from pydub import AudioSegment
+from app.services import voice
+
+
+class TestGeminiAtomicExport(unittest.TestCase):
+ def test_failed_encoder_preserves_previous_narration_and_cleans_staging(self):
+ tone = AudioSegment.silent(duration=100).set_frame_rate(24000).set_channels(1).set_sample_width(2)
+ response = SimpleNamespace(candidates=[SimpleNamespace(content=SimpleNamespace(parts=[SimpleNamespace(inline_data=SimpleNamespace(data=tone.raw_data))]))])
+ class Client:
+ def __init__(self, **kwargs):
+ self.models = SimpleNamespace(generate_content=lambda **kwargs: response)
+ def __enter__(self):
+ return self
+ def __exit__(self, *args):
+ pass
+ def failed_export(segment, out_f, **kwargs):
+ Path(out_f).write_bytes(b"partial encoder output")
+ raise OSError("encoder failed")
+ with tempfile.TemporaryDirectory() as tmp:
+ output = Path(tmp) / "narration.mp3"
+ output.write_bytes(b"previous narration")
+ with patch("google.genai.Client", Client), patch.dict(voice.config.app, {"gemini_api_key": "test-key"}), patch.object(AudioSegment, "export", failed_export):
+ result = voice.gemini_tts("Hello.", "Zephyr", 1, str(output))
+ self.assertIsNone(result)
+ self.assertEqual(output.read_bytes(), b"previous narration")
+ self.assertEqual(list(Path(tmp).iterdir()), [output])