8

编译安装 GPU 版 ffmpeg

 1 year ago
source link: https://xujinzh.github.io/2023/01/10/ffmpeg-gpu/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ffmpeg 是适用于 Linux 和类 Unix 系统的免费开源视频转换软件。视频图片处理使用 GPU 能明显提高速度,缩减运行时间。本篇介绍如何编译安装 GPU 版 ffmpeg 及使用方法。以 Ubuntu 18.04 为例。

Nvidia 驱动和 GPU 加速库安装

想使用 GPU 加速计算,就需要 GPU 驱动和加速库。请参考我的另一篇博文安装: 深度学习的 GPU 环境的配置

实践中,我首先配置的 NVIDIA 驱动版本是:NVIDIA-Linux-x86_64-470.129.06.run,CUDA 加速框架是:cuda_11.1.0_455.23.05_linux.run,深度学习加速库是: cudnn-linux-x86_64-8.6.0.163_cuda11-archive.tar.xz,编译安装 FFMPEG 后出现:Driver does not support the required nvenc API version. Required: 12.0 Found: 11.1,然后重新安装 NVIDIA 驱动版本:NVIDIA-Linux-x86_64-525.60.13.run,解决问题。

mkdir ~/nvidia/
cd ~/nvidia/
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
sudo make install

cd ~/nvidia/
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/
sudo apt install build-essential yasm cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-dev
cd ~/nvidia/ffmpeg/
./configure --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
make -j $(nproc)
sudo make install

# 安装后就可以删除下载的仓库代码了。
rm -r ~/nvidia

安装后,默认将 ffmpeg, ffprobe 安装在 /usr/local/bin 目录下。为了便于区分 CPU 版的 ffmpeg,我常将 GPU 版更名:

cd /usr/local/bin
sudo mv ffmpeg ffmpeg-gpu
sudo mv ffprobe ffprobe-gpu

CPU 版 ffmpeg 使用方法请参考我的另一篇博文:FFmpeg 视频处理等相关 ,为了便于比较,这里给出一个转码视频各位为 H264 的例子:

ffmpeg -i input.mkv -c:v libx264 -c:a aac output-cpu.mp4
frame= 6761 fps=129 q=28.0 size=   59648kB time=00:04:42.51 bitrate=1729.6kbits/s speed=5.38x

GPU 版 ffmpeg 因为编码方式改变,所以转码方式如下:

ffmpeg-gpu -y -hwaccel cuda -i input.mkv -c:v h264_nvenc -c:a aac output-gpu.mp4
frame=29343 fps=261 q=31.0 size=  189184kB time=00:20:24.02 bitrate=1266.2kbits/s speed=10.9x

可见速度是 CPU 版的两倍左右。默认使用 GPU 0,查看使用情况:

nvidia-smi

查看支持的编码:

ffmpeg-gpu --codecs

# 查看支持 H264 情况
ffmpeg-gpu -codecs | grep 264
# 结果
ffmpeg version N-109541-g94aa70d757 Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
libavutil 57. 43.100 / 57. 43.100
libavcodec 59. 56.100 / 59. 56.100
libavformat 59. 34.102 / 59. 34.102
libavdevice 59. 8.101 / 59. 8.101
libavfilter 8. 53.100 / 8. 53.100
libswscale 6. 8.112 / 6. 8.112
libswresample 4. 9.100 / 4. 9.100
DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_v4l2m2m h264_cuvid ) (encoders: h264_nvenc h264_v4l2m2m )

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK