由于最近要在视频流上Mask R-CNN模型,cuda硬解也要上,又要折腾一下环境搭建,在此记录一下。
这里的硬解说的是调用nvidia的video codec sdk来加速编解码,使用cuvid硬解的方式有两种,一种是调用ffmpeg已经嵌入的硬解方案,另外一种是先调用ffmpeg再调用codec sdk。
从开发难度来说,第一种要稍微简单一些,只需要指定ffmpeg的video codec就可以启用硬解。 下面是我搭建的一些记录
由于我的机器搭建了各种各样的开发环境,这次编译缺少的一个是yasm包,执行下面命令就可以安装。如果有缺少其它包,自行搜索一下应该可以解决。
sudo apt-get install yasm
由于编译ffmpeg需要用到nv codec的一些header,在这里下载,由于我用的ffmpeg版本最新的master分支(commit: d6489d),所以nv_header也要用最新的9.0版本,下载地址是:
https://github.com/FFmpeg/nv-codec-headers/releases
下载之后,执行
sudo make install
就可以完成header的安装。如果要卸载, 可以执行sudo make uninstall
命令
从 https://git.ffmpeg.org/ffmpeg.git clone 一下ffmpeg的源码
然后配置一下
./configure --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
sudo make -j6
sudo make install
到这里基本就安装成功了,下面验证一下硬解是否生效
由于是硬解,所以要验证一下是否生效,我用本地的一个视频文件试了一下, 用time命令对比一下同一个转码任务,哪个时间更短
ffmpeg的命令参数比较多,可以自行搜索看一下:
ffmpeg -y -i jiaofu2.rmvb -t 200 -c:v h264_nvenc output2.mp4
time ffmpeg -y -i jiaofu2.rmvb -t 200 -c:v h264_nvenc output2.mp4
ffmpeg version N-93679-gd6489ddb7a Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-27ubuntu1~18.04)
configuration: --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
libavutil 56. 26.100 / 56. 26.100
libavcodec 58. 52.100 / 58. 52.100
libavformat 58. 27.103 / 58. 27.103
libavdevice 58. 7.100 / 58. 7.100
libavfilter 7. 50.100 / 7. 50.100
libswscale 5. 4.100 / 5. 4.100
libswresample 3. 4.100 / 3. 4.100
[rm @ 0x555690d38040] Invalid stream index 2 for index at pos 2438339235
Input #0, rm, from 'jiaofu2.rmvb':
Metadata:
title : �̸�2.The.Godfather.Part.II.1974.720p.Bluray
author : ��Ӱ�ӵ۹�CNXP����С������ס�ഺ��
copyright : bbs.cnxp.com(C)2010
ASMRuleBook : #($Bandwidth >= 0),Stream0Bandwidth = 96468, Stream1Bandwidth = 1503532;
Audiences : Easy RealMedia Tool's Audience;
audioMode : music
Creation Date : 7/22/2010 17:52:32
Description : This File is Created by Easy RealMedia Tools@!
Email : [email protected]
Generated By : Easy RealMedia Tools V1.8x
HomeWeb : http://redcheek.net
Keywords : Ӱ�ӵ۹���Ʒ
Modification Date: 7/22/2010 17:52:32
videoMode : sharp
Duration: 03:22:08.25, start: 0.000000, bitrate: 1608 kb/s
Stream #0:0: Audio: cook (cook / 0x6B6F6F63), 44100 Hz, stereo, fltp, 96 kb/s
Stream #0:1: Video: rv40 (RV40 / 0x30345652), yuv420p, 1280x720, 1499 kb/s, 23.98 fps, 23.98 tbr, 1k tbn, 1k tbc
Stream mapping:
Stream #0:1 -> #0:0 (rv40 (native) -> h264 (h264_nvenc))
Stream #0:0 -> #0:1 (cook (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, mp4, to 'output2.mp4':
Metadata:
title : �̸�2.The.Godfather.Part.II.1974.720p.Bluray
author : ��Ӱ�ӵ۹�CNXP����С������ס�ഺ��
copyright : bbs.cnxp.com(C)2010
ASMRuleBook : #($Bandwidth >= 0),Stream0Bandwidth = 96468, Stream1Bandwidth = 1503532;
Audiences : Easy RealMedia Tool's Audience;
audioMode : music
Creation Date : 7/22/2010 17:52:32
Description : This File is Created by Easy RealMedia Tools@!
Email : [email protected]
Generated By : Easy RealMedia Tools V1.8x
HomeWeb : http://redcheek.net
Keywords : Ӱ�ӵ۹���Ʒ
Modification Date: 7/22/2010 17:52:32
videoMode : sharp
encoder : Lavf58.27.103
Stream #0:0: Video: h264 (h264_nvenc) (Main) (avc1 / 0x31637661), yuv420p(progressive), 1280x720, q=-1--1, 2000 kb/s, 23.98 fps, 785645.00 tbn, 23.98 tbc
Metadata:
encoder : Lavc58.52.100 h264_nvenc
Side data:
cpb: bitrate max/min/avg: 0/0/2000000 buffer size: 4000000 vbv_delay: -1
Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc58.52.100 aac
frame= 200 fps=0.0 q=15.0 size= 1280kB time=00:00:08.21 bitrate=1276.2kbits/frame= 426 fps=426 q=29.0 size= 5376kB time=00:00:17.64 bitrate=2496.3kbits/frame= 723 fps=481 q=15.0 size= 6912kB time=00:00:30.03 bitrate=1885.6kbits/frame= 1098 fps=548 q=16.0 size= 8192kB time=00:00:46.37 bitrate=1447.0kbits/frame= 1406 fps=562 q=15.0 size= 13312kB time=00:00:58.51 bitrate=1863.6kbits/frame= 1760 fps=586 q=17.0 size= 17152kB time=00:01:13.28 bitrate=1917.4kbits/frame= 2106 fps=601 q=15.0 size= 20736kB time=00:01:27.71 bitrate=1936.7kbits/frame= 2385 fps=596 q=18.0 size= 25856kB time=00:01:39.34 bitrate=2132.0kbits/frame= 2685 fps=596 q=22.0 size= 29696kB time=00:01:51.86 bitrate=2174.7kbits/frame= 2992 fps=598 q=20.0 size= 33536kB time=00:02:04.66 bitrate=2203.7kbits/frame= 3284 fps=597 q=19.0 size= 37120kB time=00:02:17.39 bitrate=2213.2kbits/frame= 3588 fps=598 q=23.0 size= 40704kB time=00:02:30.40 bitrate=2217.0kbits/frame= 3900 fps=600 q=22.0 size= 43520kB time=00:02:43.03 bitrate=2186.7kbits/frame= 4223 fps=603 q=19.0 size= 46848kB time=00:02:56.40 bitrate=2175.5kbits/frame= 4546 fps=606 q=22.0 size= 50688kB time=00:03:09.48 bitrate=2191.4kbits/frame= 4796 fps=606 q=21.0 Lsize= 53427kB time=00:03:20.00 bitrate=2188.3kbits/s dup=375 drop=0 speed=25.3x
video:50217kB audio:3055kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.290995%
[aac @ 0x555690d6f4c0] Qavg: 10399.560
real 0m8.017s
user 0m36.849s
sys 0m0.702s
使用软解:
time ffmpeg -y -i jiaofu2.rmvb -t 200 output2.mp4
ffmpeg version N-93679-gd6489ddb7a Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-27ubuntu1~18.04)
configuration: --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
libavutil 56. 26.100 / 56. 26.100
libavcodec 58. 52.100 / 58. 52.100
libavformat 58. 27.103 / 58. 27.103
libavdevice 58. 7.100 / 58. 7.100
libavfilter 7. 50.100 / 7. 50.100
libswscale 5. 4.100 / 5. 4.100
libswresample 3. 4.100 / 3. 4.100
[rm @ 0x5556cfc4b000] Invalid stream index 2 for index at pos 2438339235
Input #0, rm, from 'jiaofu2.rmvb':
Metadata:
title : �̸�2.The.Godfather.Part.II.1974.720p.Bluray
author : ��Ӱ�ӵ۹�CNXP����С������ס�ഺ��
copyright : bbs.cnxp.com(C)2010
ASMRuleBook : #($Bandwidth >= 0),Stream0Bandwidth = 96468, Stream1Bandwidth = 1503532;
Audiences : Easy RealMedia Tool's Audience;
audioMode : music
Creation Date : 7/22/2010 17:52:32
Description : This File is Created by Easy RealMedia Tools@!
Email : [email protected]
Generated By : Easy RealMedia Tools V1.8x
HomeWeb : http://redcheek.net
Keywords : Ӱ�ӵ۹���Ʒ
Modification Date: 7/22/2010 17:52:32
videoMode : sharp
Duration: 03:22:08.25, start: 0.000000, bitrate: 1608 kb/s
Stream #0:0: Audio: cook (cook / 0x6B6F6F63), 44100 Hz, stereo, fltp, 96 kb/s
Stream #0:1: Video: rv40 (RV40 / 0x30345652), yuv420p, 1280x720, 1499 kb/s, 23.98 fps, 23.98 tbr, 1k tbn, 1k tbc
Stream mapping:
Stream #0:1 -> #0:0 (rv40 (native) -> mpeg4 (native))
Stream #0:0 -> #0:1 (cook (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, mp4, to 'output2.mp4':
Metadata:
title : �̸�2.The.Godfather.Part.II.1974.720p.Bluray
author : ��Ӱ�ӵ۹�CNXP����С������ס�ഺ��
copyright : bbs.cnxp.com(C)2010
ASMRuleBook : #($Bandwidth >= 0),Stream0Bandwidth = 96468, Stream1Bandwidth = 1503532;
Audiences : Easy RealMedia Tool's Audience;
audioMode : music
Creation Date : 7/22/2010 17:52:32
Description : This File is Created by Easy RealMedia Tools@!
Email : [email protected]
Generated By : Easy RealMedia Tools V1.8x
HomeWeb : http://redcheek.net
Keywords : Ӱ�ӵ۹���Ʒ
Modification Date: 7/22/2010 17:52:32
videoMode : sharp
encoder : Lavf58.27.103
Stream #0:0: Video: mpeg4 (mp4v / 0x7634706D), yuv420p(progressive), 1280x720, q=2-31, 200 kb/s, 23.98 fps, 10981 tbn, 23.98 tbc
Metadata:
encoder : Lavc58.52.100 mpeg4
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc58.52.100 aac
frame= 204 fps=0.0 q=31.0 size= 512kB time=00:00:08.46 bitrate= 495.4kbits/frame= 379 fps=379 q=31.0 size= 1536kB time=00:00:15.76 bitrate= 798.1kbits/frame= 554 fps=369 q=31.0 size= 2560kB time=00:00:23.55 bitrate= 890.3kbits/frame= 801 fps=400 q=31.0 size= 3072kB time=00:00:33.37 bitrate= 754.1kbits/frame= 1069 fps=427 q=24.8 size= 3584kB time=00:00:44.54 bitrate= 659.1kbits/frame= 1291 fps=430 q=31.0 size= 4352kB time=00:00:53.80 bitrate= 662.6kbits/frame= 1517 fps=433 q=31.0 size= 4864kB time=00:01:03.23 bitrate= 630.2kbits/frame= 1763 fps=440 q=31.0 size= 5376kB time=00:01:13.49 bitrate= 599.3kbits/frame= 1984 fps=440 q=31.0 size= 6144kB time=00:01:22.70 bitrate= 608.6kbits/frame= 2208 fps=441 q=31.0 size= 6912kB time=00:01:32.37 bitrate= 612.9kbits/frame= 2430 fps=441 q=31.0 size= 7680kB time=00:01:41.30 bitrate= 621.0kbits/frame= 2652 fps=441 q=31.0 size= 8448kB time=00:01:50.56 bitrate= 625.9kbits/frame= 2877 fps=442 q=31.0 size= 9216kB time=00:01:59.95 bitrate= 629.4kbits/frame= 3099 fps=442 q=24.8 size= 10240kB time=00:02:09.66 bitrate= 646.9kbits/frame= 3321 fps=442 q=24.8 size= 11264kB time=00:02:19.02 bitrate= 663.7kbits/frame= 3538 fps=442 q=31.0 size= 12032kB time=00:02:27.52 bitrate= 668.1kbits/frame= 3733 fps=439 q=31.0 size= 13312kB time=00:02:35.97 bitrate= 699.2kbits/frame= 3977 fps=441 q=31.0 size= 14080kB time=00:02:45.83 bitrate= 695.5kbits/frame= 4202 fps=442 q=31.0 size= 14848kB time=00:02:55.21 bitrate= 694.2kbits/frame= 4409 fps=440 q=31.0 size= 15616kB time=00:03:03.85 bitrate= 695.8kbits/frame= 4618 fps=439 q=31.0 size= 16384kB time=00:03:13.12 bitrate= 695.0kbits/frame= 4796 fps=439 q=31.0 Lsize= 17243kB time=00:03:20.00 bitrate= 706.3kbits/s dup=375 drop=0 speed=18.3x
video:14031kB audio:3055kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.916266%
[aac @ 0x5556cfc82100] Qavg: 10399.560
real 0m10.985s
user 1m1.552s
sys 0m1.078s
从两条命令的执行结果来看,硬解已经发挥作用了。
上面测试结果跟硬件相关,不同时间测到的结果可能不一样,只是作为参考。
硬解速度是25.3x,软解速度是18.3x
硬解耗时8.01 s,软解速度是10.98 s
从耗时和加速来看,硬解比软解稍微好一些
由于测试视频是720P的普通画质,而且只是一路视频,如果处理4K或者8K的视频,而且多路的话,硬解优势可能会更明显。