Video Editing

7 min read

How to extract frames from video using FFmpeg?

This blog post will guide you through the process of extracting frames from a video using FFmpeg, a powerful command-line tool for multimedia manipulation. You'll learn how to extract all frames and specific frames and even control the output format, frame rate, etc.

Extract frames from video using FFmpeg
How to extract frames from video using FFmpeg?

FFmpeg is a comprehensive, multi-platform solution for capturing, converting, and streaming audio and video. It is capable of decoding/encoding, transcoding, and playing any media format. FFmpeg is a highly reliable tool for various media tasks, such as capturing images from a video. You can create thumbnails for your videos or capture a screenshot of the video whenever you want. Let's dive in and figure out how to extract frames using FFmpeg:

How to extract frames from video using FFmpeg?

FFmpeg Installation

Before we begin, ensure you have FFmpeg installed on your system. If not, you can download it from the official website. Here are a few factors to keep in mind:

  1. Verify that FFmpeg is correctly installed on your system by using basic commands. Open your command-line interface (Terminal, Command Prompt, etc.) and type the following command:
ffmpeg -version

This command will display the FFmpeg version installed on your system. Make sure the version matches or is newer than v5.0.1

  1. Next, it's helpful to inspect the video file to understand its properties, such as frame rate, resolution, and codecs. You can do this with FFmpeg as well. Use the following command to inspect the video file:
ffmpeg -i input.mp4

Here, replace the name of your video in place of "input.mp4". Once you execute the command, FFmpeg will offer info, including details on the video file, its duration, frame rate, resolution, codecs used, etc.

How to Extract All Frames from Video?

To extract all frames from a video using FFmpeg, follow these steps:

First, you can use a basic command like:

ffmpeg -i input.mp4 %04d.png

In this command, FFmpeg is the command-line tool used to process multimedia files.

"%04d.png" is a sequence pattern used to generate output file names. %04d.png specifies that the files will be named with a four-digit sequential number, padded with zeros if necessary, followed by the .png extension. For example, the first frame will be named 0001.png, the second 0002.png, and so on

Then, by default, FFmpeg matches the frame rate of the video source, which determines the number of images extracted per second of the video. You can find out the frame rate of your video by running the command:

ffmpeg -i input.mp4

input.mp4 provides information about the video file, including its frame rate. Again, replace the name of your video in place of "input.mp4".

How to extract frames at specific intervals from video?

To extract frames from a video at specific intervals using FFmpeg, you can employ the "-ss" argument. This argument instructs FFmpeg to locate a particular point in the input file, specified by the number of seconds (x). By using the same "-ss" argument with FFmpeg, you can direct it to seek a specific number of seconds into the input video and commence extracting images from that exact moment.

Placing the "-ss" argument before the input video ("-i") within the command is crucial. This ensures the input is parsed using keyframes and speeds up the image extraction compared to other methods.

Let's say you have a longer video, and you want to have FFmpeg extract frames every 10 seconds. You can use FFmpeg to achieve this by adjusting the "-ss" argument accordingly. Here's an example command:

ffmpeg -ss 00:00:10 -i input.mp4 %04d.png

In this command, "-ss 00:00:10" tells FFmpeg to start extracting frames from the 10th second of the video. As a result, you'll get frames extracted every 10 seconds throughout the video. Each extracted frame will be saved as a PNG file with a sequential numbering format, such as "0001.png", "0002.png", and so on.

How to extract a single frame from a video?

To extract a single frame from a video, you can use the "-frames1" option in the FFmpeg command. This option tells FFmpeg to output only one frame instead of the entire video sequence. By specifying the desired time with the "-ss" argument, you can extract a frame from that specific moment in the video. 

For example, the command—

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

—will extract a single frame from the video at the 4th second and save it as "screenshot.png".

Advanced Techniques to Extract Frames from Videos

We can generate desired outputs by including some additional arguments to the basic command. Here, we'll look at advanced techniques that are possible with FFmpeg when extracting frames from videos.

Batch Processing Multiple Videos

Batch processing lets you handle multiple videos simultaneously. This is done using scripting and command-line tools.

You can create a shell script to process multiple videos in a directory:

#!/bin/bash
for file in /path/to/videos/*.mp4; do
    ffmpeg -i "$file" -vf fps=1 /path/to/frames/"$(basename "$file" .mp4)"_%04d.png
done

This script extracts one frame per second (fps=1) from each .mp4 video in the specified directory and saves the frames as .png images.

Automating Frame Extraction with Scripts

Automation is achieved with scripting languages like Python; you can interface them with FFmpeg or other libraries.

Python lets you automate FFmpeg commands and handle complex workflows:

import os
import subprocess
def extract_frames(video_path, output_dir, fps=1):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    video_name = os.path.basename(video_path).split('.')[0]
    output_template = os.path.join(output_dir, f"{video_name}_%04d.png")
    command = ["ffmpeg", "-i", video_path, "-vf", f"fps={fps}", output_template]
    subprocess.run(command)
video_directory = "/path/to/videos"
output_directory = "/path/to/frames"
for video_file in os.listdir(video_directory):
    if video_file.endswith(".mp4"):
        extract_frames(os.path.join(video_directory, video_file), output_directory)

Combining Frame Extraction with Other FFMPEG Features

FFMPEG’s versatility allows you to combine frame extraction with other features, such as resizing, changing the frame rate, and applying filters.

  1. Changing the Frame Rate

To modify the frame rate, simply include either the argument -vf fps or -filter:v fps along with the desired number of frames to the initial command. It's a filter that changes the video to the desired constant frame rate by duplicating or removing frames as needed.

If the original video frame rate exceeds the specified frame rate, frames will be eliminated to align with the specified rate. In contrast, if the original frame rate is lower, the frames of the video will be copied.

For example, adding -vf fps=1 to the basic command will re-encode the video to 1 fps. The video frames will be dropped as the original frame rate is 30 fps. The total number of images extracted from the video will become 9 (1 image x 9.13 seconds).

ffmpeg -i input.mp4 -vf fps=1 %04d.png
  1. Resizing the Frame

The images will default to the same size as the video they were taken from. To change the size of the images, simply include -s and then input the new dimensions after the command.

The instruction provided will resize the images from 1200 × 670 pixels to 640×480 pixels.

ffmpeg -i input.mp4 -s 640x480 %04d.jpg
  1. Applying Filters

You can apply various filters during extraction, such as grayscale conversion:

ffmpeg -i input.mp4 -vf "fps=1,format=gray
  1. Frame Extraction To JPEG Format

If you want to retain the original quality of the frames you are extracting, you should consider using a lossless format like PNG images. Here is how FFmpeg extracts all frames to PNG:

ffmpeg -i input.mp4 -vf fps=1 output_%04d.png

However, PNG files can grow in size significantly when working with high-resolution videos. One solution is to utilize the JPEG format with the quality-adjusted to the highest level. The following command accomplishes this:

ffmpeg -i input.mp4 -q:v 1 frame%04d.jpg

The -q:v 1 argument here is to adjust the image quality of the output. The quality level varies between 1 (best quality) and 31 (worst quality). When saving a JPEG file, the size of the file increases as the quality value decreases.

Conclusion

Whether extracting a single frame or a series of frames, FFmpeg helps users manipulate video frames efficiently. In this tutorial, we learned how to use FFmpeg to extract frames from video. We discussed how to extract single, multiple and all frames from a video at different frame rates. We also discussed advanced techniques like automating the process, batch processing, resizing, and applying filters. We hope to have helped you with your query. If so, please feel free to share this post.

FAQs 

  1. What are the use cases for extracting video frames?

 Extracting video frames serves various purposes across industries. 

  • In machine learning, it helps with data preparation for tasks like object detection.
  • The frame-by-frame analysis enables detailed video analysis, which is key to surveillance or sports analysis. 
  • It's also handy for creating GIFs. 
  • Extracting key frames helps summarize videos or generate thumbnails. 
  1. What is fps in FFmpeg?

In FFmpeg, the avg_frame_rate value represents the average frame rate of the input video file, expressed as a fraction like 30/1 or 25/1. For example, a result of 25/1 indicates that the video file's average frame rate is 25 FPS.

  1. Can VLC extract frames from a video? 

Yes, VLC can help extract frames from video, but its frame extraction is relatively easy compared to dedicated video editing software or command-line tools like FFmpeg. You are unlikely to get advanced options that allow you to extract at specific intervals or output formats.

  1. How do I extract video frames on Android?

To extract video frames on Android,

  • Open the Gallery app on your Android device.
  • Select the video from which you want to extract frames.
  • Scroll through the video to find the exact frame you want to capture.
  • Tap the capture icon to save the selected frame as an image.
  • A preview of the captured frame will appear at the bottom left of the screen.

The process typically varies depending on your device and the specific Gallery app you've installed.

  1. What are some common errors while extracting video frames using FFmpeg?
    1. One common error encountered while extracting video frames using FFmpeg is: [buffer @ 0x174efe0] Buffering several frames is not supported. Please consume all available frames before adding a new one.
      This typically suggests that there's a problem with handling the frames
    2. Another common error is: Could not write header for output file #0 (incorrect codec parameters ?): No such file or directory.
      This typically is caused by incorrect codec parameters or if the output path specified does not exist. 
Images or Videos Loading Slow?

We compress them so your users don't need to wait.