利用 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): require every pause narration chunk before publishing
Rudy Celekli064bb971e879
diff --git a/app/services/voice.py b/app/services/voice.pyindex 31fd6c0c..3d5018d8 100644--- a/app/services/voice.py+++ b/app/services/voice.py@@ -811,13 +811,20 @@ def _concat_audio_files(audio_files: list[str], output_file: str) -> bool:def append_pcm(reader):nonlocal combined_frames+ chunk_frames = 0while chunk := reader.readframes(8192):+ if len(chunk) % 2:+ raise ValueError("incomplete PCM sample in narration chunk")combined_wave.writeframesraw(chunk)- combined_frames += len(chunk) // 2+ chunk_frames += len(chunk) // 2+ if not chunk_frames or chunk_frames != reader.getnframes():+ raise ValueError("empty or truncated narration chunk")+ combined_frames += chunk_framesfor idx, f in enumerate(audio_files):if not os.path.exists(f) or os.path.getsize(f) == 0:- continue+ logger.error(f"narration chunk is missing or empty: {f}")+ return False# 检查是否已经是 24000Hz 16-bit mono WAVis_valid_pcm_wav = False@@ -869,6 +876,7 @@ def _concat_audio_files(audio_files: list[str], output_file: str) -> bool:return Falseelse:logger.error(f"failed to decode audio chunk with ffmpeg: {res.stderr}")+ return Falseif not combined_frames:logger.error("no valid audio samples to concatenate")diff --git a/test/services/test_pause_concat_completeness.py b/test/services/test_pause_concat_completeness.pynew file mode 100644index 00000000..477b1662--- /dev/null+++ b/test/services/test_pause_concat_completeness.py@@ -0,0 +1,60 @@+from pathlib import Path+import wave++import pytest++from app.services import voice+++def write_pcm(target, frames=2400):+ with wave.open(str(target), "wb") as writer:+ writer.setnchannels(1)+ writer.setsampwidth(2)+ writer.setframerate(24000)+ writer.writeframes(b"\x01\x00" * frames)++[email protected]("bad_chunk", ["missing", "empty", "invalid", "header_only", "truncated"])[email protected]("existing", [False, True])+def test_concat_requires_every_audio_chunk(tmp_path, bad_chunk, existing):+ good = tmp_path / "good.wav"+ bad = tmp_path / "bad.wav"+ output = tmp_path / "narration.wav"+ write_pcm(good)+ if bad_chunk == "empty":+ bad.write_bytes(b"")+ elif bad_chunk == "invalid":+ bad.write_bytes(b"not audio")+ elif bad_chunk == "header_only":+ write_pcm(bad, frames=0)+ elif bad_chunk == "truncated":+ write_pcm(bad)+ bad.write_bytes(bad.read_bytes()[:-2])+ if existing:+ output.write_bytes(b"previous complete narration")+ assert voice._concat_audio_files([str(good), str(bad)], str(output)) is False+ assert output.read_bytes() == b"previous complete narration" if existing else not output.exists()+++def test_pause_tts_propagates_lost_chunk_without_replaying_synthesis(tmp_path, monkeypatch):+ calls = []++ def synthesize(**kwargs):+ calls.append(kwargs["text"])+ write_pcm(kwargs["voice_file"])+ maker = voice.ensure_legacy_submaker_fields(voice.SubMaker())+ return voice.populate_legacy_submaker_with_full_text(maker, kwargs["text"], 0.1)++ original_concat = voice._concat_audio_files++ def lose_last_chunk(files, output):+ Path(files[-1]).unlink()+ return original_concat(files, output)++ monkeypatch.setattr(voice, "_single_tts", synthesize)+ monkeypatch.setattr(voice, "_concat_audio_files", lose_last_chunk)+ output = tmp_path / "narration.mp3"+ output.write_bytes(b"previous complete narration")+ assert voice.tts("First [pause: 0.1s] Last", "en-US-JennyNeural", 1.0, str(output)) is None+ assert calls == ["First", "Last"]+ assert output.read_bytes() == b"previous complete narration"