Files
76614aef0b fix(video): remove jitter from the slow image zoom by scaling with sub-pixel precision
The zoom effect for still images resized every frame to an integer size and
centered it with an integer offset. At the default speed (about 3% per second)
most consecutive frames kept the same size and then jumped by 1px, so the
picture stuttered and wobbled, which is clearly visible after scaling to
1080x1920.

Render each frame on a fixed canvas with a center-based affine transform
(bicubic), so the scale changes continuously. The composition is unchanged:
start from the original image and grow around its center, cropping the edges.
Alpha masks are transformed the same way. Zoom outputs get a new cache tag so
previously rendered jittery clips are not reused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 11:00:58 +08:00

177 lines
8.3 KiB
Python

"""验证图片动态效果的兼容默认值、参数传递和真实渲染像素。"""
from pathlib import Path
from unittest.mock import patch
import numpy as np
import pytest
from moviepy import VideoFileClip
from PIL import Image, ImageDraw
from pydantic import ValidationError
import cli
from app.config import config
from app.models.schema import ImageMotion, MaterialInfo, TaskVideoRequest, VideoParams
from app.services import material, task, video
def test_old_requests_keep_zoom_and_invalid_motion_is_rejected():
assert TaskVideoRequest(video_subject="old task").image_motion == ImageMotion.zoom
assert TaskVideoRequest(video_subject="still", image_motion="static").image_motion == ImageMotion.static
with pytest.raises(ValidationError, match="image_motion"):
TaskVideoRequest(video_subject="bad", image_motion="unknown")
def test_api_motion_reaches_task_queue_and_rejects_invalid_input():
from fastapi.testclient import TestClient
from app import asgi
from app.controllers.v1 import video as controller
with (
patch.dict(config.app, {"api_key": ""}),
patch.object(controller.task_manager, "add_task") as enqueue,
patch.object(controller.sm.state, "update_task"),
):
client = TestClient(asgi.app)
for value, expected in ((None, "zoom"), ("static", "static"), ("zoom", "zoom")):
payload = {"video_subject": "test", "subtitle_enabled": False}
if value is not None:
payload["image_motion"] = value
response = client.post("/api/v1/videos", json=payload)
assert response.status_code == 200
assert enqueue.call_args.kwargs["params"].image_motion == expected
enqueue.reset_mock()
response = client.post("/api/v1/videos", json={"video_subject": "test", "image_motion": "bad"})
assert response.status_code == 400
enqueue.assert_not_called()
def test_cli_motion_uses_saved_value_but_explicit_argument_wins():
args = ["--video-subject", "test"]
with patch.dict(config.ui, {}, clear=True):
assert cli.build_video_params(cli.parse_args(args)).image_motion == ImageMotion.zoom
with patch.dict(config.ui, {"image_motion": "static"}, clear=True):
assert cli.build_video_params(cli.parse_args(args)).image_motion == ImageMotion.static
explicit = cli.build_video_params(cli.parse_args(args + ["--image-motion", "zoom"]))
assert explicit.image_motion == ImageMotion.zoom
with patch.dict(config.ui, {"image_motion": "bad"}, clear=True):
assert cli.build_video_params(cli.parse_args(args)).image_motion == ImageMotion.zoom
with pytest.raises(SystemExit):
cli.parse_args(args + ["--image-motion", "bad"])
@pytest.mark.parametrize("motion", [ImageMotion.static, ImageMotion.zoom])
def test_task_passes_motion_to_local_and_generated_images(motion):
params = VideoParams(video_subject="test", video_source="local", image_motion=motion)
with patch.object(video, "preprocess_video", return_value=[MaterialInfo(url="local.mp4")]) as preprocess:
assert task.get_video_materials("motion-test", params, [], 2) == ["local.mp4"]
assert preprocess.call_args.kwargs["image_motion"] == motion
params.video_source = "openai_image"
with patch.object(material, "download_videos", return_value=["generated.mp4"]) as download:
assert task.get_video_materials("motion-test", params, ["scene"], 2) == ["generated.mp4"]
assert download.call_args.kwargs["image_motion"] == motion
def test_generated_image_motion_reaches_renderer_without_extra_paid_requests(tmp_path):
item = MaterialInfo(provider="openai_image", url=str(tmp_path / "scene.png"), duration=2)
with (
patch.object(material, "generate_images_openai", return_value=[item]) as generate,
patch.object(video, "render_image_zoom_video", return_value="static.mp4") as render,
patch.object(material, "_persist_material_sources"),
):
paths = material.download_videos(
"motion-test", ["first", "unused"], source="openai_image",
audio_duration=2, max_clip_duration=2, image_motion="static",
)
assert paths == ["static.mp4"]
generate.assert_called_once()
assert render.call_args.kwargs["image_motion"] == "static"
def test_invalid_motion_is_rejected_before_paid_image_generation():
with patch.object(material, "generate_images_openai") as generate:
with pytest.raises(ValueError, match="ImageMotion"):
material.download_videos(
"motion-test", ["scene"], source="openai_image",
audio_duration=2, image_motion="invalid",
)
generate.assert_not_called()
def _white_width(frame):
_, x = np.where(frame.min(axis=2) > 220)
return int(x.max() - x.min() + 1)
def test_native_static_pixels_zoom_default_and_output_identity(tmp_path):
source = tmp_path / "scene.png"
image = Image.new("RGB", (256, 256), (220, 0, 0))
ImageDraw.Draw(image).rectangle((80, 80, 175, 175), fill="white")
image.save(source)
default = video.render_image_zoom_video(str(source), 3)
zoom_bytes = Path(default).read_bytes()
still = video.render_image_zoom_video(str(source), 3, image_motion="static")
assert still != default
assert Path(default).read_bytes() == zoom_bytes
with VideoFileClip(still, audio=False) as clip:
first, last = clip.get_frame(0), clip.get_frame(2.5)
assert clip.size == [256, 256]
assert clip.duration == pytest.approx(3, abs=0.04)
assert abs(_white_width(first) - _white_width(last)) <= 1
assert np.abs(first.astype(float) - last).mean() < 1
with VideoFileClip(default, audio=False) as clip:
assert _white_width(clip.get_frame(2.5)) > _white_width(clip.get_frame(0)) + 4
assert video.render_image_zoom_video(str(source), 3, image_motion="zoom") == default
def test_static_image_preserves_alpha_composition(tmp_path):
source = tmp_path / "alpha.png"
Image.new("RGBA", (64, 64), (240, 0, 0, 128)).save(source)
output = video.render_image_zoom_video(str(source), 0.2, image_motion="static")
with VideoFileClip(output, audio=False) as clip:
pixel = clip.get_frame(0)[32, 32]
assert 110 <= pixel[0] <= 130
assert max(pixel[1:]) < 5
@pytest.mark.parametrize("motion", ["static", "zoom"])
def test_failed_motion_render_keeps_previous_output_and_removes_partial_file(tmp_path, motion):
source = tmp_path / "scene.png"
Image.new("RGB", (64, 64), "red").save(source)
output = video.render_image_zoom_video(str(source), 0.2, image_motion=motion)
completed = Path(output).read_bytes()
def interrupted_write(_clip, filename, **_kwargs):
Path(filename).write_bytes(b"partial video")
raise RuntimeError("encoder interrupted")
with patch.object(video.CompositeVideoClip, "write_videofile", interrupted_write):
with pytest.raises(RuntimeError, match="encoder interrupted"):
video.render_image_zoom_video(str(source), 0.2, image_motion=motion)
assert Path(output).read_bytes() == completed
assert not list(tmp_path.glob(".image-zoom-*.mp4"))
def _white_span(frame, dark=20):
# 以亚像素精度估算中间几行的白色宽度:按亮度比例累加,避免整数边界掩盖细微变化。
rows = frame.astype(float)[118:138].mean(axis=2)
return ((rows - dark) / (255 - dark)).clip(0, 1).sum(axis=1).mean()
def test_zoom_grows_smoothly_every_frame_without_integer_resize_jitter():
# 旧实现按整数尺寸逐帧缩放:慢速放大时约 3/4 的相邻帧尺寸不变,随后再跳 1px,
# 成片表现为抖动/顿挫。亚像素放大应让画面每一帧都以相近的幅度增长。
image = Image.new("RGB", (256, 256), (20, 20, 20))
ImageDraw.Draw(image).rectangle((28, 28, 227, 227), fill="white")
clip = video.ImageClip(np.asarray(image)).with_duration(3)
zoomed = video._smooth_zoom_clip(clip, 0.09)
first, last = zoomed.get_frame(0), zoomed.get_frame(3)
assert first.shape == last.shape == (256, 256, 3)
assert _white_span(first) == pytest.approx(200, abs=1)
assert _white_span(last) == pytest.approx(218, abs=1.5)
spans = [_white_span(zoomed.get_frame(index / 30)) for index in range(1, 85)]
steps = np.diff(spans)
assert steps.min() > steps.mean() * 0.5
assert steps.std() / steps.mean() < 0.3