In FFmpeg, timestamps, time bases, and presentation time stamps are essential concepts for processing and converting video, as they relate to the precise representation and synchronization of video frames.
1. Timestamps
Timestamps indicate the specific time position of each frame or sample within a video or audio stream. During video processing and transcoding, accurate timestamps are crucial for smooth playback and synchronization of audio and video. Timestamps are typically represented as the time offset from the start of the video or audio stream.
2. Time Base
The time base defines the unit of time used to interpret timestamps. It is expressed as a fraction (numerator/denominator), such as 1/1000 indicating that each unit represents 1 millisecond. In FFmpeg, the AVStream structure contains a field called time_base that specifies the time base for timestamps in the stream.
3. Presentation Time Stamp (PTS)
The Presentation Time Stamp (PTS) refers to the timestamp indicating when a frame should be presented (displayed) during playback. It is a critical indicator for determining the display order of video frames. Due to the inclusion of B-frames (bidirectional predictive frames) in the encoding process, frames may differ between decoding order (DTS, Decoding Time Stamp) and display order. PTS ensures that video frames are displayed in the correct time and order even when B-frames are present.
Example
Suppose a video stream has a time base of 1/1200. If a frame has a PTS of 2400, then the display time for this frame should be 2400 * (1/1200) = 2 seconds. This means the frame should be displayed 2 seconds after the start of the video.
Summary
When using FFmpeg for video editing, transcoding, or streaming processing, understanding and correctly handling timestamps, time bases, and PTS is essential. These concepts ensure that video data is properly parsed, processed, and synchronized for playback. A common task when handling these time data is adjusting the time base to ensure compatibility with the output format or device.