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

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

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

GRANGER Nicolas
Hi all,

I need to encode images into an mkv container file using the h264
codec. I would like to set codec-specific options such as '-crf'
but I don't know how to do it. So far I have found/considered several
ways:

- let avformat_new_stream allocate AVCodecContext then use the
deprecated Stream.codec field to set the options.
- let avformat_new_stream allocate the AVCodecContext then use
av_opt_set on Stream.priv_data
- pass NULL for the codec to avformat_new_stream and find a way to
create and set the codec context manually? Is this even supported?

How else am I supposed to do this?

Best,
Nicolas
_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Idiomatic way to set encoding codec options for a stream

Strahinja Radman

I need to encode images into an mkv container file using the h264
codec. I would like to set codec-specific options such as '-crf'
but I don't know how to do it. So far I have found/considered several
ways:


Before you call avcodec_open2 set the property by calling the
 
     av_opt_set_double(enc_ctx->priv_data, "crf", 23.0, 0);

Some of the options need to be set on the "priv_data", ones that are 
codec related, others can be set directly to the enc_ctx 

     av_opt_set_int(enc_ctx, "sc_threshold", 1, 0);


--

Regards
Strahinja Radman

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Idiomatic way to set encoding codec options for a stream

GRANGER Nicolas
Thank you for the quick reply.

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?

Le jeudi 13 février 2020 à 11:07 +0100, Strahinja Radman a écrit :
I need to encode images into an mkv container file using the h264
codec. I would like to set codec-specific options such as '-crf'
but I don't know how to do it. So far I have found/considered several
ways:


Before you call avcodec_open2 set the property by calling the
 
     av_opt_set_double(enc_ctx->priv_data, "crf", 23.0, 0);

Some of the options need to be set on the "priv_data", ones that are 
codec related, others can be set directly to the enc_ctx 

     av_opt_set_int(enc_ctx, "sc_threshold", 1, 0);


_______________________________________________
Libav-user mailing list

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".
Reply | Threaded
Open this post in threaded view
|

Re: Idiomatic way to set encoding codec options for a stream

Strahinja Radman

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

_______________________________________________
Libav-user mailing list
[hidden email]
https://ffmpeg.org/mailman/listinfo/libav-user

To unsubscribe, visit link above, or email
[hidden email] with subject "unsubscribe".