最近学习了h265封装mp4,使用ffmpeg实现,因此记录一下。。参考了雷神和其他人的代码,只包含视频文件。
1.由于ffmpeg使用c实现,头文件需要带上extern "c"和 __STDC_CONSTANT_MACROS。
2.原本需要 av_register_all 和 avcodec_copy_context,但由于新版ffmpeg已经不使用AVStream->codec,需要其他方法设置 codec,av_register_all已经删除不使用。
AVStream *out_stream = avformat_new_stream((*pFormatCtx), NULL);
//Copy the settings of AVCodecContext
AVCodecContext *codecCtx = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(codecCtx, in_stream->codecpar);
codecCtx->codec_tag = 0;
if ((*pFormatCtx)->oformat->flags & AVFMT_GLOBALHEADER)
codecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
avcodec_parameters_from_context(out_stream->codecpar, codecCtx);
3. 调用的静态库和动态库需要区分x86和x64,否则很可能一直编译不过。。
4. h264/h265没有解码时间戳,填入pkt之前需要设置pts,dts和duration。
//Write PTS
AVRational time_base1 = in_stream->time_base;
//Duration between 2 frames (us)
int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(in_stream->r_frame_rate);
//Parameters
pkt.pts = (double)(frame_index*calc_duration) / (double)(av_q2d(time_base1)*AV_TIME_BASE);
pkt.dts = pkt.pts;
pkt.duration = (double)calc_duration / (double)(av_q2d(time_base1)*AV_TIME_BASE);
</