赞
踩
- #include <iostream>
- #include <fstream>
- using namespace std;
- extern "C" { //指定函数是c语言函数,函数名不包含重载标注
- //引用ffmpeg头文件
- #include <libavcodec/avcodec.h>
- }
- //预处理指令导入库
- #pragma comment(lib,"avcodec.lib")
- #pragma comment(lib,"avutil.lib")
- int main(int argc, char* argv[])
- {
- //1 分割h264 存入AVPacket
- // ffmpeg -i v1080.mp4 -s 400x300 test.h264
- string filename = "test.h264";
- ifstream ifs(filename, ios::binary);
- if (!ifs)return -1;
- unsigned char inbuf[4096] = { 0 };//用于存储h264编码流
- AVCodecID codec_id = AV_CODEC_ID_H264;
-
-
- //1 找解码器
- auto codec = avcodec_find_decoder(codec_id);
-
- //2 创建解码器上下文
- auto c = avcodec_alloc_context3(codec);
-
- //3 打开上下文
- avcodec_open2(c, NULL, NULL);
-
-
- //分割上下文
- auto parser = av_parser_init(codec_id);
- auto pkt = av_packet_alloc();
- auto frame = av_frame_alloc();
- while (!ifs.eof())
- {
- ifs.read((char*)inbuf, sizeof(inbuf));//将h264编码的流读入inbuf中,一次读4096
- int data_size = ifs.gcount();// 读取的字节数,inbuf中的字节数
- if (data_size <= 0)break;
- auto data = inbuf;
- while (data_size > 0) //一次有多帧数据
- {
-
- //通过0001 截断输出到AVPacket 返回帧大小
- int ret = av_parser_parse2(parser, c,
- &pkt->data, &pkt->size, //截断后输出到AVpacket的data中
- data, data_size, //h264编码流,待处理数据
- AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0
- );//返回消耗的输入数据字节数ret。如果所有输入数据都被消耗,则返回值等于 buf_size。如果解析过程中出现错误,则返回负数。
- data += ret;//data指针向前移动 ret个字节,继续处理inbuf未处理的数据
- data_size -= ret; //待处理的数据大小
- if (pkt->size)
- {
- //cout << pkt->size << " "<<flush;
- //发送packet到解码线程
- ret = avcodec_send_packet(c, pkt);//把avpacket给解码器进行解码
- if (ret < 0)
- break;
- //获取多帧解码数据
- while (ret >= 0)
- {
- //每次回调用av_frame_unref
- ret = avcodec_receive_frame(c, frame);//拿到解码后的数据
- if (ret < 0)
- break;
- cout << frame->format << " " << flush;
- }
- }
- }
- }
- ///取出缓存数据,防止丢帧
- int ret = avcodec_send_packet(c, NULL);
- while (ret >= 0)
- {
- ret = avcodec_receive_frame(c, frame);
- if (ret < 0)
- break;
- cout << frame->format << "-" << flush;
- }
- av_parser_close(parser);
- avcodec_free_context(&c);
- av_frame_free(&frame);
- av_packet_free(&pkt);
-
- getchar();
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。