[Libav-user] How to mimic "-q:v 1" through the ffmpeg API?

James Philbin philbinj at gmail.com
Mon Mar 14 19:45:31 CET 2016


Hi,

I'm trying to mimic the behavior of ffmpeg (the CLI) through the API
and not having much success. Specifically, the quality parameter (set
by '-q:v %d' is being ignored when set in the API). Here's the command
line i'm trying to mimic:

===
ffmpeg -i input.h264 -q:v 1 -c:v mpeg4 -r 25 output.mp4
===

Here's the code that I have currently:
===
    // Create the output context.
    avformat_alloc_output_context2(&ctx_, nullptr, nullptr, fn_.c_str());
    CHECK(ctx_ != nullptr);

    // Setup the codec options.
    codec_ = avcodec_find_encoder(AV_CODEC_ID_MPEG4);
    CHECK(codec_ != nullptr);

    // Create a new output stream.
    out_str_ = avformat_new_stream(ctx_, codec_);
    CHECK(out_str_ != nullptr);

    out_str_->id = ctx_->nb_streams - 1;
    codec_ctx_ = out_str_->codec;

    codec_ctx_->codec_id = AV_CODEC_ID_MPEG4;

    codec_ctx_->flags |= AV_CODEC_FLAG_QSCALE;
    codec_ctx_->global_quality = FF_QP2LAMBDA * quality_;

    codec_ctx_->width = width_;
    codec_ctx_->height = height_;
    codec_ctx_->codec_type = AVMEDIA_TYPE_VIDEO;
    codec_ctx_->pix_fmt = AV_PIX_FMT_YUV420P;
    codec_ctx_->max_b_frames = 1;
    codec_ctx_->gop_size = 10;

    AVRational frame_rate = av_d2q(frame_rate_, 1001000);
    codec_ctx_->time_base = av_inv_q(frame_rate);
    out_str_->time_base = av_inv_q(frame_rate);

    if (ctx_->oformat->flags & AVFMT_GLOBALHEADER) {
      out_str_->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }

    // Initialize the codec.
    CHECK_EQ(avcodec_open2(codec_ctx_, codec_, nullptr), 0);

    // Open the file and write a header.
    if (!(ctx_->oformat->flags & AVFMT_NOFILE)) {
      CHECK_EQ(avio_open(&ctx_->pb, fn_.c_str(), AVIO_FLAG_WRITE), 0);
    }
    CHECK_EQ(avformat_write_header(ctx_, nullptr), 0);
===

Changing the quality_ parameter for my API wrapper doesn't change
anything about the output. Any ideas what i'm doing wrong?

Thanks for your help,
James


More information about the Libav-user mailing list