Install FFmpeg on Nvidia CUDA Container
Using FFmpeg with NVIDIA GPU Hardware Acceleration - NVIDIA Docs
NVIDIA FFmpeg Transcoding Guide | NVIDIA Technical Blog
User Guide — container-toolkit 1.13.5 documentation
This documentation describes to install FFmpeg
on nvidia/cuda
container to use the Nvidia GPU to accelerate encoding.
If you want to know how to install FFmpeg with NVIDIA GPU on Linux, go to see that.
FFmpeg can support hardware-based decoding and encoding for Nvidia GPU card. With the help of Nvidia GPU, h264_nvenc
can lead encoding speed with 5x faster than libx264
in GTX1080 card.
Let's see how to install everything one by one on the Nvidia CUDA Docker container nvidia/cuda:12.2.0-devel-ubuntu20.04
, in which CUDA toolkit and GPU driver are already included.
It must use
nvidia/cuda:xxx-devel-xxx
image to buildFFmpeg
, because thedev
image contain all the necessary libraries.
Prerequisites
Make sure Nvidia GPU Driver is installed in your host machine! As it will be mounted into the container.
Use ldconfig
to check if the required Nvidia GPU driver libraries are available inside the container. Such as,
ldconfig -p | grep libcuda
When running in the
nvidia/cuda
Docker container, what Nvidia libraries(from the host machine) should be mounted inside the container are specified by theNVIDIA_DRIVER_CAPABILITIES
env variable, see driver-capabilities. Here forFFmpeg
to employ GPU, it should be included at least asNVIDIA_DRIVER_CAPABILITIES=video,utility
.
Step by Step
docker run --rm --runtime=nvidia \
-e NVIDIA_VISIBLE_DEVICES=all \
-e NVIDIA_DRIVER_CAPABILITIES=compute,utility \
nvidia/cuda nvidia-smi
docker run --rm --runtime=nvidia \
-e NVIDIA_VISIBLE_DEVICES=all \
-e NVIDIA_DRIVER_CAPABILITIES=compute,utility \
nvidia/cuda bash
Complete Dockerfile
The source code is available at Dockerfile
# pull official base image, NOTE to use `devel`
FROM nvidia/cuda:12.2.0-devel-ubuntu20.04
# Set environment variables to prevent interactive prompts during installation
ENV DEBIAN_FRONTEND=noninteractive
# Install dependencies
RUN apt-get update && \
apt-get install -y wget git make build-essential pkg-config yasm cmake libtool libc6 libc6-dev unzip libnuma1 libnuma-dev && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install FFmpeg 6.1
RUN mkdir -p /opt
RUN cd /opt/ && wget https://github.com/FFmpeg/nv-codec-headers/releases/download/n12.1.14.0/nv-codec-headers-12.1.14.0.tar.gz -O nv-codec-headers-12.1.14.0.tar.gz && tar -xf nv-codec-headers-12.1.14.0.tar.gz
RUN cd /opt/nv-codec-headers-12.1.14.0 && \
make install PREFIX=/usr
RUN cd /opt && wget https://ffmpeg.org/releases/ffmpeg-6.1.tar.xz -O ffmpeg-6.1.tar.xz && tar -xf ffmpeg-6.1.tar.xz
RUN cd /opt/ffmpeg-6.1 && \
./configure --enable-cuda --enable-cuvid --enable-nvdec --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --enable-libfreetype --enable-libharfbuzz --enable-libfontconfig --enable-libfribidi --enable-filter=drawtext && \
make -j 8 && \
make install PREFIX=/usr
CMD ["ffmpeg", "-version"]
Known issues
Nvidia Docker encoding stops after long running time with such error message: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
.
Possible solution:
Edit /etc/defautls/grub
,
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash systemd.unified_cgroup_hierarchy=0"
Then run update-grub
and reboot.