Generating movie from python without saving individual frames to files

后端 未结 6 1299
天涯浪人
天涯浪人 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条回答
  •  旧时难觅i
    2020-11-29 17:05

    This is great! I wanted to do the same. But, I could never compile the patched ffmpeg source (0.6.1) in Vista with MingW32+MSYS+pr enviroment... png_parser.c produced Error1 during compilation.

    So, I came up with a jpeg solution to this using PIL. Just put your ffmpeg.exe in the same folder as this script. This should work with ffmpeg without the patch under Windows. I had to use stdin.write method rather than the communicate method which is recommended in the official documentation about subprocess. Note that the 2nd -vcodec option specifies the encoding codec. The pipe is closed by p.stdin.close().

    import subprocess
    import numpy as np
    from PIL import Image
    
    rate = 1
    outf = 'test.avi'
    
    cmdstring = ('ffmpeg.exe',
                 '-y',
                 '-r', '%d' % rate,
                 '-f','image2pipe',
                 '-vcodec', 'mjpeg',
                 '-i', 'pipe:', 
                 '-vcodec', 'libxvid',
                 outf
                 )
    p = subprocess.Popen(cmdstring, stdin=subprocess.PIPE, shell=False)
    
    for i in range(10):
        im = Image.fromarray(np.uint8(np.random.randn(100,100)))
        p.stdin.write(im.tostring('jpeg','L'))
        #p.communicate(im.tostring('jpeg','L'))
    
    p.stdin.close()
    

提交回复
热议问题