How To Compile FFmpeg With NVENC H264 Hardware Encoder in Ubuntu 18.04

How To Compile FFmpeg With NVENC H264 Hardware Encoder in Ubuntu 18.04

 

To enable hardware acceleration transcoding in FFmpeg we need compile FFmpeg with NVENC h264 codec support.

 

Install necessary software:

dpkg --add-architecture i386
apt-get update
apt-get install build-essential git yasm unzip wget sysstat nasm libc6:i386 \
libavcodec-dev libavformat-dev libavutil-dev pkgconf g++ freeglut3-dev \
libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev

 

Install CUDA Toolkot 10.1, NVIDIA drivers on Ubuntu 18.04

Before proceeding installation NVIDIA drivers on Ubuntu you must disable Nouveau kernel driver. To disable the default Nouveau Nvidia driver follow guide: How to disable Nouveau Nvidia driver on Ubuntu 18.04.

Identify your installed NVIDIA video card:

# lshw -numeric -C display

Example output:

Identify NVIDIA card

In our case it’s NVIDIA GP106 Pascal family GPU with NVENC support. Here is Video Encode and Decode GPU Support Matrix. If your GPU supporting NVENC.

Install CUDA Toolkit 10.1 from NVIDIA official site.

 

Then reboot your system:

# reboot

Now NVIDIA drivers are installed.

You can check NVIDIA GPU drivers installation by command:

# nvidia-smi

Example output:

nvidia-smi output

IMPORTANT! Set up environment variable and LD_LIBRARY_PATH path.

$ export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}
$ export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib {LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

 

From NVIDIA website download Video Codec SDK from: https://developer.nvidia.com/nvidia-video-codec-sdk,

after unzip copy all headers:

# unzip Video_Codec_SDK_9.0.20.zip
# cp Video_Codec_SDK_9.0.20/Samples/common.mk /usr/local/include/

 

Install nv-codec-headers

# git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
# cd nv-codec-headers
# make
# make install

 

Install x264

It’s optional step but if you planning  using in future regular software encoding also, you need install x264.

# git clone https://code.videolan.org/videolan/x264.git
# cd x264/
# ./configure --disable-cli --enable-static --enable-shared --enable-strip
# make
# make install
# ldconfig

 

Install FFmpeg

Download FFmpeg sources from git or download latest stable FFmpeg release from FFmpeg website, compile and install

# git clone git://source.ffmpeg.org/ffmpeg.git

# cd ffmpeg/

Configure and make with –enable-nonfree –enable-nvenc options (for scale use:–enable-cuda-nvcc –enable-cuda-sdk):

# ./configure --enable-nonfree --enable-nvenc --enable-libx264 --enable-gpl --enable-cuda --enable-cuvid --enable-cuda-nvcc

# make

To check NVENC support in FFmpeg:

./ffmpeg -codecs | grep nvenc

Example output:

DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_v4l2m2m h264_cuvid ) (encoders: libx264 libx264rgb h264_nvenc h264_v4l2m2m nvenc nvenc_h264 )
DEV.L. hevc H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_cuvid ) (encoders: nvenc_hevc hevc_nvenc )

 

Compare FFmpeg Transcode Performance with NVENC and libx264

 

Software transcode example with x264:

# time ./ffmpeg -i InputVideo -vcodec libx264 -b:v 5M -acodec copy Output.mp4

NVENC transcode example with utilizing NVIDIA GPU Hardware Acceleration:

# time ./ffmpeg -i InputVideo -vcodec h264_nvenc -b:v 5M -acodec copy Output.mp4

 

Full Hardware Transcoding Example

Full hardware transcode example with CUVID and NVENC:

# ffmpeg -hwaccel cuvid -c:v h264_cuvid -i input -c:v h264_nvenc -preset slow output

Discuss article in ArsTech Forum

   

If you like what you are reading, please:

Buy me a coffeeBuy me a coffee

arstech

5 Comments

  1. I got

    Unknown option “–enable-cuda-nvcc”.
    See ./configure –help for available options.

    on ffmpeg 4.14

  2. There’s a ‘ ‘ instead of a ‘$’ in “export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib {LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}”

    Otherwise seems like a very helpful article thus far!

    • “There’s a ‘ ‘ instead of a ‘$’ in “export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib {LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}””

      So how should it be? I keep getting “not a valid identifier” error.

      • export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

        worked for me. (replaced the space after ‘lib’ with a $)

Leave a Reply