Skip to content

Commit e0c212b

Browse files
author
Nathan Goldbaum
committed
Let ffmpeg handle image rescaling for h264
1 parent 3abe6e8 commit e0c212b

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

lib/matplotlib/animation.py

+8-22
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@
6161
# how-to-encode-series-of-images-into-h264-using-x264-api-c-c )
6262

6363

64-
def adjusted_figsize(w, h, dpi, n):
65-
wnew = int(w * dpi / n) * n / dpi
66-
hnew = int(h * dpi / n) * n / dpi
67-
return wnew, hnew
68-
69-
7064
# A registry for available MovieWriter classes
7165
class MovieWriterRegistry(object):
7266
'''Registry of available writer classes by human readable name.'''
@@ -269,18 +263,6 @@ def frame_size(self):
269263
w, h = self.fig.get_size_inches()
270264
return int(w * self.dpi), int(h * self.dpi)
271265

272-
def _adjust_frame_size(self):
273-
if self.codec == 'h264':
274-
wo, ho = self.fig.get_size_inches()
275-
w, h = adjusted_figsize(wo, ho, self.dpi, 2)
276-
if not (wo, ho) == (w, h):
277-
self.fig.set_size_inches(w, h, forward=True)
278-
verbose.report('figure size (inches) has been adjusted '
279-
'from %s x %s to %s x %s' % (wo, ho, w, h),
280-
level='helpful')
281-
verbose.report('frame size in pixels is %s x %s' % self.frame_size,
282-
level='debug')
283-
284266
def setup(self, fig, outfile, dpi=None):
285267
'''
286268
Perform setup for writing the movie file.
@@ -301,7 +283,6 @@ def setup(self, fig, outfile, dpi=None):
301283
if dpi is None:
302284
dpi = self.fig.dpi
303285
self.dpi = dpi
304-
self._adjust_frame_size()
305286

306287
# Run here so that grab_frame() can write the data to a pipe. This
307288
# eliminates the need for temp files.
@@ -434,7 +415,6 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
434415
if dpi is None:
435416
dpi = self.fig.dpi
436417
self.dpi = dpi
437-
self._adjust_frame_size()
438418

439419
self.clear_temp = clear_temp
440420
self.temp_prefix = frame_prefix
@@ -557,8 +537,14 @@ def output_args(self):
557537
# with quicktime (and others). Specifying yuv420p fixes playback on
558538
# iOS,as well as HTML5 video in firefox and safari (on both Win and
559539
# OSX). Also fixes internet explorer. This is as of 2015/10/29.
560-
if self.codec == 'h264' and '-pix_fmt' not in self.extra_args:
561-
args.extend(['-pix_fmt', 'yuv420p'])
540+
if self.codec == 'h264':
541+
if '-pix_fmt' not in self.extra_args:
542+
args.extend(['-pix_fmt', 'yuv420p'])
543+
# The h264 codec requires that frames passed to it must have a
544+
# a width and height that are even numbers of pixels. This tells
545+
# ffmpeg to rescale the frames to the nearest even number of pixels
546+
# see https://stackoverflow.com/a/20848224/1382869
547+
args.extend(['-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2'])
562548
# The %dk adds 'k' as a suffix so that ffmpeg treats our bitrate as in
563549
# kbps
564550
if self.bitrate > 0:

0 commit comments

Comments
 (0)