1. Overview

Extracting frames from video files is essential for various applications, such as video production, scientific research, surveillance, and security. The extracted frames provide insights into the video’s content, enabling the detection and tracking of objects or events, and precise video editing.

FFmpeg provides a versatile and efficient way to extract frames from video files, offering a wide range of options to customize the output format, quality, and frequency of extracted frames. We can use apt or snap to install ffmpeg on a Ubuntu/Debian machine. In this tutorial, we’re going to learn the process of extracting frames from a video using FFmpeg with examples.

2. Extracting All Frames

Before we get started, let’s download a sample video file that we’ll use in our examples to extract frames:

$ wget https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_2mb.mp4

FFmpeg provides various options for extracting frames, such as specifying the frame rate, the output format, and the image quality. Let’s check out a basic command for extracting frames from a video:

$ ffmpeg -i big_buck_bunny_720p_2mb.mp4 -r 1 frame%d.png

In the above example, -i is used to provide the input video, -r defines the frame rate for extracting frames in frames per second (here, we’re using a rate of one frame per second), and frame%d.png defines the naming pattern for extracted frames. The above command will extract 15 frames from the video starting with frame1.png to frame15.png. We can check the output by using the ls command:

$ ls
frame1.png
frame2.png
...
frame15.png

3. Extracting a Single Frame

Extracting all the frames will take up a lot of space. FFmpeg allows us to extract only a single frame from the video at our desired position:

$ ffmpeg -i big_buck_bunny_720p_2mb.mp4 -ss 00:00:05 -vframes 1 frame_out.jpg

This command tells ffmpeg to extract a frame from the video at the 5th second and save it as a JPEG image file named frame_out.jpg.

Let’s take a closer look at what each option means:

  • -i specifies the input video file
  • -ss sets the start time offset to five seconds into the video, using the format hh:mm:ss
  • -vframes is used to set the number of frames to extract, which in our case is one
  • frame_out.jpg is the output file name and format

4. Extracting a Series of Frames

We can also extract multiple frames from the desired time offset. Let’s extract 25 frames from the video starting at five seconds with the rate of 10 frames per second:

$ ffmpeg -i big_buck_bunny_720p_2mb.mp4 -ss 00:00:05 -r 10 -s 640x360 -q:v 2 -vframes 25 frame%03d.jpg

The above command uses some new options:

  • -s sets the resolution of the output image, in our case 640×360 pixels
  • -q:v specifies the quality level of the output image, which ranges from 1 (highest quality) to 31 (lowest quality)
  • frame%03d.jpg specifies the output file name and format for extracted frames

The %03d sets the numbering used in file names to two digits, padded with zero if necessary.

If we want to extract all frames after five seconds, we can run the above command without the -vframes option.

5. Conclusion

In this tutorial, we learned how to extract frames from a video using FFmpeg.

We discussed the commands to extract one, multiple, and all frames from a video at different frame rates, expressed in frames per second, giving us a lot of flexibility.

We also looked at options for the quality of the extracted frames along with the naming patterns for output images.

Finally, we used FFmpeg to extract a series of frames starting from a given time offset.