Generating movie from python without saving individual frames to files

后端 未结 6 1283
天涯浪人
天涯浪人 2020-11-29 16:16

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.

In examples on

6条回答
  •  無奈伤痛
    2020-11-29 17:02

    Converting to image formats is quite slow and adds dependencies. After looking at these page and other I got it working using raw uncoded buffers using mencoder (ffmpeg solution still wanted).

    Details at: http://vokicodder.blogspot.com/2011/02/numpy-arrays-to-video.html

    import subprocess
    
    import numpy as np
    
    class VideoSink(object) :
    
        def __init__( self, size, filename="output", rate=10, byteorder="bgra" ) :
                self.size = size
                cmdstring  = ('mencoder',
                        '/dev/stdin',
                        '-demuxer', 'rawvideo',
                        '-rawvideo', 'w=%i:h=%i'%size[::-1]+":fps=%i:format=%s"%(rate,byteorder),
                        '-o', filename+'.avi',
                        '-ovc', 'lavc',
                        )
                self.p = subprocess.Popen(cmdstring, stdin=subprocess.PIPE, shell=False)
    
        def run(self, image) :
                assert image.shape == self.size
                self.p.stdin.write(image.tostring())
        def close(self) :
                self.p.stdin.close()
    

    I got some nice speedups.

提交回复
热议问题