现在的位置: 首页 > 综合 > 正文

关于ffmpeg如何提取视频的关键帧的问题

2013年12月13日 ⁄ 综合 ⁄ 共 3858字 ⁄ 字号 评论关闭

用两种方式,一是利用ffmpeg提供的可执行文件进行提取,另外就是用ffmpeg的sdk,进行开发。我下面说一下如何使用ffmpeg sdk进行提取(假设把提取的关键帧保存成bmp,源文件名是sample.mpg):

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

  1. av_register_all();
  2.     if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)
  3.         printf("error!\n");
  4.     if(av_find_stream_info(pFormatCtx)<0)
  5.         printf("error!\n");
  6.     videoStream=-1;
  7.     for(i=0; i<pFormatCtx->nb_streams; i++)
  8.         if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
  9.         {
  10.             videoStream=i;
  11.             break;
  12.         }
  13.     if(videoStream==-1)
  14.         printf("error!\n");// Didn't find a video stream
  15.     // 得到视频流编码上下文的指针
  16.     pCodecCtx=pFormatCtx->streams[videoStream]->codec;
  1. 然后选择解码器进行解码:
  2.     AVCodec *pCodec;
  3.     //  寻找视频流的解码器
  4.     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  5.     if(pCodec==NULL)
  6.         printf("error!\n");// 找不到解码器
  7.     // 打开解码器
  8.     if(avcodec_open(pCodecCtx, pCodec)<0)
  9.         printf("error!\n"); // 打不开解码器
  10. 现在开始,进入解码和提取关键帧的过程:
  11.     pFrame=avcodec_alloc_frame();
  12.     pFrameRGB = avcodec_alloc_frame();
  13.     numBytes=avpicture_get_size(PIX_FMT_BGR24, pCodecCtx->width,pCodecCtx->height);
  14.     buffer=new uint8_t[numBytes];
  15.     avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height);
  16.     pSWSCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
  17.     i=0;
  18.     while(av_read_frame(pFormatCtx,&packet)>=0)
  19.     {
  20.         if(packet.stream_index==videoStream)
  21.         {
  22.             avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size);
  23.             if(frameFinished)
  24.             {
  25.                 if(pFrame->key_frame==1) // 这就是关键帧
  26.                 {
  27.                     sws_scale(pSWSCtx, pFrame->data, pFrame->linesize,0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
  28.                     // 保存到磁盘
  29.                     char pic[200];
  30.                     sprintf(pic,"pic%d.bmp",i);
  31.                     i++;
  32.                      av_create_bmp(pic,pFrameRGB->data[0],pCodecCtx->width,pCodecCtx->height,24);
  33.                 }
  34.             }
  35.         }
  36.         av_free_packet(&packet);
  37.     }
  38. 最后,释放资源和句
  1. // 释放 RGB 图象
  2.     av_free(pFrameRGB);
  3.     // 释放YUV 帧
  4.     av_free(pFrame);
  5.     sws_freeContext(pSWSCtx);
  6.     // 关闭解码器(codec)
  7.     avcodec_close(pCodecCtx);
  8.     // 关闭视频文件
  9.     av_close_input_file(pFormatCtx);

ffmpeg SDK就支持,以下代码是ffmpeg官方小组提供的

  1. int main()  
  2. {  
  3. SwsContext *pSWSCtx;  
  4. AVFormatContext *pFormatCtx;  
  5. const char *filename="sample.mpg";  
  6. int i,videoStream,y_size;  
  7. AVCodecContext *pCodecCtx;  
  8. AVFrame *pFrame;  
  9. AVFrame *pFrameRGB;  
  10. int     numBytes,frameFinished;  
  11. uint8_t *buffer;  
  12. static AVPacket packet;  
  13. av_register_all();  
  14. if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)  
  15. printf("error!\n");  
  16. if(av_find_stream_info(pFormatCtx)<0)  
  17. printf("error!\n");  
  18. dump_format(pFormatCtx, 0, filename, false);  
  19. videoStream=-1;  
  20. for(i=0; i<pFormatCtx->nb_streams; i++)  
  21. if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)  
  22. {  
  23. videoStream=i;  
  24. break;  
  25. }  
  26. if(videoStream==-1)  
  27. printf("error!\n");// Didn't find a video stream  
  28. pCodecCtx=pFormatCtx->streams[videoStream]->codec;  
  29. AVCodec *pCodec;  
  30. pCodec=avcodec_find_decoder(pCodecCtx->codec_id);  
  31. if(pCodec==NULL)  
  32. printf("error!\n");
  33. if(avcodec_open(pCodecCtx, pCodec)<0)  
  34. printf("error!\n");
  35. pFrame=avcodec_alloc_frame();  
  36. pFrameRGB = avcodec_alloc_frame();  
  37. numBytes=avpicture_get_size(PIX_FMT_BGR24, pCodecCtx->width,pCodecCtx->height);  
  38. buffer=new uint8_t[numBytes];  
  39. avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height);  
  40. pSWSCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);  
  41. i=0;  
  42. while(av_read_frame(pFormatCtx,&packet)>=0)  
  43. {  
  44. if(packet.stream_index==videoStream)  
  45. {  
  46. avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size);  
  47. if(frameFinished)  
  48. {  
  49. if(pFrame->key_frame==1)//这里取到关键帧数据  
  50. {  
  51. sws_scale(pSWSCtx, pFrame->data, pFrame->linesize,0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);  
  52. i++;    
  53. }  
  54. }  
  55. }  
  56. av_free_packet(&packet);  
  57. }  
  58. av_free(pFrameRGB);  
  59. av_free(pFrame);  
  60. sws_freeContext(pSWSCtx);  
  61. avcodec_close(pCodecCtx);  
  62. av_close_input_file(pFormatCtx);  
  63. return 0;  
  64. }

抱歉!评论已关闭.