This covers one FFMPEG command to create a directory of still images, and an according python class that will supply the proper file path upon passing a time mark to the original video file.

FFMPEG extraction of still images:

I have been looking for a way to take a video file and convert it into a folder with properly numbered .JPG or -PNG files several times. Each time, I came across solutions that were supposed to work with ffmpeg, but didn’t. The problems were mostly in the detailed syntax, I believe.

Here is a command that works for me on an (outdated) Ubuntu 10.04. It also scales the video file up 720 HD resolution.

ffmpeg -i ~/DirectoryName/video_name.mp4 -s hd720 -r 30 -f image2  image%05d.jpg

For full HD it should probably work the same way, but you would have to insert -s hd1080. -r 30 is the frame rate at which images are taken from the video. image2 is a template for the file name format, and image%05d.jpg indicates that 5 digits will be used for numbering the still images. Changing to .png should give you PNG images instead of JPG.

Other resolutions are given here, too. This site has also helped me in arriving at a solution.

python provision of time-specific file paths:

import os
import numpy as np

class FramePathServer():
    
    ''' This provides time-based path names from a folder of frame-images. '''
    
    def __init__(self,folder_path,frame_rate=np.float(30.),
                 start_time=np.float(0),file_type='.png'):
        
        self.start_time = np.float(start_time) # offset for time marks
        self.fps = np.float(frame_rate) # frames per second
        self.folder_path = folder_path # folder that the images come from
        
        # read in file names from the folder that the path is pointing to, only
        # keep the ones that end on .png (or other specified format)
        #os.chdir(self.folder_path)
        self.file_paths = [file for file in os.listdir(self.folder_path)
            if file.endswith(file_type)]
        self.file_paths.sort()
        
        print 'Folder path:', self.folder_path
        self.num_frames = len(self.file_paths)
        print 'Number of frames:', self.num_frames
                
    def return_path(self,time):
        
        ''' returns the path to the last frame just before the time passed as 
        an argument '''
        
        frame_time = time + self.start_time
        last_frame_num = np.mod(np.floor(frame_time*self.fps),
                                self.num_frames)
        if last_frame_num < 0: last_frame_num = 0
        
        return os.path.join(self.folder_path,
                            self.file_paths[np.int(last_frame_num)])

if __name__ == "__main__":
    
    server = FramePathServer(
        '/Users/user_name/Directory/Frames')
    
    # These two lines check for basic functionality
    print len(server.file_paths)
    print server.return_path(90)

Does this work for you? What kind of approaches do you use for similar tasks?

2 thoughts on “FFMPEG extract still images from video file, python provide time-specific path

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s