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

Skip to content

Commit 6f669dd

Browse files
authored
Merge pull request #18109 from meeseeksmachine/auto-backport-of-pr-18093-on-v3.3.x
Backport PR #18093 on branch v3.3.x (Improve saving animated GIF with ffmpeg)
2 parents 5d166a5 + c8d1f6e commit 6f669dd

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/matplotlib/animation.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ def __init__(self, fps=5, codec=None, bitrate=None, extra_args=None,
310310
'MovieWriter cannot be instantiated directly. Please use one '
311311
'of its subclasses.')
312312

313-
super().__init__(fps=fps, metadata=metadata)
313+
super().__init__(fps=fps, metadata=metadata, codec=codec,
314+
bitrate=bitrate)
314315

315316
self.frame_format = 'rgba'
316317
self.extra_args = extra_args
@@ -571,7 +572,9 @@ class FFMpegBase:
571572
@property
572573
def output_args(self):
573574
args = []
574-
if not Path(self.outfile).suffix == '.gif':
575+
if Path(self.outfile).suffix == '.gif':
576+
self.codec = 'gif'
577+
else:
575578
args.extend(['-vcodec', self.codec])
576579
extra_args = (self.extra_args if self.extra_args is not None
577580
else mpl.rcParams[self._args_key])
@@ -581,6 +584,11 @@ def output_args(self):
581584
# OSX). Also fixes internet explorer. This is as of 2015/10/29.
582585
if self.codec == 'h264' and '-pix_fmt' not in extra_args:
583586
args.extend(['-pix_fmt', 'yuv420p'])
587+
# For GIF, we're telling FFMPEG to split the video stream, to generate
588+
# a palette, and then use it for encoding.
589+
elif self.codec == 'gif' and '-filter_complex' not in extra_args:
590+
args.extend(['-filter_complex',
591+
'split [a][b];[a] palettegen [p];[b][p] paletteuse'])
584592
if self.bitrate > 0:
585593
args.extend(['-b', '%dk' % self.bitrate]) # %dk: bitrate in kbps.
586594
args.extend(extra_args)

0 commit comments

Comments
 (0)