gpt4 book ai didi

python - Moviepy - 避免使用 ImageSequenceClip 写入磁盘?

转载 作者:太空宇宙 更新时间:2023-11-04 03:18:50 28 4
gpt4 key购买 nike

我正在生成大量图像并将它们写入磁盘。然后,我将一组文件名传递给 ImageSequenceClip。一个主要的瓶颈是将图像写入磁盘;有没有办法将图像保存在内存中,然后将其传递给 ImageSequenceClip,从而避免写入/读取磁盘所需的时间?

filenames = []
for i in range(0, FRAMES):
filename = "tmp/frame_%s.png" % (i)
filenames.append(filename)
center_x = IMG_WIDTH / 2
center_y = IMG_HEIGHT - ((IMG_HEIGHT - i * HEIGHT_INC) / 2)
width = IMG_WIDTH - i * WIDTH_INC
height = IMG_HEIGHT - i * HEIGHT_INC
img = vfx.crop(img_w_usr_img, x_center=center_x, y_center=center_y, width=width, height=height)
img = img.resize( (VID_WIDTH, VID_HEIGHT) )
img.save_frame(filename)
print "Proccessed: %s" % (filename)

seq = ImageSequenceClip(filenames, fps=FPS)

最佳答案

看看this文档部分。

下面是如何使用 moviepy 翻转视频

from moviepy.editor import VideoFileClip
def flip(image):
"""Flips an image vertically """
return image[::-1] # remember that image is a numpy array

clip = VideoFileClip("my_original_video.mp4")
new_clip = clip.fl_image( flip )
new_clip.write_videofile("my_new_clip", some other parameters)

请注意,moviepy 中还有一个预定义的 FX 函数 mirror_y,因此您可以简单地执行以下操作:

from moviepy.editor import *
clip = VideoFileClip("my_original_video.mp4")
new_clip = clip.fx(vfx.mirror_y)
new_clip.write_videofile("my_new_clip", some other parameters)

但是如果你真的想首先制作一个(转换后的)numpy 数组列表,你可以这样做:

from moviepy.editor import *
clip = VideoFileClip("original_file.mp4")
new_frames = [ some_effect(frame) for frame in clip.iter_frames()]
new_clip = ImageSequenceClip(new_frames, fps=clip.fps)
new_clip.write_videofile("new_file.mp4")

关于python - Moviepy - 避免使用 ImageSequenceClip 写入磁盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35344827/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com