|
|
@ -2,9 +2,7 @@
|
|
|
|
// Cffmpeg_decode::Cffmpeg_decode() {
|
|
|
|
// Cffmpeg_decode::Cffmpeg_decode() {
|
|
|
|
Cffmpeg_decode::Cffmpeg_decode(QObject *parent) : QObject(parent) {
|
|
|
|
Cffmpeg_decode::Cffmpeg_decode(QObject *parent) : QObject(parent) {
|
|
|
|
inputFormatCtx = avformat_alloc_context();
|
|
|
|
inputFormatCtx = avformat_alloc_context();
|
|
|
|
outputFormatCtx = avformat_alloc_context();
|
|
|
|
|
|
|
|
inputPacket = av_packet_alloc();
|
|
|
|
inputPacket = av_packet_alloc();
|
|
|
|
outputPacket = av_packet_alloc();
|
|
|
|
|
|
|
|
yuvFrame = av_frame_alloc();
|
|
|
|
yuvFrame = av_frame_alloc();
|
|
|
|
rgbFrame = av_frame_alloc();
|
|
|
|
rgbFrame = av_frame_alloc();
|
|
|
|
avformat_network_init();
|
|
|
|
avformat_network_init();
|
|
|
@ -13,8 +11,6 @@ Cffmpeg_decode::Cffmpeg_decode(QObject *parent) : QObject(parent) {
|
|
|
|
Cffmpeg_decode::~Cffmpeg_decode() {
|
|
|
|
Cffmpeg_decode::~Cffmpeg_decode() {
|
|
|
|
if (!inputPacket)
|
|
|
|
if (!inputPacket)
|
|
|
|
av_packet_free(&inputPacket);
|
|
|
|
av_packet_free(&inputPacket);
|
|
|
|
if (!outputPacket)
|
|
|
|
|
|
|
|
av_packet_free(&outputPacket);
|
|
|
|
|
|
|
|
if (!yuvFrame)
|
|
|
|
if (!yuvFrame)
|
|
|
|
av_frame_free(&yuvFrame);
|
|
|
|
av_frame_free(&yuvFrame);
|
|
|
|
if (!rgbFrame)
|
|
|
|
if (!rgbFrame)
|
|
|
@ -29,8 +25,6 @@ Cffmpeg_decode::~Cffmpeg_decode() {
|
|
|
|
avcodec_close(decoderCtx);
|
|
|
|
avcodec_close(decoderCtx);
|
|
|
|
if (!inputFormatCtx)
|
|
|
|
if (!inputFormatCtx)
|
|
|
|
avformat_close_input(&inputFormatCtx);
|
|
|
|
avformat_close_input(&inputFormatCtx);
|
|
|
|
if (!outputFormatCtx)
|
|
|
|
|
|
|
|
avformat_close_input(&outputFormatCtx);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Cffmpeg_decode::setUrl(QString url) { _url = url; }
|
|
|
|
void Cffmpeg_decode::setUrl(QString url) { _url = url; }
|
|
|
@ -46,7 +40,7 @@ bool Cffmpeg_decode::open_input_file() {
|
|
|
|
// 以udp方式打开,如果以tcp方式打开将udp替换为tcp
|
|
|
|
// 以udp方式打开,如果以tcp方式打开将udp替换为tcp
|
|
|
|
av_dict_set(&avdic, "rtsp_transport", "udp", 0);
|
|
|
|
av_dict_set(&avdic, "rtsp_transport", "udp", 0);
|
|
|
|
// 设置超时断开连接时间,单位微秒//listen_timeout
|
|
|
|
// 设置超时断开连接时间,单位微秒//listen_timeout
|
|
|
|
//av_dict_set(&avdic, "listen_timeout", "200000", 0);
|
|
|
|
// av_dict_set(&avdic, "listen_timeout", "200000", 0);
|
|
|
|
av_dict_set(&avdic, "stimeout", "200000", 0);
|
|
|
|
av_dict_set(&avdic, "stimeout", "200000", 0);
|
|
|
|
av_dict_set(&avdic, "max_delay", "3", 0); // 设置最大时延
|
|
|
|
av_dict_set(&avdic, "max_delay", "3", 0); // 设置最大时延
|
|
|
|
av_dict_set(&avdic, "tune", "zerolatency", 0);
|
|
|
|
av_dict_set(&avdic, "tune", "zerolatency", 0);
|
|
|
@ -55,7 +49,8 @@ bool Cffmpeg_decode::open_input_file() {
|
|
|
|
inputFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;
|
|
|
|
inputFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;
|
|
|
|
|
|
|
|
|
|
|
|
// 打开输入流
|
|
|
|
// 打开输入流
|
|
|
|
if (avformat_open_input(&inputFormatCtx, _url.toUtf8().data(), NULL, &avdic) <0) {
|
|
|
|
if (avformat_open_input(&inputFormatCtx, _url.toUtf8().data(), NULL, &avdic) <
|
|
|
|
|
|
|
|
0) {
|
|
|
|
printf("Cannot open input file.\n");
|
|
|
|
printf("Cannot open input file.\n");
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -66,7 +61,8 @@ bool Cffmpeg_decode::open_input_file() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 从输入流中找到第一个视频流
|
|
|
|
// 从输入流中找到第一个视频流
|
|
|
|
for (int i = 0; i < inputFormatCtx->nb_streams; i++) {
|
|
|
|
for (int i = 0; i < inputFormatCtx->nb_streams; i++) {
|
|
|
|
if (inputFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
if (inputFormatCtx->streams[i]->codecpar->codec_type ==
|
|
|
|
|
|
|
|
AVMEDIA_TYPE_VIDEO) {
|
|
|
|
videoStreamIndex = i;
|
|
|
|
videoStreamIndex = i;
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -101,112 +97,6 @@ bool Cffmpeg_decode::open_input_file() {
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===============================编码================================
|
|
|
|
|
|
|
|
// 1、输出文件设置
|
|
|
|
|
|
|
|
if (avformat_alloc_output_context2(&outputFormatCtx, NULL, NULL, "output_video.mp4") < 0) {
|
|
|
|
|
|
|
|
qDebug() << "Could not create output context.\n";
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 从输入流中找到第一个视频流
|
|
|
|
|
|
|
|
for (int i = 0; i < inputFormatCtx->nb_streams; i++) {
|
|
|
|
|
|
|
|
if (inputFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
|
|
|
|
videoStreamIndexOut = i;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoStreamIndexOut == -1) {
|
|
|
|
|
|
|
|
printf("Cannot find video stream in file.\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取视频流的解码器参数
|
|
|
|
|
|
|
|
videoCodecPara2 = inputFormatCtx->streams[videoStreamIndexOut]->codecpar;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3、创建输出视频流
|
|
|
|
|
|
|
|
outStream = avformat_new_stream(outputFormatCtx, NULL);
|
|
|
|
|
|
|
|
if (!outStream) {
|
|
|
|
|
|
|
|
qDebug() << "Failed allocating output stream.\n";
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//4、查找编码器
|
|
|
|
|
|
|
|
encoder = avcodec_find_encoder(videoCodecPara2->codec_id);
|
|
|
|
|
|
|
|
if(!encoder){
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//5、分配解码器上下文
|
|
|
|
|
|
|
|
encoderCtx = avcodec_alloc_context3(encoder);
|
|
|
|
|
|
|
|
if (!encoderCtx) {
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//初始化视频AVCodecContext
|
|
|
|
|
|
|
|
encoderCtx->height = videoCodecPara2->height;
|
|
|
|
|
|
|
|
encoderCtx->width = videoCodecPara2->width;
|
|
|
|
|
|
|
|
videoCodecPara2->framerate = av_guess_frame_rate(inputFormatCtx,inputFormatCtx->streams[videoStreamIndexOut],NULL);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//视频流的时间基是1 / 帧率
|
|
|
|
|
|
|
|
encoderCtx->time_base = av_inv_q(inputFormatCtx->streams[videoStreamIndexOut]->codecpar->framerate);
|
|
|
|
|
|
|
|
encoderCtx->sample_aspect_ratio = inputFormatCtx->streams[videoStreamIndexOut]->codecpar->sample_aspect_ratio;
|
|
|
|
|
|
|
|
if (encoder->pix_fmts)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
encoderCtx->pix_fmt = encoder->pix_fmts[0];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
encoderCtx->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
|
|
|
|
encoderCtx->codec_id = AV_CODEC_ID_H264;
|
|
|
|
|
|
|
|
encoderCtx->bit_rate = 2000000;
|
|
|
|
|
|
|
|
encoderCtx->time_base.num = 1;
|
|
|
|
|
|
|
|
encoderCtx->time_base.den = 20;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//全局头
|
|
|
|
|
|
|
|
if (outputFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
encoderCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// //设置选项
|
|
|
|
|
|
|
|
// AVDictionary *param = 0;
|
|
|
|
|
|
|
|
// //H.264
|
|
|
|
|
|
|
|
// if(videoCodecPara->codec_id == AV_CODEC_ID_H264) {
|
|
|
|
|
|
|
|
// av_dict_set(¶m, "preset", "slow", 0);
|
|
|
|
|
|
|
|
// av_dict_set(¶m, "tune", "zerolatency", 0);
|
|
|
|
|
|
|
|
// av_dict_set(¶m, "profile", "main", 0);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// //H.265
|
|
|
|
|
|
|
|
// if(videoCodecPara->codec_id == AV_CODEC_ID_H265){
|
|
|
|
|
|
|
|
// av_dict_set(¶m, "preset", "ultrafast", 0);
|
|
|
|
|
|
|
|
// av_dict_set(¶m, "tune", "zero-latency", 0);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//6、打开编码器
|
|
|
|
|
|
|
|
if (avcodec_open2(encoderCtx, encoder, NULL) < 0) {
|
|
|
|
|
|
|
|
qDebug() << "Error occurred when opening encoder.\n";
|
|
|
|
|
|
|
|
//return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
avcodec_parameters_from_context(outputFormatCtx->streams[0]->codecpar, encoderCtx);
|
|
|
|
|
|
|
|
//avcodec_parameters_to_context(encoderCtx,outStream->codecpar);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 打开输出文件
|
|
|
|
|
|
|
|
if (!(outputFormatCtx->oformat->flags & AVFMT_NOFILE)) {
|
|
|
|
|
|
|
|
if (avio_open(&outputFormatCtx->pb,
|
|
|
|
|
|
|
|
outputFormatCtx->url /* "output_video.mp4"*/,
|
|
|
|
|
|
|
|
AVIO_FLAG_WRITE) < 0) {
|
|
|
|
|
|
|
|
printf("Could not open output file.\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//写入输出文件的头信息
|
|
|
|
|
|
|
|
if (avformat_write_header(outputFormatCtx, NULL) < 0) {
|
|
|
|
|
|
|
|
qDebug() << "Error occurred when opening output file.\n";
|
|
|
|
|
|
|
|
//return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//格式化输出输出文件信息
|
|
|
|
|
|
|
|
av_dump_format(outputFormatCtx,0,outputFormatCtx->url,1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化图像转换器
|
|
|
|
// 初始化图像转换器
|
|
|
|
swsCtx = sws_getContext(decoderCtx->width, //
|
|
|
|
swsCtx = sws_getContext(decoderCtx->width, //
|
|
|
|
decoderCtx->height, //
|
|
|
|
decoderCtx->height, //
|
|
|
@ -219,6 +109,7 @@ bool Cffmpeg_decode::open_input_file() {
|
|
|
|
decoderCtx->width, //
|
|
|
|
decoderCtx->width, //
|
|
|
|
decoderCtx->height, //
|
|
|
|
decoderCtx->height, //
|
|
|
|
1);
|
|
|
|
1);
|
|
|
|
|
|
|
|
|
|
|
|
out_buffer = (unsigned char *)av_malloc(numBytes * sizeof(unsigned char));
|
|
|
|
out_buffer = (unsigned char *)av_malloc(numBytes * sizeof(unsigned char));
|
|
|
|
|
|
|
|
|
|
|
|
int res = av_image_fill_arrays(rgbFrame->data, //
|
|
|
|
int res = av_image_fill_arrays(rgbFrame->data, //
|
|
|
@ -231,7 +122,9 @@ bool Cffmpeg_decode::open_input_file() {
|
|
|
|
qDebug() << "Fill arrays failed.\n";
|
|
|
|
qDebug() << "Fill arrays failed.\n";
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!openSave()) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
};
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -252,6 +145,15 @@ void Cffmpeg_decode::run() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (inputPacket->stream_index == videoStreamIndex) {
|
|
|
|
if (inputPacket->stream_index == videoStreamIndex) {
|
|
|
|
|
|
|
|
// 保存裸流
|
|
|
|
|
|
|
|
if (m_formatContextSave) {
|
|
|
|
|
|
|
|
// 由于保存的m_formatContextSave只创建了一个视频流,而读取到的图像的流索引不一定为0,可能会出现错误【Invalid
|
|
|
|
|
|
|
|
// packet stream index: 1】
|
|
|
|
|
|
|
|
// 所以这里需要将stream_index指定为和m_formatContextSave中视频流索引相同,因为就一个流,所以直接设置为0
|
|
|
|
|
|
|
|
inputPacket->stream_index = 0;
|
|
|
|
|
|
|
|
av_write_frame(m_formatContextSave,
|
|
|
|
|
|
|
|
inputPacket); // 将数据包写入输出媒体文件
|
|
|
|
|
|
|
|
}
|
|
|
|
// 解码数据包
|
|
|
|
// 解码数据包
|
|
|
|
if (avcodec_send_packet(decoderCtx, inputPacket) >= 0) {
|
|
|
|
if (avcodec_send_packet(decoderCtx, inputPacket) >= 0) {
|
|
|
|
int ret;
|
|
|
|
int ret;
|
|
|
@ -276,25 +178,25 @@ void Cffmpeg_decode::run() {
|
|
|
|
QImage::Format_RGB32);
|
|
|
|
QImage::Format_RGB32);
|
|
|
|
emit sendQImage(img);
|
|
|
|
emit sendQImage(img);
|
|
|
|
QThread::msleep(28);
|
|
|
|
QThread::msleep(28);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//将解码后的帧写入输出文件
|
|
|
|
|
|
|
|
if (av_write_frame(outputFormatCtx, inputPacket) < 0) {
|
|
|
|
|
|
|
|
//qDebug() << "Error muxing packet.\n";
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
av_packet_unref(inputPacket);
|
|
|
|
av_packet_unref(inputPacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//花屏
|
|
|
|
if (IsstopPlay) {
|
|
|
|
|
|
|
|
if (m_formatContextSave) {
|
|
|
|
|
|
|
|
saveDone();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 花屏
|
|
|
|
// if (av_read_frame(inputFormatCtx, inputPacket) < 0) {
|
|
|
|
// if (av_read_frame(inputFormatCtx, inputPacket) < 0) {
|
|
|
|
// break; // 达到文件末尾
|
|
|
|
// break; // 达到文件末尾
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_formatContextSave) {
|
|
|
|
|
|
|
|
saveDone();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "All video play done";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 退出
|
|
|
|
// 退出
|
|
|
@ -305,8 +207,6 @@ void Cffmpeg_decode::stop() {
|
|
|
|
IsstopPlay = true;
|
|
|
|
IsstopPlay = true;
|
|
|
|
if (!inputPacket)
|
|
|
|
if (!inputPacket)
|
|
|
|
av_packet_free(&inputPacket);
|
|
|
|
av_packet_free(&inputPacket);
|
|
|
|
if (!outputPacket)
|
|
|
|
|
|
|
|
av_packet_free(&outputPacket);
|
|
|
|
|
|
|
|
if (!yuvFrame)
|
|
|
|
if (!yuvFrame)
|
|
|
|
av_frame_free(&yuvFrame);
|
|
|
|
av_frame_free(&yuvFrame);
|
|
|
|
if (!rgbFrame)
|
|
|
|
if (!rgbFrame)
|
|
|
@ -321,6 +221,83 @@ void Cffmpeg_decode::stop() {
|
|
|
|
avcodec_close(decoderCtx);
|
|
|
|
avcodec_close(decoderCtx);
|
|
|
|
if (!inputFormatCtx)
|
|
|
|
if (!inputFormatCtx)
|
|
|
|
avformat_close_input(&inputFormatCtx);
|
|
|
|
avformat_close_input(&inputFormatCtx);
|
|
|
|
if (!outputFormatCtx)
|
|
|
|
}
|
|
|
|
avformat_close_input(&outputFormatCtx);
|
|
|
|
|
|
|
|
|
|
|
|
void Cffmpeg_decode::setSaveFileDirPath(QString fileDirPath) {
|
|
|
|
|
|
|
|
saveFileDirPath = fileDirPath;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Cffmpeg_decode::openSave() {
|
|
|
|
|
|
|
|
QDir dir;
|
|
|
|
|
|
|
|
if (!dir.exists(saveFileDirPath)) {
|
|
|
|
|
|
|
|
dir.mkdir(saveFileDirPath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QString strName =
|
|
|
|
|
|
|
|
QString("/%1.h264")
|
|
|
|
|
|
|
|
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH-mm-ss"));
|
|
|
|
|
|
|
|
strName = saveFileDirPath + strName;
|
|
|
|
|
|
|
|
// const AVOutputFormat *ofmt = av_guess_format("mp4", NULL, NULL);
|
|
|
|
|
|
|
|
int ret = avformat_alloc_output_context2(
|
|
|
|
|
|
|
|
&m_formatContextSave, nullptr, nullptr, strName.toStdString().data());
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
|
|
|
// free();
|
|
|
|
|
|
|
|
qWarning() << "DecodeVideo Error";
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// m_videoStreamOut->codecpar->codec_tag = 0;
|
|
|
|
|
|
|
|
// if (m_formatContextSave->oformat->flags & AVFMT_GLOBALHEADER) {
|
|
|
|
|
|
|
|
// m_formatContextSave->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
|
|
|
|
// // m_videoStreamOut->codecpar->extradata = (uint8_t *)av_malloc(1024);
|
|
|
|
|
|
|
|
// // m_videoStreamOut->codecpar->extradata_size = 0;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// 创建并初始化AVIOContext以访问url所指示的资源。
|
|
|
|
|
|
|
|
ret = avio_open(&m_formatContextSave->pb, strName.toStdString().data(),
|
|
|
|
|
|
|
|
AVIO_FLAG_WRITE);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
|
|
|
// free();
|
|
|
|
|
|
|
|
qWarning() << "DecodeVideo Error";
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 向媒体文件添加新流
|
|
|
|
|
|
|
|
m_videoStreamOut = avformat_new_stream(m_formatContextSave, nullptr);
|
|
|
|
|
|
|
|
if (!m_videoStreamOut) {
|
|
|
|
|
|
|
|
qWarning() << "DecodeVideo Error";
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 拷贝一些参数,给codecpar赋值(这里使用编码器上下文进行赋值)
|
|
|
|
|
|
|
|
ret = avcodec_parameters_from_context(m_videoStreamOut->codecpar, decoderCtx);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
|
|
|
// free();
|
|
|
|
|
|
|
|
qWarning() << "DecodeVideo Error";
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 写入文件头
|
|
|
|
|
|
|
|
ret = avformat_write_header(m_formatContextSave, nullptr);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
|
|
|
// free();
|
|
|
|
|
|
|
|
qWarning() << "DecodeVideo Error";
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_writeHeader = true;
|
|
|
|
|
|
|
|
qDebug() << "开始录制视频!";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Cffmpeg_decode::saveDone() {
|
|
|
|
|
|
|
|
if (m_formatContextSave && m_writeHeader) {
|
|
|
|
|
|
|
|
av_write_trailer(m_formatContextSave); // 写入文件尾
|
|
|
|
|
|
|
|
m_writeHeader = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 关闭文件
|
|
|
|
|
|
|
|
if (m_formatContextSave && !(m_formatContextSave->flags & AVFMT_NOFILE)) {
|
|
|
|
|
|
|
|
avio_close(m_formatContextSave->pb);
|
|
|
|
|
|
|
|
// av_freep(m_videoStreamOut);
|
|
|
|
|
|
|
|
if (m_formatContextSave) {
|
|
|
|
|
|
|
|
avformat_free_context(m_formatContextSave);
|
|
|
|
|
|
|
|
m_formatContextSave = nullptr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// m_videoStreamOut = nullptr;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|