Categories
ffmpeg GPU Graphics Card Multimedia

Building ffmpeg with CUDA support

If you want to use GPU accelerated video encoding in Linux you’ll end up using ffmpeg (even if you may not know that it’s used underneath the software you’re running).

While experimenting with a Nvidia card, I found myself struggling with the Nvidia documentation about how to build ffmpeg with CUDA support.

Fix missing dependencies

While the documentation itself is nice and short, for my environment (as usual Ubuntu 24.04) it’s missing some parts (mostly package dependencies). So here’s the (even shorter) documentation how I managed to build ffmpeg with Nvidia CUDA support:

linux # sudo apt-get install build-essential yasm cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-dev nasm pkg-config
linux # git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
linux # cd nv-codec-headers && sudo make install && cd -
linux # git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/
linux # cd ffmpeg
linux # ./configure --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --disable-static --enable-shared
linux # make -j 8
linux # make install
linux # ldconfig
linux # cd -

Mind the extra packages nasm and pkg-config. While the error with the missing nasm package is reported, the building of nvenc-enabled en/decoders is silently dropped if pkg-config is missing – the build itself however succeeds.

If everything went according to plan, ffmpeg should be built with nvenc encoders. You can check that with:

linux # ffmpeg -hide_banner -hwaccels | grep cuda
cuda
linux # ffmpeg -hide_banner -encoders | grep nvenc
 V....D av1_nvenc            NVIDIA NVENC av1 encoder (codec av1)
 V....D h264_nvenc           NVIDIA NVENC H.264 encoder (codec h264)
 V....D hevc_nvenc           NVIDIA NVENC hevc encoder (codec hevc)

This took me a while so I hope this short post will save others some time.

Links

https://docs.nvidia.com/video-technologies/video-codec-sdk/11.1/ffmpeg-with-nvidia-gpu/index.html

Leave a Reply

Your email address will not be published. Required fields are marked *