参考流媒体协议 https://cloud.tencent.com/developer/article/1461580
参考雷神 https://blog.csdn.net/leixiaohua1020/article/details/42658139
参考海康视频抓拍与推流 https://www.cnblogs.com/elesos/p/9881690.html
初始化:
//初始化解封装
av_register_all();
//初始化网络
avformat_network_init();
avcodec_register_all();
AVFormatContext *ic = NULL; //指定格式
输入文件操作:
avformat_open_input(&ic,path,0,0); //打开媒体的的过程开始于avformat_open_input
av_read_frame() //从输入文件中读取一个AVPacket
流操作:
int fps = 0; //帧率
avformat_find_stream_info(ic,0); //查找文件的格式或索引 (探测)“获取文件(流)中的信息
AVStream *avStream = ic->streams[i]; //流信息:如视频:宽高,帧率 如音频:采样率,样本大小,格式
avStream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO //通过codecpar分析它们是音频还是视频,以及所有信息的参数(宽高,大小 ,包括codec_id解码器id)
AVCodec *codec = avcodec_find_decoder(ic->streams[videoStream]->codecpar->codec_id);//通过解码器id实现软解码
codec = avcodec_find_decoder_by_name("h264_mediacodec");//通过名字获取实现硬解码
解码操作:
//解码器初始化
AVCodecContext *vc = avcodec_alloc_context3(codec);//把上面创建个codec引入
帧数据操作:
AVPacket *avPacket = av_packet_alloc();//包含创建一个对象空间并做初始化
AVFrame *frame = av_frame_alloc();
//发送到线程中解码
re = avcodec_send_packet(cc, avPacket);//传递视频解码器
输出文件操作:
avformat_alloc_output_context2() //初始化输出视频码流的AVFormatContext
avformat_new_stream() //创建输出码流的AVStream
avcodec_copy_context() //拷贝输入视频码流的AVCodecContex的数值t到输出视频的AVCodecContext
avio_open() //打开输出文件
avformat_write_header() //写文件头(对于某些没有文件头的封装格式,不需要此函数 比如说MPEG2TS)
av_interleaved_write_frame() //将AVPacket(存储视频压缩码流数据)写入文件
av_write_trailer() //写文件尾(对于某些没有文件头的封装格式,不需要此函数。比如说MPEG2TS)