ffmpeg 添加 nvenc支持
运行以下命令检查当前 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: 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# 配置 pkg-config 路径
sudo cp /usr/local/x264/lib/pkgconfig/x264.pc /usr/share/pkgconfig/
测试推流
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列)