NVIDIA GPU加速FFmpeg
如果需要GPU加速FFmpeg,需要自行编译FFmpeg。这里介绍如何在有NVIDIA GPU的机器上编译安装FFmpeg,使得可以使用GPU为FFmpeg加速

如果需要GPU加速FFmpeg,需要自行编译FFmpeg。这里介绍如何在有NVIDIA GPU的机器上编译安装FFmpeg,使得可以使用GPU为FFmpeg加速。

检查

先直接nvidia-smi查看显卡情况,一般在云服务伤申请了GPU机型,显卡驱动、CUDA已经安装好了,不需要自行安装。

➜  ~ nvidia-smi
Sat Sep 17 09:31:30 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 510.85.02    Driver Version: 510.85.02    CUDA Version: 11.6     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Tesla T4            Off  | 00000000:00:1E.0 Off |                    0 |
| N/A   27C    P8     9W /  70W |    502MiB / 15360MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      3985      G   /usr/bin/X                        201MiB |
|    0   N/A  N/A      5383      G   /usr/bin/gnome-shell              155MiB |
|    0   N/A  N/A      9355      G   /usr/bin/gnome-shell              139MiB |
+-----------------------------------------------------------------------------+

依赖

安装依赖参考FFmpeg官网的 CompilationGuide

除了FFmpeg的依赖,还需要安装 ffnvcodec

➜  ~ git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
➜  ~ cd nv-codec-headers
➜  ~ sudo make install
sed 's#@@PREFIX@@#/usr/local#' ffnvcodec.pc.in > ffnvcodec.pc
install -m 0755 -d '/usr/local/include/ffnvcodec'
install -m 0644 include/ffnvcodec/*.h '/usr/local/include/ffnvcodec'
install -m 0755 -d '/usr/local/lib/pkgconfig'
install -m 0644 ffnvcodec.pc '/usr/local/lib/pkgconfig'
➜  ~ export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"
➜  ~ pkg-config --modversion ffnvcodec
11.1.5.1

配置

  PATH="$HOME/bin:$PATH" \ 
  PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
  --prefix="$HOME/ffmpeg_build" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I$HOME/ffmpeg_build/include -I/usr/local/cuda/include" \
  --extra-ldflags="-L$HOME/ffmpeg_build/lib -L/usr/local/cuda/lib64" \
  --extra-libs=-lpthread \
  --extra-libs=-lm \
  --bindir="$HOME/bin" \
  --enable-cuda \
  --enable-cuvid \
  --enable-nvenc \
  --enable-gpl \
  --enable-libnpp \
  --enable-libfdk_aac \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree

编译

make -j 8 # 有多少物理核心写多少,可以加快编译速度

安装

sudo make install

检查

ffmpeg -hwaccels

参考文档

  1. Using FFmpeg with NVIDIA GPU Hardware Acceleration

  2. FFmpeg Compilation Guide


最后修改于 2022-09-17

此篇文章的评论功能已经停用。