3

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

1 回答 1

2

查看文档的这一部分。

这是使用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") 
于 2016-02-11T16:51:58.357 回答