ffmpeg 获取视频关键帧

来源:互联网 发布:摩拜单车盈利模式 知乎 编辑:程序博客网 时间:2024/04/28 08:22
  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):

首先获取文件中的视频流:

 

然后选择解码器进行解码:

view plain
  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"); // 打不开解码器  


现在开始,进入解码和提取关键帧的过程:

view plain
  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. }  


最后,释放资源和句柄

view plain
  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);  


ffmpeg相关知识
转自:http://fengqing888.blog.163.com/blog/static/330114162011111632548120/