Generate movie from python without saving individual frames to file


Paul:

I want to create a h264 or divx movie based on the frames I generate in a python script in matplotlib. There are about 100,000 copies of this movie.

In examples on the web [eg 1], I've only seen ways to save each frame as png and then run mencoder or ffmpeg on those files. In my case, saving every frame is impractical. Is there a way to take the plot generated from matplotlib and transfer it directly to ffmpeg without generating any intermediate files?

Programming with ffmpeg's C-api is too difficult for me [eg. 2]. Also, I need an encoding with good compression, such as x264, because the movie file is too big for the subsequent steps. So it's better to stick with mencoder/ffmpeg/x264.

Is there anything pipe[3] can do?

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

[2] How to encode a series of images to H264 using x264 C API?

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

Takaswell:

This functionality is now (at least since 1.2.0, possibly 1.1) baked into matplotlib via subclasses in the MovieWriterclass and its animationmodules . You also need to install it ffmpegin advance .

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

related documentsanimation

Related


How to generate a file without saving it to disk in python?

Sasha Kalaba I am using Python 2.7 and Django 1.7. I have a method in my admin interface that generates some kind of csv file. def generate_csv(args): ... #some code that generates a dictionary to be written as csv .... # this creates a direct

How to generate a file without saving it to disk in python?

Sasha Kalaba I am using Python 2.7 and Django 1.7. I have a method in my admin interface that generates some kind of csv file. def generate_csv(args): ... #some code that generates a dictionary to be written as csv .... # this creates a direct

How to generate a file without saving it to disk in python?

Sasha Kalaba I am using Python 2.7 and Django 1.7. I have a method in my admin interface that generates some kind of csv file. def generate_csv(args): ... #some code that generates a dictionary to be written as csv .... # this creates a direct

Generate a MIDI file and play it without saving it to disk

Rodriguez I found a module that can create midi files . I can easily play the output file with pygame mixer.music , but if I try to play it without having to save to a file (playback object) it doesn't work, I get pygame.error: cannot read from RWops 。 I tried

Generate a MIDI file and play it without saving it to disk

Rodriguez I found a module that can create midi files . I can easily play the output file with pygame mixer.music , but if I try to play it without having to save to a file (play object) it doesn't work, I get pygame.error: cannot read from RWops 。 I tried to

Remove child from movie clip with several frames?

suzuiyue I have a movie clip called MC which has two layers. The second layer has 5 frames and the first layer has only one movie clip itemfrom frame 1 to frame 5 . Then, in my code, create a new name MovieClipcalled soulItemand add itemto , soulItemand then a

Remove child from movie clip with several frames?

suzuiyue I have a movie clip called MC which has two layers. The second layer has 5 frames and the first layer has only one movie clip itemfrom frame 1 to frame 5 . Then, in my code, create a new name MovieClipcalled soulItemand add itemto , soulItemand then a

Generate an image file without saving the entire image in memory

missing xor I wrote a program to generate RGB color values using three equations and write them to a data file: from struct import pack #User-set variables size = [16384, 16384] center = [0, 0] r_equation = "x ^ y" g_equation = "x - y" b_equation = "x + y" da

Download file from FileResult without saving to disk

Philip I am using MVC 4. I have a method in my controller that generates a CSV file on demand, which I want the user to then download without saving it to disk on the server side. So I pass a MemoryStream on the File() object to avoid having to save the file t

Download file from FileResult without saving to disk

Philip I am using MVC 4. I have a method in my controller that generates a CSV file on demand, which I want the user to then download without saving it to disk on the server side. So I pass a MemoryStream on the File() object to avoid having to save the file t

Python - read audio data without saving to file

Sean Zhang I'm sending audio data between the browser and my AWS Lambda function, but I find myself doing an intermediate step to save it to a file for functional purposes. Here is my code that works now: wavio.write(file="out.wav", data=out_file, rate=16000,

Python - read audio data without saving to file

Sean Zhang I'm sending audio data between the browser and my AWS Lambda function, but I find myself doing an intermediate step to save it to a file for functional purposes. Here is my code that works now: wavio.write(file="out.wav", data=out_file, rate=16000,

How to quit without saving the file in Python

Light of the city I'm new to python and have only written a few programs so far to help me with my work (I'm a sysadmin). I am now writing this script which will write the output of a MySQL query to a file. Between loops, I want to check for an additional cond

Python - read audio data without saving to file

Sean Zhang I'm sending audio data between the browser and my AWS Lambda function, but I find myself doing an intermediate step to save it to a file for functional purposes. Here is my code that works now: wavio.write(file="out.wav", data=out_file, rate=16000,

Python - read audio data without saving to file

Sean Zhang I'm sending audio data between the browser and my AWS Lambda function, but I find myself doing an intermediate step to save it to a file for functional purposes. Here is my code that works now: wavio.write(file="out.wav", data=out_file, rate=16000,

How to quit without saving the file in Python

Light of the city I'm new to python and have only written a few programs so far to help me with my work (I'm a sysadmin). I am now writing this script which will write the output of a MySQL query to a file. Between loops, I want to check for an additional cond