errors when mp4 files played in browser

I've been using matplotlib's animation capability to produce mp4 files.
No problems when the mp4 is viewed in a video player. But viewing the
files in a browser is another story. With Ubuntu 15.10 and Firefox
45.0.2, for example, I get a message "Video can't be played because the
file is corrupt."

The problem is easily reproducible with several of the animation
examples on matplotlib; eg, see:
http://matplotlib.org/examples/animation/basic_example_writer.html. This
produces two mp4 files: lines.mp4 (created with FuncAnimation) and
im.mp4 (created with ArtistAnimation). Neither mp4 opens in Firefox for
me. I also get the same result when using an animation saved with
extra_args=['-vcodec', 'libx264'] that Jake Vanderplas recommends.

My wife gets the same result on her Mac when using Firefox and Safari,
but Chrome works fine.

From a web search, I'd say the problem is browser related. For more
details, see:
https://bugzilla.mozilla.org/show_bug.cgi?id=987894
or
https://support.mozilla.org/en-US/questions/1047078

So I'm not expecting the folks at matplotlib to have a solution for a
browser issue. But I'd really like to make a few animations available on
the web. That seems to imply the use of a web browser to view the files.
Certainly that would be the most natural action; just click on the link
to view the animation.

My question then comes to this: does anyone on the matplotlib forum have
experience with viewing mp4 files in a browser? If so, do you have any
suggestions for a work-around? I've literally spent months getting a few
matplotlib animations to the point where they may be useful to others.
It never occurred to me that I'd have problems at the very last link in
the chain.

Thanks very much for your thoughts. Sorry this is so long.

Gary

My question then comes to this: does anyone on the matplotlib forum have
experience with viewing mp4 files in a browser? If so, do you have any
suggestions for a work-around? I've literally spent months getting a few
matplotlib animations to the point where they may be useful to others. It
never occurred to me that I'd have problems at the very last link in the
chain.

Thanks very much for your thoughts. Sorry this is so long.

I've put some effort into making sure the animations will render in the
Jupyter Notebook, so I'm curious: on the platforms where you're seeing
problems, what do they do with the movie at the bottom of this notebook

?

Ryan

···

--
Ryan May
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20160421/cff60683/attachment.html&gt;

I had similar problems recently due to ffmpeg changing some defaults
for the encoding. After realizing this was the problem, searching for
it and adding the correct extra arguments for the movie creation to be
passed to ffmpeg quickly solved the issue.

- Sebastian

> My question then comes to this: does anyone on the matplotlib forum
> have experience with viewing mp4 files in a browser? If so, do you
> have any suggestions for a work-around? I've literally spent months
> getting a few matplotlib animations to the point where they may be
> useful to others. It never occurred to me that I'd have problems at
> the very last link in the chain.
>
> Thanks very much for your thoughts. Sorry this is so long.
I've put some effort into making sure the animations will render in
the Jupyter Notebook, so I'm curious: on the platforms where you're
seeing problems, what do they do with the movie at the bottom of this
notebook

http://nbviewer.jupyter.org/gist/dopplershift/356f2e14832e9b676207

?

Ryan

--
Ryan May

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20160422/1493592b/attachment.sig&gt;

···

On Do, 2016-04-21 at 22:45 -0600, Ryan May 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.

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.

Any thoughts?

Gary

···

On 2016-04-22 00:45, Ryan May wrote:

My question then comes to this: does anyone on the matplotlib forum
have experience with viewing mp4 files in a browser? If so, do you
have any suggestions for a work-around? I've literally spent months
getting a few matplotlib animations to the point where they may be
useful to others. It never occurred to me that I'd have problems at
the very last link in the chain.

Thanks very much for your thoughts. Sorry this is so long.

I've put some effort into making sure the animations will render in
the Jupyter Notebook, so I'm curious: on the platforms where you're
seeing problems, what do they do with the movie at the bottom of this
notebook

http://nbviewer.jupyter.org/gist/dopplershift/356f2e14832e9b676207 [1]

?

Ryan

--

Ryan May

Links:
------
[1] http://nbviewer.jupyter.org/gist/dopplershift/356f2e14832e9b676207

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. :slight_smile:

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!

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

···

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

Sebastian,

And what *are* those correct extra arguments?

Eric

···

On 2016/04/21 8:33 PM, Sebastian Berg wrote:

I had similar problems recently due to ffmpeg changing some defaults
for the encoding. After realizing this was the problem, searching for
it and adding the correct extra arguments for the movie creation to be
passed to ffmpeg quickly solved the issue.

- Sebastian

Thanks, Sebastian. That sounds hopeful. What extra arguments did you
add? I'd like to give it a try to see if it fixes the problem I'm
having. ... Gary

···

On 2016-04-22 02:33, Sebastian Berg wrote:

I had similar problems recently due to ffmpeg changing some defaults
for the encoding. After realizing this was the problem, searching for
it and adding the correct extra arguments for the movie creation to be
passed to ffmpeg quickly solved the issue.

- Sebastian

On Do, 2016-04-21 at 22:45 -0600, Ryan May wrote:

> My question then comes to this: does anyone on the matplotlib forum
> have experience with viewing mp4 files in a browser? If so, do you
> have any suggestions for a work-around? I've literally spent months
> getting a few matplotlib animations to the point where they may be
> useful to others. It never occurred to me that I'd have problems at
> the very last link in the chain.
>
> Thanks very much for your thoughts. Sorry this is so long.
I've put some effort into making sure the animations will render in
the Jupyter Notebook, so I'm curious: on the platforms where you're
seeing problems, what do they do with the movie at the bottom of this
notebook

http://nbviewer.jupyter.org/gist/dopplershift/356f2e14832e9b676207

?

Ryan

--
Ryan May

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

Ryan May-3 wrote

So I'd try passing these to save:

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

Thanks very much, Ryan. That works!
The full extra_args I used are:
anim.save('file.mp4', extra_args=['-vcodec', 'libx264', '-pix_fmt',
'yuv420p'])

Please consider this problem solved.

Gary

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/errors-when-mp4-files-played-in-browser-tp46988p47031.html
Sent from the matplotlib - users mailing list archive at Nabble.com.