二维码
微世推网

扫一扫关注

当前位置: 首页 » 快闻头条 » 科普 » 正文

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

放大字体  缩小字体 发布日期:2018-06-08 14:27:36    作者:微世推-实习陈东旭    浏览次数:296
导读

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

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


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


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;

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

    AVCodec *pCodec;

    //  寻找视频流的解码器

    pCodec=avcodec_find_decoder(pCodecCtx- codec_id);

    if(pCodec==NULL)

        printf("error!\n");// 找不到解码器

    // 打开解码器

    if(avcodec_open(pCodecCtx, pCodec) 0)

        printf("error!\n"); // 打不开解码器

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

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

    }

最后,释放资源和句

// 释放 RGB 图象

    av_free(pframeRGB);

    // 释放YUV 帧

    av_free(pframe);

    sws_freeContext(pSWSCtx);

    // 关闭解码器(codec)

    avcodec_close(pCodecCtx);

    // 关闭视频文件

    av_close_input_file(pFormatCtx);

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


int main()   

{   

SwsContext *pSWSCtx;   

AVFormatContext *pFormatCtx;   

const char *filename="sample.mpg";   

int i,videoStream,y_size;   

AVCodecContext *pCodecCtx;   

AVframe *pframe;   

AVframe *pframeRGB;   

int     numBytes,frameFinished;   

uint8_t *buffer;   

static AVPacket packet;   

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");   

dump_format(pFormatCtx, 0, filename, false);   

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;   

AVCodec *pCodec;   

pCodec=avcodec_find_decoder(pCodecCtx- codec_id);   

if(pCodec==NULL)   

printf("error!\n");

if(avcodec_open(pCodecCtx, pCodec) 0)   

printf("error!\n"); 

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

i++;    

}   

}   

}   

av_free_packet( packet);   

}   

av_free(pframeRGB);   

av_free(pframe);   

sws_freeContext(pSWSCtx);   

avcodec_close(pCodecCtx);   

av_close_input_file(pFormatCtx);   

return 0;   

}

 
(文/微世推-实习陈东旭)
免责声明
• 
本文仅代表发布者:微世推-实习陈东旭个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,需自行承担相应责任。涉及到版权或其他问题,请及时联系我们删除处理邮件:weilaitui@qq.com。
 

Copyright©2015-2025 粤公网安备 44030702000869号

粤ICP备16078936号

微信

关注
微信

微信二维码

WAP二维码

客服

联系
客服

联系客服:

24在线QQ: 770665880

客服电话: 020-82301567

E_mail邮箱: weilaitui@qq.com

微信公众号: weishitui

韩瑞 小英 张泽

工作时间:

周一至周五: 08:00 - 24:00

反馈

用户
反馈