Skip to content

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

fix: filter invalid local materials before video merge

Harryfcf0dbc244b3
diff --git a/app/services/video.py b/app/services/video.py
index f7547ed6..1bf22ad7 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -487,47 +487,69 @@ def generate_video(
def preprocess_video(materials: List[MaterialInfo], clip_duration=4):
+ # 仅返回通过预处理校验的素材,避免低分辨率图片继续进入后续的视频合成流程。
+ valid_materials = []
+
for material in materials:
if not material.url:
continue
ext = utils.parse_extension(material.url)
try:
- clip = VideoFileClip(material.url)
+ # 图片素材直接按图片方式读取,避免先走 VideoFileClip 误判后触发不稳定的回退分支。
+ if ext in const.FILE_TYPE_IMAGES:
+ clip = ImageClip(material.url)
+ else:
+ clip = VideoFileClip(material.url)
except Exception:
+ # 非标准扩展名或探测失败时再回退到图片模式,兼容历史上直接传本地图片路径的情况。
clip = ImageClip(material.url)
+ try:
+ width = clip.size[0]
+ height = clip.size[1]
+ if width < 480 or height < 480:
+ logger.warning(f"low resolution material: {width}x{height}, minimum 480x480 required")
+ # 探测到低分辨率素材后立即关闭资源,并且不要把该素材返回给后续流程。
+ close_clip(clip)
+ continue
+
+ if ext in const.FILE_TYPE_IMAGES:
+ logger.info(f"processing image: {material.url}")
+ # 探测尺寸时已经打开过一次素材,这里先释放探测句柄,再重新创建用于导出的图片 clip。
+ close_clip(clip)
+ # Create an image clip and set its duration to 3 seconds
+ clip = (
+ ImageClip(material.url)
+ .with_duration(clip_duration)
+ .with_position("center")
+ )
+ # Apply a zoom effect using the resize method.
+ # A lambda function is used to make the zoom effect dynamic over time.
+ # The zoom effect starts from the original size and gradually scales up to 120%.
+ # t represents the current time, and clip.duration is the total duration of the clip (3 seconds).
+ # Note: 1 represents 100% size, so 1.2 represents 120% size.
+ zoom_clip = clip.resized(
+ lambda t: 1 + (clip_duration * 0.03) * (t / clip.duration)
+ )
+
+ # Optionally, create a composite video clip containing the zoomed clip.
+ # This is useful when you want to add other elements to the video.
+ final_clip = CompositeVideoClip([zoom_clip])
+
+ # Output the video to a file.
+ video_file = f"{material.url}.mp4"
+ final_clip.write_videofile(video_file, fps=30, logger=None)
+ close_clip(clip)
+ close_clip(final_clip)
+ material.url = video_file
+ logger.success(f"image processed: {video_file}")
+ else:
+ # 普通视频素材只需要读取尺寸做校验,校验完成后立即释放句柄即可。
+ close_clip(clip)
+ except Exception:
+ close_clip(clip)
+ raise
- width = clip.size[0]
- height = clip.size[1]
- if width < 480 or height < 480:
- logger.warning(f"low resolution material: {width}x{height}, minimum 480x480 required")
- continue
-
- if ext in const.FILE_TYPE_IMAGES:
- logger.info(f"processing image: {material.url}")
- # Create an image clip and set its duration to 3 seconds
- clip = (
- ImageClip(material.url)
- .with_duration(clip_duration)
- .with_position("center")
- )
- # Apply a zoom effect using the resize method.
- # A lambda function is used to make the zoom effect dynamic over time.
- # The zoom effect starts from the original size and gradually scales up to 120%.
- # t represents the current time, and clip.duration is the total duration of the clip (3 seconds).
- # Note: 1 represents 100% size, so 1.2 represents 120% size.
- zoom_clip = clip.resized(
- lambda t: 1 + (clip_duration * 0.03) * (t / clip.duration)
- )
-
- # Optionally, create a composite video clip containing the zoomed clip.
- # This is useful when you want to add other elements to the video.
- final_clip = CompositeVideoClip([zoom_clip])
+ valid_materials.append(material)
- # Output the video to a file.
- video_file = f"{material.url}.mp4"
- final_clip.write_videofile(video_file, fps=30, logger=None)
- close_clip(clip)
- material.url = video_file
- logger.success(f"image processed: {video_file}")
- return materials
+ return valid_materials