Ffmpeg拷贝编码信息

需求

  1. 转封装时需要将编码信息从输入AVCodecContext拷贝到输出AVCodecContext
  2. 自定义编码格式,需要将AVCodecContext中的数据拷贝到AVFormatContext
  3. 需要获取输入的AVCodecContext的编码信息

解决方案

旧版本的ffmpeg提供avcodec_copy_context(),用于拷贝codec信息,但是该方式已经被遗弃。

  • 旧版本的AVFormatContext中包含AVCodecContext *codec,该codec存储了编码的信息
  • 新版本的AVFormatContext中不再包含AVCodecContext *codec,取而代之的是AVCodecParameters *codecpar

AVStream到AVStream

针对需求中的场景1,旧版本可以直接从codec中获取,新版本ffmpeg操作示范代码如下:

AVFormatContext    *input_format_ctx, *output_format_ctx;

/* 打开输入操作... */

for (index = 0; index < input_format_ctx->nb_streams; index++) {
    AVStream           *input_stream, *output_stream;

    input_stream = input_format_ctx->streams[index];
    output_stream = avformat_new_stream(output_format_ctx, nullptr);

    if (output_stream == nullptr) {
        av_log(nullptr, AV_LOG_ERROR, "Could not create output stream.\n");

        goto end;
    }

    if (avcodec_parameters_copy(output_stream->codecpar, input_stream->codecpar) < 0) {
        av_log(nullptr, AV_LOG_ERROR, "Failed to copy parameters from input_stream->codecpar"
                                      "to output_stream->codecpar.\n");

        goto end;
    }
}

AVFormatContext到AVCodec

针对场景2、场景3,ffmpeg提供了两个API供我们使用:

  • avcodec_parameters_to_context
  • avcodec_parameters_from_context

解码

AVCodecContext *codec_ctx = avcodec_alloc_context3(nullptr);
if (codec_ctx == nullptr) {
    av_log(nullptr, AV_LOG_ERROR, "Alloc codec context failed.\n");

    goto end;
}

for (index = 0; index < input_format_ctx->nb_streams; index++) {
    AVStream           *input_stream, *output_stream;

    input_stream = input_format_ctx->streams[index];
    output_stream = avformat_new_stream(output_format_ctx, nullptr);

    /* 将codec从AVFormatContext拷贝出来 */
    /* Copy codec parameters from input_stream->codecpar */
    if (avcodec_parameters_to_context(codec_ctx, input_stream->codecpar) < 0) {
        av_log(nullptr, AV_LOG_ERROR, "Failed to copy codec parameters from "
                                      "input_stream->codecpar to codec_ctx.\n");

        goto end;
    }

    /* 取到输入的codec了,可以任意操作 */
    /* do something ... */

    if (output_format_ctx->oformat->flags & AVFMT_GLOBALHEADER) {
        codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    }
}

avcodec_free_context(&codec_ctx);

编码

AVFormatContext  *format_ctx;
AVCodecContext   *codec_ctx;
AVStream         *stream;

format_ctx = avformat_alloc_output_context2(&format_ctx, nullptr, nullptr,
                                            filename);
if (format_ctx == nullptr) {
    av_log(nullptr, AV_LOG_ERROR, "Alloc format context failed.\n");

    goto end;
}

codec_ctx = avcodec_alloc_context3(nullptr);
if (codec_ctx == nullptr) {
    av_log(nullptr, AV_LOG_ERROR, "Alloc codec context failed.\n");

    goto end;
}

stream = avformat_new_stream(format_ctx, nullptr);
if (stream == nullptr) {
    av_log(nullptr, AV_LOG_ERROR, "Create stream failed.\n");

    goto end;
}

codec_ctx->video_type = AVMEDIA_TYPE_VIDEO;
codec_ctx->format = AV_PIX_FMT_YUV420P;
codec_ctx->codec_id = m_fmt_ctx->oformat->video_codec;

codec_ctx->width = 1280;
codec_ctx->height = 720;
codec_ctx->gop_size = 12;
codec_ctx->time_base = AVRational{1, 25};
codec_ctx->bit_rate = 1400 * 1000;

/* 将codec拷贝到AVFormatContext中去 */

/* Copy codec parameters from codec_ctx to output_stream->codecpar */
if (avcodec_parameters_from_context(output_stream->codecpar, codec_ctx) < 0) {
    av_log(nullptr, AV_LOG_ERROR, "Failed to copy codec parameters from "
                                  "codec_ctx to output_stream->codecpar.\n");

    goto end;
}

avcodec_free_context(&codec_ctx);
avformat_free_context(output_format_ctx);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值