ffmpeg封装

本文记录了使用ffmpeg将h265视频封装为mp4文件的过程,包括设置环境、处理时间戳和文件操作等关键步骤。在实践中,需要注意ffmpeg API的更新,以及不同平台的库兼容性问题。
摘要由CSDN通过智能技术生成

最近学习了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);</
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值