Example #1
0
def get_best_screenshot(video_path, master_dir, screenshots_time_range, rate=8,  frames_dir=False):
    """
        Extract a frame from a video and save it to a file in jpeg.

        video_path: video source path
        screenshots_time_range: intervals in seconds where to pick up screenshots
        frames_dir: dir where are saved video frames (must call extract_frames first )

    """

    #import ipdb; ipdb.set_trace()

    print "Time intervals: %s" % screenshots_time_range

    for i, second_start in enumerate(screenshots_time_range):

        piclist = []

        # Step is to set the last screenshot to cast a little before the next screenshot cast to avoid having similar screenshots.
        # for example screenshot 1 at 330 secs and screenshot 2 at 335 secs, screenshots would be very similars.
        # time at wich we stop analyse screenshots (begining of next thumbnail)
        step = screenshots_time_range[1] - screenshots_time_range[0]
        second_end = screenshots_time_range[i] + int((step * 0.7))

        # convert seconds to frames number
        frames_start = second_start * rate
        frames_end = second_end * rate

        master = os.path.join(master_dir, '%s.jpg' % (i + 1))

        print "Get Thumbnail N° %s" % (i + 1)
        
        # Scann all frames in times intervals
        for frame in xrange(frames_start, frames_end):

            # build path of screenshot to analyse
            currimg = os.path.join(frames_dir, 'frame%s.jpg' % frame)

            # if last image reach, stop analyze
            if not os.path.exists(currimg):
                break

            try:

                blurcoef = get_blur(currimg)            
                piclist.append((blurcoef,currimg))

                print 'Check img : frame %s - blur factor: %s' % (currimg, blurcoef)
                
            except Exception as e:
                print 'Get Best Screenshot error: %s' % e
                break

        # sort blur pics to get the one the the highest coef = most clear
        piclist = sorted(piclist, key=lambda coef: coef[0])[-1][1]
        shutil.copy(piclist, master)

        print 'Select best thumbnail: %s' % piclist
Example #2
0
            # Do not remove black borders
            if not settings['trim']:
                REMOVE_BLACK_BORDERS = ''

            # setup thumbnail parameters
            define_size = str(settings['width']*2) + 'x' + str(settings['height']*2)
            thumbnail_size = str(settings['width']) + 'x' + str(settings['height'])
            thumbnail_settings = "-define jpeg:size=%(define)s %(input)s %(remove_black_borders)s -shave 1x1 -thumbnail %(thumbnail_size)s^ -gravity %(gravity)s -extent %(thumbnail_size)s %(image_filter)s %(ouput)s" % {'define': define_size, 'thumbnail_size': thumbnail_size, 'gravity': settings['gravity'], 'image_filter': IMAGE_FILTER, 'remove_black_borders': REMOVE_BLACK_BORDERS, 'input':master, 'ouput': thumbnail_path}

            print "Setting used to make thumbnail: %s" % thumbnail_settings

            # Make the thumbnails
            make_thumbnail(master, thumbnail_path, thumbnail_settings)

            # Calculate blur_coef and brightness of each thumbnails 
            image_blur = get_blur(thumbnail_path)
            image_brightness = get_brightness(thumbnail_path)
            infos['blur_coef'][x] = int(image_blur)
            infos['brightness'][x] = int(image_brightness)

        return infos

    except Exception, e:
        raise e
        print 'Error making screenshots'


# use it as standalone to test each functions
if __name__ == '__main__':
    test = {'video_path': 'test.mp4',
            'gravity': 'center',