FargmentMP4是一种特殊的MP4,这边他的特点及格式,不再详细说明 ,网上资料不少可以进行参考。
这边说下基于FFMPEG封装aac及h264为FargmentMP4的步骤:
-
关键代码:
-
1、创建两个输入对象和一个输出对象并打开。
-
AVFormatContext *ifmt_ctx_v = NULL, *ifmt_ctx_a = NULL, *ofmt_ctx = NULL;
-
if ((ret = avformat_open_input(&ifmt_ctx_a, in_filename_a, NULL, NULL)) < 0) {
-
printf("Could not open input file.");
-
goto end;
-
}
-
// printf("=====2========RET:%d\n",ret);
-
if ((ret = avformat_find_stream_info(ifmt_ctx_a, 0)) < 0) {
-
printf("Failed to retrieve input stream information");
-
if (acc_length > 0)
-
goto end;
-
}
-
-
if ((ret = avformat_open_input(&ifmt_ctx_v, in_filename_v, NULL, NULL)) < 0) {
-
printf("Could not open input file:%d\n", ret);
-
goto end;
-
}
-
if ((ret = avformat_find_stream_info(ifmt_ctx_v, 0)) < 0) {
-
printf("Failed to retrieve input stream information");
-
goto end;
-
}
-
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
-
if (!ofmt_ctx) {
-
printf("Could not create output context\n");
-
ret = AVERROR_UNKNOWN;
-
goto end;
-
}
-
ofmt = ofmt_ctx->oformat;
-
-
2、初始化过滤器 因为aac打包到mp4需要去掉adts头。
-
const AVBitStreamFilter *absFilter = av_bsf_get_by_name("aac_adtstoasc");
-
AVBSFContext *absCtx = NULL;
-
av_bsf_alloc(absFilter, &absCtx);
-
-
3、获取视频流信息并拷贝相应编码格式
-
for (i = 0; i < ifmt_ctx_v->nb_streams; i++) {
-
//Create output AVStream according to input AVStream
-
if (ifmt_ctx_v->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
-
AVStream *in_stream = ifmt_ctx_v->streams[i];
-
AVCodec *avcodec = avcodec_find_decoder(in_stream->codecpar->codec_id);
-
AVStream *out_stream = avformat_new_stream(ofmt_ctx, avcodec);
-
videoindex_v = i;
-
if (!out_stream) {
-
printf("Failed allocating output stream\n");
-
ret = AVERROR_UNKNOWN;
-
goto end;
-
}
-
videoindex_out = out_stream->index;
-
AVCodecContext *codec_ctx = avcodec_alloc_context3(avcodec);
-
ret = avcodec_parameters_to_context(codec_ctx, in_stream->codecpar);
-
if (ret < 0) {
-
printf("Failed to copy context from input to output stream codec context\n");
-
goto end;
-
}
-
codec_ctx->codec_tag = 0;
-
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
-
codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
-
-
ret = avcodec_parameters_from_context(out_stream->codecpar, codec_ctx);
-
int q = 0;
-
}
-
}
-
-
4、获取音频流信息并拷贝相应编码格式
-
for (i = 0; i < ifmt_ctx_a->nb_streams; i++) {
-
if (ifmt_ctx_a->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
-
AVStream *in_stream = ifmt_ctx_a->streams[i];
-
AVCodec *avcodec = avcodec_find_decoder(in_stream->codecpar->codec_id);
-
AVStream *out_stream = avformat_new_stream(ofmt_ctx, avcodec);
-
audioindex_a = i;
-
if (!out_stream) {
-
printf("Failed allocating output stream\n");
-
ret = AVERROR_UNKNOWN;
-
goto end;
-
}
-
audioindex_out = out_stream->index;
-
avcodec = avcodec_find_decoder(in_stream->codecpar->codec_id);
-
-
avcodec_parameters_copy(absCtx->par_in, in_stream->codecpar);
-
av_bsf_init(absCtx);
-
-
AVCodecContext *codec_ctx = avcodec_alloc_context3(avcodec);
-
ret = avcodec_parameters_to_context(codec_ctx, in_stream->codecpar);
-
if (ret < 0) {
-
printf("Failed to copy context from input to output stream codec context\n");
-
goto end;
-
}
-
codec_ctx->codec_tag = 0;
-
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
-
codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
-
-
ret = avcodec_parameters_from_context(out_stream->codecpar, codec_ctx);
-
out_stream->codecpar->extradata = (uint8_t*)av_malloc(2);
-
out_stream->codecpar->extradata_size = 2;
-
unsigned char dsi1[2];
-
unsigned int sampling_frequency_index = (unsigned int)get_sr_index(8000);
-
make_dsi(sampling_frequency_index, 1, dsi1);
-
memcpy(out_stream->codecpar->extradata, dsi1, 2);
-
-
break;
-
}
-
}
-
5、打开MP4文件
-
if (!(ofmt->flags & AVFMT_NOFILE)) {
-
if (avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE) < 0) {
-
printf("Could not open output file '%s'", out_filename);
-
goto end;
-
}
-
}
-
6、设置字典fmp4的关键
-
AVDictionary *movflags = nullptr;
-
const bool low_delay = true;
-
if (low_delay) {
-
// In case of low delay, set fragment duration to 200 ms.
-
av_dict_set(&movflags, "movflags", "empty_moov+default_base_moof", 0);
-
av_dict_set_int(&movflags, "frag_duration", 200 * 1000, 0);
-
}
-
else {
-
// Only produce fragment until we have next key frame.
-
av_dict_set(&movflags, "movflags", "empty_moov+default_base_moof+frag_keyframe", 0);
-
}
-
//avformat_open_input(&addofmt_ctx, addinputAacFileName, 0, &movflags);
-
ret = avformat_write_header(ofmt_ctx, &movflags);
-
if (ret < 0) {
-
fprintf(stderr, "Error occurred when opening output file\n");
-
goto end;
-
}
-
7、打包
-
-
if ((ret = av_interleaved_write_frame(ofmt_ctx, &pkt)) < 0) {
-
printf("Error muxing packet\n");
-
break;
-
}
具体工程参考:https://download.csdn.net/download/venice0708/11434379
分类:
音视频、流媒体
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2017-02-23 IAR FOR STM8 学习笔记 IAR工程的建立
2017-02-23 IAR FOR STM8 学习笔记 固件库 GPIO
2017-02-23 IAR for STM8介绍、下载、安装与注册--转
2017-02-23 ONVIF协议测试工具 ONVIF Device Test Tool 29 12.12 最新版
2017-02-23 ONVIF Device Manager v2.2.146
2017-02-23 ONVIF测试方法及工具