From 788503f1e44e567141b8ebecece5f7dec45349fa Mon Sep 17 00:00:00 2001 From: Ryan May Date: Tue, 28 Jul 2020 00:46:09 -0600 Subject: [PATCH 1/2] ENH: Improve saving animations in GIF from ffmpeg The default GIF palette from ffmpeg looks terrible with our plots. With some ffmpeg magic, we can have it autogenerate a palette from the frames of the animation. --- lib/matplotlib/animation.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index b05107f77519..65ec17d9bc6e 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -554,7 +554,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]) @@ -564,6 +566,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) From 1c02381ae45926360dad30fd9e6f854dcbfd55a2 Mon Sep 17 00:00:00 2001 From: Ryan May Date: Tue, 28 Jul 2020 01:12:23 -0600 Subject: [PATCH 2/2] BUG: Fix passing of parameters from save to MovieWriter Missed forwarding a couple of parameters when `AbstractMovieWriter` gained an `__init__`. --- lib/matplotlib/animation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index 65ec17d9bc6e..abd752c02cf9 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -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