How do you enable "veryfast" libx264 preset from code using libavcodec?

dicroce picture dicroce · Oct 2, 2013 · Viewed 7.4k times · Source

I'm using libx264 via ffmpeg (in a C++ program), and I need to know how to activate the "veryfast" preset. A grep in the x264 source tree yields:

include/x264.h:static const char * const x264_preset_names[] = { "ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow", "placebo", 0 };

Which inclines me to believe that x264 itself supports this preset as opposed to just being implemented in ffmpeg as a collection of other settings. So, the question is: How do I get ffmpeg to activate the x264 "veryfast" preset?

Answer

Wagner Patriota picture Wagner Patriota · Oct 3, 2013

You gotta create a dictionary and then use this dictionary with all the parameters you need when opening the codec!

AVDictionary * codec_options( 0 );
av_dict_set( &codec_options, "preset", "veryfast", 0 );
// av_dict_set( &codec_options, "AnyCodecParameter", "Value", 0 );
avcodec_open2( codecContext, videoCodec, &codec_options );