如何强制ffmpeg编码时输出一个关键帧

如何强制ffmpeg编码时输出一个关键帧

如何强制ffmpeg编码时输出一个关键帧

AVCodecContext *c     //编码器环境句柄
AVFrame* f                 //需要编码的一帧视频
/*在avcodec.h文件中有这样的定义
#define FF_I_TYPE  1 ///< Intra
#define FF_P_TYPE  2 ///< Predicted
#define FF_B_TYPE  3 ///< Bi-dir predicted
#define FF_S_TYPE  4 ///< S(GMC)-VOP MPEG4
#define FF_SI_TYPE 5 ///< Switching Intra
#define FF_SP_TYPE 6 ///< Switching Predicted
#define FF_BI_TYPE 7
*/
在编码前设置
f->pict_type=FF_I_TYPE; 
f->key_frame=1; 
注:该帧为I帧时,f->pict_type==FF_I_TYPE  && f->key_frame==1
然后编码
*outsize = avcodec_encode_video(c, temp, outbuf_size, f);
则编码之后通过如下参数判断是否为关键帧:
key_frame=c->coded_frame->key_frame; 
pict_type=c->coded_frame->pict_type;
key_frame==FF_I_TYPE && pict_type==1














ffmpeg如何提取视频的关键帧

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










live555+ffmpeg如何提取关键帧(I帧,P帧,B帧)

live555+ffmpeg如何提取关键帧(I帧,P帧,B帧)

开发流媒体播放器的时候,特别是在windows  mobile,symbian(S60)平台开发时,很可能遇到需要自己开发播放器的情况。

S60平台提供了CVideoPlayUtility接口可以实现流媒体播放器,但由于非开源,所以相对于自己开发播放器,很多操作受到限制。

live555主要用于网络流接收,ffmpeg则是对接收到的数据进行编码/解码

I帧,P帧,B帧是视频流中三种分类,其中I帧也就是关键帧是基础帧,P帧一般根据I帧确定,而B帧需要前面两着的信息。

举例说:

the Input sequence for video encoder

1  2   3   4   5    6   7

I    B   B   P  B   B   I

 

Let's take 1,2,3.. as PTS for simplification

 

the out sequence for video encoder ( this equals the decoder sequence)

1  4   2    3   7   5   6

I   P    B   B   I    B   B

 

播放器LIVE555收到的序列顺序就应该是:

 

1  4  2  3  7  5  6 

 

经过解码器解码,顺序又回到1 2 3 4 5 6 7这种正常顺序。

 

所以我们可以根据avcodec_decode_video来判断帧别。

avcodec_decode_video之后的顺序是一定的。严格按照1 2 3 4。。。这样的顺序来。

判断I帧,P,B帧方法:

(1):假如解码成功,则不是I帧就是P帧(根据AVFrame->keyframe判断是否是I帧)。

假如不是I帧,也不是P帧,则只能是B帧(通过pts判断)。

(2):采用AVFrame->pict_type综合pts的办法:

if(FF_I_TYPE==picture->pict_type)

                        {

                                Printlog("<II>");

                        }

                        else if(FF_P_TYPE==picture->pict_type)

                        {

                                Printlog("<PP>");

                        }

                        else if(FF_B_TYPE==picture->pict_type)

                        {

                                Printlog("<BB>");

                        }

                        else if(FF_S_TYPE==picture->pict_type)

                        {

                                Printlog("<SS>");

                        }

                        else

                        {

                                Printlog("<OtherType>");

                        }

正常情况下是不会打印出B帧的,因为解码成功的肯定是I帧或者是P帧.

你可能感兴趣的:(如何强制ffmpeg编码时输出一个关键帧)