0%

wsl 使用支持 cuda 的 ffmpeg

本次演示在 windows11 wsl2 下进行演示使用的系统是 ubuntu22
我将 ffmpeg 安装在了普通用户下,非root安装这样有个好处就是卸载的时侯非常轻松。已我不多的 C 编译经验来看,一旦将软件安装再系统目录,到时候几乎是不可能卸载干净的。还不如重装系统来得方便
我这边测试的是 ubuntu 仓库里提供的 ffmpeg 也是可以调用显卡的,这个是在我编译完 ffmpeg 的时侯才发现的,我这边有两个源 ununtu 官方源和 nvidia 的源,我也懒得研究是谁编译的了,反正是能用了
nvidia 完全按照官方文档编译的 ffmpeg 运行它提供的测试命令会报错,这完全按照文档编译也有这样的问题,我编译 ffmpeg 只是为了玩一下,所以我不会去解决这个问题(我用仓库版也有这个问题,我不清楚是不是我测试用的视频文件,引起的)
还有就是以前的使用命令可能用不了了,因为相应的解码器没有起用。你可以查资料添加对应的解码器。如果不是为了玩一票的话,建议使用 ubuntu 源提供的 ffmpeg

安装 cuda 驱动

cuda 的 wsl 驱动 win11 的物理机提供了,请不要安装 ubuntu 源提供的驱动,请使用 英伟达提供的驱动。
请参考 https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_local 的指示进行安装

1
2
3
4
5
6
7
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda-repo-wsl-ubuntu-11-8-local_11.8.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-8-local_11.8.0-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-11-8-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda

编译 ffmpeg

参考 https://docs.nvidia.com/video-technologies/video-codec-sdk/ffmpeg-with-nvidia-gpu/#compiling-for-wsl https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
这里我修改了一下 nvidia 文档的安装命令,官方的命令安装后需要额外的设置而且容易污染全局环境

1
sudo apt nstall build-essential yasm cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-dev

补充安装文档缺失的包

1
sudo apt install pkg-config nasm

下面就开始编译了
FFMPEG_INSTALL= 请填写你的安装路径
make -j 的数值一般建议填写你的cpu核心数,如果数值太高将卡爆你的电脑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
export FFMPEG_INSTALL="$HOME/.local/ffmpeg"
mkdir -p "$FFMPEG_INSTALL"

git clone https://github.com/FFmpeg/nv-codec-headers
cd nv-codec-headers
make PREFIX="$FFMPEG_INSTALL"
make install PREFIX="$FFMPEG_INSTALL"

cd ../

git clone https://github.com/FFmpeg/FFmpeg.git ffmpeg/
cd ffmpeg
PKG_CONFIG_PATH="$FFMPEG_INSTALL/lib/pkgconfig" \
./configure \
--prefix="$FFMPEG_INSTALL" \
--pkg-config-flags="--static" \
--extra-cflags="-I$FFMPEG_INSTALL/include -I/usr/local/cuda/include" \
--extra-ldflags="-L$FFMPEG_INSTALL/lib -L/usr/local/cuda/lib64" \
--extra-libs=-lpthread \
--extra-libs=-lm \
--enable-gpl \
--enable-nonfree \
--enable-cuda \
--enable-cuvid \
--enable-nvenc \
--enable-libnpp
make -j 10
make install

程序编译安装好后,需要设置,一下变量环境才可以使用
将下列变量添加到 ~/.profile

1
PATH="$HOME/.local/ffmpeg/bin:$PATH"

然后刷新一下变量环境 source ~/.profile
然后运行一下 ffmpeg ,不出意外的话,你的输出将类似于下面这样,能看到 cuda 字段泽证明编译成功了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ffmpeg version N-109344-g1bebcd43e1 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 11 (Ubuntu 11.3.0-1ubuntu1~22.04)
configuration: --prefix=/home/niconiconi/.local/ffmpeg --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --disable-static --enable-shared
libavutil 57. 43.100 / 57. 43.100
libavcodec 59. 54.100 / 59. 54.100
libavformat 59. 34.102 / 59. 34.102
libavdevice 59. 8.101 / 59. 8.101
libavfilter 8. 51.100 / 8. 51.100
libswscale 6. 8.112 / 6. 8.112
libswresample 4. 9.100 / 4. 9.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

测试安装结果

使用下列命令测试显卡转码

1
ffmpeg -y -vsync 0 -hwaccel cuvid -c:v h264_cuvid -i input.mp4 -c:a copy -c:v h264_nvenc -b:v 5M output.mp4

不出意外的话现在应该能看到显卡使用率已近跑上去了

参考资料

https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_local
https://docs.nvidia.com/cuda/wsl-user-guide/index.html
https://docs.nvidia.com/video-technologies/video-codec-sdk/ffmpeg-with-nvidia-gpu/#compiling-for-wsl