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