Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

animation save gets error writing to file with ffmpeg and ogv (or ogg) #2764

Closed
moorepants opened this issue Jan 24, 2014 · 24 comments
Closed

Comments

@moorepants
Copy link

Using: http://matplotlib.org/1.2.1/examples/animation/double_pendulum_animated.html

With these added lines at the top:

from matplotlib import __version__
print("matplotlib version: {}".format(__version__))
import os
os.system('ffmpeg')

And no clear_temp kwarg in the save method call. I get this for trying to make a double_pendulum.ogv file.

Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: run double_pendulum_animated.py
matplotlib version: 1.2.1
ffmpeg version 0.8.9-6:0.8.9-0ubuntu0.13.10.1, Copyright (c) 2000-2013 the Libav developers
  built on Nov  9 2013 19:09:46 with gcc 4.8.1
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/usr/local/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    202             else:
    203                 filename = fname
--> 204             __builtin__.execfile(filename, *where)

/home/moorepants/Desktop/double_pendulum_animated.py in <module>()
     90     interval=25, blit=True, init_func=init)
     91 
---> 92 ani.save('double_pendulum.ogv', fps=15)
     93 plt.show()

/usr/local/lib/python2.7/dist-packages/matplotlib/animation.pyc in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim)
    613                     #TODO: Need to see if turning off blit is really necessary
    614                     anim._draw_next_frame(d, blit=False)
--> 615                 writer.grab_frame()
    616 
    617         # Reconnect signal for first draw if necessary

/usr/local/lib/python2.7/dist-packages/matplotlib/animation.pyc in grab_frame(self)
    197             # frame format and dpi.
    198             self.fig.savefig(self._frame_sink(), format=self.frame_format,
--> 199                 dpi=self.dpi)
    200         except RuntimeError:
    201             out, err = self._proc.communicate()

/usr/local/lib/python2.7/dist-packages/matplotlib/figure.pyc in savefig(self, *args, **kwargs)
   1368             kwargs.setdefault('edgecolor', rcParams['savefig.edgecolor'])
   1369 
-> 1370         self.canvas.print_figure(*args, **kwargs)
   1371 
   1372         if transparent:

/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2094                 orientation=orientation,
   2095                 bbox_inches_restore=_bbox_inches_restore,
-> 2096                 **kwargs)
   2097         finally:
   2098             if bbox_inches and restore_bbox:

/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.pyc in print_raw(self, filename_or_obj, *args, **kwargs)
    482             close = False
    483         try:
--> 484             renderer._renderer.write_rgba(filename_or_obj)
    485         finally:
    486             if close:

RuntimeError: Error writing to file

@moorepants
Copy link
Author

Same error happens with .ogv or .ogg as an extension.

@moorepants
Copy link
Author

If I explicitly set the save writer to be FFmpegFileWriter() as suggest here: http://stackoverflow.com/a/16721486/467314 then it computes for a while, and I get this error for .ogg:

Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: run double_pendulum_animated.py
matplotlib version: 1.2.1
ffmpeg version 0.8.9-6:0.8.9-0ubuntu0.13.10.1, Copyright (c) 2000-2013 the Libav developers
  built on Nov  9 2013 19:09:46 with gcc 4.8.1
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/usr/local/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    202             else:
    203                 filename = fname
--> 204             __builtin__.execfile(filename, *where)

/home/moorepants/Desktop/double_pendulum_animated.py in <module>()
     91     interval=25, blit=True, init_func=init)
     92 
---> 93 ani.save('double_pendulum.ogg', writer=animation.FFMpegFileWriter(), fps=15)
     94 plt.show()

/usr/local/lib/python2.7/dist-packages/matplotlib/animation.pyc in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim)
    613                     #TODO: Need to see if turning off blit is really necessary
    614                     anim._draw_next_frame(d, blit=False)
--> 615                 writer.grab_frame()
    616 
    617         # Reconnect signal for first draw if necessary

/usr/lib/python2.7/contextlib.pyc in __exit__(self, type, value, traceback)
     22         if type is None:
     23             try:
---> 24                 self.gen.next()
     25             except StopIteration:
     26                 return

/usr/local/lib/python2.7/dist-packages/matplotlib/animation.pyc in saving(self, *args)
    166         self.setup(*args)
    167         yield
--> 168         self.finish()
    169 
    170     def _run(self):

/usr/local/lib/python2.7/dist-packages/matplotlib/animation.pyc in finish(self)
    324             raise RuntimeError('Error creating movie, return code: '
    325                     + str(self._proc.returncode)
--> 326                     + ' Try running with --verbose-debug')
    327 
    328     def cleanup(self):

RuntimeError: Error creating movie, return code: 1 Try running with --verbose-debug

@moorepants
Copy link
Author

BTW, .mp4 works. Is this just because I don't have the correct codec set?

@moorepants
Copy link
Author

I've also tried to pass the --verbose-debug arg to ffmpeg, as suggest but this doesn't seem to work:

ani.save('double_pendulum.ogv', writer=animation.FFMpegFileWriter(), fps=15, extra_args=['--verbose-debug'])

@moorepants
Copy link
Author

This doesn't seem to work either:

ani.save('double_pendulum.ogv', writer=animation.FFMpegFileWriter(extra_args=['--verbose-debug']), fps=15)

@moorepants
Copy link
Author

This fails:

ani.save('double_pendulum.ogv', writer=animation.FFMpegFileWriter(), codec='libtheora', fps=15)

ffmpeg -codecs says I have encoding of libtheora support.

@moorepants
Copy link
Author

So, this used to work, at least in Matplollib 1.1.1 and whatever standard install of ffmpeg I had in that version of Ubuntu, see this notebook:

http://www.moorepants.info/blog/npendulum.html

@tacaswell
Copy link
Member

Have you tried this on master? There was some work on the argument order to ffmeg recently.

@dawehner
Copy link

I had a similar problem myself and figured out that it depends on the used DPI when you save. This is the list of working DPIs (checked range(50, 501, 1))

[50,
 54,
 59,
 61,
 66,
 73,
 78,
 85,
 89,
 92,
 96,
 103,
 108,
 115,
 120,
 122,
 127,
 131,
 134,
 138,
 141,
 145,
 150,
 157,
 164,
 169,
 173,
 176,
 180,
 183,
 187,
 192,
 199,
 206,
 211,
 215,
 218,
 222,
 225,
 229,
 234,
 241,
 248,
 253,
 257,
 260,
 264,
 267,
 271,
 276,
 283,
 290,
 295,
 299,
 302,
 306,
 309,
 313,
 318,
 325,
 332,
 337,
 344,
 348,
 351,
 355,
 360,
 362,
 367,
 374,
 379,
 381,
 386,
 390,
 393,
 397,
 404,
 405,
 409,
 412,
 416,
 423,
 428,
 432,
 435,
 439,
 440,
 442,
 446,
 447,
 451,
 454,
 458,
 461,
 465,
 470,
 474,
 477,
 481,
 484,
 488,
 489,
 493,
 496,
 500]

@tacaswell tacaswell added this to the v1.4.x milestone Jul 12, 2014
@tacaswell
Copy link
Member

@danielballan does pyav also do writing?

@danielballan
Copy link
Contributor

Yes, see for example how this is implemented in PIMS.

@amunizp
Copy link

amunizp commented Dec 11, 2014

Does not work on windows 7 using VERSION 1.3.1 of matplotlib.

Sample code

# -*- coding: utf-8 -*-
"""
Created on Thu Dec 11 11:52:11 2014

@author: amp
http://matplotlib.org/examples/index.html#examples-index

"""
# This example uses a MovieWriter directly to grab individual frames and
# write them to a file. This avoids any event loop integration, but has
# the advantage of working with even the Agg backend. This is not recommended
# for use in an interactive setting.
# -*- noplot -*-

import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as manimation

FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Movie Test', artist='Matplotlib',
        comment='Movie support!')
writer = FFMpegWriter(fps=15, metadata=metadata)

fig = plt.figure()
l, = plt.plot([], [], 'k-o')

plt.xlim(-5, 5)
plt.ylim(-5, 5)

x0,y0 = 0, 0

with writer.saving(fig, "writer_test2.ogv", 100):
    for i in range(100):
        x0 += 0.1 * np.random.randn()
        y0 += 0.1 * np.random.randn()
        l.set_data(x0, y0)
        writer.grab_frame()

@tacaswell
Copy link
Member

Can you please provide a traceback?

Does it work in 1.4.2? We do not support the 1.3.x series anymore and
there will be no 1.3.2.
On Thu Dec 11 2014 at 2:59:39 PM amunizp notifications@github.com wrote:

Does not work on windows 7 using VERSION 1.3.1 of matplotlib.

Sample code

-- coding: utf-8 --"""Created on Thu Dec 11 11:52:11 2014@author: amphttp://matplotlib.org/examples/index.html#examples-index"""# This example uses a MovieWriter directly to grab individual frames and# write them to a file. This avoids any event loop integration, but has# the advantage of working with even the Agg backend. This is not recommended# for use in an interactive setting.# -- noplot --

import numpy as npimport matplotlib
matplotlib.use("Agg")import matplotlib.pyplot as pltimport matplotlib.animation as manimation

FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Movie Test', artist='Matplotlib',
comment='Movie support!')
writer = FFMpegWriter(fps=15, metadata=metadata)

fig = plt.figure()
l, = plt.plot([], [], 'k-o')

plt.xlim(-5, 5)
plt.ylim(-5, 5)

x0,y0 = 0, 0
with writer.saving(fig, "writer_test2.ogv", 100):
for i in range(100):
x0 += 0.1 * np.random.randn()
y0 += 0.1 * np.random.randn()
l.set_data(x0, y0)
writer.grab_frame()


Reply to this email directly or view it on GitHub
#2764 (comment)
.

@amunizp
Copy link

amunizp commented Dec 11, 2014

I do not know what traceback is.
Maybe this? The same thing happens with ubuntu 14.10

/usr/lib/pymodules/python2.7/matplotlib/__init__.py:1173: UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

  warnings.warn(_use_error_msg)


Traceback (most recent call last):

  File "<ipython-input-1-526883dfa5ab>", line 1, in <module>
    runfile('/home/kamehouse/ownCloud/compartiendo/matplotlibanimation.py', wdir='/home/kamehouse/ownCloud/compartiendo')

  File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 586, in runfile
    execfile(filename, namespace)

  File "/home/kamehouse/ownCloud/compartiendo/matplotlibanimation.py", line 39, in <module>
    writer.grab_frame()

  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 204, in grab_frame
    dpi=self.dpi, **savefig_kwargs)

  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)

  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2220, in print_figure
    **kwargs)

  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 497, in print_raw
    renderer._renderer.write_rgba(filename_or_obj)

RuntimeError: Error writing to file

@amunizp
Copy link

amunizp commented Dec 11, 2014

I am using python 2.7 that is installed in spyder in both systems. Same version of matplotlib 1.3.1

OK, I will have to find out how to update matplotlib in both ubuntu and windows. Is there any chance that anything will break?

@benrbray
Copy link

The posted code from amunzip also fails for matplotlib 1.4.2, Python 3.3, Windows 7, 64-bit. Error trace:

Traceback (most recent call last):
  File "testplot.py", line 39, in <module>
    writer.grab_frame()
  File "c:\Python33\lib\site-packages\matplotlib\animation.py", line 210, in gra
b_frame
    dpi=self.dpi, **savefig_kwargs)
  File "c:\Python33\lib\site-packages\matplotlib\figure.py", line 1470, in savef
ig
    self.canvas.print_figure(*args, **kwargs)
  File "c:\Python33\lib\site-packages\matplotlib\backend_bases.py", line 2194, i
n print_figure
    **kwargs)
  File "c:\Python33\lib\site-packages\matplotlib\backends\backend_agg.py", line
513, in print_raw
    renderer._renderer.write_rgba(filename_or_obj)
RuntimeError: Error writing to file

@mattjj
Copy link

mattjj commented Jan 8, 2015

I got the same error as amunizp (also on mpl 1.4.2, Python 2.7.9, OS X, using miniconda with everything updated).

@tacaswell
Copy link
Member

@dopplershift Do any of these tracebacks make sense to you?

Something is clearly getting messed up with the pipe.

Other things that look troubling is that setup discards *args unused...

@mattjj
Copy link

mattjj commented Jan 8, 2015

This might not be helpful, but I put an ipdb session right before the call to write_rgba and it looks like the file object is fine:

ipdb> print filename_or_obj
<open file '<fdopen>', mode 'wb' at 0x10cdc0300>
ipdb> renderer._renderer.write_rgba(filename_or_obj)
*** RuntimeError: Error writing to file

Actually, it looks like my ffmpeg might be broken... I'll reinstall it.

@mattjj
Copy link

mattjj commented Jan 8, 2015

Reinstalling ffmpeg (using MacPorts) totally fixed it for me. I noticed it was busted because I tried ffmpeg -v in the shell, which I probably should have done earlier. I just upgraded to Yosemite, so maybe I had an old version or something.

Sorry about that!

@dopplershift
Copy link
Contributor

Glad you got it working. Please re-open if you have more problems.

@cancan101
Copy link
Contributor

I am still seeing this issue (on Linux). As pointed out in #2764 (comment), I think the issue has to do with some DPIs not working (although the acceptable values I get are different).

I am using v1.3.1 and I also see the issue in 1.4.3.

@dopplershift
Copy link
Contributor

Does it work if you change the writer creation line to:

writer = FFMpegWriter(fps=15, metadata=metadata, codec='libtheora')

?

@ylockerman
Copy link

I have had the same error reported. From what I can tell matplotlib starts a ffmpeg process and then writes to it's stdin through a file interface. What it seems like is happening is that ffmpeg sees a frame that is invalid according to the parameters given when it was called. At that time ffmpeg quits. However, the python code dose not seem to know until it tries to write to ffmpeg on the next frame. Thus the crash. This can happen for a large number of reasons, and I would recommend matplotlib include some way to figure out why it happened (if possible).

It seems like it was caused (for me) by setting the bitrate too high. If you want to get some more debugging information ffmpeg adding

matplotlib.rcParams['animation.ffmpeg_args'] = '-report'

which will cause ffmpeg to create a log file each time it is run. I found the maximum bitrate of my source images and setting the param:

matplotlib.rcParams['animation.bitrate'] = 2000

I hope this helps. (I am using '1.4.3' on windows with WinPython)

moorepants added a commit to moorepants/pydy that referenced this issue Jun 11, 2015
Fixes issue pydy#216.

- Removed the "bloggy" style text.
- Specified the dependencies.
- Included specific dependencies at the end of the notebook.
- Removed the ogv output, as Firefox now supports mp4 and matplotlib seems to
  fail on ogg: matplotlib/matplotlib#2764
- avconv is required as the writer.
- Changed to the linearizer introduced in SymPy 0.7.6.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants