日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

最新版ffmpeg 提取视频关键帧

發布時間:2025/3/21 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最新版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格式,保存函數如下:

void SaveBmp(AVCodecContext *CodecContex, AVFrame *Picture, int width, int height,int num) {AVPicture pPictureRGB;//RGB圖片static struct SwsContext *img_convert_ctx;img_convert_ctx = sws_getContext(width, height, CodecContex->pix_fmt, width, height,\PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);// 確認所需緩沖區大小并且分配緩沖區空間avpicture_alloc(&pPictureRGB, PIX_FMT_RGB24, width, height);sws_scale(img_convert_ctx, Picture->data, Picture->linesize,\0, height, pPictureRGB.data, pPictureRGB.linesize);int lineBytes = pPictureRGB.linesize[0], i=0;char fileName[1024]={0};char * bmpSavePath = "%d.bmp";//time_t ltime;//time(<ime);//sprintf(fileName,bmpSavePath , ltime);???????????????????????????sprintf(fileName,bmpSavePath , num);FILE *pDestFile = fopen(fileName, "wb");BITMAPFILEHEADER btfileHeader;btfileHeader.bfType = MAKEWORD(66, 77); btfileHeader.bfSize = lineBytes*height; btfileHeader.bfReserved1 = 0; btfileHeader.bfReserved2 = 0; btfileHeader.bfOffBits = 54;BITMAPINFOHEADER bitmapinfoheader;bitmapinfoheader.biSize = 40; bitmapinfoheader.biWidth = width; bitmapinfoheader.biHeight = height; bitmapinfoheader.biPlanes = 1; bitmapinfoheader.biBitCount = 24;bitmapinfoheader.biCompression = BI_RGB; bitmapinfoheader.biSizeImage = lineBytes*height; bitmapinfoheader.biXPelsPerMeter = 0; bitmapinfoheader.biYPelsPerMeter = 0; bitmapinfoheader.biClrUsed = 0; bitmapinfoheader.biClrImportant = 0;fwrite(&btfileHeader, 14, 1, pDestFile);fwrite(&bitmapinfoheader, 40, 1, pDestFile);for(i=height-1; i>=0; i--){fwrite(pPictureRGB.data[0]+i*lineBytes, lineBytes, 1, pDestFile);}fclose(pDestFile);avpicture_free(&pPictureRGB); }


?

總結

以上是生活随笔為你收集整理的最新版ffmpeg 提取视频关键帧的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。