2013-12-17 182 views
1

我有一個視頻,它具有23.98 fps,這可以從命令行中的Quicktime和ffmpeg中看到。錯誤地,OpenCV認爲它有23 fps。我有興趣從ffmpeg中找到一個編程方式來查找視頻fps'。ffmpeg,C++ - 在程序中獲取fps

+0

命令行版本的ffmpeg的FPS我用的就是每秒(fps)的值的視頻幀'的ffmpeg -i yourfilenamehere.avi -vcodec複製-acodec副本-f null/dev/null 2>&1 | grep'frame ='| cut -f 2 -d'''in linux或'ffmpeg -i yourfilenamehere.avi -vcodec copy -acodec copy -f null/dev/null 2>&1 | findstr'frame =''在窗口中(xp向上) – GMasucci

回答

1

快速查看OpenCV的來源顯示如下:

double CvCapture_FFMPEG::get_fps() 
{ 
    double fps = r2d(ic->streams[video_stream]->r_frame_rate); 

#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0) 
    if (fps < eps_zero) 
    { 
     fps = r2d(ic->streams[video_stream]->avg_frame_rate); 
    } 
#endif 

    if (fps < eps_zero) 
    { 
     fps = 1.0/r2d(ic->streams[video_stream]->codec->time_base); 
    } 

    return fps; 
} 

所以看起來很正確。也許通過這個部分運行一個調試會話來驗證這個值? AVStreamavg_frame_rateAVRational,所以它應該能夠保持精確的值。也許如果你的代碼使用第二個if塊,由於舊的ffmpeg版本time_base設置不正確?

編輯

如果調試看看,如果r_frame_rateavg_frame_rate不同,因爲至少根據this他們往往基於用於編解碼器不同。由於您沒有提及視頻格式,所以很難猜測,但似乎至少在H264中,您應該直接使用avg_ frame_rate,並且從r_frame_rate獲得的值可能會造成混亂。

0

libavformat版本55.1.100發佈於2013-03-29,av_guess_frame_rate()已添加。

/** 
* Guess the frame rate, based on both the container and codec information. 
* 
* @param ctx the format context which the stream is part of 
* @param stream the stream which the frame is part of 
* @param frame the frame for which the frame rate should be determined, may be NULL 
* @return the guessed (valid) frame rate, 0/1 if no idea 
*/ 
AVRational av_guess_frame_rate(AVFormatContext *ctx, AVStream *stream, AVFrame *frame); 
1

要使用FFMPEG-C++

/* find first stream */ 
for(int i=0; i<pAVFormatContext->nb_streams ;i++) 
{ 
if(pAVFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) 
/* if video stream found then get the index */ 
{ 
    VideoStreamIndx = i; 
    break; 
} 
} 


/* if video stream not availabe */ 
if((VideoStreamIndx) == -1) 
{ 
    std::cout<<"video streams not found"<<std::endl; 
    return -1; 
} 
/* get video fps */ 
int videoFPS = av_q2d(ptrAVFormatContext->streams[VideoStreamIndx]->r_frame_rate); 
std::cout<<"fps :"<<videoFPS<<std::endl;