61
61
# how-to-encode-series-of-images-into-h264-using-x264-api-c-c )
62
62
63
63
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
-
70
64
# A registry for available MovieWriter classes
71
65
class MovieWriterRegistry (object ):
72
66
'''Registry of available writer classes by human readable name.'''
@@ -269,18 +263,6 @@ def frame_size(self):
269
263
w , h = self .fig .get_size_inches ()
270
264
return int (w * self .dpi ), int (h * self .dpi )
271
265
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
-
284
266
def setup (self , fig , outfile , dpi = None ):
285
267
'''
286
268
Perform setup for writing the movie file.
@@ -301,7 +283,6 @@ def setup(self, fig, outfile, dpi=None):
301
283
if dpi is None :
302
284
dpi = self .fig .dpi
303
285
self .dpi = dpi
304
- self ._adjust_frame_size ()
305
286
306
287
# Run here so that grab_frame() can write the data to a pipe. This
307
288
# eliminates the need for temp files.
@@ -434,7 +415,6 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
434
415
if dpi is None :
435
416
dpi = self .fig .dpi
436
417
self .dpi = dpi
437
- self ._adjust_frame_size ()
438
418
439
419
self .clear_temp = clear_temp
440
420
self .temp_prefix = frame_prefix
@@ -557,8 +537,14 @@ def output_args(self):
557
537
# with quicktime (and others). Specifying yuv420p fixes playback on
558
538
# iOS,as well as HTML5 video in firefox and safari (on both Win and
559
539
# 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' ])
562
548
# The %dk adds 'k' as a suffix so that ffmpeg treats our bitrate as in
563
549
# kbps
564
550
if self .bitrate > 0 :
0 commit comments