Skip to content

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

fix(video): isolate image intermediates (#1572)

Rudy Mizrahi Celekli8c5c444b4b4f
diff --git a/app/services/video.py b/app/services/video.py
index 33225326..3662be48 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -1,4 +1,5 @@
import itertools
+import hashlib
from concurrent.futures import ThreadPoolExecutor
import io
import math
@@ -710,8 +711,11 @@ def concat_video_clips_with_ffmpeg(
def _sanitize_image_file(image_path: str) -> str:
# 某些本地图片虽然能被 Pillow 打开,但会因为损坏的 EXIF/eXIf 元数据导致
# ImageClip 在解析阶段直接抛异常。这里重新导出一份“干净图片”,把坏元数据剥离掉。
- image_root, _ = os.path.splitext(image_path)
- sanitized_path = f"{image_root}.sanitized.png"
+ # 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")
temp_path = ""
try:
@@ -1861,7 +1865,11 @@ 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.
- video_file = f"{image_path}.zoom-{clip_duration}.mp4"
+ source_identity = hashlib.sha256(os.fsencode(os.path.abspath(image_path))).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"
+ )
descriptor, temp_path = tempfile.mkstemp(
prefix=".image-zoom-",
suffix=".mp4",
diff --git a/test/services/test_image_sanitize_identity.py b/test/services/test_image_sanitize_identity.py
new file mode 100644
index 00000000..b6ba4f60
--- /dev/null
+++ b/test/services/test_image_sanitize_identity.py
@@ -0,0 +1,67 @@
+import threading
+from unittest.mock import patch
+
+from moviepy import ImageClip, VideoFileClip
+from PIL import Image
+
+from app.services import video
+
+
+def test_concurrent_same_stem_images_render_their_own_pixels(tmp_path):
+ red = tmp_path / "scene.jpg"
+ green = tmp_path / "scene.png"
+ Image.new("RGB", (64, 64), "red").convert("CMYK").save(red)
+ exif = Image.Exif()
+ exif[274] = 6
+ Image.new("RGB", (64, 64), "lime").save(green, exif=exif)
+ first_waiting, second_finished = threading.Event(), threading.Event()
+ outputs, errors = {}, []
+ calls = 0
+ lock = threading.Lock()
+
+ def delayed_decode(path, *args, **kwargs):
+ nonlocal calls
+ with lock:
+ calls += 1
+ first = calls == 1
+ if first:
+ first_waiting.set()
+ assert second_finished.wait(10)
+ return ImageClip(path, *args, **kwargs)
+
+ def render_red():
+ try:
+ outputs["red"] = video.render_image_zoom_video(str(red), clip_duration=0.1)
+ except Exception as error:
+ errors.append(error)
+
+ with patch.object(video, "ImageClip", side_effect=delayed_decode):
+ thread = threading.Thread(target=render_red)
+ thread.start()
+ try:
+ assert first_waiting.wait(5)
+ outputs["green"] = video.render_image_zoom_video(str(green), clip_duration=0.1)
+ finally:
+ second_finished.set()
+ thread.join(timeout=15)
+ assert not thread.is_alive()
+ assert not errors
+ for name, channel in [("red", 0), ("green", 1)]:
+ with VideoFileClip(outputs[name], audio=False) as clip:
+ pixel = clip.get_frame(0)[32, 32]
+ assert pixel[channel] > 200, (name, pixel)
+ assert pixel[1 - channel] < 30, (name, pixel)
+ with Image.open(red) as original:
+ assert original.mode == "CMYK"
+ with Image.open(green) as original:
+ assert original.getexif()[274] == 6
+
+
+def test_long_source_name_renders_without_exceeding_filesystem_name_limit(tmp_path):
+ source = tmp_path / ("a" * 240 + ".jpg")
+ Image.new("RGB", (64, 64), "red").convert("CMYK").save(source)
+ output = video.render_image_zoom_video(str(source), clip_duration=0.1)
+ assert len(__import__("os").fsencode(__import__("os").path.basename(output))) <= 255
+ with VideoFileClip(output, audio=False) as clip:
+ assert clip.get_frame(0)[32, 32, 0] > 200
+ assert source.exists()
diff --git a/test/services/test_video.py b/test/services/test_video.py
index 9171772a..9e099757 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -635,7 +635,7 @@ class TestVideoService(unittest.TestCase):
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"],
+ [Path(output).name],
)
def test_preprocess_video_rejects_material_outside_local_videos(self):