这是调整编码器的粗略指南:
我们将从基础知识开始,因为得出这样的结论是有害的,即快速的期权选择权突然会提高预期的输出而又不了解预期的目标和期望:
1.首先了解编码器的选项。
对于基于NVENC的编码器,首先要学习每种编码器采用的选项(请注意,我在Linux上,这就是为什么我使用xclip将编解码器选项复制到剪贴板,然后再粘贴到此处的原因):
(一种)。对于H.264编码器:
ffmpeg -hide_banner -h encoder=h264_nvenc | xclip -sel clip
输出:
Encoder h264_nvenc [NVIDIA NVENC H.264 encoder]:
General capabilities: delay
Threading capabilities: none
Supported pixel formats: yuv420p nv12 p010le yuv444p yuv444p16le bgr0 rgb0 cuda
h264_nvenc AVOptions:
-preset <int> E..V.... Set the encoding preset (from 0 to 11) (default medium)
default E..V....
slow E..V.... hq 2 passes
medium E..V.... hq 1 pass
fast E..V.... hp 1 pass
hp E..V....
hq E..V....
bd E..V....
ll E..V.... low latency
llhq E..V.... low latency hq
llhp E..V.... low latency hp
lossless E..V....
losslesshp E..V....
-profile <int> E..V.... Set the encoding profile (from 0 to 3) (default main)
baseline E..V....
main E..V....
high E..V....
high444p E..V....
-level <int> E..V.... Set the encoding level restriction (from 0 to 51) (default auto)
auto E..V....
1 E..V....
1.0 E..V....
1b E..V....
1.0b E..V....
1.1 E..V....
1.2 E..V....
1.3 E..V....
2 E..V....
2.0 E..V....
2.1 E..V....
2.2 E..V....
3 E..V....
3.0 E..V....
3.1 E..V....
3.2 E..V....
4 E..V....
4.0 E..V....
4.1 E..V....
4.2 E..V....
5 E..V....
5.0 E..V....
5.1 E..V....
-rc <int> E..V.... Override the preset rate-control (from -1 to INT_MAX) (default -1)
constqp E..V.... Constant QP mode
vbr E..V.... Variable bitrate mode
cbr E..V.... Constant bitrate mode
vbr_minqp E..V.... Variable bitrate mode with MinQP (deprecated)
ll_2pass_quality E..V.... Multi-pass optimized for image quality (deprecated)
ll_2pass_size E..V.... Multi-pass optimized for constant frame size (deprecated)
vbr_2pass E..V.... Multi-pass variable bitrate mode (deprecated)
cbr_ld_hq E..V.... Constant bitrate low delay high quality mode
cbr_hq E..V.... Constant bitrate high quality mode
vbr_hq E..V.... Variable bitrate high quality mode
-rc-lookahead <int> E..V.... Number of frames to look ahead for rate-control (from 0 to INT_MAX) (default 0)
-surfaces <int> E..V.... Number of concurrent surfaces (from 0 to 64) (default 0)
-cbr <boolean> E..V.... Use cbr encoding mode (default false)
-2pass <boolean> E..V.... Use 2pass encoding mode (default auto)
-gpu <int> E..V.... Selects which NVENC capable GPU to use. First GPU is 0, second is 1, and so on. (from -2 to INT_MAX) (default any)
any E..V.... Pick the first device available
list E..V.... List the available devices
-delay <int> E..V.... Delay frame output by the given amount of frames (from 0 to INT_MAX) (default INT_MAX)
-no-scenecut <boolean> E..V.... When lookahead is enabled, set this to 1 to disable adaptive I-frame insertion at scene cuts (default false)
-forced-idr <boolean> E..V.... If forcing keyframes, force them as IDR frames. (default false)
-b_adapt <boolean> E..V.... When lookahead is enabled, set this to 0 to disable adaptive B-frame decision (default true)
-spatial-aq <boolean> E..V.... set to 1 to enable Spatial AQ (default false)
-temporal-aq <boolean> E..V.... set to 1 to enable Temporal AQ (default false)
-zerolatency <boolean> E..V.... Set 1 to indicate zero latency operation (no reordering delay) (default false)
-nonref_p <boolean> E..V.... Set this to 1 to enable automatic insertion of non-reference P-frames (default false)
-strict_gop <boolean> E..V.... Set 1 to minimize GOP-to-GOP rate fluctuations (default false)
-aq-strength <int> E..V.... When Spatial AQ is enabled, this field is used to specify AQ strength. AQ strength scale is from 1 (low) - 15 (aggressive) (from 1 to 15) (default 8)
-cq <float> E..V.... Set target quality level (0 to 51, 0 means automatic) for constant quality mode in VBR rate control (from 0 to 51) (default 0)
-aud <boolean> E..V.... Use access unit delimiters (default false)
-bluray-compat <boolean> E..V.... Bluray compatibility workarounds (default false)
-init_qpP <int> E..V.... Initial QP value for P frame (from -1 to 51) (default -1)
-init_qpB <int> E..V.... Initial QP value for B frame (from -1 to 51) (default -1)
-init_qpI <int> E..V.... Initial QP value for I frame (from -1 to 51) (default -1)
-qp <int> E..V.... Constant quantization parameter rate control method (from -1 to 51) (default -1)
-weighted_pred <int> E..V.... Set 1 to enable weighted prediction (from 0 to 1) (default 0)
-coder <int> E..V.... Coder type (from -1 to 2) (default default)
default E..V....
auto E..V....
cabac E..V....
cavlc E..V....
ac E..V....
vlc E..V....
(b)。对于HEVC / H.265编码器:
ffmpeg -hide_banner -h encoder=hevc_nvenc | xclip -sel clip
输出:
Encoder hevc_nvenc [NVIDIA NVENC hevc encoder]:
General capabilities: delay
Threading capabilities: none
Supported pixel formats: yuv420p nv12 p010le yuv444p yuv444p16le bgr0 rgb0 cuda
hevc_nvenc AVOptions:
-preset <int> E..V.... Set the encoding preset (from 0 to 11) (default medium)
default E..V....
slow E..V.... hq 2 passes
medium E..V.... hq 1 pass
fast E..V.... hp 1 pass
hp E..V....
hq E..V....
bd E..V....
ll E..V.... low latency
llhq E..V.... low latency hq
llhp E..V.... low latency hp
lossless E..V.... lossless
losslesshp E..V.... lossless hp
-profile <int> E..V.... Set the encoding profile (from 0 to 4) (default main)
main E..V....
main10 E..V....
rext E..V....
-level <int> E..V.... Set the encoding level restriction (from 0 to 186) (default auto)
auto E..V....
1 E..V....
1.0 E..V....
2 E..V....
2.0 E..V....
2.1 E..V....
3 E..V....
3.0 E..V....
3.1 E..V....
4 E..V....
4.0 E..V....
4.1 E..V....
5 E..V....
5.0 E..V....
5.1 E..V....
5.2 E..V....
6 E..V....
6.0 E..V....
6.1 E..V....
6.2 E..V....
-tier <int> E..V.... Set the encoding tier (from 0 to 1) (default main)
main E..V....
high E..V....
-rc <int> E..V.... Override the preset rate-control (from -1 to INT_MAX) (default -1)
constqp E..V.... Constant QP mode
vbr E..V.... Variable bitrate mode
cbr E..V.... Constant bitrate mode
vbr_minqp E..V.... Variable bitrate mode with MinQP (deprecated)
ll_2pass_quality E..V.... Multi-pass optimized for image quality (deprecated)
ll_2pass_size E..V.... Multi-pass optimized for constant frame size (deprecated)
vbr_2pass E..V.... Multi-pass variable bitrate mode (deprecated)
cbr_ld_hq E..V.... Constant bitrate low delay high quality mode
cbr_hq E..V.... Constant bitrate high quality mode
vbr_hq E..V.... Variable bitrate high quality mode
-rc-lookahead <int> E..V.... Number of frames to look ahead for rate-control (from 0 to INT_MAX) (default 0)
-surfaces <int> E..V.... Number of concurrent surfaces (from 0 to 64) (default 0)
-cbr <boolean> E..V.... Use cbr encoding mode (default false)
-2pass <boolean> E..V.... Use 2pass encoding mode (default auto)
-gpu <int> E..V.... Selects which NVENC capable GPU to use. First GPU is 0, second is 1, and so on. (from -2 to INT_MAX) (default any)
any E..V.... Pick the first device available
list E..V.... List the available devices
-delay <int> E..V.... Delay frame output by the given amount of frames (from 0 to INT_MAX) (default INT_MAX)
-no-scenecut <boolean> E..V.... When lookahead is enabled, set this to 1 to disable adaptive I-frame insertion at scene cuts (default false)
-forced-idr <boolean> E..V.... If forcing keyframes, force them as IDR frames. (default false)
-spatial_aq <boolean> E..V.... set to 1 to enable Spatial AQ (default false)
-temporal_aq <boolean> E..V.... set to 1 to enable Temporal AQ (default false)
-zerolatency <boolean> E..V.... Set 1 to indicate zero latency operation (no reordering delay) (default false)
-nonref_p <boolean> E..V.... Set this to 1 to enable automatic insertion of non-reference P-frames (default false)
-strict_gop <boolean> E..V.... Set 1 to minimize GOP-to-GOP rate fluctuations (default false)
-aq-strength <int> E..V.... When Spatial AQ is enabled, this field is used to specify AQ strength. AQ strength scale is from 1 (low) - 15 (aggressive) (from 1 to 15) (default 8)
-cq <float> E..V.... Set target quality level (0 to 51, 0 means automatic) for constant quality mode in VBR rate control (from 0 to 51) (default 0)
-aud <boolean> E..V.... Use access unit delimiters (default false)
-bluray-compat <boolean> E..V.... Bluray compatibility workarounds (default false)
-init_qpP <int> E..V.... Initial QP value for P frame (from -1 to 51) (default -1)
-init_qpB <int> E..V.... Initial QP value for B frame (from -1 to 51) (default -1)
-init_qpI <int> E..V.... Initial QP value for I frame (from -1 to 51) (default -1)
-qp <int> E..V.... Constant quantization parameter rate control method (from -1 to 51) (default -1)
-weighted_pred <int> E..V.... Set 1 to enable weighted prediction (from 0 to 1) (default 0)
2.了解硬件的局限性,并在应用选项之前先坚持合理的默认设置:
请参阅此答案,以了解NVENC会遇到的硬件限制,特别是对于Pascal上的HEVC编码。
有关使用FFmpeg的当前NVIDIA硬件可用的硬件加速基础结构,请参见以下答案。
然后,使用该信息进行下一步。
3.语法很重要:
这是您必须将参数传递给FFmpeg的顺序:
(一种)。调用二进制文件。
(b)。-loglevel
在声明输入之前,将任何参数传递给FFmpeg(例如直接传递给FFmpeg )。
(C)。如果您使用的是任何硬件加速解码,例如cuvid
,请在此处声明并包含所需的任何特定参数。在这一点上,必须提到解码器具有特定的约束,例如预期的输入分辨率,支持的编解码器等,因此,建议在生产中确定并验证对硬件加速解码器的需求是否失败。在此阶段会导致编码失败,并且无法恢复。实际上,MPV开发人员已经反复提到了这一点,而不依靠硬件加速解码来执行关键任务内容。
(d)。声明您的输入。对于流,请使用URL,如果需要,请在需要时附加其他标志(例如缓冲区大小)。对于本地资源(在可访问的文件系统上),需要绝对文件路径。
(e)。(可选)插入过滤器。这对于调整大小,像素格式对话,去隔行等功能是必需的。请注意,根据此处使用的过滤器,基于硬件的解码器(如(c)节中所述)会引入约束,要求您的过滤器必须是能够处理,否则您的编码将失败。
(F)。调出相应的视频和音频编码器,并通过必要的参数给他们,如映射,比特率,编码器预设等当涉及到比特率,确保所需值可通过设置-b:v
,-maxrate:v
和-bufsize:v
选项。不要将这些留空。这是说明这些值为何重要的一个很好的起点。与往常一样,从指定预设开始。向下滚动至底部以查看有关使用此特定编码器的预设对性能的影响的注释。
(G)。FFmpeg可以根据输出文件的所选扩展名推断出文件所需的输出格式,但建议显式声明输出格式(通过-f选项),以便在需要时可以将其他选项传递给基础多路复用器,例如HLS,mpegts和DASH之类的流格式经常会遇到这种情况。
(H)。输出文件的绝对路径。
在上面的示例中,引用为:
ffmpeg -i "e:\input.ts" -vcodec h264_nvenc -preset slow -level 4.1
-qmin 10 -qmax 52 "e:\output.mp4"
您可以通过指定适当的比特率(通过和设置),启用自适应量化编码技术(支持空间和时间AQ方法,一次只能使用一种)来提高输出质量-b:v
,-maxrate:v
并且-bufsize:v
可以选择(并单独使用)如下所示,启用加权预测(这将禁用B帧支持),并在必要时启用可选过滤器,以进行适当的缩小和调整大小。下面的示例显示了片段处理mpeg2中编码的mpegts输入:
ffmpeg -loglevel debug -threads 4 -hwaccel cuvid -c:v mpeg2_cuvid -i "e:\input.ts" \
-filter:v hwupload_cuda,scale_npp=w=1920:h=1080:interp_algo=lanczos \
-c:v h264_nvenc -b:v 4M -maxrate:v 5M -bufsize:v 8M -profile:v main \
-level:v 4.1 -rc:v vbr_hq -rc-lookahead:v 32 \
-spatial_aq:v 1 -aq-strength:v 15 -coder:v cabac \
-f mp4 "e:\output.mp4"
警告:请注意,加权预测(-weighted_pred
)不能与自适应量化同时启用。尝试这样做会导致编码器初始化失败。
上面的代码段假定输入文件是MPEG2流。如果不是这种情况,请在分析后切换到正确的CUVID解码器:
ffprobe -i e:\input.ts
如果是H.264 / AVC,则修改代码段,如下所示:
ffmpeg -loglevel debug -threads 4 -hwaccel cuvid -c:v h264_cuvid -i "e:\input.ts" \
-filter:v hwupload_cuda,scale_npp=w=1920:h=1080:interp_algo=lanczos \
-c:v h264_nvenc -b:v 4M -maxrate:v 5M -bufsize:v 8M -profile:v main \
-level:v 4.1 -rc:v vbr_hq -rc-lookahead:v 32 -spatial_aq:v 1 \
-aq-strength:v 15 -coder:v cabac \
-f mp4 "e:\output.mp4"
我注意到,为NVENC启用自适应量化或加权预测选项可能会带来稳定性问题,尤其是对于特定的设备驱动程序组合。在可能的情况下,考虑结合使用B帧(不超过3个)和-refs:v
设置为16左右的通用选项,而不是切换AQ和加权预测:
ffmpeg -loglevel debug -threads 4 -hwaccel cuvid -c:v h264_cuvid -i "e:\input.ts" \
-filter:v hwupload_cuda,scale_npp=w=1920:h=1080:interp_algo=lanczos \
-c:v h264_nvenc -b:v 4M -maxrate:v 5M -bufsize:v 8M -profile:v main \
-level:v 4.1 -rc:v vbr_hq -rc-lookahead:v 32 -refs:v 16 \
-bf:v 3 -coder:v cabac \
-f mp4 "e:\output.mp4"
尤其是使用Turing,您还可以从启用B帧中受益,如下所示(请参见切换-b_ref_mode:v middle)
:
ffmpeg -loglevel debug -threads 4 -hwaccel cuvid -c:v h264_cuvid -i "e:\input.ts" \
-filter:v hwupload_cuda,scale_npp=w=1920:h=1080:interp_algo=lanczos \
-c:v h264_nvenc -b:v 4M -maxrate:v 5M -bufsize:v 8M -profile:v main \
-level:v 4.1 -rc:v vbr_hq -rc-lookahead:v 32 -refs:v 16 \
-bf:v 3 -coder:v cabac -b_ref_mode:v middle \
-f mp4 "e:\output.mp4"
关于线程计数的额外说明(通过-threads
选项传递给ffmpeg ):
超过特定阈值的更多编码器线程会增加延迟,并且将具有更高的编码内存占用量。由于编码延迟的增加,在恒定比特率模式和称为VBV(视频缓冲区验证器)的近恒定比特率模式下,线程数越多,质量下降越明显。关键帧比其他帧类型需要更多的数据,以免产生质量差的关键帧。
零延迟或切片线程模式没有延迟,但是此选项进一步恶化了支持的编码器中的多线程质量。
因此,明智的做法是在延迟很重要的地方限制编码的线程数,因为感知的编码器吞吐量的增加抵消了长期可能带来的任何优势。
而且,由于您使用的是Windows,因此您可能需要删除\
上面的外壳转义符,因为我是从测试上述命令的Unix框中编写此脚本的。
有关对预设和隔行编码注意事项的性能影响的说明:
为了获得高吞吐量,低延迟性能,请确保使用llhp
或llhq
预设。这对于诸如实时流这样的工作负载最为有用,因为实时流预计将与更广泛的设备实现更广泛的兼容性,因此,可以完全省略诸如B帧之类的性能削弱功能,以便在更高的比特率之间取得良好的权衡。使用和吞吐量。较高的预设(例如default medium
)会迅速减少质量输出的回报,同时会导致编码器吞吐量的显着下降。之间的质量差异llhp
,并llhq
通过测量为Netflix的VMAF 几乎可以忽略不计,但与前者相比,编码器的性能提升(在我的试验台上约30%以上)绝对是可观的。
对于llhp
和llhq
预设以及正在使用的其他预设,您还可以通过传递-rc:v
编码器选项公开的参数来覆盖内置的速率控制方法。例如,使用恒定比特率编码,您可以指定-rc:v cbr
(这比cbr_ld_hq
速率控制方法快得多,可将吞吐量提高约20%)。请注意,所选的预设对吞吐量影响最大,其次是预设选项(例如使用的速率控制方法),您可以根据需要选择这些选项(可选)。
考虑您的编码工作流程并根据需要进行调整。根据您的源内容,使用的过滤器链,特定的pltform配置变量(例如GPU和驱动程序版本)等,您的里程肯定会有所不同。
同时,请注意,即使在使用较旧的Volta NVENC编码器的1660Ti系列中,NVIDIA也已显式禁用了图灵在所有层上的隔行编码。如果需要隔行编码支持,请改用Pascal或更旧的SKU。