Use FFmpeg to extract frames from video

Every video consists of still images. Cameras piece these images together in a successive order to create an illusion of motion. You can extract any of the images (commonly called frames) that a video is made up of.

Extracting frames from videos is useful for creating thumbnails and poster images. And also for capturing key images for documentation and blog posts, such as film reviews.

This guide will teach you how to extract frames from videos using FFmpeg. By the end, you should be able to do the following:

  • Extract the first frame from a video.
  • Extract a frame from a specific timestamp.
  • Extract all frames from a video.
  • Extracting frames at set intervals.

What is FFmpeg?

FFmpeg is a popular command-line tool that is great for manipulating video and audio. You can use it to trim and compress videos, change file formats, extract frames, and more.

It's free and works on many operating systems. Visit the official website to download FFmpeg if you don't have it. Then, follow the installation instructions for your platform.

Run the following command to confirm you have FFmpeg running on your system.

ffmpeg -version

You should see some build information in your terminal. That shows the installation was successful.

Navigate to the directory containing your video file and follow along. For this tutorial, we'll use a five-second BMX bike video which you can download and rename to input.mp4. You can also use the following command to download and rename the file:

wget https://shotstack-assets.s3.ap-southeast-2.amazonaws.com/footage/bmx.mp4 -O input.mp4

Extracting the first frame using FFmpeg.

Run the command below in your terminal to extract the first frame from the video.

ffmpeg -i input.mp4 -frames:v 1 first.jpg

Let's break down what each part of the command does:

-i input.mp4:: The -i flag specifies which video to extract the video from. In this tutorial, input.mp4 is the name of the file we're using. Replace input.mp4 with the name of your video file. -frames:v 1: Tells FFmpeg to extract only one frame. You can add the timestamp ss 0:00:00 before -frames to show you want to extract the first file. The command would look like this.

ffmpeg -i input.mp4 ss 0:00:00 -frames:v 1 first.jpg

By default, if you don't specify any timestamp, FFmpeg will extract the first frame.

Here is the first frame of the video saved as a first.jpg:

The first frame from our example input video

You can view the extracted image using any image viewer or photo editing software on your computer.

Extracting a frame from a specific timestamp

Use the command below to extract a frame from a specific timestamp in your video.

ffmpeg -i input.mp4 -ss 00:00:02 -frames:v 1 specific.jpg

This command is like the one from the previous section. The difference is the timestamp -ss 00:00:02. It tells FFmpeg the exact frame to extract from the video. For this example, FFmpeg will extract the frame from two seconds into the video.

Here is the frame extracted at second 2 called specific.jpg:

Example of a frame extracted from a specified timestamp.

Extracting all frames from a video

Extracting all frames from a video means making a separate image file for each frame. You can do that using the command below.

ffmpeg -i input.mp4 frame_%04d.jpg

This command will extract all the frames from the video. But you should use the command with caution. Here's why:

Our video is a five-second video shot at 25 frames per second (fps). This means each second of the video has 25 frames. Running the above command for this video will generate 125 frames (5 x 25 = 125 frames). And if you were to run it for a 60-second video shot at 30 frames per second, you would get 1800 images (60 x 30 = 1800 frames).

You use frame_%04d.jpg like a placeholder to label the output files in a clear, sequential order. The %004d part at the end means each output file will have a four-digit ID at the end. For example, the first two frames are frame_0001.jpg, frame_0002.jpg, and so on up to frame_0142.jpg,

Extracting video frames at set intervals

To avoid creating thousands of unnecessary frames as outlined in the previous example you can extract a frame every second or at specific intervals. Do do this, you can adjust the frame rate. You do that by adding -vf fps=1 before the output file name.

ffmpeg -i input.mp4 -vf fps=1 frame_%04d.jpg

The example creates a frame for every second of the video. The -vf flag stands for video filter. It allows you to filter out which frames you want to extract. In this case, we've passed fps=1 to -vf which will extract only frames at the mark of each second in the video.

Using the -vf fps=1 will come in handy for situations like when you need to create a sprite sheet. You can easily choose the intervals between the frames you want to use.

This time you will only have 6 files frame_0001.jpg to frame_0006.jpg.

Conclusion

Extracting frames from videos is a useful feature to include in video-based applications. This article covers some practical use cases for extracting frames. You've also learned different ways to extract frames from video using FFmpeg. We covered in detail how to extract the first frame, specific frames at set times, and all frames.

Using FFmpeg is a great way to extract frames from your desktop, but if you want to create frames on a server or at scale you might want to use an FFmpeg alternative. Shotstack is a video platform that can be used to extract frames using an API.

Peace Aisosa

BY PEACE AISOSA
26th March, 2024

Become an Automated Video Editing Pro

Every month we share articles like this one to keep you up to speed with automated video editing.


You might also like

How to use FFmpeg (with examples)

How to use FFmpeg (with examples)

Andrew Bone
Generate audio waveform videos using FFmpeg

Generate audio waveform videos using FFmpeg

Jeff Shillitto
How to compress video using FFmpeg

How to compress video using FFmpeg

Maab Saleem