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

时间:2016-02-11 16:30:34

标签: python numpy moviepy

我正在生成大量图像并将其写入磁盘。然后我将文件名数组传递给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)

1 个答案:

答案 0 :(得分:2)

查看文档的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") 
相关问题