Skip to content

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

fix(video): retain image renders across pixel revisions

Rudy Celekli6fdde6fd9fe7
diff --git a/app/services/video.py b/app/services/video.py
index 485da337..647407ba 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -1903,7 +1903,13 @@ def render_image_zoom_video(image_path: str, clip_duration: int = 5) -> str:
# 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.
- source_identity = hashlib.sha256(os.fsencode(os.path.abspath(image_path))).hexdigest()
+ source_digest = hashlib.sha256(os.fsencode(os.path.abspath(image_path)))
+ source_frame = clip.get_frame(0)
+ source_digest.update(str(source_frame.shape).encode())
+ source_digest.update(source_frame.tobytes())
+ if clip.mask is not None:
+ source_digest.update(clip.mask.get_frame(0).tobytes())
+ source_identity = source_digest.hexdigest()
duration_identity = hashlib.sha256(str(clip_duration).encode()).hexdigest()[:16]
video_file = os.path.join(
os.path.dirname(image_path), f"{source_identity}.zoom-{duration_identity}.mp4"
diff --git a/test/services/test_image_zoom_pixel_revision_identity.py b/test/services/test_image_zoom_pixel_revision_identity.py
new file mode 100644
index 00000000..059f5db0
--- /dev/null
+++ b/test/services/test_image_zoom_pixel_revision_identity.py
@@ -0,0 +1,24 @@
+import shutil
+from pathlib import Path
+
+import pytest
+from moviepy import VideoFileClip
+from PIL import Image
+
+from app.services import video
+
+
+def test_updated_image_does_not_replace_previous_rendered_pixels(tmp_path):
+ if not shutil.which("ffmpeg"):
+ pytest.skip("native FFmpeg required")
+ source = tmp_path / "scene.png"
+ Image.new("RGB", (64, 64), "red").save(source)
+ first = video.render_image_zoom_video(str(source), clip_duration=0.1)
+ first_bytes = Path(first).read_bytes()
+ Image.new("RGB", (64, 64), "blue").save(source)
+ second = video.render_image_zoom_video(str(source), clip_duration=0.1)
+ assert first != second
+ assert Path(first).read_bytes() == first_bytes
+ for output, channel in [(first, 0), (second, 2)]:
+ with VideoFileClip(output, audio=False) as rendered:
+ assert rendered.get_frame(0)[32, 32, channel] > 200
diff --git a/test/services/test_video.py b/test/services/test_video.py
index aa89c42f..f4f06762 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -12,6 +12,7 @@ from io import StringIO
from pathlib import Path
from unittest.mock import patch
+import numpy as np
from loguru import logger
from moviepy import (
ImageClip,
@@ -558,6 +559,11 @@ class TestVideoService(unittest.TestCase):
def test_image_zoom_renders_keep_distinct_clip_durations(self):
"""Two tasks must not overwrite one image render with another duration."""
class FakeImageClip:
+ mask = None
+
+ def get_frame(self, _time):
+ return np.zeros((2, 2, 3), dtype=np.uint8)
+
def __init__(self, _path):
self.duration = 0
@@ -594,6 +600,11 @@ class TestVideoService(unittest.TestCase):
def test_failed_image_zoom_render_preserves_previous_complete_clip(self):
"""A failed rerender must leave the last verified MP4 available."""
class FakeImageClip:
+ mask = None
+
+ def get_frame(self, _time):
+ return np.zeros((2, 2, 3), dtype=np.uint8)
+
duration = 0
def __init__(self, _path):