#include "combineVideo.h"
int combineVideo::open_input_file()
{
int ret = 0;
ifmt_ctx = (AVFormatContext**)av_malloc((global_ctx.video_num)*sizeof(AVFormatContext*));
for (int i = 0; i < global_ctx.video_num; i++)
{
*(ifmt_ctx + i) = NULL;
const char * Cfilename = global_ctx.filenames[i].toStdString().c_str();
if ((ret = avformat_open_input((ifmt_ctx + i),Cfilename , NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
return ret;
}
if ((ret = avformat_find_stream_info(ifmt_ctx[i], NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
return ret;
}
av_dump_format(ifmt_ctx[i], 0, Cfilename, 0);
}
return 0;
}
int combineVideo::open_output_file()
{
int ret = -1;
const char * out_name = global_ctx.outfilename.toStdString().c_str();
if ((ret = avformat_alloc_output_context2(&out_fmtctx, NULL, NULL, out_name)) < 0)
{
printf("can not alloc context for output!\n");
return ret;
}
//new stream for out put
for (int i = 0; i < ifmt_ctx[0]->nb_streams; i++)
{
if (ifmt_ctx[0]->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
//video_stream_index = i;
out_video_stream = avformat_new_stream(out_fmtctx, NULL);
if (!out_video_stream)
{
printf("Failed allocating output1 video stream\n");
ret = AVERROR_UNKNOWN;
return ret;
}
if ((ret = avcodec_copy_context(out_video_stream->codec, ifmt_ctx[0]->streams[i]->codec)) < 0)
{
printf("can not copy the video codec context!\n");
return ret;
}
avcodec_parameters_from_context(out_video_stream->codecpar, out_video_stream->codec);
out_video_stream->codec->codec_tag = 0;
if(out_fmtctx->oformat->flags & AVFMT_GLOBALHEADER)
{
out_video_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}
else if (ifmt_ctx[0]->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
//audio_stream_index = i;
out_audio_stream = avformat_new_stream(out_fmtctx, NULL);
if (!out_audio_stream)
{
printf("Failed allocating output1 video stream\n");
ret = AVERROR_UNKNOWN;
return ret;
}
if ((ret = avcodec_copy_context(out_audio_stream->codec, ifmt_ctx[0]->streams[i]->codec)) < 0)
{
printf("can not copy the video codec context!\n");
return ret;
}
avcodec_parameters_from_context(out_audio_stream->codecpar, out_audio_stream->codec);
out_audio_stream->codec->codec_tag = 0;
if(out_fmtctx->oformat->flags & AVFMT_GLOBALHEADER)
{
out_audio_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}
}
//open output file
if (!(out_fmtctx->oformat->flags & AVFMT_NOFILE))
{
if ((ret = avio_open(&out_fmtctx->pb, out_name, AVIO_FLAG_WRITE)) < 0)
{
printf("can not open the out put file handle!\n");
return ret;
}
}
//write out file header
if ((ret = avformat_write_header(out_fmtctx, NULL)) < 0)
{
printf( "Error occurred when opening video output file\n");
return ret;
}
av_dump_format(out_fmtctx, 0, out_name, 1);
}
int combineVideo::videocombine(QStringList fileList,QString outputfilename)
{
AVPacket pkt;
int pts_v, pts_a, dts_v, dts_a;
//sprintf(out_name, "combine.%s", argv[3]);
int iRet = 0;
initfileInfo(fileList,outputfilename);
av_register_all();
if(iRet = open_input_file() < 0)
{
freeAllPointer();
return iRet;
}
if(iRet = open_output_file() < 0)
{
freeAllPointer();
return iRet;
}
for (int i = 0; i < global_ctx.video_num; i++)
{
while(1)
{
if(0 > av_read_frame(ifmt_ctx[i], &pkt))
{
float vedioDuraTime, audioDuraTime;
video_stream_index = getOutAVCodecContextIndex(ifmt_ctx[i],AVMEDIA_TYPE_VIDEO);
audio_stream_index = getOutAVCodecContextIndex(ifmt_ctx[i],AVMEDIA_TYPE_AUDIO);
//calc the first media dura time
vedioDuraTime = pts_v * av_q2d(ifmt_ctx[i]->streams[video_stream_index]->time_base);
audioDuraTime = pts_a * av_q2d(ifmt_ctx[i]->streams[audio_stream_index]->time_base);
//calc the pts and dts end of the first media
if (audioDuraTime > vedioDuraTime)//得出
{
dts_v = pts_v = audioDuraTime / av_q2d(ifmt_ctx[i]->streams[video_stream_index]->time_base);
dts_a++;
pts_a++;
}
else
{
dts_a = pts_a = vedioDuraTime / av_q2d(ifmt_ctx[i]->streams[audio_stream_index]->time_base);
dts_v++;
pts_v++;
}
//input_ctx = in2_fmtctx;
break;
}
AVStream *Newstream = ifmt_ctx[i]->streams[pkt.stream_index];
if (AVMEDIA_TYPE_VIDEO == Newstream->codec->codec_type)
{
if (i > 0)
{
pkt.pts += pts_v;
pkt.dts += dts_v;
}
else
{
pts_v = pkt.pts;
dts_v = pkt.dts;
}
}
else if (AVMEDIA_TYPE_AUDIO == Newstream->codec->codec_type)
{
if (i > 0)
{
pkt.pts += pts_a;
pkt.dts += dts_a;
}
else
{
pts_a = pkt.pts;
dts_a = pkt.dts;
}
}
pkt.pts = av_rescale_q_rnd(pkt.pts, ifmt_ctx[i]->streams[pkt.stream_index]->time_base,
out_fmtctx->streams[pkt.stream_index]->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, ifmt_ctx[i]->streams[pkt.stream_index]->time_base,
out_fmtctx->streams[pkt.stream_index]->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt.pos = -1;
//double showtime2 = pkt.pts*av_q2d(out_fmtctx->streams[pkt.stream_index]->time_base);
//printf( "write packet\n");
if (av_interleaved_write_frame(out_fmtctx, &pkt) < 0)
{
printf( "Error muxing packet\n");
//break;
}
av_free_packet(&pkt);
}
}
av_write_trailer(out_fmtctx);
freeAllPointer();
printf( "write end\n");
}
void combineVideo::initfileInfo(QStringList fileList,QString outputfilename)
{
int Isize = fileList.size();
//global_ctx = (GlobalContext*)av_malloc(sizeof(GlobalContext));
for (int i = 0; i < Isize; ++i)
{
QString strText = fileList.at(i);
global_ctx.filenames.append(strText);
}
global_ctx.video_num = Isize;
global_ctx.outfilename = outputfilename;
}
void combineVideo::freeAllPointer()
{
for (int i = 0; i < global_ctx.video_num; i++)
avformat_close_input(&(ifmt_ctx[i]));
if (out_fmtctx && !(out_fmtctx->oformat->flags & AVFMT_NOFILE))
avio_close(out_fmtctx->pb);
avformat_free_context(out_fmtctx);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
qt 基于ffmpeg 实现视频的拼接、合并 (281个子文件)
libavcodec.so.58.35.100 11.39MB
libavformat.so.58.20.100 2.32MB
libswscale.so.5.3.100 482KB
libavutil.so.56.22.100 403KB
libswresample.so.3.3.100 86KB
libavdevice.so.58.5.100 73KB
libavfilter.so.7.40.101 2.51MB
libswresample.so.3 86KB
libswscale.so.5 482KB
libavutil.so.56 403KB
libavcodec.so.58 11.39MB
libavformat.so.58 2.32MB
libavdevice.so.58 73KB
libavfilter.so.7 2.51MB
libavcodec.dll.a 982KB
libavdevice.dll.a 457KB
libavutil.dll.a 340KB
libavformat.dll.a 134KB
libavfilter.dll.a 45KB
libswscale.dll.a 22KB
libswresample.dll.a 15KB
libpostproc.dll.a 7KB
SDL_config.h.cmake 18KB
combineVideo.cpp 8KB
main.cpp 463B
avutil-56.def 12KB
avcodec-58.def 6KB
avformat-58.def 4KB
avfilter-7.def 2KB
swscale-5.def 756B
avdevice-58.def 544B
swresample-3.def 473B
postproc-55.def 223B
avcodec-58.dll 47.85MB
avformat-58.dll 10.68MB
avfilter-7.dll 10.52MB
avdevice-58.dll 2.63MB
SDL2.dll 1.4MB
avutil-56.dll 856KB
swscale-5.dll 558KB
swresample-3.dll 425KB
postproc-55.dll 132KB
ffmpeg.exe 349KB
ffprobe.exe 184KB
ffplay.exe 153KB
ffmpeg 245KB
ffplay 125KB
ffprobe 140KB
SDL_opengl_glext.h 714KB
avcodec.h 207KB
avformat.h 115KB
SDL_opengles2_gl2ext.h 96KB
SDL_opengl.h 81KB
SDL_egl.h 72KB
SDL_hints.h 56KB
SDL_video.h 45KB
SDL_render.h 44KB
avfilter.h 41KB
SDL_haptic.h 38KB
opt.h 35KB
SDL_audio.h 35KB
pixfmt.h 33KB
SDL_opengles2_gl2.h 31KB
avio.h 31KB
SDL_events.h 30KB
frame.h 29KB
mem.h 23KB
packet.h 23KB
hwcontext.h 22KB
SDL_stdinc.h 22KB
swresample.h 21KB
SDL_surface.h 20KB
intreadwrite.h 18KB
avdevice.h 17KB
SDL_pixels.h 17KB
common.h 16KB
codec.h 16KB
pixdesc.h 16KB
SDL_keycode.h 15KB
SDL_scancode.h 15KB
codec_id.h 15KB
SDL_gamecontroller.h 14KB
SDL_joystick.h 14KB
avstring.h 14KB
SDL_thread.h 13KB
SDL_test_fuzzer.h 13KB
hdr_dynamic_metadata.h 12KB
swscale.h 12KB
imgutils.h 11KB
log.h 11KB
SDL_assert.h 11KB
bsf.h 11KB
SDL_mouse.h 11KB
SDL_atomic.h 11KB
SDL_vulkan.h 11KB
buffer.h 10KB
SDL_system.h 10KB
samplefmt.h 10KB
SDL_opengles2_khrplatform.h 10KB
channel_layout.h 9KB
共 281 条
- 1
- 2
- 3
资源评论
- YG亲测源码屋2020-10-28垃圾,下载了不能运行,都被否决了,还要自己改,
鸿雁高飞jj
- 粉丝: 5
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 的霍夫曼变换答题卡识别源码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab+bp神经网络的神经网络汉字识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功