从python生成电影,而不将单个帧保存到文件 [英] Generating movie from python without saving individual frames to files

查看:206
本文介绍了从python生成电影,而不将单个帧保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在matplotlib中的python脚本中生成的框架中创建一个h264或divx电影。这部电影中有大约100k帧。

I would like to create an h264 or divx movie from frames that I generate in a python script in matplotlib. There are about 100k frames in this movie.

在网络上的例子中1],我只看到了将每个框架保存为png,然后在这些文件上运行mencoder或ffmpeg的方法。在我的情况下,保存每个框架是不切实际的。有没有办法从matplotlib生成的图,并将其直接导入ffmpeg,不生成中间文件?

In examples on the web [eg. 1], I have only seen the method of saving each frame as a png and then running mencoder or ffmpeg on these files. In my case, saving each frame is impractical. Is there a way to take a plot generated from matplotlib and pipe it directly to ffmpeg, generating no intermediate files?

使用ffmpeg的C-api编程对我来说太难了[例如2]。此外,我需要一个具有良好压缩率的编码,如x264,因为影片文件将不适用于后续步骤。所以这将是伟大的坚持mencoder / ffmpeg / x264。

Programming with ffmpeg's C-api is too difficult for me [eg. 2]. Also, I need an encoding that has good compression such as x264 as the movie file will otherwise be too large for a subsequent step. So it would be great to stick with mencoder/ffmpeg/x264.

有什么可以做的管道[3]?

Is there something that can be done with pipes [3]?

[1] http://matplotlib.sourceforge.net/ examples / animation / movie_demo.html

[2] 一个使用x264 C API将一系列图像编码到H264?

[3] http://www.ffmpeg.org/ffmpeg-doc.html#SEC41

推荐答案

此功能现在(至少从1.2.0,可能是1.1)通过 MovieWriter 类烘焙成matplotlib,它的子 - 动画模块中的类。

This functionality is now (at least as of 1.2.0, maybe 1.1) baked into matplotlib via the MovieWriter class and it's sub-classes in the animation module.

import matplotlib.animation as animation
import numpy as np
from pylab import *


dpi = 100

def ani_frame():
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_aspect('equal')
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    im = ax.imshow(rand(300,300),cmap='gray',interpolation='nearest')
    im.set_clim([0,1])
    fig.set_size_inches([5,5])


    tight_layout()


    def update_img(n):
        tmp = rand(300,300)
        im.set_data(tmp)
        return im

    #legend(loc=0)
    ani = animation.FuncAnimation(fig,update_img,300,interval=30)
    writer = animation.writers['ffmpeg'](fps=30)

    ani.save('demo.mp4',writer=writer,dpi=dpi)
    return ani

动画文档

这篇关于从python生成电影,而不将单个帧保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆