使用cpu进行软编解码时,cpu效率低并且占用高。使用硬件加速,能够明显降低CPU的占用,参看博客 ffmpeg学习(16)AVDevice使用。 这里以使用英伟达gpu进行h264编解码加速为例说明,其他平台类似。
1、winodws硬件加速支持
在windows平台直接下载官方的预编译ffmpeg、lib开发包,都已经支持了英伟达显卡硬件加速。使用命令ffmpeg.exe -hide_banner true -codecs |findstr h264
,查询结果如下
可以看到英伟达硬件支持的解码器有 h264_cuvid,编码器有h264_nvenc、nvenc_h264、nvenc。
2、linux下硬件加速支持
使用sudo apt get install ffmpeg
后是默认不支持的硬件加速的,查看支持使用命令 ffmpeg -hide_banner true -codecs | grep h264
,结果如下
并没有出现同windows下类似带有nv或者cu字样的的编解码器名称。
为了使用硬件加速功能,需要从源代码进行编译,添加硬件加速支持。
编译ffnvcodec
再nvidia硬件上进行ffmpeg加速,需要准备ffnvcodec。
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
sudo make install
编译环境
sudo apt-get install build-essential yasm cmake libnuma1 libnuma-dev
后面编译ffmpeg源码时可以根据缺少项再安装。
ffmpeg编译
可以使用git命令 git clone https://git.ffmpeg.org/ffmpeg.git /ffmpeg
拉取最新的ffmpeg分支,这里以ffmpeg n4.0.2版本为例
编译与安装命令
./configure --enable-shared --enable-nonfree --enable-cuda-sdk --enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64 \
--prefix=/home/wanggao/software/FFmpeg-n4.0.2/install
make -j8
sudo make install
其他完整的编译选项
./configure --enable-shared --enable-nonfree --enable-cuda-sdk --enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64 \
--prefix=/home/wanggao/software/FFmpeg-n4.0.2/install \
\
--disable-doc \
--disable-htmlpages \
--disable-podpages \
--disable-txtpages \
--disable-manpages \
\
--enable-gpl \
--enable-libx264 \
--enable-encoder=libx264 \
--enable-decoder=h264 \
注意:上述命令行编译了所有内容,导致编译生成的库较大,可以根据具体情况编译需要的模块。
先使用 –disable-all 禁用所有模块,再添加 –enable-xxxxx 开启xxxxx模块
上面添加了H.264的支持。
编译完成后,再次命令 ffmpeg -hide_banner true -codecs | grep 264
查看支持情况
这里明显对nvidia的硬件有了特定的编码器 h264_cuvid、解码器nvenc和h264_nvenc。
查看相关编解码器的信息,例如查看解码器h264_cuvid信息,如下
再查看编码器h264_nvenc的信息(还包含preset、profile、level以及其他更多编码参数),仅列出支持的像素编码,如下图
3、linux下硬件加速使用、问题解决
在ffmpeg源代码中可以使用 如下代码
AVCodec *enc = avcodec_find_encoder_by_name("h264_nvenc");
AVCodec *dec = avcodec_find_decoder_by_name("h264_cuvid");
正常运行时,发现cpu占用降低,会有gpu的占用。
可能出现的的问题
在编码程序中,设定编码器h264_nvenc,运行程序报错
[h264_nvenc @ 0x7fad9030f100] Driver does not support the required nvenc API version. Required: 11.0 Found: 9.0
[h264_nvenc @ 0x7fad9030f100] The minimum required Nvidia driver for nvenc is 390.25 or newer
这种错误明确提示驱动版本不支持。我们可以在nv-codec-headers/README
文件中有要求说明
FFmpeg version of headers required to interface with Nvidias codec APIs.
Corresponds to Video Codec SDK version 11.0.10.
Minimum required driver versions:
Linux: 455.28 or newer
Windows: 456.71 or newer
查看本机的驱动版本已经gpu型号,驱动版本为 418.67, 是低于要求版本的,需要更新驱动。
但是我们注意到,机器的显卡名称不全。首先使用 lspci | grep -i vga
查看显卡的PCI ID号,为 NVIDIA Corporation Device 1e04 (rev a1)。
通过可以通过英伟达显卡PCI ID查询
列表 http://pci-ids.ucw.cz/read/PC/10de
查询 http://pci-ids.ucw.cz/mods/PC/10de?action=help?help=pci
知道了显卡类型,之后就是去官网下载安装合适版本驱动了。文章来源:https://www.toymoban.com/news/detail-408225.html
安装驱动,测试代码,一切OK。文章来源地址https://www.toymoban.com/news/detail-408225.html
到了这里,关于ffmpeg学习 源代码编译、英伟达硬件加速的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!