Looking for a solution to use FFmpeg GPU decoding for CCTV cameras, but noticed that the hardware acceleration flags such as -libmfx, -libvpl, cuda_sdk, etc., are listed as non-free.
Here's a guide to help you set up FFmpeg with GPU decoding using free software.
Prerequisites
- Install FFmpeg: Download the latest version of FFmpeg from the official website (https://ffmpeg.org/download.html) and follow the installation instructions for your operating system.
Install NVIDIA CUDA Toolkit (for NVIDIA GPUs)
-
Download the NVIDIA CUDA Toolkit: Go to the NVIDIA CUDA Toolkit download page (https://developer.nvidia.com/cuda-toolkit-archive) and download the version that matches your operating system.
-
Install the CUDA Toolkit: Follow the installation instructions provided by NVIDIA.
-
Set the CUDA environment variables: Add the following lines to your
.bashrcor.bash_profilefile:
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
- Restart your terminal to apply the changes.
Install OpenCL (for AMD GPUs)
- Install OpenCL headers and libraries: On Ubuntu, you can install the
opencl-headersandopencl-icd-opencl-devpackages using the following command:
sudo apt-get install opencl-headers opencl-icd-opencl-dev
-
Install the AMD OpenCL SDK: Download the AMD OpenCL SDK from the official website (https://developer.amd.com/tools/opencl-zone/sdk/downloads/) and follow the installation instructions provided by AMD.
-
Set the OpenCL environment variables: Add the following lines to your
.bashrcor.bash_profilefile:
export LD_LIBRARY_PATH=/opt/AMDAPPSDK/lib/x86_64-linux-gnu/:$LD_LIBRARY_PATH
export PATH=/opt/AMDAPPSDK/bin/:$PATH
- Restart your terminal to apply the changes.
Configure FFmpeg for GPU decoding
- Find your GPU information: Run the following command to check your GPU and its capabilities:
nvidia-smi
For AMD GPUs, use the rocm-smi command.
- Configure FFmpeg to use the GPU: Edit the
/etc/ffmpeg/ffmpeg.conffile (create it if it doesn't exist) and add the following lines:
[codec]
hwaccel_device = /usr/local/cuda/lib64/libnvcuvid.so
[decoder_h264_nvenc]
hwaccel = yes
[decoder_h265_nvenc]
hwaccel = yes
Replace /usr/local/cuda/lib64/libnvcuvid.so with the path to the libnvcuvid.so file on your system.
- Restart FFmpeg: Run the following command to restart FFmpeg:
sudo service ffmpeg restart
Example command for GPU decoding
Here's an example command to decode a H.264 CCTV stream using FFmpeg with GPU decoding:
ffmpeg -i rtsp://username:password@ip_address:port -c:v h264_nvenc -gpu 0
Replace username, password, ip_address, and port with the appropriate values for your CCTV stream.
References
- FFmpeg official website: https://ffmpeg.org/
- NVIDIA CUDA Toolkit download page: https://developer.nvidia.com/cuda-toolkit-archive
- AMD OpenCL SDK download page: https://developer.amd.com/tools/opencl-zone/sdk/downloads/
- Install OpenCL headers and libraries on Ubuntu: https://askubuntu.com/questions/674858/how-to-install-opencl-headers-and-libraries-on-ubuntu-12-04
- AMD OpenCL SDK installation instructions: https://developer.amd.com/tools/opencl-zone/sdk/documentation/amd-opencl-sdk-installation-guide/Pages/default.aspx