C++ ffmpeg,C++;-在程序中获取fps

C++ ffmpeg,C++;-在程序中获取fps,c++,ffmpeg,C++,Ffmpeg,我有一个23.98 fps的视频,这可以从命令行中的Quicktime和ffmpeg中看到。OpenCV错误地认为它有23 fps。我感兴趣的是找到一种从ffmpeg中查找视频fps的编程方法。快速查看OpenCV源代码,可以看到以下内容: double CvCapture_FFMPEG::get_fps() { double fps = r2d(ic->streams[video_stream]->r_frame_rate); #if LIBAVFORMAT_BUILD

我有一个23.98 fps的视频,这可以从命令行中的Quicktime和ffmpeg中看到。OpenCV错误地认为它有23 fps。我感兴趣的是找到一种从ffmpeg中查找视频fps的编程方法。

快速查看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;
}
double-CvCapture\u FFMPEG::get\u fps()
{
双fps=r2d(ic->streams[视频流]->r\u帧速率);
#如果LIBAVFORMAT\u BUILD>=CALC\u FFMPEG\u版本(52111,0)
如果(fps流[视频流]->平均帧速率);
}
#恩迪夫
如果(fps流[视频流]->编解码器->时基);
}
返回fps;
}
看起来很对。可能通过此部件运行调试会话以验证此时的值?的
avg\u frame\u rate
是一个
AVRational
,因此它应该能够保持精确的值。如果您的代码由于较旧的ffmpeg版本而使用第二个if块,
time\u base
是否未设置正确

编辑


如果进行调试,请查看
r\u frame\u rate
avg\u frame\u rate
是否不同,因为至少根据使用的编解码器,它们往往不同。由于您没有提到视频格式,因此很难猜测,但似乎至少对于H264,您应该使用
avg_uuframe_urate
直接,并且从
r_uframe_urate
获得的值可能会把事情搞砸。
libavformat
版本
55.1.100
于2013-03-29发布,
av guess_uframe_urate()添加了

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

使用FFMPEG-C获取每秒视频帧(fps)值++

/* 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 */
 double videoFPS = av_q2d(ptrAVFormatContext->streams[VideoStreamIndx]->r_frame_rate);
 std::cout<<"fps :"<<videoFPS<<std::endl;
/*查找第一个流*/
对于(int i=0;inb_流;i++)
{
如果(pAVFormatContext->streams[i]->codec->codec\u type==AVMEDIA\u type\u VIDEO)
/*如果找到视频流,则获取索引*/
{
VideoStreamIndx=i;
打破
}
}
/*如果视频流不可用*/
如果((VideoStreamIndx)=-1)
{

std::cout我使用的ffmpeg fps命令行版本是
ffmpeg-I yourfilename here.avi-vcodec copy-acodec copy-f null/dev/null 2>&1 | grep'frame='| cut-f2-d'
在linux中或
ffmpeg-I yourfilename here.avi-vcodec copy-acodec copy-f null/dev/null 2>&1 | findstr'frame=
(xp以上)启动ffmpeg版本3.4时,AvcodeContext已降低了codec参数。此
pAVFormatContext->streams[i]->codec->codec\u type==AVMEDIA\u type\u VIDEO
现在需要更改为
pAVFormatContext->streams[i]->codecpar->codec\u type==AVMEDIA\u type\u VIDEO
在FFmpeg的avformat.h文件中,在r\u帧速率附近有一条注释,上面写着“注意,这个值只是一个猜测!”