linux平台下nvidia如何支持ffmpeg硬解码h264

文章目录

  • 前言
  • 正文
    • 1.编译安装需要的环境
    • 2.测试使用

前言

正文

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
./configure --enable-nonfree --enable-cuda-sdk --enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64

6.Compile
make -j8

7.Install the libraries.
sudo make install

2.测试使用

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                     .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                    .D.V..... GPU to be used for decoding
  -surfaces                  .D.V..... Maximum surfaces to be used for decoding (from 0 to INT_MAX) (default 25)
  -drop_second_field     .D.V..... Drop second field when deinterlacing (default false)
  -crop                   .D.V..... Crop (top)x(bottom)x(left)x(right)
  -resize                 .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了,成功

你可能感兴趣的:(ffmpeg)