目录
一、编码分析
1.解码线程:
编辑2.编码线程:
编辑
编辑
二、ffmpeg编码
1.注册所有组件
2.编码初始化函数
(2)打开视频流
4.查找编码器
5. 写文件头信息,写到formatContex中
6.发送一帧数据给编码器
7.将像素数据转码压缩码流数据
8.写一帧数据到文件
一、编码分析
想要编码必须是解码时获取的YUV格式——> H264 ——>mp4等格式
1.解码线程:
2.编码线程:
二、ffmpeg编码
1.注册所有组件
av_register_all();
2.编码初始化函数
(1)选择文件后缀
//1.通过文件后缀得到最佳输出格式
AVOutputFormat* outputFormat=
av_guess_format(nullptr,"../fileout/Warcraft3_End.H264",nullptr);
if(outputFormat==nullptr)
{
qDebug()<<"av_guess_format fail";
}
else {
qDebug()<<"av_guess_format success";
}
//1.1 设置格式
this->formatContext->oformat=outputFormat;
(2)打开视频流
int res=avio_open(
&(this->formatContext->pb),"../fileout/Warcraft3_End.H264",AVIO_FLAG_WRITE);
if(res<0)
{
qDebug()<<"avio_open fail";
}
else {
qDebug()<<"avio_open success";
}
(3)新建视频流
//2.1 新建视频流
AVStream *newStream=avformat_new_stream(this->formatContext,nullptr);
if(newStream==nullptr)
{
qDebug()<<"avformat_new_stream fail";
}
else {
qDebug()<<"avformat_new_stream success";
}
//2.2 编码器上下文环境
this->codecContext=newStream->codec;
//2.3 设置信息
this->codecContext->width=800;//编码视频文件宽 (根据实际宽高改变)
this->codecContext->height=368;//编码视频文件高
this->codecContext->bit_rate=579000;//编码视频文件码率
this->codecContext->framerate={24,1};//编码视频文件帧率
this->codecContext->time_base={1,24};//编码视频文件时间基
//2.4 设置高级信息
this->codecContext->gop_size=10;//I/P/B 以10帧为一组
this->codecContext->qmax=51;//清晰度
this->codecContext->qmin=10;//清晰度
this->codecContext->max_b_frames=0;//B压缩为0
this->codecContext->pix_fmt=AV_PIX_FMT_YUV420P;
this->codecContext->codec_type=AVMEDIA_TYPE_VIDEO;
this->codecContext->codec_id=outputFormat->video_codec;
4.查找编码器
//4.查找编码器
AVCodec *codec=avcodec_find_encoder(this->codecContext->codec_id);
//4.1 打开编码器
res=avcodec_open2(this->codecContext,codec,nullptr);
if(res!=0)
{
qDebug()<<"avcodec_open2 fail";
}else {
qDebug()<<"avcodec_open2 success";
}
5. 写文件头信息,写到formatContex中
//4. 写文件头信息,写到formatContex中
res=avformat_write_header(this->formatContext,nullptr);
if(res<0)
{
qDebug()<<"avformat_write_header fail";
}else {
qDebug()<<"avformat_write_header success";
}
6.发送一帧数据给编码器
int res=avcodec_send_frame(this->codecContext,yuv);
if(res!=0)
{
qDebug()<<"avcodec_send_frame fail";
}else {
qDebug()<<"avcodec_send_frame success";
}
7.将像素数据转码压缩码流数据
res=avcodec_receive_packet(this->codecContext,this->pkt);
if(res!=0)
{
qDebug()<<"avcodec_receive_packet fail";
break;
}else {
qDebug()<<"avcodec_receive_packet success";
}
8.写一帧数据到文件
res=av_interleaved_write_frame(this->formatContext,this->pkt);
if(res!=0)
{
qDebug()<<"av_interleaved_write_frame fail";
}else {
qDebug()<<"av_interleaved_write_frame success"<<page;
}
9.写尾帧
void EncodeVideo::writeTailter()
{
//写尾帧信息
av_write_trailer(this->formatContext);
//关闭视频流
avio_close(this->formatContext->pb);
//关闭视频流上下文
avformat_free_context(this->formatContext);
}
#include "encodevideo.h"EncodeVideo::EncodeVideo():QThread()
{this->register_all();this->formatContext=avformat_alloc_context();this->pkt=av_packet_alloc();this->pktIndex=0;}EncodeVideo::~EncodeVideo()
{}void EncodeVideo::register_all()
{//注册所有组件av_register_all();
}void EncodeVideo::initEncode()
{//1.通过文件后缀得到最佳输出格式AVOutputFormat* outputFormat=av_guess_format(nullptr,"../fileout/Warcraft3_End.H264",nullptr);if(outputFormat==nullptr){qDebug()<<"av_guess_format fail";}else {qDebug()<<"av_guess_format success";}//1.1 设置格式this->formatContext->oformat=outputFormat;//2.打开视频流int res=avio_open(&(this->formatContext->pb),"../fileout/Warcraft3_End.H264",AVIO_FLAG_WRITE);if(res<0){qDebug()<<"avio_open fail";}else {qDebug()<<"avio_open success";}//2.1 新建视频流AVStream *newStream=avformat_new_stream(this->formatContext,nullptr);if(newStream==nullptr){qDebug()<<"avformat_new_stream fail";}else {qDebug()<<"avformat_new_stream success";}//2.2 编码器上下文环境this->codecContext=newStream->codec;//2.3 设置信息this->codecContext->width=800;//编码视频文件宽this->codecContext->height=368;//编码视频文件高this->codecContext->bit_rate=579000;//编码视频文件码率this->codecContext->framerate={24,1};//编码视频文件帧率this->codecContext->time_base={1,24};//编码视频文件时间基//2.4 设置高级信息this->codecContext->gop_size=10;//I/P/B 以10帧为一组this->codecContext->qmax=51;//清晰度this->codecContext->qmin=10;//清晰度this->codecContext->max_b_frames=0;//B压缩为0this->codecContext->pix_fmt=AV_PIX_FMT_YUV420P;this->codecContext->codec_type=AVMEDIA_TYPE_VIDEO;this->codecContext->codec_id=outputFormat->video_codec;//3. 查找编码器AVCodec *codec=avcodec_find_encoder(this->codecContext->codec_id);//3.1 打开编码器res=avcodec_open2(this->codecContext,codec,nullptr);if(res!=0){qDebug()<<"avcodec_open2 fail";}else {qDebug()<<"avcodec_open2 success";}//4. 写文件头信息,写到formatContex中res=avformat_write_header(this->formatContext,nullptr);if(res<0){qDebug()<<"avformat_write_header fail";}else {qDebug()<<"avformat_write_header success";}
}void EncodeVideo::run()
{//队列有两帧画面在取第一帧,可以避免资源争抢while(1){if(YuvQueue.size()==0){continue;}//从队列取一帧数据AVFrame *yuv=YuvQueue.dequeue();//5.发送一帧数据给编码器int res=avcodec_send_frame(this->codecContext,yuv);if(res!=0){qDebug()<<"avcodec_send_frame fail";}else {qDebug()<<"avcodec_send_frame success";}while(res>=0){//下标yuv->pts=this->pktIndex++;qDebug()<<"this->pktIndex="<<this->pktIndex;//6.将像素数据转码压缩码流数据res=avcodec_receive_packet(this->codecContext,this->pkt);if(res!=0){qDebug()<<"avcodec_receive_packet fail";break;}else {qDebug()<<"avcodec_receive_packet success";}//7.写一帧数据到文件res=av_interleaved_write_frame(this->formatContext,this->pkt);if(res!=0){qDebug()<<"av_interleaved_write_frame fail";}else {qDebug()<<"av_interleaved_write_frame success"<<page;}}//释放av_packet_unref(this->pkt);}
}void EncodeVideo::reciverYUV(AVFrame *fram)
{//队列入队YuvQueue.enqueue(fram);
}void EncodeVideo::CloseFile()
{this->writeTailter();qDebug()<<"writeTailter";
}void EncodeVideo::writeTailter()
{//写尾帧信息av_write_trailer(this->formatContext);//关闭视频流avio_close(this->formatContext->pb);//关闭视频流上下文avformat_free_context(this->formatContext);
}