Saving animations

I’ve been using the animations subpackage since it was introduced, but I only recently tried to save an animation using the save method. Unfortnately, I get a RuntimeError whenever I try to use it:

File “/Users/Tony/python/devel/mpl/lib/matplotlib/backends/backend_agg.py”, line 4

52, in print_raw

renderer._renderer.write_rgba(filename_or_obj)

RuntimeError: Error writing to file

This can be reproduced with any animation example, e.g. simple_anim.py, by replacing plt.show() with ani.save('simple_anim.avi'). The actual error is from a shell call to ffmpeg, which roughly boils down to:

···

#~~~~

import subprocess

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([0, 1])

command = [‘ffmpeg’, ‘-f’, ‘rawvideo’, ‘-vcodec’, ‘rawvideo’,

‘-s’, ‘800x600’, ‘-pix_fmt’, ‘rgba’, ‘-r’, ‘5.0’,

‘-i’, ‘pipe:’, ‘-vcodec’, ‘mpeg4’, ‘-y’, ‘test.avi’]

proc= subprocess.Popen(command, shell=False, stdout=subprocess.PIPE,

stderr=subprocess.STDOUT, stdin=subprocess.PIPE)

fileobj = proc.stdin

fig.savefig(fileobj, format=‘rgba’, dpi=100)

#~~~~

(I just copied the ffmpeg command from an example run, so the option values may vary on other systems.) The error here is the same, and, unfortunately, not very informative. Is this saving functionality platform dependent? (I’m on osx.)

Thanks,

-Tony

Tony,

The animation saving feature has be completely rewritten by Ryan recently and is in master. Could you give that a try?

Ben Root

···

On Sat, Apr 7, 2012 at 1:34 PM, Tony Yu <tsyu80@…149…> wrote:

I’ve been using the animations subpackage since it was introduced, but I only recently tried to save an animation using the save method. Unfortnately, I get a RuntimeError whenever I try to use it:

File “/Users/Tony/python/devel/mpl/lib/matplotlib/backends/backend_agg.py”, line 4

52, in print_raw

renderer._renderer.write_rgba(filename_or_obj)

RuntimeError: Error writing to file

This can be reproduced with any animation example, e.g. simple_anim.py, by replacing plt.show() with ani.save('simple_anim.avi'). The actual error is from a shell call to ffmpeg, which roughly boils down to:

#~~~~

import subprocess

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([0, 1])

command = [‘ffmpeg’, ‘-f’, ‘rawvideo’, ‘-vcodec’, ‘rawvideo’,

‘-s’, ‘800x600’, ‘-pix_fmt’, ‘rgba’, ‘-r’, ‘5.0’,

‘-i’, ‘pipe:’, ‘-vcodec’, ‘mpeg4’, ‘-y’, ‘test.avi’]

proc= subprocess.Popen(command, shell=False, stdout=subprocess.PIPE,

stderr=subprocess.STDOUT, stdin=subprocess.PIPE)

fileobj = proc.stdin

fig.savefig(fileobj, format=‘rgba’, dpi=100)

#~~~~

(I just copied the ffmpeg command from an example run, so the option values may vary on other systems.) The error here is the same, and, unfortunately, not very informative. Is this saving functionality platform dependent? (I’m on osx.)

Thanks,

-Tony

Hey Ben,

This is error is using the current master, actually. I think the previous implementation saved images and read them with ffmpeg (or some other utility) instead of piping directly to ffmpeg.

-Tony

(Ben: sorry for the duplicate emails. Forgot to “reply-all”)

···

On Sat, Apr 7, 2012 at 8:40 PM, Benjamin Root <ben.root@…553…> wrote:

On Sat, Apr 7, 2012 at 1:34 PM, Tony Yu <tsyu80@…149…> wrote:

I’ve been using the animations subpackage since it was introduced, but I only recently tried to save an animation using the save method. Unfortnately, I get a RuntimeError whenever I try to use it:

File “/Users/Tony/python/devel/mpl/lib/matplotlib/backends/backend_agg.py”, line 4

52, in print_raw

renderer._renderer.write_rgba(filename_or_obj)

RuntimeError: Error writing to file

This can be reproduced with any animation example, e.g. simple_anim.py, by replacing plt.show() with ani.save('simple_anim.avi'). The actual error is from a shell call to ffmpeg, which roughly boils down to:

#~~~~

import subprocess

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([0, 1])

command = [‘ffmpeg’, ‘-f’, ‘rawvideo’, ‘-vcodec’, ‘rawvideo’,

‘-s’, ‘800x600’, ‘-pix_fmt’, ‘rgba’, ‘-r’, ‘5.0’,

‘-i’, ‘pipe:’, ‘-vcodec’, ‘mpeg4’, ‘-y’, ‘test.avi’]

proc= subprocess.Popen(command, shell=False, stdout=subprocess.PIPE,

stderr=subprocess.STDOUT, stdin=subprocess.PIPE)

fileobj = proc.stdin

fig.savefig(fileobj, format=‘rgba’, dpi=100)

#~~~~

(I just copied the ffmpeg command from an example run, so the option values may vary on other systems.) The error here is the same, and, unfortunately, not very informative. Is this saving functionality platform dependent? (I’m on osx.)

Thanks,

-Tony

Tony,

The animation saving feature has be completely rewritten by Ryan recently and is in master. Could you give that a try?

Ben Root

It shouldn't be OS-dependent. In fact, as I keep being told "OSX is
practically unix!". (Sorry, I just get tired of being told that and
then having simple unix functionality fail spectacularly. I'm
better...I swear.)

Would it be possible to try the mencoder support instead? (If it's
possible to get that installed). Also, could you try the ffmpeg_file
"backend" which uses the temp files? It will be helpful to try to
narrow down whether the piping itself is making it angry or if
something is wrong with ffmpeg. You should just be able to do:

anim.save(..., writer='ffmpeg_file') or anim.save(..., writer='mencoder')

Also, can you run any/all of them with --verbose-debug? That might
help give a bit more error information.

Ryan

···

On Sun, Apr 8, 2012 at 9:13 AM, Tony Yu <tsyu80@...149...> wrote:

On Sat, Apr 7, 2012 at 8:40 PM, Benjamin Root <ben.root@...553...> wrote:

On Sat, Apr 7, 2012 at 1:34 PM, Tony Yu <tsyu80@...149...> wrote:

I've been using the animations subpackage since it was introduced, but I
only recently tried to save an animation using the `save` method.
Unfortnately, I get a RuntimeError whenever I try to use it:

...
File
"/Users/Tony/python/devel/mpl/lib/matplotlib/backends/backend_agg.py", line
4
52, in print_raw
renderer._renderer.write_rgba(filename_or_obj)
RuntimeError: Error writing to file

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma