当前位置: 首页 > news >正文

ffmpeg 添加 nvenc 硬件编解码支持

可参考:
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: 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列)

注意:

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.


http://www.mrgr.cn/news/98990.html

相关文章:

  • 力扣-hot100(盛最多水的容器-双指针)
  • springcloud alibaba
  • Vue3 + TypeScript中provide和inject的用法示例
  • 论文阅读:2024 arxiv AI Safety in Generative AI Large Language Models: A Survey
  • 红黑树insert笔记,外带一点迭代器思考
  • Java拼团项目
  • 经济指标学习(二)
  • Java 序列化与反序列化终极解析
  • C++17 信号量模拟实现
  • 文件上传Ⅰ
  • leetcode第20题(有效的括号)
  • FreeRTOS任务通知
  • MDA测量数据查看器【内含工具和源码地址】
  • Qt QTimer 详解与使用指南
  • 力扣刷题Day 20:柱状图中最大的矩形(84)
  • 解锁C++ gRPC:快速入门指南
  • Java集合框架深度解析:HashMap、HashSet、TreeMap、TreeSet与哈希表原理详解
  • Json 在线格式化 - 加菲工具
  • 工厂方法模式详解及在自动驾驶场景代码示例(c++代码实现)
  • 【多目标进化算法】NSGA-II 算法(结合例子)