Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve saving animated GIF with ffmpeg #18093

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
Jul 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ def __init__(self, fps=5, codec=None, bitrate=None, extra_args=None,
'MovieWriter cannot be instantiated directly. Please use one '
'of its subclasses.')

super().__init__(fps=fps, metadata=metadata)
super().__init__(fps=fps, metadata=metadata, codec=codec,
bitrate=bitrate)

self.frame_format = 'rgba'
self.extra_args = extra_args
Expand Down Expand Up @@ -554,7 +555,9 @@ class FFMpegBase:
@property
def output_args(self):
args = []
if not Path(self.outfile).suffix == '.gif':
if Path(self.outfile).suffix == '.gif':
self.codec = 'gif'
else:
args.extend(['-vcodec', self.codec])
extra_args = (self.extra_args if self.extra_args is not None
else mpl.rcParams[self._args_key])
Expand All @@ -564,6 +567,11 @@ def output_args(self):
# OSX). Also fixes internet explorer. This is as of 2015/10/29.
if self.codec == 'h264' and '-pix_fmt' not in extra_args:
args.extend(['-pix_fmt', 'yuv420p'])
# For GIF, we're telling FFMPEG to split the video stream, to generate
# a palette, and then use it for encoding.
elif self.codec == 'gif' and '-filter_complex' not in extra_args:
args.extend(['-filter_complex',
'split [a][b];[a] palettegen [p];[b][p] paletteuse'])
if self.bitrate > 0:
args.extend(['-b', '%dk' % self.bitrate]) # %dk: bitrate in kbps.
args.extend(extra_args)
Expand Down