前言
正文
1.编译安装需要的环境
参考 jianshu.com/p/032b47c48ada 注意这是window的
linux可以参考官网说明
https://developer.download.nvidia.cn/designworks/ffmpeg/secure/Using_FFmpeg_with_NVIDIA_GPU_Hardware_Acceleration_v01.4.pdf?dp1Oy2tu6NTWK0yia_DyQgFH2-yb2N-o-jb6UXCstzU6Cj92bTb0F93KFN0WnhYbX4tf1Hvn-Tn4IgWlViwWrCtzb4qJxuyAyqD2ISAOqR6XAC8G52EQSjCMg1IYWF-0KmotmXVrMT8jM14MEsOnhjt6gi8eV6JnS_Xfh_LlCzpHd8MgC6SG_YwvwDtJeCABjGX5giY
2.2.2.1Compiling for LinuxFFmpegwith NVIDIA GPU acceleration is supported on all Linux platforms.To compileFFmpegon Linux, do the following:
1.Clone ffnvcodec
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
2.Install ffnvcodec
cd nv-codec-headers && sudo make install && cd ..
3.Clone FFmpeg's public GIT repository.
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/
4.Install necessary packages.
sudo apt-get install build-essential yasm cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-dev
5.Configure
# ffmpeg 3.x
./configure --enable-nonfree --prefix=/usr/local/ffmpeg3.4.9 \
--enable-cuda-sdk --enable-cuvid --enable-nvenc --enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64 \
--enable-gpl --enable-shared --enable-pic --disable-libx264 --disable-libx265
#ffmpeg5.0
./configure --enable-nonfree --prefix=/usr/local/ffmpeg5.0 \
--enable-cuda-nvcc --enable-cuvid --enable-nvenc --enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64 \
--enable-libx264 --enable-libx265 --enable-gpl --enable-shared --enable-pic
6.Compile
make -j8
7.Install the libraries.
sudo make install
ps: ffmpeg-5.0直接编译成功,nvidia驱动版本470
【问题】编译ffmpeg-3.x时提示 nvcc fatal : Unsupported gpu architecture ‘compute_30’
【解决办法】 大致原因是ffmpeg的3.x版本编译时设置了显卡架构compute_30, 而本机的显卡对不上。设置位于ffmpeg/configure文件中。
参考http://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/ 查询显卡对应的架构应该是什么。
输入以下指令查询显卡信息,这里是GeForce GTX 1080 Ti
$ lspci | grep -i nvidia
01:00.0 VGA compatible controller: NVIDIA Corporation GP102 [GeForce GTX 1080 Ti] (rev a1)
01:00.1 Audio device: NVIDIA Corporation GP102 HDMI Audio Controller (rev a1)
03:00.0 VGA compatible controller: NVIDIA Corporation GP102 [GeForce GTX 1080 Ti] (rev a1)
03:00.1 Audio device: NVIDIA Corporation GP102 HDMI Audio Controller (rev a1)
对应的架构为compute_61
修改配置,重新编译即可成功
2.配置可执行文件和库到系统路径
sudo vim ~/.bashrc
# 在最末添加配置
export LD_LIBRARY_PATH=/usr/local/ffmpeg{version}/lib:$LD_LIBRARY_PATH
export PATH=/usr/local/ffmpeg{version}/bin:$PATH
#重新激活配置
source ~/.bashrc
## 测试
$ ffmpeg -version
ffmpeg version 3.4.9 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --enable-nonfree --prefix=/usr/local/ffmpeg3.4.9 --enable-cuda-sdk --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --enable-libx265 --enable-gpl --enable-shared --enable-pic --enable-libx264
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
3.测试使用
ffmpeg -c:v h264_cuvid -i rtsp://xxxx output.mp4
-c:v 命令也就是-codec:v 相当于-vcodec , 代表视频编解码器
测试结果发现:cpu依然居高不下,gpu没动静,姿势不对
翻了下书:
1)查看解码器参数
ffmpeg -h decoder=h264_cuvid
ffmpeg version 4.1.4 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 6.5.0 (Ubuntu 6.5.0-2ubuntu1~18.04) 20181026
configuration: --enable-shared --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264 --enable-cuda --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-cflags=-fPIC --extra-ldflags=-L/usr/local/cuda/lib64
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
Decoder h264_cuvid [Nvidia CUVID H264 decoder]:
General capabilities: delay avoidprobe hardware
Threading capabilities: none
Supported hardware devices: cuda
Supported pixel formats: cuda nv12 p010le p016le
h264_cuvid AVOptions:
-deint <int> .D.V..... Set deinterlacing mode (from 0 to 2) (default weave)
weave .D.V..... Weave deinterlacing (do nothing)
bob .D.V..... Bob deinterlacing
adaptive .D.V..... Adaptive deinterlacing
-gpu <string> .D.V..... GPU to be used for decoding
-surfaces <int> .D.V..... Maximum surfaces to be used for decoding (from 0 to INT_MAX) (default 25)
-drop_second_field <boolean> .D.V..... Drop second field when deinterlacing (default false)
-crop <string> .D.V..... Crop (top)x(bottom)x(left)x(right)
-resize <string> .D.V..... Resize (width)x(height)
可以看到解码支持的像素格式有: cuda nv12 p010le p016le
2)换个姿势测试
突然发现上面cpu居高不下,有可能是编码用的,所以给换下
ffmpeg -hwaccel cuvid -vcodec h264_cuvid -i rtsp://xxxx -c:v h264_nvenc output.mp4
[问题1]:这里指定了硬编码和硬解码,结果报错了
Stream #0:0 -> #0:0 (h264 (h264_cuvid) -> h264 (h264_nvenc))
Press [q] to stop, [?] for help
[h264_nvenc @ 0x561de9b93780] Driver does not support the required nvenc API version. Required: 9.1 Found: 9.0
[h264_nvenc @ 0x561de9b93780] The minimum required Nvidia driver for nvenc is 390.25 or newer
看到第一句提示,我想起来nv-codec-headers.git下来是9.1版本 而不是9.0
看到第二句,我查了下nvidia驱动版本,430.26符合条件
因此去github上下载9.0版本
https://github.com/FFmpeg/nv-codec-headers/releases/tag/n9.0.18.3
(话说有人说直接checkout到9.0版本,怎么操作的,告诉我一下)
安装完后重新编译安装ffmpeg
./configure --enable-shared --prefix=/usr/local/ffmpeg --enable
-gpl --enable-libx264 --enable-cuda-sdk --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
结束之后,重新使用硬编解码,果然不吃cpu了,成功