利用 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): time out FFmpeg narration (#1472)
Rudy Mizrahi Celeklicce2da724e9f
diff --git a/app/services/voice.py b/app/services/voice.pyindex b5253755..53ce9148 100644--- a/app/services/voice.py+++ b/app/services/voice.py@@ -85,6 +85,7 @@ VOXCPM_REFERENCE_AUDIO_MAX_WAV_BYTES = 5 * 1024 * 1024VOXCPM_REFERENCE_AUDIO_CONVERSION_TIMEOUT_SECONDS = 15VOXCPM_REFERENCE_AUDIO_MAX_DURATION_SECONDS = 120VOXCPM_REFERENCE_AUDIO_FILE_TYPES = ("wav", "mp3", "m4a", "aac", "ogg", "flac")+_DEFAULT_TTS_FFMPEG_TIMEOUT_SECONDS = 600_VOXCPM_NON_RETRYABLE_STATUS_CODES = {400, 401, 403, 404, 422}_VOXCPM_RETRY_DELAY_SECONDS = (1.0, 2.0)NO_VOICE_NAME = "no-voice"@@ -557,6 +558,9 @@ def generate_silent_audio(duration_seconds: float, output_file: str) -> bool:ffmpeg_binary = utils.get_ffmpeg_binary()command = [ffmpeg_binary,+ "-nostdin",+ "-v",+ "error","-y","-f","lavfi",@@ -568,33 +572,12 @@ def generate_silent_audio(duration_seconds: float, output_file: str) -> bool:"libmp3lame","-q:a","4",- output_file,]logger.info(f"generating silent audio for no-voice mode, duration: {duration_seconds:.2f}s")- result = subprocess.run(- command,- capture_output=True,- text=True,- encoding="utf-8",- errors="replace",- check=False,- )- if result.returncode != 0:- logger.error(- "failed to generate silent audio: "- f"{(result.stderr or result.stdout or '').strip()}"- )- return False- if not os.path.exists(output_file) or os.path.getsize(output_file) <= 0:- logger.error(- "silent audio output file is missing or empty, "- f"file: {output_file}, duration: {duration_seconds:.2f}s"- )- return False- return True+ return _publish_tts_ffmpeg_output(command, output_file, "silent narration encode")def _single_tts(@@ -731,6 +714,69 @@ def _single_tts(return azure_tts_v1(text, voice_name, voice_rate, voice_file)+def _run_tts_ffmpeg(command: list[str], stage: str):+ """Bound TTS FFmpeg work and reap the child on timeout."""+ raw_timeout = config.app.get(+ "ffmpeg_tts_timeout_seconds",+ _DEFAULT_TTS_FFMPEG_TIMEOUT_SECONDS,+ )+ try:+ timeout = float(raw_timeout)+ except (TypeError, ValueError) as exc:+ raise ValueError("ffmpeg_tts_timeout_seconds must be positive") from exc+ if not math.isfinite(timeout) or timeout <= 0:+ raise ValueError("ffmpeg_tts_timeout_seconds must be positive")++ try:+ # subprocess.run kills and waits for the child on timeout. Do not allow+ # FFmpeg to wait for terminal input in an unattended generation task.+ return subprocess.run(+ command,+ capture_output=True,+ text=True,+ encoding="utf-8",+ errors="replace",+ check=False,+ timeout=timeout,+ )+ except subprocess.TimeoutExpired:+ logger.error(f"FFmpeg {stage} timed out after {timeout:g} seconds")+ except OSError as exc:+ logger.error(f"failed to start FFmpeg {stage}: {exc}")+ return None+++def _publish_tts_ffmpeg_output(+ command: list[str], output_file: str, stage: str+) -> bool:+ """Encode beside the destination and replace only after a complete output."""+ output_dir = os.path.dirname(output_file) or "."+ with tempfile.TemporaryDirectory(+ prefix=".mpt-tts-audio-", dir=output_dir+ ) as publish_temp:+ staged_output = os.path.join(+ publish_temp, f"narration{os.path.splitext(output_file)[1] or '.mp3'}"+ )+ result = _run_tts_ffmpeg([*command, staged_output], stage)+ if result is None:+ return False+ if result.returncode != 0:+ logger.error(+ f"FFmpeg {stage} failed: "+ f"{(result.stderr or result.stdout or '').strip()}"+ )+ return False+ if not os.path.exists(staged_output) or os.path.getsize(staged_output) == 0:+ logger.error(f"FFmpeg {stage} produced no narration audio")+ return False+ try:+ os.replace(staged_output, output_file)+ except OSError as exc:+ logger.error(f"failed to publish {stage} audio: {exc}")+ return False+ return True++def _concat_audio_files(audio_files: list[str], output_file: str) -> bool:"""使用 PCM 解码与统一重编码合并多个音频分段。@@ -791,6 +837,9 @@ def _concat_audio_files(audio_files: list[str], output_file: str) -> bool:pcm_wav = os.path.join(concat_temp, f"chunk_{idx}.wav")cmd = [ffmpeg_binary,+ "-nostdin",+ "-v",+ "error","-y","-i",f,@@ -803,7 +852,9 @@ def _concat_audio_files(audio_files: list[str], output_file: str) -> bool:"pcm_s16le",pcm_wav,]- res = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", check=False)+ res = _run_tts_ffmpeg(cmd, "pause audio decode")+ if res is None:+ return Falseif res.returncode == 0 and os.path.exists(pcm_wav):try:with wave.open(pcm_wav, "rb") as wf:@@ -824,6 +875,9 @@ def _concat_audio_files(audio_files: list[str], output_file: str) -> bool:command = [ffmpeg_binary,+ "-nostdin",+ "-v",+ "error","-y","-i",temp_combined_wav,@@ -831,16 +885,10 @@ def _concat_audio_files(audio_files: list[str], output_file: str) -> bool:"libmp3lame","-q:a","4",- output_file,]- result = subprocess.run(command, capture_output=True, text=True, encoding="utf-8", errors="replace", check=False)- if result.returncode != 0:- logger.error(- "failed to encode concatenated audio to mp3: "- f"{(result.stderr or result.stdout or '').strip()}"- )- return False- return os.path.exists(output_file) and os.path.getsize(output_file) > 0+ return _publish_tts_ffmpeg_output(+ command, output_file, "pause audio encode"+ )def _tts_with_pauses(@@ -933,6 +981,9 @@ def _tts_with_pauses(ffmpeg_binary = utils.get_ffmpeg_binary()cmd = [ffmpeg_binary,+ "-nostdin",+ "-v",+ "error","-y","-i",chunk_audio_file,@@ -945,7 +996,9 @@ def _tts_with_pauses("pcm_s16le",chunk_wav,]- res = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", check=False)+ res = _run_tts_ffmpeg(cmd, "speech chunk decode")+ if res is None:+ return Noneif res.returncode != 0 or not os.path.exists(chunk_wav) or os.path.getsize(chunk_wav) == 0:logger.error(f"failed to decode speech chunk audio to PCM WAV: {speech_text[:50]}, "diff --git a/config.example.toml b/config.example.tomlindex d7efbfb5..ad649350 100644--- a/config.example.toml+++ b/config.example.toml@@ -445,6 +445,12 @@ subtitle_provider = "edge"# is stopped and the task reports a failure instead of staying at 50% forever.# ffmpeg_concat_timeout_seconds = 3600+# No-voice MP3 and pause-tag narration use FFmpeg. Bound each invocation so a+# stuck conversion fails the task rather than waiting indefinitely (default:+# 600 seconds per invocation). Increase this on unusually slow machines or for+# very long narration chunks.+# ffmpeg_tts_timeout_seconds = 600+# Leave unset to follow the app default (libx264). Hardware encoders are optional;# unsupported encoders automatically fall back to libx264.# Available values: "libx264", "h264_nvenc", "h264_amf", "h264_qsv",diff --git a/test/services/test_voice.py b/test/services/test_voice.pyindex 6655b83d..5ec52be8 100644--- a/test/services/test_voice.py+++ b/test/services/test_voice.py@@ -2,6 +2,7 @@ import asyncioimport base64import osimport shutil+import subprocessimport unittestimport sysimport tempfile@@ -187,6 +188,20 @@ class TestVoiceService(unittest.TestCase):self.assertFalse(vs.generate_silent_audio(3.0, voice_file))+ def test_no_voice_ffmpeg_timeout_preserves_previous_output(self):+ def timed_out(command, **kwargs):+ self.assertIn("-nostdin", command)+ self.assertGreater(kwargs.get("timeout", 0), 0)+ Path(command[-1]).write_bytes(b"partial-silence")+ raise subprocess.TimeoutExpired(command, kwargs["timeout"])++ with tempfile.TemporaryDirectory() as temp_dir:+ output = Path(temp_dir) / "silent.mp3"+ output.write_bytes(b"previous-success")+ with patch.object(vs.subprocess, "run", side_effect=timed_out):+ self.assertFalse(vs.generate_silent_audio(3.0, str(output)))+ self.assertEqual(output.read_bytes(), b"previous-success")+def test_empty_voice_name_does_not_enable_no_voice_mode(self):"""空 voice 通常意味着配置缺失或接口参数错误,不能自动切到无配音模式。@@ -1876,6 +1891,42 @@ class TestElevenLabsVoice(unittest.TestCase):self.assertTrue(read_sizes)self.assertLessEqual(max(read_sizes), 8192)+ def test_pause_audio_decode_timeout_fails_without_publishing(self):+ def timed_out(command, **kwargs):+ self.assertIn("-nostdin", command)+ self.assertGreater(kwargs.get("timeout", 0), 0)+ raise subprocess.TimeoutExpired(command, kwargs["timeout"])++ with tempfile.TemporaryDirectory() as temp_dir:+ inputs = [str(Path(temp_dir) / f"chunk-{index}.mp3") for index in range(2)]+ for input_path in inputs:+ Path(input_path).write_bytes(b"compressed-audio")+ output = str(Path(temp_dir) / "voice.mp3")+ with patch.object(vs.subprocess, "run", side_effect=timed_out):+ self.assertFalse(vs._concat_audio_files(inputs, output))+ self.assertFalse(Path(output).exists())++ def test_pause_audio_encode_timeout_preserves_previous_output(self):+ def timed_out(command, **kwargs):+ self.assertIn("-nostdin", command)+ self.assertGreater(kwargs.get("timeout", 0), 0)+ Path(command[-1]).write_bytes(b"partial-output")+ raise subprocess.TimeoutExpired(command, kwargs["timeout"])++ with tempfile.TemporaryDirectory() as temp_dir:+ inputs = [str(Path(temp_dir) / f"chunk-{index}.wav") for index in range(2)]+ for input_path in inputs:+ with vs.wave.open(input_path, "wb") as writer:+ writer.setnchannels(1)+ writer.setsampwidth(2)+ writer.setframerate(24000)+ writer.writeframes(b"\0\0" * 20)+ output = Path(temp_dir) / "voice.mp3"+ output.write_bytes(b"previous-success")+ with patch.object(vs.subprocess, "run", side_effect=timed_out):+ self.assertFalse(vs._concat_audio_files(inputs, str(output)))+ self.assertEqual(output.read_bytes(), b"previous-success")+def test_tts_with_pauses_shifts_submaker_timeline(self):"""测试包含停顿标签时,SubMaker 时间轴和音频拼接正确偏移。"""from edge_tts.srt_composer import Subtitle@@ -2371,6 +2422,31 @@ class TestElevenLabsVoice(unittest.TestCase):)self.assertIsNone(result)+ def test_tts_with_pauses_decode_timeout_returns_failure(self):+ def fake_single_tts(text, voice_name, voice_rate, voice_file, voice_volume=1.0):+ Path(voice_file).write_bytes(b"synthesized-audio")+ return vs.SubMaker()++ def timed_out(command, **kwargs):+ self.assertIn("-nostdin", command)+ self.assertGreater(kwargs.get("timeout", 0), 0)+ raise subprocess.TimeoutExpired(command, kwargs["timeout"])++ with tempfile.TemporaryDirectory() as temp_dir:+ output = Path(temp_dir) / "narration.mp3"+ with (+ patch.object(vs, "_single_tts", side_effect=fake_single_tts),+ patch.object(vs.subprocess, "run", side_effect=timed_out),+ ):+ result = vs._tts_with_pauses(+ text="Hello [pause: 1s] world",+ voice_name="zh-CN-XiaoxiaoNeural",+ voice_rate=1.0,+ voice_file=str(output),+ )+ self.assertIsNone(result)+ self.assertFalse(output.exists())+def test_tts_passes_original_text_unchanged_without_pauses(self):"""测试无停顿标签时,tts 将原始文本原样直通给 _single_tts,不执行正则替换或清洗。"""original_text = " Leading and trailing spaces, [regular bracket] and punctuation! \nNew line here. "