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

Moviepy - avoid writing to disk with ImageSequenceClip?

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

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")