In your case, you'll need to build FFmpeg from source. Use the example shown below.
For the supported and available hardware accelerated features you can achieve with a current generation NVENC-capable NVIDIA GPU, see this answer.
For the build:
FFmpeg build with NVENC and all available CUDA-based filters on Ubuntu 18.04LTS:
Build platform: Ubuntu 18.04LTS
Goals: Generate an FFmpeg build that can utilize NVENC and all available CUDA-based filters on Ubuntu 18.04LTS+. The build will be prefixed to /opt
.
Ensure the platform is up to date:
sudo apt update && sudo apt -y upgrade && sudo apt -y dist-upgrade
Install baseline dependencies first (inclusive of OpenCL headers+)
sudo apt-get -y install autoconf automake build-essential libass-dev \
libtool libssl-dev \
pkg-config texinfo zlib1g-dev cmake mercurial libbz2-dev rtmpdump librtmp-dev libunistring2 libunistring-dev opencl-headers ocl-icd-*
Create the workspace:
sudo mkdir -p /opt/ffmpeg
mkdir -p ~/ffmpeg_sources
Install CUDA 10 SDK :
Ensure that you have the latest driver:
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update && sudo apt-get -y upgrade
On Ubuntu 18.04LTS desktop, this should be enough for the current device driver:
sudo apt-get install nvidia-kernel-source-430 nvidia-driver-430
On Ubuntu 18.04LTS server systems, this will do:
sudo apt install nvidia-headless-430 nvidia-utils-430 libnvidia-common-430 libnvidia-encode-430 libnvidia-decode-430 libnvidia-fbc1-430
We keep the device driver up to the latest version so as to pass ffnvcodec
driver version check.
It's wise to confirm the latest driver version first. Do so by checking the repository here: https://launchpad.net/~graphics-drivers/+archive/ubuntu/ppa
Fetch the SDK binary installer first:
cd ~/ffmpeg_sources
wget -c -v -nc https://developer.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.168_418.67_linux.run
chmod +x ./cuda_10.1.168_418.67_linux.run
./cuda_10.1.168_418.67_linux.run
You'll observe that we're using the binary installer. This is most useful for these who're on headless systems (without X) as it will install CUDA without pulling in unnecessary dependencies.
You could also use the official CUDA repository provided by NVIDIA for Ubuntu, but I cannot recommend it as it pulls in a lot of unnecessary packages.
Confirm that the library configuration file for CUDA libraries also exists with the correct settings:
/etc/ld.so.conf.d/cuda.conf
The content should be:
/usr/local/cuda/lib64
When done, load the new configuration:
sudo ldconfig -vvvv
Now, set up the environment variables for CUDA:
Edit the /etc/environment
file and append the following:
CUDA_HOME=/usr/local/cuda
Now, append the PATH variable with the following:
/usr/local/cuda/bin:/opt/ffmpeg/bin
When done, remember to source the file:
source /etc/environment
And proceed.
On NVENC enablement:
Take note that changes to the inclusion of third party headers affects new builds, and this is fixed by:
cd ~/ffmpeg_sources
git clone https://github.com/FFmpeg/nv-codec-headers
cd nv-codec-headers
make
sudo make install PREFIX="/opt/ffmpeg_build"
Build a usable FFmpeg binary prefixed to /opt/ffmpeg:
Include extra components as needed:
(a). Build and deploy nasm: Nasm is an assembler for x86 optimizations used by x264 and FFmpeg. Highly recommended or your resulting build may be very slow.
Note that we've now switched away from Yasm to nasm, as this is the current assembler that x265,x264, among others, are adopting.
cd ~/ffmpeg_sources
wget https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.gz
tar xzvf nasm-2.14.02.tar.gz
cd nasm-2.14.02
./configure --prefix="/opt/ffmpeg_build" --bindir="/opt/ffmpeg/bin"
make -j$(nproc) VERBOSE=1
sudo make -j$(nproc) install
make -j$(nproc) distclean
(b). Build and deploy libx264 statically: This library provides a H.264 video encoder. See the H.264 Encoding Guide for more information and usage examples. This requires ffmpeg to be configured with --enable-gpl --enable-libx264
.
cd ~/ffmpeg_sources
git clone http://git.videolan.org/git/x264.git
cd x264/
PATH="/opt/ffmpeg/bin:$PATH" ./configure --prefix="/opt/ffmpeg_build" --enable-static --enable-shared
PATH="/opt/ffmpeg/bin:$PATH" make -j$(nproc) VERBOSE=1
sudo make -j$(nproc) install VERBOSE=1
make -j$(nproc) distclean
(c ). Build and configure libx265: This library provides a H.265/HEVC video encoder. See the H.265 Encoding Guide for more information and usage examples.
cd ~/ffmpeg_sources
hg clone https://bitbucket.org/multicoreware/x265
cd ~/ffmpeg_sources/x265/build/linux
PATH="/opt/ffmpeg/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="/opt/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make -j$(nproc) VERBOSE=1
sudo make -j$(nproc) install VERBOSE=1
make -j$(nproc) clean VERBOSE=1
(d). Build and deploy the libfdk-aac library: This provides an AAC audio encoder. See the AAC Audio Encoding Guide for more information and usage examples. This requires ffmpeg to be configured with --enable-libfdk-aac
(and --enable-nonfree
if you also included --enable-gpl
).
cd ~/ffmpeg_sources
wget -O fdk-aac.tar.gz https://github.com/mstorsjo/fdk-aac/tarball/master
tar xzvf fdk-aac.tar.gz
cd mstorsjo-fdk-aac*
autoreconf -fiv
./configure --prefix="/opt/ffmpeg_build" --disable-shared
make -j$(nproc)
sudo make -j$(nproc) install
make -j$(nproc) distclean
(e). Build and configure libvpx:
cd ~/ffmpeg_sources
git clone https://github.com/webmproject/libvpx
cd libvpx
./configure --prefix="/opt/ffmpeg_build" --enable-runtime-cpu-detect --enable-vp9 --enable-vp8 \
--enable-postproc --enable-vp9-postproc --enable-multi-res-encoding --enable-webm-io --enable-better-hw-compatibility --enable-vp9-highbitdepth --enable-onthefly-bitpacking --enable-realtime-only --cpu=native --as=nasm
time make -j$(nproc)
sudo time make -j$(nproc) install
time make clean -j$(nproc)
time make distclean
(f). Build LibVorbis:
cd ~/ffmpeg_sources
git clone https://git.xiph.org/vorbis.git
cd vorbis
autoreconf -ivf
./configure --enable-static --prefix="/opt/ffmpeg_build"
time make -j$(nproc)
sudo make -j$(nproc) install
time make clean -j$(nproc)
time make distclean
Now add the new library path /opt/ffmpeg_build/lib
to your ldconfig:
sudo su
echo /opt/ffmpeg_build/lib > /etc/ld.so.conf.d/ffmpeg.conf
Then run:
sudo ldconfig -vvvv
Then proceed.
(g). Build FFmpeg:
cd ~/ffmpeg_sources
git clone https://github.com/FFmpeg/FFmpeg -b master
cd FFmpeg
PATH="/opt/ffmpeg/bin:$PATH" PKG_CONFIG_PATH="/opt/ffmpeg_build/lib/pkgconfig" ./configure \
--pkg-config-flags="--static" \
--prefix="/opt/ffmpeg" \
--bindir="/opt/ffmpeg/bin" \
--extra-cflags="-I/opt/ffmpeg_build/include" \
--extra-ldflags="-L/opt/ffmpeg_build/lib" \
--enable-cuda-nvcc \
--enable-cuvid \
--enable-libnpp \
--extra-cflags="-I/usr/local/cuda/include/" \
--extra-ldflags=-L/usr/local/cuda/lib64/ \
--enable-nvenc \
--enable-libass \
--disable-debug \
--enable-libvorbis \
--enable-libvpx \
--enable-opencl \
--enable-gpl \
--cpu=native \
--enable-libfdk-aac \
--enable-libx264 \
--enable-libx265 \
--enable-openssl \
--enable-librtmp \
--extra-libs="-lpthread -lm -lz" \
--enable-nonfree
PATH="/opt/ffmpeg/bin:$PATH" make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) distclean
hash -r
Since we added /opt/ffmpeg/bin
to the system path, you can call up FFmpeg directly.