Skip to content

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

feat(video): add ZoomIn/ZoomOut transitions

Kizuno18e5dec5a556ed
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 3a4703cd..0201bcc9 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -32,4 +32,4 @@ jobs:
run: uv run python -m compileall app cli.py main.py webui test
- name: Run deterministic unit tests
- run: uv run python -X utf8 -m unittest test.services.test_state test.services.test_task test.services.test_schema test.services.test_webui_i18n
+ run: uv run python -X utf8 -m unittest test.services.test_state test.services.test_task test.services.test_schema test.services.test_video_effects test.services.test_webui_i18n
diff --git a/app/models/schema.py b/app/models/schema.py
index 9e228f66..f1e84516 100644
--- a/app/models/schema.py
+++ b/app/models/schema.py
@@ -27,6 +27,8 @@ class VideoTransitionMode(str, Enum):
fade_out = "FadeOut"
slide_in = "SlideIn"
slide_out = "SlideOut"
+ zoom_in = "ZoomIn"
+ zoom_out = "ZoomOut"
class VideoAspect(str, Enum):
diff --git a/app/services/utils/video_effects.py b/app/services/utils/video_effects.py
index 1a764347..72b57d7d 100644
--- a/app/services/utils/video_effects.py
+++ b/app/services/utils/video_effects.py
@@ -1,4 +1,6 @@
+import numpy as np
from moviepy import Clip, ColorClip, CompositeVideoClip, vfx
+from PIL import Image
# FadeIn
@@ -71,3 +73,73 @@ def slideout_transition(clip: Clip, t: float, side: str) -> Clip:
return CompositeVideoClip([background, moving_clip], size=(width, height)).with_duration(
clip.duration
)
+
+
+# 保留原始设计的 20% 缩放幅度,让三秒左右的短片也有清晰可见的 Ken Burns 运动感。
+# 缩放稳定性由下方的亚像素中心采样保证,不通过削弱效果幅度来掩盖源视频编码闪烁。
+_ZOOM_MAX_SCALE = 1.2
+
+
+def _zoom_frame(frame: np.ndarray, scale_factor: float) -> np.ndarray:
+ """使用亚像素中心裁剪实现无黑边且稳定的缩放效果。
+
+ 不能先把裁剪宽高转换为整数:缩放比例连续变化时,整数边界会按不同步长跳动,
+ 并在奇偶尺寸切换时改变半像素采样相位,最终表现为画面抖动。Pillow 的 EXTENT
+ 变换可以直接接收浮点边界,在固定输出画布上完成亚像素采样;左右、上下边界
+ 始终围绕同一个浮点中心对称,因此适用于整段视频持续缓慢缩放的场景。
+ """
+ if scale_factor <= 0:
+ raise ValueError("scale_factor must be greater than zero")
+
+ # 1 倍缩放直接返回原帧,避免无意义的重采样造成首帧轻微模糊。
+ if abs(scale_factor - 1.0) < 1e-9:
+ return frame
+
+ height, width = frame.shape[:2]
+ crop_width = width / scale_factor
+ crop_height = height / scale_factor
+ left = (width - crop_width) / 2
+ top = (height - crop_height) / 2
+ right = left + crop_width
+ bottom = top + crop_height
+
+ image = Image.fromarray(frame)
+ transformed = image.transform(
+ (width, height),
+ Image.Transform.EXTENT,
+ (left, top, right, bottom),
+ # 视频连续缩放更关注相邻帧的一致性。BICUBIC/LANCZOS 虽然单帧更锐利,
+ # 但高频纹理跨越采样网格时容易出现振铃和亮度闪烁;BILINEAR 更柔和,
+ # 能以少量锐度损失换取更稳定的动态观感。
+ resample=Image.Resampling.BILINEAR,
+ )
+ return np.asarray(transformed)
+
+
+def zoomin_transition(clip: Clip, t: float) -> Clip:
+ """在整个片段内从原始画面平滑放大到 1.2 倍。"""
+ # t 暂时保留,用于与其它转场函数保持统一调用签名;缩放需要覆盖完整片段,
+ # 否则短暂缩放结束后画面会突然静止,不适合静态或低运动量素材。
+ _ = t
+ duration = max(clip.duration, 0.001)
+
+ def scale_effect(get_frame, current_time: float):
+ progress = min(max(current_time / duration, 0), 1)
+ scale_factor = 1 + (_ZOOM_MAX_SCALE - 1) * progress
+ return _zoom_frame(get_frame(current_time), scale_factor)
+
+ return clip.transform(scale_effect)
+
+
+def zoomout_transition(clip: Clip, t: float) -> Clip:
+ """在整个片段内从 1.2 倍平滑缩小到原始画面。"""
+ # 与 zoomin_transition 一致,t 仅用于兼容统一的转场调用接口。
+ _ = t
+ duration = max(clip.duration, 0.001)
+
+ def scale_effect(get_frame, current_time: float):
+ progress = min(max(current_time / duration, 0), 1)
+ scale_factor = _ZOOM_MAX_SCALE - (_ZOOM_MAX_SCALE - 1) * progress
+ return _zoom_frame(get_frame(current_time), scale_factor)
+
+ return clip.transform(scale_effect)
diff --git a/app/services/video.py b/app/services/video.py
index cbca6829..cf812d65 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -678,12 +678,18 @@ def combine_videos(
clip = video_effects.slidein_transition(clip, 1, shuffle_side)
elif transition_value == VideoTransitionMode.slide_out.value:
clip = video_effects.slideout_transition(clip, 1, shuffle_side)
+ elif transition_value == VideoTransitionMode.zoom_in.value:
+ clip = video_effects.zoomin_transition(clip, 1)
+ elif transition_value == VideoTransitionMode.zoom_out.value:
+ clip = video_effects.zoomout_transition(clip, 1)
elif transition_value == VideoTransitionMode.shuffle.value:
transition_funcs = [
lambda c: video_effects.fadein_transition(c, 1),
lambda c: video_effects.fadeout_transition(c, 1),
lambda c: video_effects.slidein_transition(c, 1, shuffle_side),
lambda c: video_effects.slideout_transition(c, 1, shuffle_side),
+ lambda c: video_effects.zoomin_transition(c, 1),
+ lambda c: video_effects.zoomout_transition(c, 1),
]
shuffle_transition = random.choice(transition_funcs)
clip = shuffle_transition(clip)
diff --git a/test/resources/1.png.mp4 b/test/resources/1.png.mp4
deleted file mode 100644
index 05e351b8..00000000
Binary files a/test/resources/1.png.mp4 and /dev/null differ
diff --git a/test/resources/2.png.mp4 b/test/resources/2.png.mp4
deleted file mode 100644
index 6e622c70..00000000
Binary files a/test/resources/2.png.mp4 and /dev/null differ
diff --git a/test/resources/3.png.mp4 b/test/resources/3.png.mp4
deleted file mode 100644
index 9f06ed3c..00000000
Binary files a/test/resources/3.png.mp4 and /dev/null differ
diff --git a/test/services/test_video_effects.py b/test/services/test_video_effects.py
new file mode 100644
index 00000000..48c3f7bc
--- /dev/null
+++ b/test/services/test_video_effects.py
@@ -0,0 +1,120 @@
+import sys
+import unittest
+from pathlib import Path
+
+import numpy as np
+
+sys.path.insert(0, str(Path(__file__).parent.parent.parent))
+
+from moviepy import ImageClip
+
+from app.models.schema import VideoTransitionMode
+from app.services.utils import video_effects
+
+
+def _gradient_clip(width=64, height=48, duration=1.0):
+ """创建非均匀渐变画面,确保缩放前后的像素差异可以被可靠检测。"""
+ x = np.linspace(0, 255, width, dtype=np.uint8)
+ y = np.linspace(0, 255, height, dtype=np.uint8)
+ frame = np.stack(np.meshgrid(x, y), axis=-1).sum(axis=-1) % 256
+ rgb = np.stack([frame] * 3, axis=-1).astype(np.uint8)
+ return ImageClip(rgb).with_duration(duration)
+
+
+def _detail_frame(width=128, height=96):
+ """创建包含高频细节的 RGB 帧,用于观察亚像素缩放是否连续响应。"""
+ x = np.arange(width, dtype=np.int16)
+ y = np.arange(height, dtype=np.int16)[:, None]
+ return np.stack(
+ (
+ (x + y) % 256,
+ (3 * x + y) % 256,
+ (x + 3 * y) % 256,
+ ),
+ axis=-1,
+ ).astype(np.uint8)
+
+
+class TestZoomTransitions(unittest.TestCase):
+ def test_schema_has_zoom_members(self):
+ self.assertEqual(VideoTransitionMode.zoom_in.value, "ZoomIn")
+ self.assertEqual(VideoTransitionMode.zoom_out.value, "ZoomOut")
+
+ def test_zoomin_preserves_geometry_and_zooms_over_time(self):
+ clip = _gradient_clip()
+ zoomed = video_effects.zoomin_transition(clip, 1)
+ self.addCleanup(zoomed.close)
+ self.addCleanup(clip.close)
+
+ self.assertEqual(zoomed.size, clip.size)
+ self.assertEqual(zoomed.duration, clip.duration)
+
+ first = zoomed.get_frame(0)
+ last = zoomed.get_frame(clip.duration - 0.01)
+ original = clip.get_frame(0)
+
+ self.assertEqual(first.shape, original.shape)
+ self.assertEqual(first.dtype, np.uint8)
+ # 放大从 1 倍开始,因此首帧应与原始画面保持一致。
+ np.testing.assert_allclose(first, original, atol=2)
+ # 末帧来自中心裁剪并放大后的区域,应与原始画面存在明显差异。
+ self.assertGreater(np.abs(last.astype(int) - original.astype(int)).max(), 2)
+
+ def test_zoomout_starts_zoomed_and_returns_to_source(self):
+ clip = _gradient_clip()
+ zoomed = video_effects.zoomout_transition(clip, 1)
+ self.addCleanup(zoomed.close)
+ self.addCleanup(clip.close)
+
+ self.assertEqual(zoomed.size, clip.size)
+
+ first = zoomed.get_frame(0)
+ last = zoomed.get_frame(clip.duration)
+ original = clip.get_frame(0)
+
+ # 缩小的首帧为 1.2 倍画面,结束时精确回到原始比例。
+ self.assertGreater(np.abs(first.astype(int) - original.astype(int)).max(), 2)
+ np.testing.assert_allclose(last, original, atol=2)
+
+ def test_zoom_frame_rejects_invalid_scale_factor(self):
+ frame = np.zeros((8, 8, 3), dtype=np.uint8)
+
+ with self.assertRaisesRegex(ValueError, "scale_factor"):
+ video_effects._zoom_frame(frame, 0)
+
+ def test_zoom_frame_responds_to_subpixel_scale_changes(self):
+ frame = _detail_frame()
+
+ first = video_effects._zoom_frame(frame, 1.1)
+ second = video_effects._zoom_frame(frame, 1.1001)
+
+ # 这两个比例在旧的整数裁剪算法中会落入相同裁剪尺寸,产生完全相同的帧,
+ # 随后在跨过整数边界时突然跳变。亚像素采样应当能响应这种微小比例变化。
+ self.assertGreater(np.count_nonzero(first != second), 0)
+ self.assertLessEqual(
+ np.abs(first.astype(np.int16) - second.astype(np.int16)).max(),
+ 1,
+ )
+
+ def test_zoom_frame_keeps_center_stable_for_odd_resolution(self):
+ width, height = 59, 75
+ center_x, center_y = width // 2, height // 2
+ x = np.arange(width, dtype=np.int16) - center_x
+ y = np.arange(height, dtype=np.int16)[:, None] - center_y
+ radial = np.clip(x**2 + y**2, 0, 255).astype(np.uint8)
+ frame = np.stack((radial, radial, radial), axis=-1)
+
+ zoomed = video_effects._zoom_frame(frame, 1.2)
+
+ self.assertEqual(zoomed.shape, frame.shape)
+ self.assertEqual(zoomed.dtype, frame.dtype)
+ # 奇数宽高只有一个精确中心像素,缩放后该像素不应发生横向或纵向漂移。
+ np.testing.assert_allclose(
+ zoomed[center_y, center_x],
+ frame[center_y, center_x],
+ atol=1,
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/webui/Main.py b/webui/Main.py
index 7b6c399a..91e4b449 100644
--- a/webui/Main.py
+++ b/webui/Main.py
@@ -2143,6 +2143,8 @@ def _render_video_settings(panel, params):
(tr("FadeOut"), VideoTransitionMode.fade_out.value),
(tr("SlideIn"), VideoTransitionMode.slide_in.value),
(tr("SlideOut"), VideoTransitionMode.slide_out.value),
+ (tr("ZoomIn"), VideoTransitionMode.zoom_in.value),
+ (tr("ZoomOut"), VideoTransitionMode.zoom_out.value),
]
selected_transition_mode = stable_selectbox(
tr("Video Transition Mode"),
diff --git a/webui/i18n/en.json b/webui/i18n/en.json
index 96b06492..b5da9a3d 100644
--- a/webui/i18n/en.json
+++ b/webui/i18n/en.json
@@ -67,6 +67,8 @@
"FadeOut": "FadeOut",
"SlideIn": "SlideIn",
"SlideOut": "SlideOut",
+ "ZoomIn": "ZoomIn",
+ "ZoomOut": "ZoomOut",
"Video Ratio": "Video Aspect Ratio",
"Portrait": "Portrait 9:16",
"Landscape": "Landscape 16:9",
diff --git a/webui/i18n/ru.json b/webui/i18n/ru.json
index a03596ee..4306abcd 100644
--- a/webui/i18n/ru.json
+++ b/webui/i18n/ru.json
@@ -69,6 +69,8 @@
"FadeOut": "Плавное исчезновение",
"SlideIn": "Въезд кадра",
"SlideOut": "Выезд кадра",
+ "ZoomIn": "Приближение",
+ "ZoomOut": "Отдаление",
"Video Ratio": "Формат видео",
"Portrait": "Вертикальное 9:16",
"Landscape": "Горизонтальное 16:9",
diff --git a/webui/i18n/zh.json b/webui/i18n/zh.json
index b62fddd5..ad9397d0 100644
--- a/webui/i18n/zh.json
+++ b/webui/i18n/zh.json
@@ -67,6 +67,8 @@
"FadeOut": "渐出",
"SlideIn": "滑动入",
"SlideOut": "滑动出",
+ "ZoomIn": "放大",
+ "ZoomOut": "缩小",
"Video Ratio": "视频比例",
"Portrait": "竖屏 9:16(抖音视频)",
"Landscape": "横屏 16:9(西瓜视频)",