利用 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 image zoom clips atomically by duration
Rudy Celekli006d62b073ad
diff --git a/app/services/video.py b/app/services/video.pyindex 40b9f966..e31c838c 100644--- a/app/services/video.py+++ b/app/services/video.py@@ -1639,6 +1639,7 @@ def render_image_zoom_video(image_path: str, clip_duration: int = 5) -> str:素材源的失败约定处理。"""clip = ImageClip(image_path).with_duration(clip_duration).with_position("center")+ temp_path = ""try:# Apply a zoom effect using the resize method.# A lambda function is used to make the zoom effect dynamic over time.@@ -1653,14 +1654,26 @@ def render_image_zoom_video(image_path: str, clip_duration: int = 5) -> str:# This is useful if you want to add other elements to the video.final_clip = CompositeVideoClip([zoom_clip])try:- # Output the video to a file.- video_file = f"{image_path}.mp4"- final_clip.write_videofile(video_file, fps=30, logger=None)- return video_file+ # The duration changes the rendered content, so it must be part of+ # the output identity. Different tasks may render the same image+ # concurrently; only publish a complete MP4 after MoviePy closes it.+ video_file = f"{image_path}.zoom-{clip_duration}.mp4"+ descriptor, temp_path = tempfile.mkstemp(+ prefix=".image-zoom-",+ suffix=".mp4",+ dir=os.path.dirname(os.path.abspath(video_file)),+ )+ os.close(descriptor)+ final_clip.write_videofile(temp_path, fps=30, logger=None)finally:close_clip(final_clip)+ os.replace(temp_path, video_file)+ temp_path = ""+ return video_filefinally:close_clip(clip)+ if temp_path:+ delete_files(temp_path)def preprocess_video(materials: List[MaterialInfo], clip_duration=4):diff --git a/test/services/test_video.py b/test/services/test_video.pyindex e96d54bc..bdc859fc 100644--- a/test/services/test_video.py+++ b/test/services/test_video.py@@ -489,6 +489,89 @@ class TestVideoService(unittest.TestCase):if os.path.exists(safe_img_path):os.remove(safe_img_path)+ def test_image_zoom_renders_keep_distinct_clip_durations(self):+ """Two tasks must not overwrite one image render with another duration."""+ class FakeImageClip:+ def __init__(self, _path):+ self.duration = 0++ def with_duration(self, duration):+ self.duration = duration+ return self++ def with_position(self, _position):+ return self++ def resized(self, _scale):+ return self++ class FakeCompositeClip:+ def __init__(self, clips):+ self.duration = clips[0].duration++ def write_videofile(self, output, **_kwargs):+ Path(output).write_bytes(f"duration={self.duration}".encode())++ with tempfile.TemporaryDirectory() as temp_dir:+ image_path = os.path.join(temp_dir, "image.png")+ with (+ patch.object(vd, "ImageClip", FakeImageClip),+ patch.object(vd, "CompositeVideoClip", FakeCompositeClip),+ ):+ first = vd.render_image_zoom_video(image_path, clip_duration=4)+ second = vd.render_image_zoom_video(image_path, clip_duration=7)++ self.assertNotEqual(first, second)+ self.assertEqual(Path(first).read_bytes(), b"duration=4")+ self.assertEqual(Path(second).read_bytes(), b"duration=7")++ def test_failed_image_zoom_render_preserves_previous_complete_clip(self):+ """A failed rerender must leave the last verified MP4 available."""+ class FakeImageClip:+ duration = 0++ def __init__(self, _path):+ pass++ def with_duration(self, duration):+ self.duration = duration+ return self++ def with_position(self, _position):+ return self++ def resized(self, _scale):+ return self++ writes = 0++ class FakeCompositeClip:+ def __init__(self, _clips):+ pass++ def write_videofile(self, output, **_kwargs):+ nonlocal writes+ writes += 1+ Path(output).write_bytes(b"complete" if writes == 1 else b"partial")+ if writes == 2:+ raise RuntimeError("render interrupted")++ with tempfile.TemporaryDirectory() as temp_dir:+ image_path = os.path.join(temp_dir, "image.png")+ with (+ patch.object(vd, "ImageClip", FakeImageClip),+ patch.object(vd, "CompositeVideoClip", FakeCompositeClip),+ ):+ output = vd.render_image_zoom_video(image_path, clip_duration=5)+ with self.assertRaisesRegex(RuntimeError, "render interrupted"):+ vd.render_image_zoom_video(image_path, clip_duration=5)++ self.assertEqual(Path(output).read_bytes(), b"complete")+ self.assertEqual(+ sorted(path.name for path in Path(temp_dir).iterdir()),+ ["image.png.zoom-5.mp4"],+ )+def test_preprocess_video_rejects_material_outside_local_videos(self):"""local 素材路径来自 API 参数,不能允许任意绝对路径进入 MoviePy。