1. FFMPEGtimestamp conception and Unit Conversion
a. FFMPEG timebase is presentationby AVRational
typedef struct AVRational{
int num; ///< numerator
int den; ///< denominator
} AVRational;
Convert rational to double call av_q2d(AVRationala) function
b. FFMPEG internal timebase macrodefine
#define AV_TIME_BASE 1000000
#define AV_TIME_BASE_Q (AVRational){1, AV_TIME_BASE}
AV_TIME_BASE : FFmpeg internal timebase and timestamp definitions, the unit is microsecond.
Convert time to ffmpeg internal timestamp:
timestamp(ffmpeg internal timestamp us) = AV_TIME_BASE * time(second)
Convert ffmpeg internal timestamp to time(second)
time(second) = AV_TIME_BASE_Q * timestamp(ffmpeg internal timebase)
Example:
Media Duration(s) = AVFormatContext.duration / AV_TIME_BASE
Sreaming_start_time(s) = AVFormatContext.start_time/ AV_TIME_BASE
Getting DTS/PTS
DTS decides when send frame to decode mode.
AVStream .time_base’s fundamental unit of time is in seconds
Convert dts in second units
DTS(s) = packet.dts * av_q2d(AVStream .time_base)
2. Seekfunction
Media player supports seek to any positionof media file then start playing
Through av_seek_frame or avformat_seek_file function to implementseek feature
av_seek_frame(AVFormatContext*s, int stream_index, int64_t timestamp,
int flags)
if stream_index is -1, timestamp’s unit will be ffmpeg internaltimestamp unit,so we need convert in AV_TIME_BASE units(us)
if stream_index is the selected by specify AVStream index,the timestamps are in units of the stream_index, So we need convert to in AVStream.time_baseunits
flags : AVSEEK_FLAG_BACKWARD // Seek the nearest IDR frame backward
AVSEEK_FLAG_ANY // seek to any frame, evennon-keyframes
AVSEEK_FLAG_FRAME //seeking based on frame number
Commonly we set the flag to AVSEEK_FLAG_BACKWARD
3. Getmedia duration and video frame rate
Media duration = AVFormatContext. Duration,the unit is in AV_TIME_BASE
Function av_guess_frame_rate to get framerate
frame_rate =av_q2d(av_guess_frame_rate(context,context->streams[i],NULL));