[Matplotlib-users] errors when mp4 files played in browser

Ryan May rmay31 at gmail.com
Fri Apr 22 17:28:09 EDT 2016

On Fri, Apr 22, 2016 at 7:55 AM, <gml at garymlewis.com> wrote:

> Thanks Ryan. Very cool nbviewer link you sent me. I use a Jupyter notebook
> for pretty much all my python development. Very happy with it. ... Anyway,
> to answer your question. Yes, the movie at the bottom of the notebook works
> fine for me. Does that imply my issue is not the browser? I see you set
> matplotlib.rcParams['animation.html'] = 'html5'. My default was set to
> 'none'. Changing it to 'html5' did not seem to change anything, however.
> When I open the animation mp4 in Firefox, it immediately says the file is
> corrupt.

The default is set to none so that nothing breaks by default--as you've
seen, making movies can be a bit touchy. :)

Changing the 'animation.html' setting only changes if (and how) animations
are converted to html for the jupyter notebook; it has no impact on how
things are saved to mp4. The fact that the movies in the link work, though,
mean that it is indeed possible to make matplotlib's animation support
write mp4 files that work on those platforms. I note the following from our
code that makes html5 videos:

        # For h264, the default format is yuv444p, which is not compatible
        # with quicktime (and others). Specifying yuv420p fixes playback on
        # iOS,as well as HTML5 video in firefox and safari (on both Win and
        # OSX). Also fixes internet explorer. This is as of 2015/10/29.
        if self.codec == 'h264' and '-pix_fmt' not in self.extra_args:
            args.extend(['-pix_fmt', 'yuv420p'])

So I'd try passing these to save:

anim.save('file.mp4', extra_args=['-pix_fmt', 'yuv420p'])

> I do have one additional item to add that I discovered almost by accident
> today. I recreated the animation (using a jupyter notebook, as always), but
> this time I had the linux terminal open where I had started the jupyter
> notebook. Nothing unusual until the animation completed and I used Firefox
> to open the mp4 file. Once I did this, I got the following error at the
> terminal:
> [h264 ........] decode-slice-header-error and 'no frame!'
> I am a video novice, but a little search on the error indicated that a
> keyframe might be missing. Again, this points to user error rather than
> browser error.

It could be related--I honestly have no clue. I google around finding
various solutions, trying stuff until it works. The pix_fmt stuff was
enough to get it to work for me.

Give it a spin and let me know!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20160422/8f08b1f2/attachment.html>

More information about the Matplotlib-users mailing list