Broken pipe error when saving animation for html5

Hi all,

I would like to use matplotlib’s animation API to create a movie that can be embedded into an html5 video tag. To do this, I am following a couple 2012 blog posts from Jake VanderPlas: http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ and http://jakevdp.github.io/blog/2013/05/12/embedding-matplotlib-animations/

My understanding is that I need to use the x264 codec to do this. Unfortunately, I get a Broken Pipe Error when running the basic example below. I get the same error when calling the animation.to_html5_video() method.

Does anyone have an idea about why I’m getting the Broken Pipe error? Do I really need the x264 codec to embed matplotlib animations into html? Thanks!

Kyle

Example code:

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animate
__fig = plt.figure()
ax = plt.axes(xlim = (0, 2), ylim = (-2, 2))
line, = ax.plot([], [], linewidth = 2)

initialization function: plot the background of each frame

def init():
line.set_data([], [])
return line,

animation function. This is called sequentially

def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,

call the animator

anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
myWriter = animation.FFMpegWriter(fps = 30, extra_args=[’-vcodec’, ‘libx264’])
anim.save(‘basic_animation.mp4’, writer = myWriter)__

plt.show()

Traceback:

_Traceback (most recent call last):
File “test.py”, line 26, in
anim.save(‘basic_animation.mp4’, writer = myWriter)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/animat
ion.py”, line 810, in save
writer.grab_frame(**savefig_kwargs)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/animat
ion.py”, line 230, in grab_frame
dpi=self.dpi, **savefig_kwargs)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/figure
.py”, line 1565, in savefig
self.canvas.print_figure(*args, **kwargs)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/backen
ds/backend_qt5agg.py”, line 196, in print_figure
FigureCanvasAgg.print_figure(self, *args, **kwargs)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/backen
d_bases.py”, line 2232, in print_figure
**kwargs)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/backen
ds/backend_agg.py”, line 519, in print_raw
fileobj.write(renderer.renderer.buffer_rgba())
BrokenPipeError: [Errno 32] Broken pipe

I’m using Anaconda/Python 3.5 with matplotlib 1.5.1. I installed FFmpeg 3.0 in Debian Jessie by compiling FFmpeg from source into a dpkg according to these instructions: http://superuser.com/questions/286675/how-to-install-ffmpeg-on-debian I also have Debian packages x264 and libx264-142 installed.

Try running with ‘–verbose-debug’ as in:

python basic_example_writer.py --verbose-debug

That should give you standard out and standard error from running ffmpeg, which might be illustrative.

x264 is probably the most universally supported codec for html5 video. Another option would be to use imagemagick with the ImagemagickWriter and make an animated gif.

Ryan

···

On Wed, Feb 24, 2016 at 1:34 PM, Kyle Douglass <kyle.m.douglass@…287…> wrote:

Hi all,

I would like to use matplotlib’s animation API to create a movie that can be embedded into an html5 video tag. To do this, I am following a couple 2012 blog posts from Jake VanderPlas: http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ and http://jakevdp.github.io/blog/2013/05/12/embedding-matplotlib-animations/

My understanding is that I need to use the x264 codec to do this. Unfortunately, I get a Broken Pipe Error when running the basic example below. I get the same error when calling the animation.to_html5_video() method.

Does anyone have an idea about why I’m getting the Broken Pipe error? Do I really need the x264 codec to embed matplotlib animations into html? Thanks!

Kyle

Example code:

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animate
__fig = plt.figure()
ax = plt.axes(xlim = (0, 2), ylim = (-2, 2))
line, = ax.plot(, , linewidth = 2)

initialization function: plot the background of each frame

def init():
line.set_data(, )
return line,

animation function. This is called sequentially

def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,

call the animator

anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
myWriter = animation.FFMpegWriter(fps = 30, extra_args=[‘-vcodec’, ‘libx264’])
anim.save(‘basic_animation.mp4’, writer = myWriter)__

plt.show()

Traceback:

_Traceback (most recent call last):
File “test.py”, line 26, in
anim.save(‘basic_animation.mp4’, writer = myWriter)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/animat
ion.py”, line 810, in save
writer.grab_frame(**savefig_kwargs)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/animat
ion.py”, line 230, in grab_frame
dpi=self.dpi, **savefig_kwargs)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/figure
.py”, line 1565, in savefig
self.canvas.print_figure(*args, **kwargs)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/backen
ds/backend_qt5agg.py”, line 196, in print_figure
FigureCanvasAgg.print_figure(self, *args, **kwargs)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/backen
d_bases.py”, line 2232, in print_figure
**kwargs)
File “/home/kmdouglass/anaconda3/lib/python3.5/site-packages/matplotlib/backen
ds/backend_agg.py”, line 519, in print_raw
fileobj.write(renderer.renderer.buffer_rgba())
BrokenPipeError: [Errno 32] Broken pipe

I’m using Anaconda/Python 3.5 with matplotlib 1.5.1. I installed FFmpeg 3.0 in Debian Jessie by compiling FFmpeg from source into a dpkg according to these instructions: http://superuser.com/questions/286675/how-to-install-ffmpeg-on-debian I also have Debian packages x264 and libx264-142 installed.


Site24x7 APM Insight: Get Deep Visibility into Application Performance

APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month

Monitor end-to-end web transactions and take corrective actions now

Troubleshoot faster and improve end-user experience. Signup Now!

http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Ryan May

Try running with '--verbose-debug' as in:

python basic_example_writer.py --verbose-debug

Thanks for the suggestion, Ryan. By doing so I discovered that the error
was caused by ffmpeg not finding the libx264 library; the error was not at
all connected with matplotlib.

As it turns out, I neglected to use the *--enable-libx264 --enable-gpl
--enable-nonfree* flags when running the configuration script for ffmpeg
just before compiling. I recompiled ffmpeg and now everything is working
properly.

Thanks again,
Kyle