(video, directory, full_resolution, viewport)
return ret
Extract and number the video frames
Extract and number the video frames
Extract and number the video frames
def extract_frames(video, directory, full_resolution, viewport):
    """Extract and number the video frames"""
    ret = False
    logging.info("Extracting frames from " + video + " to " + directory)
    decimate = get_decimate_filter()
    if decimate is not None:
        crop = ''
        if viewport is not None:
            crop = 'crop={0}:{1}:{2}:{3},'.format(
                viewport['width'], viewport['height'], viewport['x'], viewport['y'])
        scale = 'scale=iw*min({0:d}/iw\\,{0:d}/ih):ih*min({0:d}/iw\\,{0:d}/ih),'.format(
            options.thumbsize)
        if full_resolution:
            scale = ''
        # escape directory name
        # see https://en.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/image_sequence#Percent_in_filename
        dir_escaped = directory.replace("%", "%%")
        command = ['ffmpeg', '-v', 'debug', '-i', video, '-vsync', '0',
                   '-vf', crop + scale + decimate + '=0:64:640:0.001',
                   os.path.join(dir_escaped, 'img-%d.png')]
        logging.debug(' '.join(command))
        lines = []
        proc = subprocess.Popen(command, stderr=subprocess.PIPE, universal_newlines=True)
        while proc.poll() is None:
            lines.append(proc.stderr.readline())

        pattern = re.compile(r'keep pts:[0-9]+ pts_time:(?P<timecode>[0-9\.]+)')
        frame_count = 0
        for line in lines:
            match = re.search(pattern, line)
            if match:
                frame_count += 1
                frame_time = int(math.ceil(float( \
                    match.groupdict().get('timecode')) * 1000))
                src = os.path.join(
                    directory, 'img-{0:d}.png'.format(frame_count))
                dest = os.path.join(
                    directory, 'video-{0:06d}.png'.format(frame_time))
                logging.debug('Renaming ' + src + ' to ' + dest)
                os.rename(src, dest)
                ret = True
    return ret
def extract_frames ( video , directory , full_resolution , viewport ) : ret = False logging . info ( "Extracting frames from " + video + " to " + directory ) decimate = get_decimate_filter ( ) if decimate is not None : crop = '' if viewport is not None : crop = 'crop={0}:{1}:{2}:{3},' . format ( viewport [ 'width' ] , viewport [ 'height' ] , viewport [ 'x' ] , viewport [ 'y' ] ) scale = 'scale=iw*min({0:d}/iw\\,{0:d}/ih):ih*min({0:d}/iw\\,{0:d}/ih),' . format ( options . thumbsize ) if full_resolution : scale = '' # escape directory name # see https://en.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/image_sequence#Percent_in_filename dir_escaped = directory . replace ( "%" , "%%" ) command = [ 'ffmpeg' , '-v' , 'debug' , '-i' , video , '-vsync' , '0' , '-vf' , crop + scale + decimate + '=0:64:640:0.001' , os . path . join ( dir_escaped , 'img-%d.png' ) ] logging . debug ( ' ' . join ( command ) ) lines = [ ] proc = subprocess . Popen ( command , stderr = subprocess . PIPE , universal_newlines = True ) while proc . poll ( ) is None : lines . append ( proc . stderr . readline ( ) ) pattern = re . compile ( r'keep pts:[0-9]+ pts_time:(?P<timecode>[0-9\.]+)' ) frame_count = 0 for line in lines : match = re . search ( pattern , line ) if match : frame_count += 1 frame_time = int ( math . ceil ( float ( match . groupdict ( ) . get ( 'timecode' ) ) * 1000 ) ) src = os . path . join ( directory , 'img-{0:d}.png' . format ( frame_count ) ) dest = os . path . join ( directory , 'video-{0:06d}.png' . format ( frame_time ) ) logging . debug ( 'Renaming ' + src + ' to ' + dest ) os . rename ( src , dest ) ret = True return ret