ffmpeg 獲取視頻關鍵幀


1:http://blog.csdn.net/tipboy/article/details/7052484

2:ffmpeg教程:http://dranger.com/ffmpeg/tutorial01.html

 

  1. av_register_all();   
  2.    
  3.     if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)   
  4.         printf("error!\n");   
  5.    
  6.     if(av_find_stream_info(pFormatCtx)<0)   
  7.         printf("error!\n");   
  8.    
  9.     videoStream=-1;   
  10.    
  11.     for(i=0; i<pFormatCtx->nb_streams; i++)   
  12.         if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)   
  13.         {   
  14.             videoStream=i;   
  15.             break;   
  16.         }   
  17.     if(videoStream==-1)   
  18.         printf("error!\n");// Didn't find a video stream   
  19.    
  20.     // 得到視頻流編碼上下文的指針   
  21.     pCodecCtx=pFormatCtx->streams[videoStream]->codec;  


 

用兩種方式,一是利用ffmpeg提供的可執行文件進行提取,另外就是用ffmpeg的sdk,進行開發。我下面說一下如何使用ffmpeg sdk進行提取(假設把提取的關鍵幀保存成bmp,源文件名是sample.mpg):

首先獲取文件中的視頻流:

 

然后選擇解碼器進行解碼:

  1. AVCodec *pCodec;   
  2.    
  3. //  尋找視頻流的解碼器   
  4. pCodec=avcodec_find_decoder(pCodecCtx->codec_id);   
  5. if(pCodec==NULL)   
  6.     printf("error!\n");// 找不到解碼器   
  7.    
  8. // 打開解碼器   
  9. if(avcodec_open(pCodecCtx, pCodec)<0)   
  10.     printf("error!\n"); // 打不開解碼器  


現在開始,進入解碼和提取關鍵幀的過程:

  1. pFrame=avcodec_alloc_frame();   
  2. pFrameRGB = avcodec_alloc_frame();   
  3. numBytes=avpicture_get_size(PIX_FMT_BGR24, pCodecCtx->width,pCodecCtx->height);   
  4. buffer=new uint8_t[numBytes];   
  5. avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height);   
  6. pSWSCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);   
  7.    
  8. i=0;   
  9. while(av_read_frame(pFormatCtx,&packet)>=0)   
  10. {   
  11.     if(packet.stream_index==videoStream)   
  12.     {   
  13.         avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size);   
  14.         if(frameFinished)   
  15.         {   
  16.             if(pFrame->key_frame==1) // 這就是關鍵幀   
  17.             {   
  18.                 sws_scale(pSWSCtx, pFrame->data, pFrame->linesize,0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);   
  19.                 // 保存到磁盤   
  20.                 char pic[200];   
  21.                 sprintf(pic,"pic%d.bmp",i);   
  22.                 i++;   
  23.                  av_create_bmp(pic,pFrameRGB->data[0],pCodecCtx->width,pCodecCtx->height,24);   
  24.             }   
  25.         }   
  26.     }   
  27.     av_free_packet(&packet);   
  28. }  


最后,釋放資源和句柄

  1. // 釋放 RGB 圖象   
  2.     av_free(pFrameRGB);   
  3.     // 釋放YUV 幀   
  4.     av_free(pFrame);   
  5.    
  6.     sws_freeContext(pSWSCtx);   
  7.    
  8.    
  9.     // 關閉解碼器(codec)   
  10.     avcodec_close(pCodecCtx);   
  11.    
  12.     // 關閉視頻文件   
  13.     av_close_input_file(pFormatCtx);  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM