3

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?

2
  • 2
    Try to set pContext_->codec_type = 1; // video Commented May 12, 2016 at 13:18
  • the error tells us where to look try setting pContext->sample_fmt try this this with all available values as of ffmpeg 4.2.4 there are less then 15 values Commented Mar 6, 2021 at 21:43

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

it was the resolution - 720x480 works .. Thank you .. (i also changed the other values u recommended)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.