Set AVC/H.264 profile when encoding video in Android using MediaCodec API

For Android Kitkat devices we can set desired AVC profile and level into media format as per code snippet below(sets baseline with level 1.3). Please set this before starting MediaCodec.

format.setInteger(MediaFormat.KEY_PROFILE, MediaCodecInfo.CodecProfileLevel.AVCProfileBaseline);
format.setInteger(MediaFormat.KEY_LEVEL, MediaCodecInfo.CodecProfileLevel.AVCLevel13);

There is no explicit profile selection in the MediaCodec API. Codec will select a profile on its own. It will also select a level based on input width/height and frame-rate numbers, but it may select a level that is higher than minimum required for the configuration.

You can query the encoder codec for the supported levels to see which profiles it may generate, but you will not be able to select a preferred one. Most likely the codec will select the highest profile it can handle for the given level, but there is no guarantee for that.


Starting with Android 5.0, it is possible to set profile (SDK Level 21) and level (SDK Level 23).

http://developer.android.com/reference/android/media/MediaFormat.html#KEY_PROFILE http://developer.android.com/reference/android/media/MediaFormat.html#KEY_LEVEL

Haven't yet tried how well these work in practice.