Batch File FFmpeg: Output Every 5th Frame
In this article, we will focus on the topic of creating a batch file to use the FFmpeg "output every 5th frame" expression. We will cover key concepts, such as FFmpeg, batch files, and the select filter. By the end, you will have a solid understanding of how to extract every 5th frame from a video using a batch file and the FFmpeg select filter.
What is FFmpeg?
FFmpeg is a free and open-source software project consisting of a vast software suite of libraries and programs for handling video, audio, and other multimedia files and streams. It is widely used for formats conversion, streaming, and for manipulating multimedia data.
What is a Batch File?
A batch file is a script file in Windows operating systems that contains a series of commands to be executed by the command-line interpreter. Batch files are useful for running repetitive tasks or sequences of commands automatically.
FFmpeg Select Filter
The select filter is a powerful FFmpeg feature that allows you to select specific frames from a video based on defined criteria, like frame numbers. In our case, we want to output every 5th frame of a video.
Creating a Batch File for Outputting Every 5th Frame with FFmpeg
To create a batch file for this, follow these steps:
- Create a new text file with the extension
.bat. - Add the following code:
@echo off
setlocal EnableDelayedExpansion
set "input_file=input.mp4"
set "output_folder=output_frames"
if not exist "%output_folder%" mkdir "%output_folder%"
for /L %%i in (0, 5, 1000) do (
ffmpeg -i "!input_file!" -vf "select=mod(n,5),setpts=N/FRAME_RATE/TB" -q:v 2 -update 1 "!output_folder!\frame_%%i.png"
)
In the code above, we define the input file, output folder, and set the frame rate based on the input file's metadata. We then loop from 0 to 1000 and increment by 5, to extract every 5th frame. Replace input.mp4 with your input file name, and configure the output folder as desired.
FFmpeg Select Filter Expression
"select=mod(n,5)" specifies that every 5th frame should be output based on the frame number n and the modulo operation with a divisor 5.