Skip to content

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

fix(video): isolate FFmpeg concat manifests across concurrent renders

Rudy Celekli30ca4059e2a7
diff --git a/app/services/video.py b/app/services/video.py
index 5ccd8845..bba1533b 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -558,10 +558,22 @@ def concat_video_clips_with_ffmpeg(
output_dir: str,
max_duration: float | None = None,
):
- concat_list_file = os.path.join(output_dir, "ffmpeg-concat-list.txt")
- with open(concat_list_file, "w", encoding="utf-8") as fp:
- for clip_file in clip_files:
- fp.write(f"file '{_format_ffmpeg_concat_path(clip_file)}'\n")
+ # Separate renders may share a directory. Each FFmpeg process must keep its
+ # own manifest until all codec attempts finish, without overwriting or
+ # deleting another render's list.
+ concat_list_file = None
+ try:
+ with tempfile.NamedTemporaryFile(
+ mode="w", encoding="utf-8", prefix="ffmpeg-concat-", suffix=".txt",
+ dir=output_dir, delete=False,
+ ) as fp:
+ concat_list_file = fp.name
+ for clip_file in clip_files:
+ fp.write(f"file '{_format_ffmpeg_concat_path(clip_file)}'\n")
+ except Exception:
+ if concat_list_file:
+ delete_files(concat_list_file)
+ raise
def build_command(codec: str) -> list[str]:
command = [
diff --git a/test/services/test_concat_manifest_isolation.py b/test/services/test_concat_manifest_isolation.py
new file mode 100644
index 00000000..88ffb510
--- /dev/null
+++ b/test/services/test_concat_manifest_isolation.py
@@ -0,0 +1,52 @@
+import tempfile
+import threading
+import unittest
+from concurrent.futures import ThreadPoolExecutor
+from pathlib import Path
+from types import SimpleNamespace
+from unittest.mock import patch
+
+from app.services import video
+
+
+class TestConcatManifestIsolation(unittest.TestCase):
+ def test_concurrent_concat_reads_only_its_own_inputs(self):
+ barrier = threading.Barrier(2)
+ observed = {}
+ 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")
+ barrier.wait(timeout=5)
+ return SimpleNamespace(returncode=0)
+
+ def concat(name):
+ video.concat_video_clips_with_ffmpeg(
+ [str(Path(directory) / f"{name}-source.mp4")],
+ str(Path(directory) / f"{name}.mp4"), 1, directory,
+ )
+
+ with patch.object(video, "_run_concat_with_heartbeat", side_effect=run), patch.object(
+ video, "_get_effective_video_codec", return_value="libx264"
+ ):
+ 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.assertEqual(len(set(manifests)), 2)
+ self.assertTrue(all(not manifest.exists() for manifest in manifests))
+
+ def test_encoder_failure_removes_only_its_manifest(self):
+ with tempfile.TemporaryDirectory() as directory:
+ unrelated = Path(directory) / "ffmpeg-concat-list.txt"
+ unrelated.write_text("another operation's manifest", encoding="utf-8")
+ with patch.object(video, "_run_concat_with_heartbeat", side_effect=RuntimeError("encoder failed")), patch.object(
+ video, "_get_effective_video_codec", return_value="libx264"
+ ):
+ with self.assertRaisesRegex(RuntimeError, "encoder failed"):
+ video.concat_video_clips_with_ffmpeg(["source.mp4"], "result.mp4", 1, directory)
+ self.assertEqual(unrelated.read_text(encoding="utf-8"), "another operation's manifest")
+ self.assertEqual(list(Path(directory).iterdir()), [unrelated])