Tutorials · 2 min read

Extract Frames

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

Extract a set of images (frames) from your video that are evenly spaced throughout using FFMPEG and Python. If you want, at the end you can stitch the frames back together into a video that can be used as a video preview or trailer.

Erikka Innes

April 22, 2021

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

Prerequisites

You need to have Python and FFMPEG installed.

Extract Images From Your Video

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

python

  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!

Try out more than 80 features for free

Access all the features for as long as you need.
No commitment or credit card required

Video API, simplified

Fully customizable API to manage everything video. From encoding to delivery, in minutes.

Built for Speed

The fastest video encoding platform. Serve your users globally with 140+ points of presence. 

Let end-users upload videos

Finally, an API that allows your end-users to upload videos and start live streams in a few clicks.

Affordable

Volume discounts and usage-based pricing to ensure you don’t exceed your budget.