博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FFmpeg 解码
阅读量:2345 次
发布时间:2019-05-10

本文共 2196 字,大约阅读时间需要 7 分钟。

#define BUFFERSIZE 1024//注册所支持的所有的文件(容器)格式及其对应的CODEC av_register_all()    /*     av_register_all该函数注册支持的所有的文件格式(容器)及其对应的CODEC,只需要调用一次,故一般放在main函数中。也可以注册某个特定的容器格式,但通常来说不需要这么做     */    av_register_all();    AVFormatContext *pFormatCtx;    pFormatCtx = avformat_alloc_context();    char url[BUFFERSIZE];    bzero(&url, sizeof(BUFFERSIZE));    //打开文件 avformat_open_input() 对应的是 avformat_close_input()    /*     avformat_open_input该函数读取文件的头信息,并将其信息保存到AVFormatContext结构体中。其调用如下     AVFormatContext* pFormatCtx = nullptr;     avformat_open_input(&pFormatCtx, filenName, nullptr, nullptr)     第一个参数是AVFormatContext结构体的指针,第二个参数为文件路径;第三个参数用来设定输入文件的格式,如果设为null,将自动检测文件格式;第四个参数用来填充AVFormatContext一些字段以及Demuxer的private选项。     AVFormatContext包含有较多的码流信息参数,通常由avformat_open_input创建并填充关键字段。     */    avformat_open_input(&pFormatCtx, url, NULL, NULL);    //从文件中提取流信息 avformat_find_stream_info()    avformat_find_stream_info(pFormatCtx, NULL);    //在多个数据流中找到视频流 video stream(类型为MEDIA_TYPE_VIDEO)    int index = -1;    AVCodecContext *pCodecCtx;    for (int i=0; i
nb_streams; i++) { if (pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) { index = i; pCodecCtx = pFormatCtx->streams[i]->codec; break; } } if (index<0) { printf("can not find stream"); } //查找video stream 相对应的解码器 avcodec_find_decoder AVCodec *pCodec =avcodec_find_decoder(pCodecCtx->codec_id); //打开解码器 avcodec_open2() int open_ret =avcodec_open2(pCodecCtx, pCodec, NULL); if (open_ret<0) { printf("open coder failed"); return -1; } //为解码帧分配内存 av_frame_alloc() AVFrame *picFrame = av_frame_alloc(); AVPacket package ; //从流中读取读取数据到Packet中 av_read_frame() while (av_read_frame(pFormatCtx, &package)>=0) { if (package.stream_index==index) { int finished = 0; // 对video 帧进行解码,调用 avcodec_decode_video2() avcodec_decode_video2(pCodecCtx, picFrame, &finished, &package); if (finished) { //do something } } } //关闭 avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); av_frame_free(&picFrame); av_packet_unref(&package);

转载地址:http://pqnvb.baihongyu.com/

你可能感兴趣的文章
Java工程结构管理(BuildPath/系统库/外部库)
查看>>
将代码托管到Coding
查看>>
Myeclipse/Eclipse在pull时发生冲突报错Checkout conflict with files
查看>>
JS-异步提交表单的几种方式
查看>>
Http请求中的Context-Type及其SpringMVC中的使用
查看>>
SpringMVC实现上传下载文件
查看>>
AJAX异步提交表单(表单数据有文件流、字符串)
查看>>
MyBatis中遇到的一些问题
查看>>
在JSP使用EL和JSTL判断指定元素是否存在于指定集合中
查看>>
JDBC学习笔记
查看>>
Activiti配置Oracle不能自动创建表/流程启动是表或试图不存在的问题
查看>>
Oracle学习笔记
查看>>
JQuery EasyUI学习总结
查看>>
AJAX学习笔记
查看>>
order by子句
查看>>
JavaEE知识了解
查看>>
Servlet学习总结
查看>>
JSP学习总结
查看>>
数据库连接池
查看>>
移动开发一点知识
查看>>