logo api.video

Extract a set of frames from a video with FFMPEG and Python

April 22, 2021 - Erikka Innes in Python

You can extract a list of frames at evenly spaced intervals through your video using Python and FFMPEG. This information could be used to do a quick skim of a video or create a trailer for the video programmatically - you could stitch the frames back together afterwards into a short video.

If you need a place to upload and host your video from, api.video has you covered! Sign up here: api.video sign up

And check out the reference documentation for our API here: API reference documentation


You need to have Python and FFMPEG installed. We explain how to do this in this article Live Stream to the Browser with FFMPEG CLI and Python.

Extract Images From Your Video

The code sample for how to extract images for your video uses the ffmpeg-python module.

import ffmpeg

YOUR_FILE = 'sample-mov-file.mov'
probe = ffmpeg.probe(YOUR_FILE)
time = float(probe['streams'][0]['duration']) // 2
width = probe['streams'][0]['width']

# Set how many spots you want to extract a video from. 
parts = 7

intervals = time // parts
intervals = int(intervals)
interval_list = [(i * intervals, (i + 1) * intervals) for i in range(parts)]
i = 0

for item in interval_list:
        .input(YOUR_FILE, ss=item[1])
        .filter('scale', width, -1)
        .output('Image' + str(i) + '.jpg', vframes=1)
    i += 1
  1. You should have the video you want to extract images from in the same folder as the code sample if you want to use the sample as-is. Add the complete name of your video file in the spot that says 'sample-mov-file.mov'.

  2. Now we use ffmpeg.probe to get information about your video. You're going to want the length of the video so we can figure out how far apart the images we extract should be.

  3. Set parts equal to the number of spots you want to extract a video from.

  4. Now I use list comprehension to grab a list of intervals in seconds in your video.

  5. I want to start getting images from the end of each interval, so I use item[1] in the for loop to get the end of each interval for my images. The loop will run through and extract one frame each time, label it ImageZ where Z starts at 0 and goes up to the total number of images you requested.

  6. You can stitch these images together into a video using this code sample: Assemble video from sequence of frames

If you have any questions or suggestions, please share them on our community forum.

Start building right now with api.video!


Erikka Innes

Developer Evangelist