- 下载并编译安装 FFmpeg NVIDIA headers (“ffnvcodec”)
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
(# 有多个分支,根据GPU驱动版本可能需要切换分支,本文采用git checkout sdk/9.1)
make
sudo make install
- 安装ffmpeg的相关依赖
sudo apt-get install build-essential cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-de libx265-dev libx264-dev
- 下载ffmpeg源码
- 编译前配置
./configure --prefix=/usr/local/ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --disable-stripping --enable-avresample --enable-shared --enable-libx264 --enable-libx265 --enable-cuda --enable-cuvid --enable-nvenc --enable-gpl
此处的配置很关键,把想要开启或者想要关闭的功能需要写清楚
5. 编译 & 安装
make
make install
- 如果出现了无执行权限的sh脚本,给其赋予执行权限即可
例如:
chmod u+x ./ffbuid/version.sh
6.建立软连接
sudo ln -s /usr/local/ffmpeg/bin/ffmpeg /usr/bin/ffmpeg
sudo ln -s /usr/local/ffmpeg/bin/ffprobe /usr/bin/ffprobe
7.验证
- 列出gpu解码器
> mpeg -decoders | grep cuvid
ffmpeg version 4.2.7 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
configuration: --prefix=/usr/local/ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --disable-stripping --enable-avresample --enable-shared --enable-libx264 --enable-libx265 --enable-cuda --enable-cuvid --enable-nvenc --enable-gpl
libavutil 56. 31.100 / 56. 31.100
libavcodec 58. 54.100 / 58. 54.100
libavformat 58. 29.100 / 58. 29.100
libavdevice 58. 8.100 / 58. 8.100
libavfilter 7. 57.100 / 7. 57.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 5.100 / 5. 5.100
libswresample 3. 5.100 / 3. 5.100
libpostproc 55. 5.100 / 55. 5.100
V..... h264_cuvid Nvidia CUVID H264 decoder (codec h264)
V..... hevc_cuvid Nvidia CUVID HEVC decoder (codec hevc)
V..... mjpeg_cuvid Nvidia CUVID MJPEG decoder (codec mjpeg)
V..... mpeg1_cuvid Nvidia CUVID MPEG1VIDEO decoder (codec mpeg1video)
V..... mpeg2_cuvid Nvidia CUVID MPEG2VIDEO decoder (codec mpeg2video)
V..... mpeg4_cuvid Nvidia CUVID MPEG4 decoder (codec mpeg4)
V..... vc1_cuvid Nvidia CUVID VC1 decoder (codec vc1)
V..... vp8_cuvid Nvidia CUVID VP8 decoder (codec vp8)
V..... vp9_cuvid Nvidia CUVID VP9 decoder (codec vp9)
- 列出GPU编码器
> ffmpeg -encoders | grep nvenc
ffmpeg version 4.2.7 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
configuration: --prefix=/usr/local/ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --disable-stripping --enable-avresample --enable-shared --enable-libx264 --enable-libx265 --enable-cuda --enable-cuvid --enable-nvenc --enable-gpl
libavutil 56. 31.100 / 56. 31.100
libavcodec 58. 54.100 / 58. 54.100
libavformat 58. 29.100 / 58. 29.100
libavdevice 58. 8.100 / 58. 8.100
libavfilter 7. 57.100 / 7. 57.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 5.100 / 5. 5.100
libswresample 3. 5.100 / 3. 5.100
libpostproc 55. 5.100 / 55. 5.100
V..... h264_nvenc NVIDIA NVENC H.264 encoder (codec h264)
V..... nvenc NVIDIA NVENC H.264 encoder (codec h264)
V..... nvenc_h264 NVIDIA NVENC H.264 encoder (codec h264)
V..... nvenc_hevc NVIDIA NVENC hevc encoder (codec hevc)
V..... hevc_nvenc NVIDIA NVENC hevc encoder (codec hevc)
- ffmpeg常用命令
ffmpeg [global options] {[infile options]['-i' 'infile'] ...} {[outfile options] 'outfile' ...}
参数选项由三部分组成:可选的一组全局参数、一组或多组输入文件参数、一组或多组输出文件参数,其中,每组输入文件参数以‘-i’为结束标记;每组输出文件参数以输出文件名为结束标记。
简单来说, -i参数的之前的为输入文件的参数,-i参数之后的为输出文件的参数。
一些常用命令:
- 全CPU转码h264视频(CPU解码器libx264)到h264(CPU编码器libx264)
ffmpeg -i test.mp4 -c:v libx264 -y test-cpu.mp4
- 全GPU转码h264视频(GPU解码器h264_cuvid)到h264(GPU编码器h264_nvenc)
ffmpeg -hwaccel cuvid -c:v h264_cuvid -i test.mp4 -c:v h264_nvenc -y test-gpu.mp4
- 全GPU转码mpeg4(如mp4v)视频(GPU解码器mpeg4_cuvid)到h264(GPU编码器h264_nvenc)
ffmpeg -hwaccel cuvid -c:v mpeg4_cuvid -i test-mp4v.mp4 -c:v h264_nvenc -y test-mp4v-gpu.mp4
- CPU解码GPU编码方式转码
ffmpeg -i test.mp4 -c:v h264_nvenc -y test-gpu.mp4
- ffmeg软解码与硬解码的效率比对
测试视频为4k视频,
libx264编解码的fps为7.2,
h264_cuvid解码,h264_nvenc解码的fps为125。
如果使用1080p视频测试,fps提升效果更为明显,有几百倍的提升。