lib/imageio/plugins/ffmpeg.py
def _initialize(self):
            """ Creates the pipe to ffmpeg. Open the file to write to. """

            # Get parameters
            # Note that H264 is a widespread and very good codec, but if we
            # do not specify a bitrate, we easily get crap results.
            sizestr = "%dx%d" % (self._size[1], self._size[0])
            fps = self.request.kwargs.get('fps', 10)
            default_codec = 'libx264'
            if self._filename.lower().endswith('.wmv'):
                # This is a safer default codec on windows to get videos that
                # will play in powerpoint and other apps. H264 is not always
                # available on windows.
                default_codec = 'msmpeg4'
            codec = self.request.kwargs.get('codec', default_codec)
            bitrate = self.request.kwargs.get('bitrate', None)
            quality = self.request.kwargs.get('quality', 5)
            ffmpeg_log_level = self.request.kwargs.get('ffmpeg_log_level',
                                                       'warning')
            extra_ffmpeg_params = self.request.kwargs.get('ffmpeg_params', [])
            # You may need to use -pix_fmt yuv420p for your output to work in
            # QuickTime and most other players. These players only supports
            # the YUV planar color space with 4:2:0 chroma subsampling for
            # H.264 video. Otherwise, depending on your source, ffmpeg may
            # output to a pixel format that may be incompatible with these
            # players. See
            # https://trac.ffmpeg.org/wiki/Encode/H.264#Encodingfordumbplayers
            pixelformat = self.request.kwargs.get('pixelformat', 'yuv420p')

            # Get command
            cmd = [self._exe, '-y',
                   "-f", 'rawvideo',
                   "-vcodec", "rawvideo",
                   '-s', sizestr,
                   '-pix_fmt', self._pix_fmt,
                   '-r', "%.02f" % fps,
                   '-i', '-', '-an',
                   '-vcodec', codec,
                   '-pix_fmt', pixelformat,
                   ]
            # Add fixed bitrate or variable bitrate compression flags
            if bitrate is not None:
                cmd += ['-b:v', str(bitrate)]
            elif quality is not None:  # If None, then we don't add anything
                if quality < 0 or quality > 10:
                    raise ValueError("ffpmeg writer quality parameter out of"
                                     "range. Expected range 0 to 10.")
                quality = 1 - quality / 10.0
                if codec == "libx264":
                    # crf ranges 0 to 51, 51 being worst.
                    quality = int(quality * 51)
                    cmd += ['-crf', str(quality)]  # for h264
                else:  # Many codecs accept q:v
                    # q:v range can vary, 1-31, 31 being worst
                    # But q:v does not always have the same range.
                    # May need a way to find range for any codec.
                    quality = int(quality*30)+1
                    cmd += ['-qscale:v', str(quality)]  # for others
            cmd += ['-r', "%d" % fps]

            # Note, for most codecs, the image dimensions must be divisible by
            # 16 the default for the macro_block_size is 16. Check if image is
            # divisible, if not have ffmpeg upsize to nearest size and warn
            # user they should correct input image if this is not desired.
            macro_block_size = self.request.kwargs.get('macro_block_size', 16)
            if macro_block_size is not None and macro_block_size > 1 and \
                    (self._size[1] % macro_block_size > 0 or
                        self._size[0] % macro_block_size > 0):
                out_w = self._size[1]
                if self._size[1] % macro_block_size > 0:
                    out_w += macro_block_size - \
                        (self._size[1] % macro_block_size)
                out_h = self._size[0]
                if self._size[0] % macro_block_size > 0:
                    out_h += macro_block_size - \
                        (self._size[0] % macro_block_size)
                cmd += ['-vf', 'scale={0}:{1}'.format(out_w, out_h)]
                logging.warning(
                    "IMAGEIO FFMPEG_WRITER WARNING: input image is not"
                    " divisible by macro_block_size={0}, resizing from {1} "
                    "to {2} to ensure video compatibility with most codecs "
                    "and players. To prevent resizing, make your input "
                    "image divisible by the macro_block_size or set the "
                    "macro_block_size to None (risking incompatibility). You "
                    "may also see a FFMPEG warning concerning "
                    "speedloss due to "
                    "data not being aligned.".format(macro_block_size,
                                                     self._size[:2],
                                                     (out_h, out_w)))

            if ffmpeg_log_level:
                # Rather than redirect stderr to a pipe, just set minimal
                # output from ffmpeg by default. That way if there are warnings
                # the user will see them.
                cmd += ['-v', ffmpeg_log_level]
            cmd += extra_ffmpeg_params
            cmd.append(self._filename)
            self._cmd = " ".join(cmd)  # For showing command if needed
            if ('warning' not in ffmpeg_log_level) and \
                    ('quiet' not in ffmpeg_log_level):
                print("RUNNING FFMPEG COMMAND:", self._cmd, file=sys.stderr)

            # Launch process
            self._proc = sp.Popen(cmd, stdin=sp.PIPE,
                                  stdout=sp.PIPE, stderr=None)
            # Warning, directing stderr to a pipe on windows will cause ffmpeg
            # to hang if the buffer is not periodically cleared using
            # StreamCatcher or other means.
 Creates the pipe to ffmpeg. Open the file to write to. 
Creates the pipe to ffmpeg. Open the file to write to.
Creates the pipe to ffmpeg . Open the file to write to .
def _initialize(self):
            sizestr = "%dx%d" % (self._size[1], self._size[0])
            fps = self.request.kwargs.get('fps', 10)
            default_codec = 'libx264'
            if self._filename.lower().endswith('.wmv'):
                default_codec = 'msmpeg4'
            codec = self.request.kwargs.get('codec', default_codec)
            bitrate = self.request.kwargs.get('bitrate', None)
            quality = self.request.kwargs.get('quality', 5)
            ffmpeg_log_level = self.request.kwargs.get('ffmpeg_log_level',
                                                       'warning')
            extra_ffmpeg_params = self.request.kwargs.get('ffmpeg_params', [])
            pixelformat = self.request.kwargs.get('pixelformat', 'yuv420p')
            cmd = [self._exe, '-y',
                   "-f", 'rawvideo',
                   "-vcodec", "rawvideo",
                   '-s', sizestr,
                   '-pix_fmt', self._pix_fmt,
                   '-r', "%.02f" % fps,
                   '-i', '-', '-an',
                   '-vcodec', codec,
                   '-pix_fmt', pixelformat,
                   ]
            if bitrate is not None:
                cmd += ['-b:v', str(bitrate)]
            elif quality is not None:  
                if quality < 0 or quality > 10:
                    raise ValueError("ffpmeg writer quality parameter out of"
                                     "range. Expected range 0 to 10.")
                quality = 1 - quality / 10.0
                if codec == "libx264":
                    quality = int(quality * 51)
                    cmd += ['-crf', str(quality)]  
                else:  
                    quality = int(quality*30)+1
                    cmd += ['-qscale:v', str(quality)]  
            cmd += ['-r', "%d" % fps]
            macro_block_size = self.request.kwargs.get('macro_block_size', 16)
            if macro_block_size is not None and macro_block_size > 1 and \
                    (self._size[1] % macro_block_size > 0 or
                        self._size[0] % macro_block_size > 0):
                out_w = self._size[1]
                if self._size[1] % macro_block_size > 0:
                    out_w += macro_block_size - \
                        (self._size[1] % macro_block_size)
                out_h = self._size[0]
                if self._size[0] % macro_block_size > 0:
                    out_h += macro_block_size - \
                        (self._size[0] % macro_block_size)
                cmd += ['-vf', 'scale={0}:{1}'.format(out_w, out_h)]
                logging.warning(
                    "IMAGEIO FFMPEG_WRITER WARNING: input image is not"
                    " divisible by macro_block_size={0}, resizing from {1} "
                    "to {2} to ensure video compatibility with most codecs "
                    "and players. To prevent resizing, make your input "
                    "image divisible by the macro_block_size or set the "
                    "macro_block_size to None (risking incompatibility). You "
                    "may also see a FFMPEG warning concerning "
                    "speedloss due to "
                    "data not being aligned.".format(macro_block_size,
                                                     self._size[:2],
                                                     (out_h, out_w)))
            if ffmpeg_log_level:
                cmd += ['-v', ffmpeg_log_level]
            cmd += extra_ffmpeg_params
            cmd.append(self._filename)
            self._cmd = " ".join(cmd)  
            if ('warning' not in ffmpeg_log_level) and \
                    ('quiet' not in ffmpeg_log_level):
                print("RUNNING FFMPEG COMMAND:", self._cmd, file=sys.stderr)
            self._proc = sp.Popen(cmd, stdin=sp.PIPE,
                                  stdout=sp.PIPE, stderr=None)
def _initialize ( self ) : sizestr = "%dx%d" % ( self . _size [ 1 ] , self . _size [ 0 ] ) fps = self . request . kwargs . get ( 'fps' , 10 ) default_codec = 'libx264' if self . _filename . lower ( ) . endswith ( '.wmv' ) : default_codec = 'msmpeg4' codec = self . request . kwargs . get ( 'codec' , default_codec ) bitrate = self . request . kwargs . get ( 'bitrate' , None ) quality = self . request . kwargs . get ( 'quality' , 5 ) ffmpeg_log_level = self . request . kwargs . get ( 'ffmpeg_log_level' , 'warning' ) extra_ffmpeg_params = self . request . kwargs . get ( 'ffmpeg_params' , [ ] ) pixelformat = self . request . kwargs . get ( 'pixelformat' , 'yuv420p' ) cmd = [ self . _exe , '-y' , "-f" , 'rawvideo' , "-vcodec" , "rawvideo" , '-s' , sizestr , '-pix_fmt' , self . _pix_fmt , '-r' , "%.02f" % fps , '-i' , '-' , '-an' , '-vcodec' , codec , '-pix_fmt' , pixelformat , ] if bitrate is not None : cmd += [ '-b:v' , str ( bitrate ) ] elif quality is not None : if quality < 0 or quality > 10 : raise ValueError ( "ffpmeg writer quality parameter out of" "range. Expected range 0 to 10." ) quality = 1 - quality / 10.0 if codec == "libx264" : quality = int ( quality * 51 ) cmd += [ '-crf' , str ( quality ) ] else : quality = int ( quality * 30 ) + 1 cmd += [ '-qscale:v' , str ( quality ) ] cmd += [ '-r' , "%d" % fps ] macro_block_size = self . request . kwargs . get ( 'macro_block_size' , 16 ) if macro_block_size is not None and macro_block_size > 1 and ( self . _size [ 1 ] % macro_block_size > 0 or self . _size [ 0 ] % macro_block_size > 0 ) : out_w = self . _size [ 1 ] if self . _size [ 1 ] % macro_block_size > 0 : out_w += macro_block_size - ( self . _size [ 1 ] % macro_block_size ) out_h = self . _size [ 0 ] if self . _size [ 0 ] % macro_block_size > 0 : out_h += macro_block_size - ( self . _size [ 0 ] % macro_block_size ) cmd += [ '-vf' , 'scale={0}:{1}' . format ( out_w , out_h ) ] logging . warning ( "IMAGEIO FFMPEG_WRITER WARNING: input image is not" " divisible by macro_block_size={0}, resizing from {1} " "to {2} to ensure video compatibility with most codecs " "and players. To prevent resizing, make your input " "image divisible by the macro_block_size or set the " "macro_block_size to None (risking incompatibility). You " "may also see a FFMPEG warning concerning " "speedloss due to " "data not being aligned." . format ( macro_block_size , self . _size [ : 2 ] , ( out_h , out_w ) ) ) if ffmpeg_log_level : cmd += [ '-v' , ffmpeg_log_level ] cmd += extra_ffmpeg_params cmd . append ( self . _filename ) self . _cmd = " " . join ( cmd ) if ( 'warning' not in ffmpeg_log_level ) and ( 'quiet' not in ffmpeg_log_level ) : print ( "RUNNING FFMPEG COMMAND:" , self . _cmd , file = sys . stderr ) self . _proc = sp . Popen ( cmd , stdin = sp . PIPE , stdout = sp . PIPE , stderr = None )
Creates the pipe to ffmpeg.
Creates the pipe to ffmpeg .
""" Creates the pipe to ffmpeg. Open the file to write to. """ # Get parameters # Note that H264 is a widespread and very good codec, but if we # do not specify a bitrate, we easily get crap results. # This is a safer default codec on windows to get videos that # will play in powerpoint and other apps. H264 is not always # available on windows. # You may need to use -pix_fmt yuv420p for your output to work in # QuickTime and most other players. These players only supports # the YUV planar color space with 4:2:0 chroma subsampling for # H.264 video. Otherwise, depending on your source, ffmpeg may # output to a pixel format that may be incompatible with these # players. See # https://trac.ffmpeg.org/wiki/Encode/H.264#Encodingfordumbplayers # Get command # Add fixed bitrate or variable bitrate compression flags # If None, then we don't add anything # crf ranges 0 to 51, 51 being worst. # for h264 # Many codecs accept q:v # q:v range can vary, 1-31, 31 being worst # But q:v does not always have the same range. # May need a way to find range for any codec. # for others # Note, for most codecs, the image dimensions must be divisible by # 16 the default for the macro_block_size is 16. Check if image is # divisible, if not have ffmpeg upsize to nearest size and warn # user they should correct input image if this is not desired. # Rather than redirect stderr to a pipe, just set minimal # output from ffmpeg by default. That way if there are warnings # the user will see them. # For showing command if needed # Launch process # Warning, directing stderr to a pipe on windows will cause ffmpeg # to hang if the buffer is not periodically cleared using # StreamCatcher or other means.
self