Skip to content

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

fix(video): isolate sanitized image pixel snapshots

Rudy Celekli91b3a9bafb3f
diff --git a/app/services/video.py b/app/services/video.py
index 647407ba..ff0ee325 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -717,8 +717,7 @@ def _sanitize_image_file(image_path: str) -> str:
# Bound the intermediate basename while retaining complete source identity.
# Different extensions must not overwrite pixels, and valid long filenames
# must not exceed the filesystem's component limit after adding a suffix.
- source_identity = hashlib.sha256(os.fsencode(os.path.abspath(image_path))).hexdigest()
- sanitized_path = os.path.join(os.path.dirname(image_path), f"{source_identity}.sanitized.png")
+ source_digest = hashlib.sha256(os.fsencode(os.path.abspath(image_path)))
temp_path = ""
try:
@@ -730,6 +729,13 @@ def _sanitize_image_file(image_path: str) -> str:
mode = "RGBA" if "A" in upright.getbands() or "transparency" in upright.info else "RGB"
cleaned_image = upright.convert(mode)
cleaned_image.info.clear()
+ source_digest.update(str(cleaned_image.size).encode())
+ source_digest.update(cleaned_image.mode.encode())
+ source_digest.update(cleaned_image.tobytes())
+ sanitized_path = os.path.join(
+ os.path.dirname(image_path),
+ f"{source_digest.hexdigest()}.sanitized.png",
+ )
descriptor, temp_path = tempfile.mkstemp(
prefix=".image-sanitize-", suffix=".png",
dir=os.path.dirname(os.path.abspath(sanitized_path)),
diff --git a/test/services/test_image_sanitize_pixel_snapshot.py b/test/services/test_image_sanitize_pixel_snapshot.py
new file mode 100644
index 00000000..dba9d4b3
--- /dev/null
+++ b/test/services/test_image_sanitize_pixel_snapshot.py
@@ -0,0 +1,49 @@
+import threading
+from unittest.mock import patch
+
+from moviepy import ImageClip, VideoFileClip
+from PIL import Image
+
+from app.services import video
+
+
+def test_inflight_sanitized_image_keeps_pixels_when_source_is_replaced(tmp_path):
+ source = tmp_path / "scene.jpg"
+ Image.new("RGB", (64, 64), "red").convert("CMYK").save(source)
+ first_waiting = threading.Event()
+ second_finished = threading.Event()
+ outputs, errors = {}, []
+ calls = [0]
+
+ def delayed_decode(path, *args, **kwargs):
+ calls[0] += 1
+ if calls[0] == 1:
+ first_waiting.set()
+ assert second_finished.wait(10)
+ return ImageClip(path, *args, **kwargs)
+
+ def first_render():
+ try:
+ outputs["first"] = video.render_image_zoom_video(str(source), clip_duration=0.1)
+ except Exception as exc:
+ errors.append(exc)
+
+ with patch.object(video, "ImageClip", side_effect=delayed_decode):
+ worker = threading.Thread(target=first_render)
+ worker.start()
+ try:
+ assert first_waiting.wait(5)
+ Image.new("RGB", (64, 64), "blue").convert("CMYK").save(source)
+ # A different duration keeps the two final output paths independent.
+ outputs["second"] = video.render_image_zoom_video(str(source), clip_duration=0.2)
+ finally:
+ second_finished.set()
+ worker.join(15)
+ assert not worker.is_alive()
+ assert errors == []
+ with VideoFileClip(outputs["first"], audio=False) as rendered:
+ pixel = rendered.get_frame(0)[32, 32]
+ assert pixel[0] > 200 and pixel[2] < 30
+ with VideoFileClip(outputs["second"], audio=False) as rendered:
+ pixel = rendered.get_frame(0)[32, 32]
+ assert pixel[2] > 200 and pixel[0] < 30