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

Skip to content

Commit 0b6e803

Browse files
committed
ENH: add avconv movie writer for animations
1 parent de724f1 commit 0b6e803

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

lib/matplotlib/animation.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,57 @@ def _args(self):
390390
self._base_temp_name()] + self.output_args
391391

392392

393+
# Base class of avconv information. Has the config keys and the common set
394+
# of arguments that controls the *output* side of things.
395+
class AVConvBase:
396+
exec_key = 'animation.avconv_path'
397+
args_key = 'animation.avconv_args'
398+
399+
@property
400+
def output_args(self):
401+
# The %dk adds 'k' as a suffix so that avconv treats our bitrate as in
402+
# kbps
403+
args = ['-vcodec', self.codec]
404+
if self.bitrate > 0:
405+
args.extend(['-b', '%dk' % self.bitrate])
406+
if self.extra_args:
407+
args.extend(self.extra_args)
408+
for k, v in self.metadata.items():
409+
args.extend(['-metadata', '%s=%s' % (k, v)])
410+
411+
return args + ['-y', self.outfile]
412+
413+
414+
# Combine AVConv options with pipe-based writing
415+
@writers.register('avconv')
416+
class AVConvWriter(MovieWriter, AVConvBase):
417+
def _args(self):
418+
# Returns the command line parameters for subprocess to use
419+
# avconv to create a movie using a pipe.
420+
args = [self.bin_path(), '-f', 'rawvideo', '-vcodec', 'rawvideo',
421+
'-s', '%dx%d' % self.frame_size, '-pix_fmt', self.frame_format,
422+
'-r', str(self.fps)]
423+
# Logging is quieted because subprocess.PIPE has limited buffer size.
424+
if not verbose.ge('debug'):
425+
args += ['-loglevel', 'quiet']
426+
args += ['-i', 'pipe:'] + self.output_args
427+
return args
428+
429+
430+
#Combine AVConv options with temp file-based writing
431+
@writers.register('avconv_file')
432+
class AVConvFileWriter(FileMovieWriter, AVConvBase):
433+
supported_formats = ['png', 'jpeg', 'ppm', 'tiff', 'sgi', 'bmp',
434+
'pbm', 'raw', 'rgba']
435+
436+
def _args(self):
437+
# Returns the command line parameters for subprocess to use
438+
# avconv to create a movie using a collection of temp images
439+
return [self.bin_path(), '-vframes', str(self._frame_counter),
440+
'-r', str(self.fps), '-i',
441+
self._base_temp_name()] + self.output_args
442+
443+
393444
# Base class of mencoder information. Contains configuration key information
394445
# as well as arguments for controlling *output*
395446
class MencoderBase:

lib/matplotlib/rcsetup.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ def validate_hinting(s):
327327
['xelatex', 'lualatex', 'pdflatex'])
328328

329329
validate_movie_writer = ValidateInStrings('animation.writer',
330-
['ffmpeg', 'ffmpeg_file', 'mencoder', 'mencoder_file',
330+
['ffmpeg', 'ffmpeg_file',
331+
'avconv', 'avconv_file',
332+
'mencoder', 'mencoder_file',
331333
'imagemagick', 'imagemagick_file'])
332334

333335
validate_movie_frame_fmt = ValidateInStrings('animation.frame_format',
@@ -627,7 +629,9 @@ def __call__(self, s):
627629
'animation.frame_format' : ['png', validate_movie_frame_fmt], # Controls image format when frames are written to disk
628630
'animation.ffmpeg_path' : ['ffmpeg', str], # Path to FFMPEG binary. If just binary name, subprocess uses $PATH.
629631
'animation.ffmpeg_args' : ['', validate_stringlist], # Additional arguments for ffmpeg movie writer (using pipes)
630-
'animation.mencoder_path' : ['mencoder', str], # Path to FFMPEG binary. If just binary name, subprocess uses $PATH.
632+
'animation.avconv_path' : ['avconv', str], # Path to AVConv binary. If just binary name, subprocess uses $PATH.
633+
'animation.avconv_args' : ['', validate_stringlist], # Additional arguments for avconv movie writer (using pipes)
634+
'animation.mencoder_path' : ['mencoder', str], # Path to MENCODER binary. If just binary name, subprocess uses $PATH.
631635
'animation.mencoder_args' : ['', validate_stringlist], # Additional arguments for mencoder movie writer (using pipes)
632636
'animation.convert_path' : ['convert', str], # Path to convert binary. If just binary name, subprocess uses $PATH
633637
'animation.convert_args' : ['', validate_stringlist], # Additional arguments for mencoder movie writer (using pipes)

matplotlibrc.template

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,11 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
434434
#animation.frame_format: 'png' # Controls frame format used by temp files
435435
#animation.ffmpeg_path: 'ffmpeg' # Path to ffmpeg binary. Without full path
436436
# $PATH is searched
437-
#animation.ffmpeg_args: '' # Additional arugments to pass to mencoder
438-
#animation.mencoder_path: 'ffmpeg' # Path to mencoder binary. Without full path
437+
#animation.ffmpeg_args: '' # Additional arugments to pass to ffmpeg
438+
#animation.avconv_path: 'avconv' # Path to ffmpeg binary. Without full path
439+
# $PATH is searched
440+
#animation.avconv_args: '' # Additional arugments to pass to ffmpeg
441+
#animation.mencoder_path: 'mencoder'
442+
# Path to mencoder binary. Without full path
439443
# $PATH is searched
440444
#animation.mencoder_args: '' # Additional arugments to pass to mencoder

0 commit comments

Comments
 (0)