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

Skip to content

Commit 788503f

Browse files
committed
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.
1 parent fecfa1a commit 788503f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/matplotlib/animation.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,9 @@ class FFMpegBase:
554554
@property
555555
def output_args(self):
556556
args = []
557-
if not Path(self.outfile).suffix == '.gif':
557+
if Path(self.outfile).suffix == '.gif':
558+
self.codec = 'gif'
559+
else:
558560
args.extend(['-vcodec', self.codec])
559561
extra_args = (self.extra_args if self.extra_args is not None
560562
else mpl.rcParams[self._args_key])
@@ -564,6 +566,11 @@ def output_args(self):
564566
# OSX). Also fixes internet explorer. This is as of 2015/10/29.
565567
if self.codec == 'h264' and '-pix_fmt' not in extra_args:
566568
args.extend(['-pix_fmt', 'yuv420p'])
569+
# For GIF, we're telling FFMPEG to split the video stream, to generate
570+
# a palette, and then use it for encoding.
571+
elif self.codec == 'gif' and '-filter_complex' not in extra_args:
572+
args.extend(['-filter_complex',
573+
'split [a][b];[a] palettegen [p];[b][p] paletteuse'])
567574
if self.bitrate > 0:
568575
args.extend(['-b', '%dk' % self.bitrate]) # %dk: bitrate in kbps.
569576
args.extend(extra_args)

0 commit comments

Comments
 (0)