roari
roari

Reputation: 39

ffmpeg: avcodec_open2 returns invalid argument

I'm reusing the sample code from the developer 64-bit release of FFmpeg in my application to encode a video:

AVCodec* pCodec_{nullptr};
AVCodecContext* pContext_{nullptr};

avcodec_register_all();
pCodec_ = avcodec_find_encoder(AV_CODEC_ID_MPEG2VIDEO);
if (!pCodec_) {}

pContext_ = avcodec_alloc_context3(pCodec_);
if (!pContext_) {}

pContext_->bit_rate = 400000;
pContext_->width = size.width();
pContext_->height = size.height();

pContext_->time_base.den = 1;
pContext_->time_base.num = fps;

pContext_->gop_size = 10;
pContext_->max_b_frames = 1;
pContext_->pix_fmt = AV_PIX_FMT_BGR0;

if (codec_id == AV_CODEC_ID_H264) {
    av_opt_set(pContext_->priv_data, "preset", "slow", 0);
}

int err = avcodec_open2(pContext_, pCodec_, nullptr);
if (err < 0) {}

AVCodec* and AVCodecContext* look like they are allocated correctly. avcodec_open2 then returns invalid argument (-22).

I use: Windows 10 64, VS2013 Compiler, Qt Creator IDE, ffmpeg(2016-05-12) 64bit.

The sample I took the code from is decoding_encoding.c.

Any ideas?

Upvotes: 3

Views: 7543

Answers (1)

philipp
philipp

Reputation: 1823

Just some Ideas:

  • look at this: FFMPEG - format not available? seems that ffpmeg expects you to set a pix_fmt which is natively supported by the encoder.
  • Your time base seems odd. I guess it should be 1/fps, not fps/1
  • Play with the bit rate. I vaguely remember that, at least some years ago, this value was expected to be in kbit/s of bit/s, depending on the codec. (Also 400 kbit/s is quite low for mpeg2, but it should still work, I guess).
  • for a first test, use standard resolutions like 720 × 480. IIRC mpeg2 only supports certain resolutions, at least both dimensions have to be multiples of 8.

Upvotes: 1

Related Questions