可参考:
Using FFmpeg with NVIDIA GPU Hardware Acceleration - NVIDIA Docshttps://docs.nvidia.com/video-technologies/video-codec-sdk/13.0/ffmpeg-with-nvidia-gpu/index.html运行以下命令检查当前 FFmpeg 是否支持
hevc_nvenc
:
ffmpeg -hide_banner -encoders | grep nvenc
- 若输出包含
hevc_nvenc
,说明编码器已集成,问题出在驱动或参数配置 - 若无输出,则需要手动编译 ffmpeg
安装显卡驱动、cuda和cudnn
NVIDIA 官方编解码支持矩阵:
Video Encode and Decode GPU Support Matrix | NVIDIA Developer
显卡驱动需要升级到 570 以上
# cuda 12.6
wget https://developer.download.nvidia.com/compute/cuda/12.6.3/local_installers/cuda_12.6.3_560.35.05_linux.run
sudo sh cuda_12.6.3_560.35.05_linux.run# 编辑环境变量
vim ~/.bashrc
export PATH=/usr/local/cuda-12.6/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-12.6/lib64:$LD_LIBRARY_PATH# cudnn
wget https://developer.download.nvidia.com/compute/cudnn/9.8.0/local_installers/cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb
sudo dpkg -i cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb
sudo cp /var/cudnn-local-repo-ubuntu2204-9.8.0/cudnn-local-8138232B-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cudnn# 确定cudnn 安装和版本
cat /usr/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
安装编解码器头文件
# 安装 nv-codec-headers(关键步骤)
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
sudo make install
安装 nasm
git clone https://github.com/netwide-assembler/nasm
cd nasm
./autogen.sh
./configure
make -j4
sudo make install
编译ffmpeg
git clone https://git.ffmpeg.org/ffmpeg.git
cd ffmpeg# 配置编译参数(核心优化)
./configure \--prefix=/usr/local/ffmpeg-7.1.1 \--enable-gpl \--enable-nonfree \--enable-cuda-nvcc \--enable-nvenc \--enable-cuvid \--enable-cuda-llvm \--enable-shared \--extra-cflags="-I/usr/local/cuda/include" \--extra-ldflags="-L/usr/local/cuda/lib64" \--enable-opengl \--enable-libx264 \--enable-libx265 \--enable-libfreetype \--enable-libtheora# 多线程编译(根据 CPU 核心数调整)
make -j$(nproc) && sudo make install# 添加动态库路径
echo "/usr/local/ffmpeg-7.1.1/lib" | sudo tee /etc/ld.so.conf.d/ffmpeg.conf
sudo ldconfig# 添加环境变量
export PATH=/usr/local/ffmpeg-7.1.1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/ffmpeg-7.1.1/lib:$LD_LIBRARY_PATH
如果遇到
ERROR: failed checking for nvcc
注意看一下 cat ffbuild/config.log 日志
日志最后显示:
nvcc -gencode arch=compute_60,code=sm_60 -O2 -std=c++11 -m64 -ptx -c -o /tmp/ffconf.eoC3Drkf/test.o /tmp/ffconf.eoC3Drkf/test.cu
gcc: fatal error: cannot execute 'cc1plus': execvp: No such file or directory
compilation terminated.
nvcc fatal : Failed to preprocess host compiler properties.
ERROR: failed checking for nvcc.
发现需要 gcc 11 版本,当前系统默认是gcc 12 的版本,可以临时切换一下gcc版本:
sudo mv /usr/bin/gcc /usr/bin/gcc.bak
sudo ln -s /usr/bin/gcc-11 /usr/bin/gcc
gcc --version
如果遇到
ERROR: x264 not found using pkg-config
# 从源码编译安装(推荐)
git clone https://code.videolan.org/videolan/x264.git
cd x264
./configure --enable-shared --enable-pic
make -j$(nproc)
sudo make install
测试推流
ffmpeg -stream_loop -1 -i input.mp4 \-c:v h264_nvenc -preset p1 -profile high -b:v 4000k \-rtsp_transport udp -muxdelay 0.5 -f rtsp rtsp://127.0.0.1:8554/stream1
- 通过
nvidia-smi -q | grep Encoder
查看当前NVENC利用率 - nvidia-smi dmon -s pucvmet # 观察NVENC利用率(%enc列)
注意:
NVIDIA GPU能够处理的并发 NVENC 会话数量是有限的。例如,RTX 3060 最多支持 9 个并发 NVENC 会话。尝试启动第 10 个会话会导致您遇到的错误。 OpenEncodeSessionEx 失败:不兼容的客户端密钥 (21): (无详细信息) 表示 NVIDIA NVENC 编码器由于 GPU 允许的并发编码会话数量限制而无法初始化。
错误如下:
[vist#0:0/h264 @ 0x608651a88700] Resumed reading at pts 0.000 with rate 1.050 after a lag of 0.500s
[hevc_nvenc @ 0x608651a882c0] OpenEncodeSessionEx failed: incompatible client key (21): (no details)
[hevc_nvenc @ 0x608651a882c0] No capable devices found
[vost#0:0/hevc_nvenc @ 0x608651a85c80] [enc:hevc_nvenc @ 0x608651a86700] Error while opening encoder - maybe incorrect parameters such as bit_rate, rate, width or height.
[vf#0:0 @ 0x608651a60480] Error sending frames to consumers: Generic error in an external library
[vf#0:0 @ 0x608651a60480] Task finished with error code: -542398533 (Generic error in an external library)
[vf#0:0 @ 0x608651a60480] Terminating thread with return code -542398533 (Generic error in an external library)
[vost#0:0/hevc_nvenc @ 0x608651a85c80] [enc:hevc_nvenc @ 0x608651a86700] Could not open encoder before EOF
[vost#0:0/hevc_nvenc @ 0x608651a85c80] Task finished with error code: -22 (Invalid argument)
[vost#0:0/hevc_nvenc @ 0x608651a85c80] Terminating thread with return code -22 (Invalid argument)
[out#0/rtp_mpegts @ 0x608651a56d00] Nothing was written into output file, because at least one of its streams received no packets.