利用 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(video): publish concatenated videos only after successful encoding
RudyCeleklif3e9a6db5e30
diff --git a/app/services/video.py b/app/services/video.pyindex e249a6ce..18ca9247 100644--- a/app/services/video.py+++ b/app/services/video.py@@ -575,6 +575,8 @@ def concat_video_clips_with_ffmpeg(delete_files(concat_list_file)raise+ staged_output = None+def build_command(codec: str) -> list[str]:command = [utils.get_ffmpeg_binary(),@@ -594,23 +596,29 @@ def concat_video_clips_with_ffmpeg(]if max_duration is not None and max_duration > 0:command.extend(["-t", f"{max_duration:.3f}"])- command.append(output_file)+ command.append(staged_output)return commanddef run_concat(codec: str):command = build_command(codec)# 使用 ffmpeg 只做一次串联与编码,避免 MoviePy 逐段合并时反复重编码,# 从而降低画质劣化与颜色偏移风险。阻塞等待期间由心跳日志体现任务仍在运行。- result = _run_concat_with_heartbeat(command, output_file)+ result = _run_concat_with_heartbeat(command, staged_output)if result.returncode != 0:error_message = (result.stderr or result.stdout or "").strip()raise RuntimeError(error_message or "ffmpeg concat failed")return codectry:+ descriptor, staged_output = tempfile.mkstemp(+ prefix=".ffmpeg-concat-",+ suffix=os.path.splitext(output_file)[1] or ".mp4",+ dir=os.path.dirname(os.path.abspath(output_file)),+ )+ os.close(descriptor)effective_codec = _get_effective_video_codec()try:- return run_concat(effective_codec)+ result_codec = run_concat(effective_codec)except TimeoutError:# A hung encoder is not evidence that another codec will work. Do# not spend a second timeout period retrying the same input.@@ -620,9 +628,14 @@ def concat_video_clips_with_ffmpeg(raiseresult_codec = run_concat(_DEFAULT_VIDEO_CODEC)_disable_runtime_video_codec(effective_codec, str(exc))- return result_codec+ # Failed attempts and in-progress output stay private until FFmpeg has+ # finished. Publication errors must not trigger another codec attempt.+ if os.path.getsize(staged_output) == 0:+ raise RuntimeError("ffmpeg concat produced no output")+ os.replace(staged_output, output_file)+ return result_codecfinally:- delete_files(concat_list_file)+ delete_files([concat_list_file, staged_output])def _sanitize_image_file(image_path: str) -> str:diff --git a/test/services/test_concat_manifest_isolation.py b/test/services/test_concat_manifest_isolation.pyindex 88ffb510..104c4770 100644--- a/test/services/test_concat_manifest_isolation.py+++ b/test/services/test_concat_manifest_isolation.py@@ -13,17 +13,20 @@ class TestConcatManifestIsolation(unittest.TestCase):def test_concurrent_concat_reads_only_its_own_inputs(self):barrier = threading.Barrier(2)observed = {}+ render = threading.local()manifests = []with tempfile.TemporaryDirectory() as directory:def run(command, output_file):manifest = Path(command[command.index("-i") + 1])manifests.append(manifest)barrier.wait(timeout=5)- observed[output_file] = manifest.read_text(encoding="utf-8")+ observed[render.name] = manifest.read_text(encoding="utf-8")+ Path(command[-1]).write_bytes(b"encoded-video")barrier.wait(timeout=5)return SimpleNamespace(returncode=0)def concat(name):+ render.name = namevideo.concat_video_clips_with_ffmpeg([str(Path(directory) / f"{name}-source.mp4")],str(Path(directory) / f"{name}.mp4"), 1, directory,@@ -35,7 +38,7 @@ class TestConcatManifestIsolation(unittest.TestCase):with ThreadPoolExecutor(max_workers=2) as executor:list(executor.map(concat, ["first", "second"]))for name in ("first", "second"):- self.assertIn(f"{name}-source.mp4", observed[str(Path(directory) / f"{name}.mp4")])+ self.assertIn(f"{name}-source.mp4", observed[name])self.assertEqual(len(set(manifests)), 2)self.assertTrue(all(not manifest.exists() for manifest in manifests))diff --git a/test/services/test_media_atomic_concat.py b/test/services/test_media_atomic_concat.pynew file mode 100644index 00000000..d85eda34--- /dev/null+++ b/test/services/test_media_atomic_concat.py@@ -0,0 +1,46 @@+from pathlib import Path+import subprocess++import pytest++from app.services import video++[email protected]("previous", [None, b"previous complete video"])[email protected]("error", [RuntimeError("encode failed"), TimeoutError("encoder timed out")])+def test_concat_failure_never_publishes_partial_video(tmp_path, monkeypatch, previous, error):+ output = tmp_path / "combined.mp4"+ if previous is not None:+ output.write_bytes(previous)+ monkeypatch.setattr(video, "_get_effective_video_codec", lambda: "libx264")++ def failed_encode(command, reported_output):+ Path(command[-1]).write_bytes(b"partial encoding")+ raise error++ monkeypatch.setattr(video, "_run_concat_with_heartbeat", failed_encode)+ with pytest.raises(type(error)):+ video.concat_video_clips_with_ffmpeg(["input.mp4"], str(output), 1, str(tmp_path))+ if previous is None:+ assert not output.exists()+ else:+ assert output.read_bytes() == previous+ assert sorted(p.name for p in tmp_path.iterdir()) == ([output.name] if previous is not None else [])+++def test_concat_success_publishes_real_ffmpeg_video(tmp_path, monkeypatch):+ source = tmp_path / "source.mp4"+ output = tmp_path / "combined.mp4"+ binary = video.utils.get_ffmpeg_binary()+ subprocess.run(+ [binary, "-y", "-f", "lavfi", "-i", "color=c=blue:s=64x64:r=10",+ "-t", "0.2", "-c:v", "libx264", "-pix_fmt", "yuv420p", str(source)],+ check=True, capture_output=True,+ )+ output.write_bytes(b"old video")+ monkeypatch.setattr(video, "_get_effective_video_codec", lambda: "libx264")+ assert video.concat_video_clips_with_ffmpeg([str(source)], str(output), 1, str(tmp_path)) == "libx264"+ with video.VideoFileClip(str(output), audio=False) as clip:+ assert clip.duration >= 0.19+ assert clip.size == [64, 64]+ assert sorted(p.name for p in tmp_path.iterdir()) == ["combined.mp4", "source.mp4"]diff --git a/test/services/test_video.py b/test/services/test_video.pyindex d684b90c..04f56786 100644--- a/test/services/test_video.py+++ b/test/services/test_video.py@@ -856,6 +856,7 @@ class TestVideoService(unittest.TestCase):stdout="",stderr="nvenc device not available",)+ Path(command[-1]).write_bytes(b"encoded-video")return types.SimpleNamespace(returncode=0, stdout="", stderr="")with tempfile.TemporaryDirectory() as temp_dir:@@ -1343,6 +1344,7 @@ class TestVideoService(unittest.TestCase):"""最终拼接时应裁到音频时长,避免安全余量带来明显静音尾巴。"""def fake_run(command, capture_output, text, check, **kwargs):+ Path(command[-1]).write_bytes(b"encoded-video")return types.SimpleNamespace(returncode=0, stdout="", stderr="")with tempfile.TemporaryDirectory() as temp_dir:@@ -1361,7 +1363,8 @@ class TestVideoService(unittest.TestCase):command = run.call_args.args[0]self.assertEqual(command[command.index("-t") + 1], "10.000")- self.assertLess(command.index("-t"), command.index(output_file))+ self.assertLess(command.index("-t"), len(command) - 1)+ self.assertNotEqual(command[-1], output_file)def test_concat_video_clips_logs_heartbeat_while_ffmpeg_runs(self):"""@@ -1372,6 +1375,7 @@ class TestVideoService(unittest.TestCase):def slow_run(command, capture_output, text, check, **kwargs):# 模拟一次耗时拼接:这段窗口内心跳线程应至少记录一次存活日志。time.sleep(0.2)+ Path(command[-1]).write_bytes(b"encoded-video")return types.SimpleNamespace(returncode=0, stdout="", stderr="")with tempfile.TemporaryDirectory() as temp_dir: