当前位置:   article > 正文

FFmpeg获取是视频的关键帧_ffmpeg 获取视频关键帧

ffmpeg 获取视频关键帧

如何通过FFmpeg获取是视频的关键帧

av_register_all();   

    if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)   
        printf("error!\n");   

    if(av_find_stream_info(pFormatCtx)<0)   
        printf("error!\n");   

    videoStream=-1;   

    for(i=0; i<pFormatCtx->nb_streams; i++)   
        if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)   
        {   
            videoStream=i;   
            break;   
        }   
    if(videoStream==-1)   
        printf("error!\n");// Didn't find a video stream   

    // 得到视频流编码上下文的指针   
    pCodecCtx=pFormatCtx->streams[videoStream]->codec;  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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

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

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

AVCodec *pCodec;   

//  寻找视频流的解码器   
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);   
if(pCodec==NULL)   
    printf("error!\n");// 找不到解码器   

// 打开解码器   
if(avcodec_open(pCodecCtx, pCodec)<0)   
    printf("error!\n"); // 打不开解码器  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

pFrame=avcodec_alloc_frame();   
pFrameRGB = avcodec_alloc_frame();   
numBytes=avpicture_get_size(PIX_FMT_BGR24, pCodecCtx->width,pCodecCtx->height);   
buffer=new uint8_t[numBytes];   
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx->height);   
pSWSCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);   

i=0;   
while(av_read_frame(pFormatCtx,&packet)>=0)   
{   
    if(packet.stream_index==videoStream)   
    {   
        avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size);   
        if(frameFinished)   
        {   
            if(pFrame->key_frame==1) // 这就是关键帧   
            {   
                sws_scale(pSWSCtx, pFrame->data, pFrame->linesize,0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);   
                // 保存到磁盘   
                char pic[200];   
                sprintf(pic,"pic%d.bmp",i);   
                i++;   
                 av_create_bmp(pic,pFrameRGB->data[0],pCodecCtx->width,pCodecCtx->height,24);   
            }   
        }   
    }   
    av_free_packet(&packet);   
}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

最后,释放资源和句柄

// 释放 RGB 图象   
    av_free(pFrameRGB);   
    // 释放YUV 帧   
    av_free(pFrame);   

    sws_freeContext(pSWSCtx);   


    // 关闭解码器(codec)   
    avcodec_close(pCodecCtx);   

    // 关闭视频文件   
    av_close_input_file(pFormatCtx);  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/272151
推荐阅读
相关标签
  

闽ICP备14008679号