AV_TIME_BASE
ffmpeg中的內部計時單位(時間基),ffmepg中的所有時間都是于它為一個單位,AV_TIME_BASE定義為:
#define AV_TIME_BASE 1000000
其實是一種分數的表示形式,其中的1表示分子, AV_TIME_BASE 也就是1000000,表示的是分母,所以它其實就是1微秒,也就是 1/1000000 秒。
比如一段時長為60s的視頻,讀出來 duration為60000000,即 duration/AV_TIME_BASE = 60000000/1000000 = 60s
AVRational
AVRational在ffmpeg中的定義為
/**
* Rational number (pair of numerator and denominator).
*/
typedef struct AVRational{
int num; ///< Numerator
int den; ///< Denominator
} AVRational;
num為分數,den為分母.AVRational表示的其實是一個有理數.
在ffmpeg中在很多地方 會用AVRational time_base 表示一個時間基,即一個時間單元.比如要算一個視頻幀的PTS(顯示時間)
double video_time = frame->best_effort_timestamp * av_q2d(time_base);
- best_effort_timestamp是讀取到的視頻幀pts
- time_base 為 formatContext->streams[i]->time_base 即視頻流的時間基,類型為AVRational, 表示每個時間單位為 num/den 秒
- This is the fundamental unit of time (in seconds) in terms
- of which frame timestamps are represented.
- av_q2d 定義為
static inline double av_q2d(AVRational a){
return a.num / (double) a.den;
}
就是一個分子處分母的運算
假設讀到的 best_effort_timestamp 為 60000000, time_base 為 1 / 1000000, 則計算出來
video_time = 60000000 * (1/1000000) = 60. 表示這一幀在60s處顯示