How to extract all the frames from a video using FFmpeg?

I want to extract all the frames of a video into a folder. I’ve heard that FFmpeg is the tool for the task but don’t know how to do it.

1 Like

You heard correctly, that FFmpeg is the right tool to achieve your task. To prepare each frame of a video for additional analysis or alteration, it is necessary to convert it into an image file (such as a JPEG or PNG).

Here’s an in-depth tutorial on using FFmpeg to extract every frame from a video:

1. Download FFmpeg

Make sure to install FFmpeg on your system before you proceed further.

  1. Install FFmpeg on Windows
  2. Install FFmpeg on Ubuntu
  3. Install FFmpeg on Mac

2. Go to the Directory for the Video

Using the “cd” command in your terminal or command prompt, navigate to the directory containing the video you wish to process.

3. Use the FFmpeg command to execute

Use the FFmpeg command below to extract each and every frame from the video:

ffmpeg -i input.mp4 -q:v 2 output_%04d.jpg

  • Replace input.mp4 with the name of your video file.
  • The -q:v 2 option specifies the output image quality, with lower values resulting in higher quality. You can adjust this setting as needed.
  • output_%04d.jpg is the naming pattern for the output images. %04d serves as a placeholder for a four-digit number, which will be used to label the frames.
  • You can change the file format (e.g., to PNG) by modifying the file extension (e.g., output_%04d.png).

4. Extraction of Monitor Frames

The frame extraction procedure will begin with FFmpeg, which will convert each frame into a separate picture file. The length of the video will determine how long this procedure takes.

5. Examine the result

The directory where you ran the FFmpeg command will include a series of image files after the frame extraction is finished. Sequentially numbered frames from the video are represented by each image.

Remarks:

  • You can adjust the extracted frame quality with the -q:v option. Higher quality is achieved with lower numbers, but the resulting files are larger.
  • To suit your tastes, you can change the output format and naming convention. For example, %04d.png can be used for PNG format, and %06d.bmp for BMP format.
  • You can use the -r option to determine the frame extraction rate. For instance, you can use the FFmpeg command with -r 30 to extract frames at a rate of 30 frames per second.

Remember that, especially for longer recordings, frame extraction could produce a large number of image files. Prepare for a significant need for storage.

For more visit:

Why is this -r flag used?

So basically, there is no frame rate for a collection of image files. Use -r before the input if you wish to under-sample the video file.

Is it possible to resize a particular image of the frame of a video or what if I wish to take the screenshot image in ffmpeg?

Yes, For resizing the image:

Use the -s flag in the command

ffmpeg -i file.mp4 -s 640x480 %04d.jpg

For screenshot add -frames:v 1 in the command as shown below:

ffmpeg -ss 00:00:04 -i filemp4 -frames:v 1 screenshot.png