Skip to content

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

fix(video): skip unreadable sources during clip selection

Rudy Celekli8091427dd2f8
diff --git a/app/services/video.py b/app/services/video.py
index 6b3a971c..40b9f966 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -858,10 +858,27 @@ def combine_videos(
subclipped_items = []
video_duration = 0
for video_path in video_paths:
- clip = _open_video_clip_quietly(video_path)
- clip_duration = clip.duration
- clip_w, clip_h = clip.size
- close_clip(clip)
+ clip = None
+ try:
+ clip = _open_video_clip_quietly(video_path)
+ clip_duration = float(clip.duration)
+ clip_w, clip_h = clip.size
+ if (
+ not math.isfinite(clip_duration)
+ or clip_duration <= 0
+ or not all(
+ math.isfinite(float(dimension)) and float(dimension) > 0
+ for dimension in (clip_w, clip_h)
+ )
+ ):
+ raise ValueError("invalid video duration or dimensions")
+ except Exception as exc:
+ logger.warning(
+ f"skipping unreadable video source: path={video_path}, error={exc}"
+ )
+ continue
+ finally:
+ close_clip(clip)
start_time = 0
@@ -1029,6 +1046,8 @@ def combine_videos(
# merge video clips progressively, avoid loading all videos at once to avoid memory overflow
logger.info("starting clip merging process")
if not processed_clips:
+ if video_paths:
+ raise RuntimeError("no readable video clips available for merging")
logger.warning("no clips available for merging")
return combined_video_path
diff --git a/test/services/test_video.py b/test/services/test_video.py
index 4fcee5f3..e96d54bc 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -1152,6 +1152,69 @@ class TestVideoService(unittest.TestCase):
self.assertFalse(Path(temp_dir, "temp-clip-1.mp4").exists())
self.assertFalse(Path(temp_dir, "temp-clip-2.mp4").exists())
+ def test_combine_videos_skips_unreadable_source_when_good_clip_remains(self):
+ """A stale corrupt cache clip must not discard healthy downloaded footage."""
+ class FakeAudioClip:
+ duration = 0.5
+
+ class FakeVideoClip:
+ duration = 1.0
+ size = (1080, 1920)
+ w = 1080
+ h = 1920
+
+ def subclipped(self, _start, _end):
+ return self
+
+ def open_clip(source):
+ if source == "corrupt.mp4":
+ raise OSError("FFmpeg could not read video metadata")
+ return FakeVideoClip()
+
+ used_sources = []
+ 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"),
+ patch.object(vd, "concat_video_clips_with_ffmpeg") as concat,
+ patch.object(vd, "delete_files"),
+ ):
+ vd.combine_videos(
+ combined_video_path=os.path.join(temp_dir, "combined.mp4"),
+ video_paths=["corrupt.mp4", "healthy.mp4"],
+ audio_file="audio.mp3",
+ video_concat_mode=vd.VideoConcatMode.sequential,
+ used_video_paths=used_sources,
+ )
+
+ concat.assert_called_once()
+ self.assertEqual(used_sources, ["healthy.mp4"])
+
+ def test_combine_videos_reports_failure_if_every_source_is_unreadable(self):
+ """Never return an output path for an input set that yielded no clips."""
+ class FakeAudioClip:
+ duration = 1.0
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with (
+ patch.object(vd, "AudioFileClip", return_value=FakeAudioClip()),
+ patch.object(
+ vd,
+ "_open_video_clip_quietly",
+ side_effect=OSError("invalid cached video"),
+ ),
+ patch.object(vd, "concat_video_clips_with_ffmpeg") as concat,
+ ):
+ with self.assertRaisesRegex(RuntimeError, "no readable video clips"):
+ vd.combine_videos(
+ combined_video_path=os.path.join(temp_dir, "combined.mp4"),
+ video_paths=["corrupt.mp4"],
+ audio_file="audio.mp3",
+ )
+
+ concat.assert_not_called()
+
def test_concat_video_clips_limits_output_to_audio_duration(self):
"""最终拼接时应裁到音频时长,避免安全余量带来明显静音尾巴。"""