Using FFmpeg to Extract Every 30th Frame: Actual Frame Numbers
FFmpeg is a powerful, open-source tool used for handling multimedia files, including audio, video, and images. In this article, we will focus on using FFmpeg to extract every 30th frame from a video, while displaying the actual frame numbers. This technique can be useful for various applications, such as video analysis, frame-by-frame comparisons, or creating thumbnails.
Prerequisites
Before proceeding, make sure you have FFmpeg installed on your system. You can download it from the official website: https://ffmpeg.org/download.html.
FFmpeg Command Structure
The basic structure of the FFmpeg command for extracting frames is as follows:
ffmpeg -i input.mp4 -vf "select='not(mod(n,30))',showinfo" -f null -Here's a breakdown of the command:
-i input.mp4: Specifies the input video file-vf "select='not(mod(n,30))',showinfo": Video filter that selects every 30th frame using the modulo operation and displays information about the selected frames-f null -: Forces FFmpeg to only show the selected frames without saving them to a file
Displaying Actual Frame Numbers
To display the actual frame numbers, we need to modify the FFmpeg command:
ffmpeg -i input.mp4 -vf "select='not(mod(n,30))',showinfo" -f null -loglevel error 2>&1 | grep "pts_time" | awk '{print $3}' | cut -d'.' -f1 | sed 's/[[:space:]]//g' | cat -nHere's what each part of the command does:
grep "pts_time": Filters the output to only show the lines containing the presentation timestampawk '{print $3}': Extracts the third field, which contains the timestampcut -d'.' -f1: Splits the timestamp by the decimal point and keeps only the integer partsed 's/[[:space:]]//g': Removes any whitespace characters from the outputcat -n: Displays line numbers, which represent the actual frame numbers
FFmpeg is a powerful tool for extracting frames from videos. By combining filtering and parsing commands, you can extract every 30th frame while displaying the actual frame numbers. This technique can be useful for various applications, such as video analysis, frame-by-frame comparisons, or creating thumbnails.