[Libav-user] Idiomatic way to set encoding codec options for a stream

Strahinja Radman dr.strashni at gmail.com
Thu Feb 13 17:01:28 EET 2020


>
>
> If I understand correctly, the AVCodecContext is instantiated separately
> from the FormatContext and Streams.
> So what I have to do is:
>
> 1- Instanciate the codec context and configure it as you suggested
> 2- Instanciate an AVFormatContext
> 3- Add a stream without codec using avformat_new_stream(fmt_ctx, NULL)
> 4- set stream id and update codecpar using avcodec_parameters_from_context
> 5- generate the format header with avformat_write_header
> 6- proceed to the frame encoding loop
>
> right?
>

There is a transcoding.c example on FFmpeg's github repo. But in essence you
create new AVFormatContext first using the

  avformat_alloc_output_context2(ofmt_ctx, NULL, NULL, ofn.c_str());

After that you add streams, as much as you need or as much as your
container
supports using the

   out_stream = avformat_new_stream(ofmt_ctx, NULL);

Once you have your stream then you open the appropriate encoder for data
that
you want to encode

      encoder = avcodec_find_encoder(AV_CODEC_ID_H264);

and based on returned value you create your AVCodecContext with

    enc_ctx = avcodec_alloc_context3(encoder);

You set required options using the

     av_opt_set_double(enc_ctx->priv_data, "crf", 23.00, 0);

and then simply open the AVCodecContext calling the

    ret = avcodec_open2(enc_ctx, encoder, NULL);

You then copy encoder properties to the streams AVCodecParameters

    ret = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);

open the file and write header.
    ret = avio_open(ofmt_ctx->pb, ofn.c_str(), AVIO_FLAG_WRITE);
    ret = avformat_write_header(ofmt_ctx, NULL);


-- 

Regards
Strahinja Radman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20200213/215b405b/attachment.html>


More information about the Libav-user mailing list