当前位置:
首页 >
最新版ffmpeg 提取视频关键帧
发布时间:2025/3/21
48
豆豆
生活随笔
收集整理的这篇文章主要介绍了
最新版ffmpeg 提取视频关键帧
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
(如果有转载的请注明哈)
对于ffmpeg的配置请看我的上篇博客:http://blog.csdn.net/kuaile123/article/details/11367309
所用视频为 flv格式的,用的vs2010,电脑为64位,下面的也是64位,别下错了。
因为ffmpeg的函数和版本有关系,这里记录下我所用的整合的版本,是昨天下的最新版的,需要请下载
http://download.csdn.net/detail/kuaile123/6232827(因为博主没有积分可用了,所以需要积分)
32位的请去官网下载。
从网上找到的都是旧版本的函数,函数的讲解可用直接自己看里面include中的.h文件,自己根据新版的文件自己弄出来的。
需要用到libavformat 用来处理解析视频文件并将包含在其中的流分离出来, 而libavcodec 则处理原始音频和视频流的解码
还是上代码:
完整的代码下载:http://download.csdn.net/detail/kuaile123/6232905
//注册库中含有的所有可用的文件格式和编码器,这样当打开一个文件时,它们才能够自动选择相应的文件格式和编码器。av_register_all();int ret;// 打开视频文件if((ret=avformat_open_input(&pInputFormatContext, sourceFile, NULL, NULL))!=0){cout<<" can't open file "<<endl;return -1;}// 取出文件流信息if(avformat_find_stream_info(pInputFormatContext,NULL)<0){ cout<<" can't find suitable codec parameters"<<endl;return -1;}//用于诊断 //产品中不可用//dump_format(pInputFormatContext, 0, sourceFile, false);//仅仅处理视频流//只简单处理我们发现的第一个视频流// 寻找第一个视频流int videoIndex = -1;for(int i=0; i<pInputFormatContext->nb_streams; i++) {if(pInputFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){videoIndex = i;break;}} if(-1 == videoIndex){cout<<" can't find video stream !"<<endl;return -1;}// 得到视频流编码上下文的指针pInputCodecContext = pInputFormatContext->streams[videoIndex]->codec; // 寻找视频流的解码器pInputCodec = avcodec_find_decoder(pInputCodecContext->codec_id); if(NULL == pInputCodec){cout<<"can't decode "<<endl;return -1;}// 通知解码器我们能够处理截断的bit流,bit流帧边界可以在包中//视频流中的数据是被分割放入包中的。因为每个视频帧的数据的大小是可变的,//那么两帧之间的边界就不一定刚好是包的边界。这里,我们告知解码器我们可以处理bit流。if(pInputCodec->capabilities & CODEC_CAP_TRUNCATED){pInputCodecContext->flags|=CODEC_FLAG_TRUNCATED;}//打开解码器if(avcodec_open2(pInputCodecContext, pInputCodec,NULL) != 0) {cout<<"decode error"<<endl;return -1;}int videoHeight;int videoWidth;videoWidth = pInputCodecContext->width;videoHeight = pInputCodecContext->height; AVPacket InPack;int len = 0;AVFrame OutFrame;int nComplete=0;
int nFrame = 0;AVRational avRation = pInputCodecContext->time_base;float frameRate = (float)avRation.den/avRation.num;//av_seek_frame(pInputFormatContext,0);while((av_read_frame(pInputFormatContext, &InPack) >= 0)){len = avcodec_decode_video2(pInputCodecContext, &OutFrame, &nComplete, &InPack); //判断是否是关键帧if(nComplete > 0 && OutFrame.key_frame){ //解码一帧成功SaveBmp(pInputCodecContext, &OutFrame, videoWidth, videoHeight,nFrame); nFrame++;}}cout<<" save frame number: "<<nFrame<<endl;avcodec_close(pInputCodecContext); av_free(pInputFormatContext);
保存为bmp格式,保存函数如下:
总结
以上是生活随笔为你收集整理的最新版ffmpeg 提取视频关键帧的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: c++ 调用 ffmpeg 编程
- 下一篇: libSVM介绍