Extract Keyframe Timestamps from Video Using FFmpeg: A Comprehensive Guide
In this article, we will provide a detailed guide on how to extract keyframe timestamps from a video file using the powerful FFmpeg tool. FFmpeg is a free and open-source project consisting of a vast software suite that provides various multimedia-related capabilities, such as encoding, decoding, muxing, demuxing, filtering, and streaming audio and video.
What are Keyframes?
In video compression, a keyframe (also known as an I-frame) is a frame that contains a complete image. Consecutive frames that follow a keyframe, called P-frames and B-frames, only store the differences between the current and previous frames. Keyframes are used to provide reference points for decoding a video, as they contain the complete image data.
Why Extract Keyframe Timestamps?
Extracting keyframe timestamps can be useful in various scenarios, such as video editing, analytics, or forensics. By having a list of keyframe timestamps, you can quickly navigate to specific points in a video or use the data for further processing.
Using FFmpeg to Extract Keyframe Timestamps
FFmpeg provides a powerful command-line interface to extract keyframe timestamps. To do this, you can use the following command:
ffprobe -show_frames -select_streams v:0 -log_level error video.mp4Here's a breakdown of the command:
ffprobe: FFmpeg's probing tool to gather information from multimedia files.-show_frames: Shows frame-specific data.-select_streams v:0: Selects the first video stream.-log_level error: Reduces the output verbosity.video.mp4: Replace it with your video file name.
Running this command will provide a list of frames in the video. Keyframes will have a pkey_frame value of 1. You can filter the output to only display keyframes by using grep:
ffprobe -show_frames -select_streams v:0 -log_level error video.mp4 | grep pkey_frame=1Formatting the Output
You can format the output to extract only the timestamp and frame number by using awk:
ffprobe -show_frames -select_streams v:0 -log_level error video.mp4 | grep pkey_frame=1 | awk '{print $12 " " $13}'This command formats the output as follows:
media_time_ms[0] presentation_time_ms[0]You can convert the output to a more readable format (e.g., seconds) using bc:
ffprobe -show_frames -select_streams v:0 -log_level error video.mp4 | grep pkey_frame=1 | awk '{print ($12 + 0.0) / 1000 " " ($13 + 0.0) / 1000}'FFmpeg is a powerful multimedia processing tool that can be used for various tasks, such as extracting keyframe timestamps from a video file. You can follow the steps provided in this article to extract keyframe timestamps using FFmpeg and format the output as desired.