FFmpeg h264_vaapi intel hardware accelerated video encoding works on Ubuntu 17.10. FFmpeg (version 3.3.4-2) supports full H.264 and HEVC encode in VAAPI on supported hardware (Haswell+) that works reliably well to be termed “production-ready”.
ffmpeg -loglevel verbose -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -i "input file" -vf 'format=nv12,hwupload' -threads 8 -aspect 16:9 -f matroska -acodec aac -b:v 2000k -vcodec h264_vaapi "output file"
ffmpeg -loglevel verbose -vaapi_device /dev/dri/renderD128 -i "input.mp4" -vf 'format=nv12,hwupload' -vcodec h264_vaapi -qp 19 -bf 2 -acodec aac "output.mp4"
ffmpeg -vaapi_device /dev/dri/renderD128 -i "input.mp4" -vf 'format=nv12,hwupload' -c:v h264_vaapi -qp 27 -g 25 -c:a aac -metadata:s:a language=eng -metadata creation_time=now -loglevel verbose "output.mp4"
ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device /dev/dri/renderD128 -i "input.mp4" -c:v h264_vaapi -qp 27 -g 25 -c:a aac -metadata:s:a language=eng -metadata creation_time=now -loglevel verbose "output.mp4"
Let’s break down these arguments to their meaning:
- -loglevel : tells ffmpeg to log ffmpeg events as verbose output, and is completely optional. You can disregard this.
- -vaapi_device : This is important. You must select a valid VAAPI H/W context device to which you will upload textures to via hwupload, formatted in the NV12 colorspace. This points to a /dev/dri/render*_ file on your Linux system.
- -vf : This is an inbuilt ffmpeg option that allows you to specify codec options/arguments to be passed to our encoder, in this case, h264_vaapi. Here, we tell ffmpeg to convert all textures to one colorspace, NV12 (As it’s the one accepted by Intel’s QuickSync hardware encoder) and to also use hwupload, an ffmpeg intrinsic, that tells the program to asynchronously copy the converted pixel data to VAAPI’s surfaces.
- – threads : Specifies the number of threads that FFmpeg should use. By default, use the number of logical processors available on your processor here. On Intel processors that support Hyperthreading, multiply the number of cores your processor has by 2.
- -f : Specifies the container format specification you can use. This can be Matroska, webm, mp4, etc. Take your pick (as per your container constraints).
- -acodec : Specifies the audio codec to use when transcoding the video’s audio stream. We can use ffmpeg’s muxers to copy the audio stream as is, untouched.
- -vcodec : Selects the video encoder to use. In this case, we selected h264_vaapi, our key point of interest here.
- -hwaccel vaapi : This instructs ffmpeg to use VAAPI based hardware accelerated decode (for supported codecs, see platform limits), and it can drastically lower the processor load during the process. Note that you should only use this option if your hardware supports hardware-accelerated decoding via VAAPI for the source fornat being encoded.
- Using the vaapi_scaler in the video filters : It is possible to use Intel’s QuickSync hardware via VAAPI for resize and scaling (when up-or downscaling the input source to a higher or lower resolution), using a filter snippet such as the one shown below:
You may specify a different resolution by changing the dimensions in =w= and :h= to suit your needs.
- -hwaccel_output_format : This option should be used every time you declare the -hwaccel method as vaapi , so that the decode stage takes place entirely in hardware. This option generates decode output directly on VAAPI hardware surfaces, speeding up decode performance significantly.
You may confirm supported decode formats on your setup by running :