Extracting Every 30th Frame as 720p Images using FFmpeg
In this article, we will discuss how to extract every 30th frame from a video and save it as a 720p image using the FFmpeg tool. FFmpeg is a powerful, open-source tool that can handle a wide range of multimedia formats and perform various operations, such as video and audio encoding, decoding, transcoding, muxing, demuxing, filtering, and more.
Prerequisites
Before we start, make sure you have the following:
- A video file to extract frames from
- FFmpeg installed on your system
FFmpeg Command Structure
The basic structure of the FFmpeg command for extracting frames is as follows:
ffmpeg -i input.mp4 -vf "fps=1/30" -q:v 2 output_%04d.pngLet's break down the command:
-i input.mp4: Specifies the input video file-vf "fps=1/30": Sets the frame rate to extract frames (in this case, every 30th frame)-q:v 2: Sets the quality of the output images (a lower value results in higher quality)output_%04d.png: Specifies the output file name pattern for the extracted images
Resizing Images to 720p
To resize the extracted images to 720p, we can use the scale filter. The updated command is:
ffmpeg -i input.mp4 -vf "fps=1/30,scale=-2:720" -q:v 2 output_%04d.pngThe scale filter resizes the video frame to the specified height (720p) while maintaining the aspect ratio. The -2 value for the width specifies that FFmpeg should automatically calculate the width based on the aspect ratio.
Extracting Every 30th Frame
Now that we have the command to extract frames and resize them, we can modify it to extract every 30th frame. We can achieve this by using the mod filter:
ffmpeg -i input.mp4 -vf "fps=1/30,scale=-2:720,mod(n\,30)" -q:v 2 output_%04d.pngThe mod(n\,30) filter only passes every 30th frame to the output. The final command is:
ffmpeg -i input.mp4 -vf "fps=1/30,scale=-2:720,mod(n\,30)" -q:v 2 output_%04d.pngIn this article, we discussed how to extract every 30th frame from a video and save it as a 720p image using the FFmpeg tool. We covered the basic structure of the FFmpeg command and demonstrated how to resize images to 720p and extract every 30th frame using the scale and mod filters.