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

Skip to content

Commit 7924451

Browse files
committed
Merge pull request #1536 from jakevdp/anim_avconv
ENH: add AVConv movie writer for animations
2 parents eba5ffd + c9f5926 commit 7924451

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

lib/matplotlib/animation.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def list(self):
5353
''' Get a list of available MovieWriters.'''
5454
return self.avail.keys()
5555

56+
def is_available(self, name):
57+
return name in self.avail
58+
5659
def __getitem__(self, name):
5760
if not self.avail:
5861
raise RuntimeError("No MovieWriters available!")
@@ -390,6 +393,24 @@ def _args(self):
390393
self._base_temp_name()] + self.output_args
391394

392395

396+
# Base class of avconv information. AVConv has identical arguments to
397+
# FFMpeg
398+
class AVConvBase(FFMpegBase):
399+
exec_key = 'animation.avconv_path'
400+
args_key = 'animation.avconv_args'
401+
402+
403+
# Combine AVConv options with pipe-based writing
404+
@writers.register('avconv')
405+
class AVConvWriter(AVConvBase, FFMpegWriter):
406+
pass
407+
408+
# Combine AVConv options with file-based writing
409+
@writers.register('avconv_file')
410+
class AVConvFileWriter(AVConvBase, FFMpegFileWriter):
411+
pass
412+
413+
393414
# Base class of mencoder information. Contains configuration key information
394415
# as well as arguments for controlling *output*
395416
class MencoderBase:

lib/matplotlib/rcsetup.py

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

338338
validate_movie_writer = ValidateInStrings('animation.writer',
339-
['ffmpeg', 'ffmpeg_file', 'mencoder', 'mencoder_file',
339+
['ffmpeg', 'ffmpeg_file',
340+
'avconv', 'avconv_file',
341+
'mencoder', 'mencoder_file',
340342
'imagemagick', 'imagemagick_file'])
341343

342344
validate_movie_frame_fmt = ValidateInStrings('animation.frame_format',
@@ -636,7 +638,9 @@ def __call__(self, s):
636638
'animation.frame_format' : ['png', validate_movie_frame_fmt], # Controls image format when frames are written to disk
637639
'animation.ffmpeg_path' : ['ffmpeg', str], # Path to FFMPEG binary. If just binary name, subprocess uses $PATH.
638640
'animation.ffmpeg_args' : ['', validate_stringlist], # Additional arguments for ffmpeg movie writer (using pipes)
639-
'animation.mencoder_path' : ['mencoder', str], # Path to FFMPEG binary. If just binary name, subprocess uses $PATH.
641+
'animation.avconv_path' : ['avconv', str], # Path to AVConv binary. If just binary name, subprocess uses $PATH.
642+
'animation.avconv_args' : ['', validate_stringlist], # Additional arguments for avconv movie writer (using pipes)
643+
'animation.mencoder_path' : ['mencoder', str], # Path to MENCODER binary. If just binary name, subprocess uses $PATH.
640644
'animation.mencoder_args' : ['', validate_stringlist], # Additional arguments for mencoder movie writer (using pipes)
641645
'animation.convert_path' : ['convert', str], # Path to convert binary. If just binary name, subprocess uses $PATH
642646
'animation.convert_args' : ['', validate_stringlist], # Additional arguments for mencoder movie writer (using pipes)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
import tempfile
3+
import numpy as np
4+
from matplotlib import pyplot as plt
5+
from matplotlib import animation
6+
from matplotlib.testing.noseclasses import KnownFailureTest
7+
8+
9+
WRITER_OUTPUT = dict(ffmpeg='mp4', ffmpeg_file='mp4',
10+
mencoder='mp4', mencoder_file='mp4',
11+
avconv='mp4', avconv_file='mp4',
12+
imagemagick='gif', imagemagick_file='gif')
13+
14+
15+
16+
# Smoke test for saving animations. In the future, we should probably
17+
# design more sophisticated tests which compare resulting frames a-la
18+
# matplotlib.testing.image_comparison
19+
def test_save_animation_smoketest():
20+
for writer, extension in WRITER_OUTPUT.iteritems():
21+
yield check_save_animation, writer, extension
22+
23+
24+
def check_save_animation(writer, extension='mp4'):
25+
if not animation.writers.is_available(writer):
26+
raise KnownFailureTest("writer '%s' not available on this system"
27+
% writer)
28+
fig, ax = plt.subplots()
29+
line, = ax.plot([], [])
30+
31+
def init():
32+
line.set_data([], [])
33+
return line,
34+
35+
def animate(i):
36+
x = np.linspace(0, 10, 100)
37+
y = np.sin(x + i)
38+
line.set_data(x, y)
39+
return line,
40+
41+
# Use NamedTemporaryFile: will be automatically deleted
42+
F = tempfile.NamedTemporaryFile(suffix='.' + extension)
43+
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5)
44+
anim.save(F.name, fps=30, writer=writer)
45+
46+
47+
if __name__ == '__main__':
48+
import nose
49+
nose.runmodule()

matplotlibrc.template

Lines changed: 7 additions & 3 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 arguments to pass to ffmpeg
438+
#animation.avconv_path: 'avconv' # Path to avconv binary. Without full path
439439
# $PATH is searched
440-
#animation.mencoder_args: '' # Additional arugments to pass to mencoder
440+
#animation.avconv_args: '' # Additional arguments to pass to avconv
441+
#animation.mencoder_path: 'mencoder'
442+
# Path to mencoder binary. Without full path
443+
# $PATH is searched
444+
#animation.mencoder_args: '' # Additional arguments to pass to mencoder

0 commit comments

Comments
 (0)