Skip to content

FIX: Fix for FFmpeg + GIF #17401

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

Merged
merged 2 commits into from
May 15, 2020
Merged

FIX: Fix for FFmpeg + GIF #17401

merged 2 commits into from
May 15, 2020

Conversation

larsoner
Copy link
Contributor

Working on making a GIF thumbnail for sphinx-gallery/sphinx-gallery#150 I stumbled across this bug writing GIF with FFmpeg:

Minimal example
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation


def _update_line(num):
    line.set_data(data[..., :num])
    return line,


fig, ax = plt.subplots()
data = np.random.RandomState(0).rand(2, 25)
line, = ax.plot([], [], 'r-')
ax.set(xlim=(0, 1), ylim=(0, 1))
ani = animation.FuncAnimation(fig, _update_line, 25, interval=100, blit=True)
ani.save('test.gif', 'ffmpeg')

Produces:

$ python -ui anim.py 
MovieWriter stderr:
[gif @ 0x555affbb0580] GIF muxer supports only a single video GIF stream.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Error initializing output stream 0:0 -- 

Traceback (most recent call last):
...
  File "/home/larsoner/python/matplotlib/lib/matplotlib/backends/backend_agg.py", line 429, in print_raw
    fh.write(renderer.buffer_rgba())
BrokenPipeError: [Errno 32] Broken pipe

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
...
  File "/home/larsoner/python/matplotlib/lib/matplotlib/animation.py", line 397, in cleanup
    raise subprocess.CalledProcessError(
subprocess.CalledProcessError: Command '['ffmpeg', '-f', 'rawvideo', '-vcodec', 'rawvideo', '-s', '640x480', '-pix_fmt', 'rgba', '-r', '10.0', '-loglevel', 'error', '-i', 'pipe:', '-vcodec', 'h264', '-pix_fmt', 'yuv420p', '-y', 'test.gif']' returned non-zero exit status 1.
>>> exit()

Basically the problem seems to be that you shouldn't pass -vcodec when using GIF. This PR works around the problem by not adding those arguments when the outputfile ends with .gif. Not the most elegant solution, but it seems to work.

I can try to add some simple variant of this as a test if this does seem like the right fix.

@tacaswell tacaswell added this to the v3.3.0 milestone May 14, 2020
Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 in principle, but need to fix the tests (I have proposed a patch).

Co-authored-by: Thomas A Caswell <tcaswell@gmail.com>
@tacaswell
Copy link
Member

I'm going to merge over the failure to downolad jquery-ui on azure.

Thanks @larsoner !

@tacaswell tacaswell merged commit 60fd88f into matplotlib:master May 15, 2020
@larsoner larsoner deleted the ffmpeg branch May 15, 2020 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants