Skip to content

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

fix(video): remove temporary clips if concat fails

Rudy Celekli80e86017fd3f
diff --git a/app/services/video.py b/app/services/video.py
index 9a0593ca..b15f5040 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -991,24 +991,26 @@ def combine_videos(
clip_files = [clip.file_path for clip in processed_clips]
logger.info(f"concatenating {len(clip_files)} clips with ffmpeg")
- concat_video_clips_with_ffmpeg(
- clip_files=clip_files,
- output_file=combined_video_path,
- threads=threads,
- output_dir=output_dir,
- max_duration=audio_duration,
- )
- if used_video_paths is not None:
- # Exclude safety-margin clips that FFmpeg trims entirely from the output.
- elapsed = 0.0
- for clip in processed_clips:
- if elapsed >= audio_duration:
- break
- used_video_paths.append(clip.source_file_path)
- elapsed += clip.duration
-
- # clean temp files
- delete_files(clip_files)
+ try:
+ concat_video_clips_with_ffmpeg(
+ clip_files=clip_files,
+ output_file=combined_video_path,
+ threads=threads,
+ output_dir=output_dir,
+ max_duration=audio_duration,
+ )
+ if used_video_paths is not None:
+ # Exclude safety-margin clips that FFmpeg trims entirely from the output.
+ elapsed = 0.0
+ for clip in processed_clips:
+ if elapsed >= audio_duration:
+ break
+ used_video_paths.append(clip.source_file_path)
+ elapsed += clip.duration
+ finally:
+ # FFmpeg failures and timeouts must not strand one encoded MP4 per clip.
+ # Repeated clips share a path; delete_files already deduplicates them.
+ delete_files(clip_files)
logger.info("video combining completed")
return combined_video_path
diff --git a/test/services/test_video.py b/test/services/test_video.py
index e6f39b70..4010b654 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -1003,6 +1003,49 @@ class TestVideoService(unittest.TestCase):
self.assertEqual(write_mock.call_count, 4)
self.assertEqual(concat_mock.call_args.kwargs["max_duration"], 10.0)
+ def test_combine_videos_cleans_temp_clips_when_concat_fails(self):
+ """A failed final merge must not strand encoded clips on disk."""
+
+ class FakeAudioClip:
+ duration = 1.0
+
+ class FakeVideoClip:
+ duration = 2.0
+ size = (1080, 1920)
+ w = 1080
+ h = 1920
+
+ def subclipped(self, _start, _end):
+ return self
+
+ def write_clip(_clip, output_file, **_kwargs):
+ Path(output_file).write_bytes(b"encoded clip")
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ output_file = os.path.join(temp_dir, "combined.mp4")
+ temp_clip = Path(temp_dir, "temp-clip-1.mp4")
+ with (
+ patch.object(vd, "AudioFileClip", return_value=FakeAudioClip()),
+ patch.object(vd, "_open_video_clip_quietly", return_value=FakeVideoClip()),
+ patch.object(
+ vd, "_write_videofile_with_codec_fallback", side_effect=write_clip
+ ),
+ patch.object(
+ vd,
+ "concat_video_clips_with_ffmpeg",
+ side_effect=RuntimeError("concat failed"),
+ ),
+ ):
+ with self.assertRaisesRegex(RuntimeError, "concat failed"):
+ vd.combine_videos(
+ combined_video_path=output_file,
+ video_paths=["clip.mp4"],
+ audio_file="audio.mp3",
+ video_concat_mode=vd.VideoConcatMode.sequential,
+ )
+
+ self.assertFalse(temp_clip.exists())
+
def test_concat_video_clips_limits_output_to_audio_duration(self):
"""最终拼接时应裁到音频时长,避免安全余量带来明显静音尾巴。"""