Encoding h264

Libav does not provide a native encoder for h264 but wraps x264 and supports hardware-accelerated encoding using Intel MediaSDK and NVIDIA NVENC.


Building x264 support

x264 is a GPL software Libav compiled with it results being GPL as well.

Assuming your systems has x264 installed all you need to do is

./configure --enable-libx264 --enable-gpl

Once it is built and installed you can check for its availability

avconv -codecs | grep h264
DEV.LS h264                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (encoders: libx264 )

x264 can be built in 8bit and 10bit flavour, libav supports both but its behaviour regarding some options changes slightly.


Recent avconv provide an automatic codec mapping, thus

avconv -i source -c:v h264 -c:a copy out.mkv


avconv -i source -c:v libx264 -c:a copy out.mkv

are equivalent

Some Libav custom branches might provide support for additional third party encoders, use -c:v libx264 to be sure.

Suggested settings

x264 has a large number of options most of them mapped directly, all of them a through -x264-params using a string compatible with the x264 cli output.

Preset and Tune

x264 comes with a batch of default presets and tunings. They normally provide a proper result for the task.

avconv -i source -c:v libx264 -preset medium -tune film -c:a copy out.mkv 

The full list of tuning and presets can be checked using the x264 cli.

The presets are ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo.

The tunings are film, animation, grain, stillimage, psnr, ssim, fastdecode, zerolatency.

Each preset is twice as slow as the previous, tunings and preset can be mixed and matched, e.g. -preset fast -tune film


Profiles enforce a standard, ensuring compatibility with specific targets by restricting the encoding features used. Nowadays most decoders support the high profile, so this setting could be ignored.

Hardware decoders might have a limited buffer, while x264 by default sets the number of reference frames relatively high.

In order to make Full HD content decodable by smart-tvs is suggested to tune the -refs parameter to lower values down to 3-4 or to use the main profile.

The high profile support pixel formats that might not be supported by browsers (such as Firefox) and simpler devices.

To ensure the greater compatibility is advised to pass -pix_fmt yuv420p option.

Rate Control

Constant Rate Factor

The simplest to use, Constant Rate Factor -crf it targets a quality level and tries to maintain it over the encoding.

The value range is from 0 to 53 (or 63 in 10bit mode) and maps to the same range of the quantizer, ideally it would provide the same perceptual quality of the constant-quantizer rate-control but in less space by discarding information human eyes would not perceive. As rule of thumb every increase by 5 halves the bitrate, 0 being lossless encoding.

avconv -i source -c:v libx264 -crf 38 -crf_max 42 -c:a aac out.mov

You can prevent the rate-control from overshooting/undershooting the bitrate by using the vbv options -maxrate, -minrate and -bufsize options.

You can prevent lowering the quality too much (accepting an higher resulting filesize) by passing -crf_max, the option accepts the same range as -crf.

avconv -i source -c:v libx264 -crf 25 -crf_max 35  -maxrate 4M -bufsize 2M -c:a copy out.flv

Average Bitrate target (two-pass encoding)

In order to precisely hit a defined filesize, thus a precise average bitrate, two-pass encoding is suggested. It takes more time than using crf and the result perceptually cannot be better, but in many scenarios the size requirement is the most important.

avconv -i input -c:v libx264 -preset medium -b:v 800k -pass 1 -an -f null -
avconv -i input -c:v libx264 -preset medium -b:v 800k -pass 2 -c:a aac out.mkv

Pseudo Constant Bitrate

X264 does not have a native constant bitrate mode. Setting -qcomp to 0 and setting the vbv should get a decent result.

avconv -i input -c:v libx264 -bufsize 2M -maxrate 2M -qcomp 0 out.ts


Setting -crf or -qp to 0 forces x264 in lossless mode the -preset settings then impact just the speed/size ratio.

Faster encoding

avconv -i input -c:v libx264 -preset superfast -qp 0 -c:a copy out.mkv

Smaller filesize

avconv -i input -c:v libx264 -preset veryslow -qp 0 -c:a copy out.mkv

Additional resources

Intel MediaSDK

Enabling libmfx support

The Intel QSV engine can be accessed by multiple means, for encoding Libav uses the MediaSDK mfx_dispatch.


Install msys2 from https://msys2.github.io/

pacman -S git mingw-w64-x86_64-gcc

Download and install the SDK from https://software.intel.com/en-us/media-sdk , it will update PATH and INTELMEDIASDKROOT

Install mfx_dispatch

pacman -S automake autoconf libtool make

Configure and compile:

cd mfx_dispatch
autoreconf -ivf
./configure --prefix=/mingw64
make -j 8
make install

Install Libav

git clone git://git.libav.org/libav

packman -S mingw-w64-x86_64-yasm mingw-w64-x86_64-pkg-config 

Configure and compile:

cd libav
./configure --enable-libmfx --prefix=/mingw64
make -j 8
make install



Install the Media SDK

The current Media SDK for Linux sources are available on github.

You may follow the instructions provided there to install the most recent version of it.

Install Libav

Libav 12 and further do support libmfx out of box.

Configure and compile:

cd libav
./configure --enable-libmfx
make -j 8
make install




Transcoding with hardware deinterlacing and/or scaling

Advanced Options


The CUDA and the NVENC headers are currently proprietary, builds supporting it are non-redistributable.

Enabling NVENC support


Make sure you have the current sdk and make sure nvEncodeAPI.h header and make sure it is in the include path and that the library is in the library path.


The NVENC support can leverage CUDA for scaling, so make sure it is installed and the cuda.h header is in the include path and the libcuda.so is in the library path.

Libav configuration

./configure --enable-nvenc --enable-cuda --enable-nonfree

Should be enough, if the headers are in custom paths --extra-cflags="-I/path/to/header" must be passed to make the headers reachable.




Transcoding with hardware deinterlacing and/or scaling

Advanced Options