FFmpeg:在Windows上使用AMD GPU对x264进行编码?

11
我目前正在尝试在Windows 10上使用FFmpeg在我的联想笔记本电脑上录制视频,使用内置的网络摄像头。我的一个目标是尽量降低CPU使用率,所以我想将h264编码推到GPU上进行处理。
现在在我的笔记本电脑上有一点棘手。因为它使用了两个GPU。第一个GPU是Intel HD 5500图形单元,作为CPU的一部分。这个GPU主要用于非要求高的应用程序,比如办公等,以节省能源。另一个GPU是AMD R5 M330,将用于图形密集型应用程序,比如游戏。
目前,我正在使用以下命令在Intel HD GPU上对网络摄像头流进行编码:
ffmpeg -f dshow -vcodec mjpeg -video_size 1280x720 -framerate 30 video="Lenovo EasyCamera":audio="Mikrofon (Realtek High Definition Audio)" -c:v h264_qsv -g 60 -q 28 -look_ahead 0 -preset:v faster -c:a aac -q:a 0.6 -r 30 output.mp4

这个目前确实有效,但似乎这个GPU的功率不足以跟上更高比特率或大量i帧的帧速率。视频开始出现缺失和跳帧。如果我使用CPU编码,一切都很顺利。
现在我的笔记本电脑有了第二个AMD GPU,功率更大,尝试在上面进行编码会很好,但我找不到关于如何在Windows 10上使用AMD硬件进行h264编码的任何信息。所以我的问题是:ffmpeg命令如何使用AMD硬件进行h264编码?

1
他们还没有在Windows上实现AMD VCE。https://trac.ffmpeg.org/wiki/HWAccelIntro - halfelf
好吧,那太遗憾了,但看起来 AMF 支持计划在今年实现... 看来我需要对此耐心等待... - ZeroTek
2
现在最新版本似乎可以工作了,请尝试使用“-c:v h264_amf”。 - LB--
1
@LB-- 谢谢,我确认了-c:v hevc_amf在RX 560 4GB和最新的Windows版本的ffmpeg上可以工作。相比CPU,它非常快。但是-crf标志似乎不起作用。 - SaltySub2
@LB-- H.264和H.265的AMD编码在VLC,MPC-HC中似乎存在一些问题,例如在跳转到不同时间时播放回放出现问题... FWIW - SaltySub2
2
@SaltySub嗯,总的来说,VLC在播放H.264或HEVC视频方面并不是很好,无论来源如何。如果你将其上传到YouTube,你会发现它能够被正常处理。至于-crf标志,这不是正确的选项来使用AMF编码器,你需要使用-qp_p和-qp_i,如fullhelp中所解释的那样。 - LB--
1个回答

16

当前版本的ffmpeg现在支持主要GPU供应商的硬件编码。这里是使用AMD显卡时,h264_amf(H.264)和hevc_amf(H.265或HEVC)编码器的选项,取自ffmpeg -h full:

h264_amf AVOptions:
  -usage             <int>        E..V.... Encoder Usage (from 0 to 3) (default transcoding)
     transcoding                  E..V.... Generic Transcoding
     ultralowlatency              E..V.... 
     lowlatency                   E..V.... 
     webcam                       E..V.... Webcam
  -profile           <int>        E..V.... Profile (from 66 to 257) (default main)
     main                         E..V.... 
     high                         E..V.... 
     constrained_baseline              E..V.... 
     constrained_high              E..V.... 
  -level             <int>        E..V.... Profile Level (from 0 to 62) (default auto)
     auto                         E..V.... 
     1.0                          E..V.... 
     1.1                          E..V.... 
     1.2                          E..V.... 
     1.3                          E..V.... 
     2.0                          E..V.... 
     2.1                          E..V.... 
     2.2                          E..V.... 
     3.0                          E..V.... 
     3.1                          E..V.... 
     3.2                          E..V.... 
     4.0                          E..V.... 
     4.1                          E..V.... 
     4.2                          E..V.... 
     5.0                          E..V.... 
     5.1                          E..V.... 
     5.2                          E..V.... 
     6.0                          E..V.... 
     6.1                          E..V.... 
     6.2                          E..V.... 
  -quality           <int>        E..V.... Quality Preference (from 0 to 2) (default speed)
     speed                        E..V.... Prefer Speed
     balanced                     E..V.... Balanced
     quality                      E..V.... Prefer Quality
  -rc                <int>        E..V.... Rate Control Method (from -1 to 3) (default -1)
     cqp                          E..V.... Constant Quantization Parameter
     cbr                          E..V.... Constant Bitrate
     vbr_peak                     E..V.... Peak Contrained Variable Bitrate
     vbr_latency                  E..V.... Latency Constrained Variable Bitrate
  -enforce_hrd       <boolean>    E..V.... Enforce HRD (default false)
  -filler_data       <boolean>    E..V.... Filler Data Enable (default false)
  -vbaq              <boolean>    E..V.... Enable VBAQ (default false)
  -frame_skipping    <boolean>    E..V.... Rate Control Based Frame Skip (default false)
  -qp_i              <int>        E..V.... Quantization Parameter for I-Frame (from -1 to 51) (default -1)
  -qp_p              <int>        E..V.... Quantization Parameter for P-Frame (from -1 to 51) (default -1)
  -qp_b              <int>        E..V.... Quantization Parameter for B-Frame (from -1 to 51) (default -1)
  -preanalysis       <boolean>    E..V.... Pre-Analysis Mode (default false)
  -max_au_size       <int>        E..V.... Maximum Access Unit Size for rate control (in bits) (from 0 to INT_MAX) (default 0)
  -header_spacing    <int>        E..V.... Header Insertion Spacing (from -1 to 1000) (default -1)
  -bf_delta_qp       <int>        E..V.... B-Picture Delta QP (from -10 to 10) (default 4)
  -bf_ref            <boolean>    E..V.... Enable Reference to B-Frames (default true)
  -bf_ref_delta_qp   <int>        E..V.... Reference B-Picture Delta QP (from -10 to 10) (default 4)
  -intra_refresh_mb  <int>        E..V.... Intra Refresh MBs Number Per Slot in Macroblocks (from 0 to INT_MAX) (default 0)
  -coder             <int>        E..V.... Coding Type (from 0 to 2) (default auto)
     auto                         E..V.... Automatic
     cavlc                        E..V.... Context Adaptive Variable-Length Coding
     cabac                        E..V.... Context Adaptive Binary Arithmetic Coding
  -me_half_pel       <boolean>    E..V.... Enable ME Half Pixel (default true)
  -me_quarter_pel    <boolean>    E..V.... Enable ME Quarter Pixel (default true)
  -aud               <boolean>    E..V.... Inserts AU Delimiter NAL unit (default false)
  -log_to_dbg        <boolean>    E..V.... Enable AMF logging to debug output (default false)
hevc_amf AVOptions:
  -usage             <int>        E..V.... Set the encoding usage (from 0 to 3) (default transcoding)
     transcoding                  E..V.... 
     ultralowlatency              E..V.... 
     lowlatency                   E..V.... 
     webcam                       E..V.... 
  -profile           <int>        E..V.... Set the profile (default main) (from 1 to 1) (default main)
     main                         E..V.... 
  -profile_tier      <int>        E..V.... Set the profile tier (default main) (from 0 to 1) (default main)
     main                         E..V.... 
     high                         E..V.... 
  -level             <int>        E..V.... Set the encoding level (default auto) (from 0 to 186) (default auto)
     auto                         E..V.... 
     1.0                          E..V.... 
     2.0                          E..V.... 
     2.1                          E..V.... 
     3.0                          E..V.... 
     3.1                          E..V.... 
     4.0                          E..V.... 
     4.1                          E..V.... 
     5.0                          E..V.... 
     5.1                          E..V.... 
     5.2                          E..V.... 
     6.0                          E..V.... 
     6.1                          E..V.... 
     6.2                          E..V.... 
  -quality           <int>        E..V.... Set the encoding quality (from 0 to 10) (default speed)
     balanced                     E..V.... 
     speed                        E..V.... 
     quality                      E..V.... 
  -rc                <int>        E..V.... Set the rate control mode (from -1 to 3) (default -1)
     cqp                          E..V.... Constant Quantization Parameter
     cbr                          E..V.... Constant Bitrate
     vbr_peak                     E..V.... Peak Contrained Variable Bitrate
     vbr_latency                  E..V.... Latency Constrained Variable Bitrate
  -header_insertion_mode <int>        E..V.... Set header insertion mode (from 0 to 2) (default none)
     none                         E..V.... 
     gop                          E..V.... 
     idr                          E..V.... 
  -gops_per_idr      <int>        E..V.... GOPs per IDR 0-no IDR will be inserted (from 0 to INT_MAX) (default 60)
  -preanalysis       <boolean>    E..V.... Enable preanalysis (default false)
  -vbaq              <boolean>    E..V.... Enable VBAQ (default false)
  -enforce_hrd       <boolean>    E..V.... Enforce HRD (default false)
  -filler_data       <boolean>    E..V.... Filler Data Enable (default false)
  -max_au_size       <int>        E..V.... Maximum Access Unit Size for rate control (in bits) (from 0 to INT_MAX) (default 0)
  -min_qp_i          <int>        E..V.... min quantization parameter for I-frame (from -1 to 51) (default -1)
  -max_qp_i          <int>        E..V.... max quantization parameter for I-frame (from -1 to 51) (default -1)
  -min_qp_p          <int>        E..V.... min quantization parameter for P-frame (from -1 to 51) (default -1)
  -max_qp_p          <int>        E..V.... max quantization parameter for P-frame (from -1 to 51) (default -1)
  -qp_p              <int>        E..V.... quantization parameter for P-frame (from -1 to 51) (default -1)
  -qp_i              <int>        E..V.... quantization parameter for I-frame (from -1 to 51) (default -1)
  -skip_frame        <boolean>    E..V.... Rate Control Based Frame Skip (default false)
  -me_half_pel       <boolean>    E..V.... Enable ME Half Pixel (default true)
  -me_quarter_pel    <boolean>    E..V.... Enable ME Quarter Pixel  (default true)
  -aud               <boolean>    E..V.... Inserts AU Delimiter NAL unit (default false)
  -log_to_dbg        <boolean>    E..V.... Enable AMF logging to debug output (default false)

举个例子,ffmpeg -i input.mkv -c:v hevc_amf -rc cqp -qp_p 0 -qp_i 0 -c:a copy output.mkv 是无损的。需要注意的是,虽然速度更快,但相同质量的文件大小会比libx264或libx265大得多 - 这就是硬件编码器目前的情况。您可能想要使用硬件编码器记录无损视频以获得速度,并稍后使用像libx264或libx265这样的软件编码器来缩小文件大小。


3
请注意,虽然硬件编码速度更快,但相同质量的文件大小将显著大于使用libx264或libx265的情况 - 这就是目前硬件编码器的工作方式。-- 这对我来说是新闻!非常奇怪。有任何来源或已发表的比较吗? - André Levy
3
@AndréLevy 近年来情况已经发生了变化,但你可以轻松地进行比较:录制一个无损视频,然后使用软件和硬件编码器重新编码以比较输出结果。在我测试的时候,硬件编码器总是产生更差的质量或需要更大的文件才能匹配软件编码器的质量。这是因为硬件编码器是固定功能编码器。 - LB--
1
@AndréLevy 这是一个权衡。硬件编解码器能够快速工作,并且使用非常少的功耗/内存,因为它们以特定的方式设置。在软件中,有很多灵活性,但这就需要付出资源方面的代价。 - Brad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接