centos安装支持gpu加速的ffmpeg

发布时间 2023-09-01 13:59:29作者: 剑小破

阿里云ECS环境:
规格:ecs.gn6i-c16g1.4xlarge
显卡:T4
镜像:centos7.5
cuda安装版本:12.2

1.安装显卡驱动 https://www.nvidia.com/Download/Find.aspx?lang=cn
wget https://cn.download.nvidia.com/tesla/535.104.05/NVIDIA-Linux-x86_64-535.104.05.run
chmod +x NVIDIA-Linux-x86_64-535.104.05.run
sh NVIDIA-Linux-x86_64-535.104.05.run
验证:nvidia-smi
2.安装ffnvcodec
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
make
make install
添加环境变量
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"
3.安装cuda https://developer.nvidia.com/cuda-toolkit-archive
wget https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run
sh cuda_12.2.0_535.54.03_linux.run

vi /etc/profile
在文件末尾添加以下内容:
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
保存并退出后,执行以下命令使修改生效:
source /etc/profile
验证:nvcc -V

4.安装cudnn https://developer.nvidia.com/rdp/cudnn-archive
下载 cudnn-linux-x86_64-8.9.2.26_cuda12-archive.tar.xz
tar -xvf cudnn-linux-x86_64-8.9.2.26_cuda12-archive.tar.xz
cd cudnn-linux-x86_64-8.9.2.26_cuda12-archive
cp lib/* /usr/local/cuda/lib64/
cp include/* /usr/local/cuda/include/

5.安装ffmpeg
安装基本开发工具 sudo yum groupinstall "Development Tools"
安装RPM Fusion存储库
sudo yum install -y https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
sudo yum install -y https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
安装编解码库和依赖
sudo yum install fdk-aac-devel
sudo yum install x264-devel
下载ffmpeg源码
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/
cd ffmpeg
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-shared --enable-ffmpeg --enable-ffplay --enable-ffprobe --enable-libx264 --enable-nvenc --enable-cuda --enable-cuvid --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
make -j$(nproc)
make install
验证:
ffmpeg -hwaccels
ffmpeg -codecs | grep "h264"
报错:ffmpeg: error while loading shared libraries: libavdevice.so.60: cannot open shared object file: No such file or directory
sudo find / -name "libavdevice.so.60"
export LD_LIBRARY_PATH=/root/ffmpeg/libavdevice:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH