我正在使用 Moviepy2 渲染基于图像的视频。有一张图像应该作为整个视频的背景图像。多个图像应按顺序淡入、显示几秒钟并再次淡出。
我的代码生成视频,但
foreground_clip
没有淡入和淡出。在褪色过程中我只看到一个黑匣子。
def create_video(background_image_path, foreground_images_folder, output_video_path):
# Load and resize the background image
background_clip = ImageClip(background_image_path)
print(background_clip)
print(background_clip.size)
background_clip = background_clip.resized(VIDEO_RESOLUTION).with_duration(IMAGE_DISPLAY_DURATION)
# List all images in the foreground images folder
foreground_images = [os.path.join(foreground_images_folder, img) for img in os.listdir(foreground_images_folder) if img.endswith(('png', 'jpg', 'jpeg'))]
# Create video clips for each foreground image
clips = []
for img_path in foreground_images:
print(f"Processing image: {img_path}")
foreground_clip = ImageClip(img_path).resized(0.8).with_duration(IMAGE_DISPLAY_DURATION)
foreground_clip = foreground_clip.with_position('center').with_effects([vfx.CrossFadeIn(1)]).with_effects([vfx.CrossFadeOut(1)])
composite_clip = CompositeVideoClip([background_clip, foreground_clip])
clips.append(composite_clip)
#clips.append(foreground_clip)
# Concatenate all clips
final_clip = concatenate_videoclips(clips, method="compose")
# Write the final video to a file
final_clip.write_videofile(output_video_path, fps=24)
如果您尝试(设置位置后)添加另一行,如下所示:
foreground_clip=foreground_clip.with_effects([vfx.FadeIn(1,0), vfx.FadeOut(1,0)])