FFMPEG timestamp conception and Unit Conversion

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));

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值