利用 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): clean failed clip encodes before continuing
Rudy Celeklie6869a4926c3
diff --git a/app/services/video.py b/app/services/video.pyindex b15f5040..276c1779 100644--- a/app/services/video.py+++ b/app/services/video.py@@ -878,8 +878,12 @@ def combine_videos(f"remaining: {required_video_duration - video_duration:.2f}s")+ source_clip = None+ clip = None+ clip_file = Nonetry:- clip = _open_video_clip_quietly(subclipped_item.file_path).subclipped(+ source_clip = _open_video_clip_quietly(subclipped_item.file_path)+ clip = source_clip.subclipped(subclipped_item.start_time, subclipped_item.end_time)# 播放速度属于素材本身属性,应在转场前应用。这样 Fade/Slide 等一秒转场@@ -949,7 +953,6 @@ def combine_videos(# Store clip duration before closingclip_duration_saved = clip.duration- close_clip(clip)processed_clips.append(SubClippedVideoClip(@@ -961,9 +964,19 @@ def combine_videos())video_duration += clip_duration_saved+ clip_file = Noneexcept Exception as e:logger.error(f"failed to process clip: {str(e)}")+ finally:+ # The derived clip shares its FFmpeg reader with the source. If+ # subclipping itself failed, close the original source instead.+ close_clip(clip if clip is not None else source_clip)+ # MoviePy may leave a truncated MP4 even when encoding raises. It+ # was never added to processed_clips, so concat cleanup cannot see it.+ # Close the reader first so Windows can remove the partial file.+ if clip_file:+ delete_files(clip_file)# loop processed clips until the video duration covers the audio duration and the small safety margin.if video_duration < required_video_duration:diff --git a/test/services/test_video.py b/test/services/test_video.pyindex 4010b654..e6a2b784 100644--- a/test/services/test_video.py+++ b/test/services/test_video.py@@ -1046,6 +1046,65 @@ class TestVideoService(unittest.TestCase):self.assertFalse(temp_clip.exists())+ def test_combine_videos_cleans_failed_encoded_clip_and_reader(self):+ """A bad source must not strand a partial MP4 or an FFmpeg reader."""++ class FakeAudioClip:+ duration = 0.5++ def close(self):+ pass++ class FakeVideoClip:+ duration = 1.0+ size = (1080, 1920)+ w = 1080+ h = 1920++ def __init__(self, source):+ self.source = source+ self.close_calls = 0+ self.reader = self++ def subclipped(self, _start, _end):+ derived = FakeVideoClip(self.source)+ derived_clips.append(derived)+ return derived++ def close(self):+ self.close_calls += 1++ derived_clips = []++ def open_clip(source):+ return FakeVideoClip(source)++ def write_clip(clip, output_file, **_kwargs):+ Path(output_file).write_bytes(b"partial")+ if clip.source == "bad.mp4":+ raise RuntimeError("encode failed")++ with tempfile.TemporaryDirectory() as temp_dir:+ with (+ patch.object(vd, "AudioFileClip", return_value=FakeAudioClip()),+ patch.object(vd, "_open_video_clip_quietly", side_effect=open_clip),+ patch.object(+ vd, "_write_videofile_with_codec_fallback", side_effect=write_clip+ ),+ patch.object(vd, "concat_video_clips_with_ffmpeg") as concat,+ ):+ vd.combine_videos(+ combined_video_path=os.path.join(temp_dir, "combined.mp4"),+ video_paths=["bad.mp4", "good.mp4"],+ audio_file="audio.mp3",+ video_concat_mode=vd.VideoConcatMode.sequential,+ )++ concat.assert_called_once()+ self.assertEqual(derived_clips[0].close_calls, 1)+ self.assertFalse(Path(temp_dir, "temp-clip-1.mp4").exists())+ self.assertFalse(Path(temp_dir, "temp-clip-2.mp4").exists())+def test_concat_video_clips_limits_output_to_audio_duration(self):"""最终拼接时应裁到音频时长,避免安全余量带来明显静音尾巴。"""