FFmpeg转码(1)

FFmpeg软编码(H.264)


查看libx264编码信息
ffmpeg -h encoder=libx264
Encoder libx264 [libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10]:
    General capabilities: delay threads 
    Threading capabilities: auto
    Supported pixel formats: yuv420p yuvj420p yuv422p yuvj422p yuv444p yuvj444p nv12 nv16 nv21 yuv420p10le yuv422p10le yuv444p10le nv20le

x264编码参数
libx264 AVOptions:
  -preset                 E..V..... Set the encoding preset (cf. x264 --fullhelp) (default "medium")
  -tune                   E..V..... Tune the encoding params (cf. x264 --fullhelp)
  -profile                E..V..... Set profile restrictions (cf. x264 --fullhelp) 
  -fastfirstpass         E..V..... Use fast settings when encoding first pass (default true)
  -level                  E..V..... Specify level (as defined by Annex A)
  -passlogfile            E..V..... Filename for 2 pass stats
  -wpredp                 E..V..... Weighted prediction for P-frames
  -a53cc                 E..V..... Use A53 Closed Captions (if available) (default true)
  -x264opts               E..V..... x264 options
  -crf                     E..V..... Select the quality for constant quality mode (from -1 to FLT_MAX) (default -1)
  -crf_max                 E..V..... In CRF mode, prevents VBV from lowering quality beyond this point. (from -1 to FLT_MAX) (default -1)
  -qp                        E..V..... Constant quantization parameter rate control method (from -1 to INT_MAX) (default -1)
  -aq-mode                   E..V..... AQ method (from -1 to INT_MAX) (default -1)
     none                         E..V.....
     variance                     E..V..... Variance AQ (complexity mask)
     autovariance                 E..V..... Auto-variance AQ
     autovariance-biased              E..V..... Auto-variance AQ with bias to dark scenes
  -aq-strength             E..V..... AQ strength. Reduces blocking and blurring in flat and textured areas. (from -1 to FLT_MAX) (default -1)
  -psy                   E..V..... Use psychovisual optimizations. (default auto)
  -psy-rd                 E..V..... Strength of psychovisual optimization, in : format.
  -rc-lookahead              E..V..... Number of frames to look ahead for frametype and ratecontrol (from -1 to INT_MAX) (default -1)
  -weightb               E..V..... Weighted prediction for B-frames. (default auto)
  -weightp                   E..V..... Weighted prediction analysis method. (from -1 to INT_MAX) (default -1)
     none                         E..V.....
     simple                       E..V.....
     smart                        E..V.....
  -ssim                  E..V..... Calculate and print SSIM stats. (default auto)
  -intra-refresh         E..V..... Use Periodic Intra Refresh instead of IDR frames. (default auto)
  -bluray-compat         E..V..... Bluray compatibility workarounds. (default auto)
  -b-bias                    E..V..... Influences how often B-frames are used (from INT_MIN to INT_MAX) (default INT_MIN)
  -b-pyramid                 E..V..... Keep some B-frames as references. (from -1 to INT_MAX) (default -1)
     none                         E..V.....
     strict                       E..V..... Strictly hierarchical pyramid
     normal                       E..V..... Non-strict (not Blu-ray compatible)
  -mixed-refs            E..V..... One reference per partition, as opposed to one reference per macroblock (default auto)
  -8x8dct                E..V..... High profile 8x8 transform. (default auto)
  -fast-pskip            E..V..... (default auto)
  -aud                   E..V..... Use access unit delimiters. (default auto)
  -mbtree                E..V..... Use macroblock tree ratecontrol. (default auto)
  -deblock                E..V..... Loop filter parameters, in  form.
  -cplxblur                E..V..... Reduce fluctuations in QP (before curve compression) (from -1 to FLT_MAX) (default -1)
  -partitions             E..V..... A comma-separated list of partitions to consider. Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all
  -direct-pred               E..V..... Direct MV prediction mode (from -1 to INT_MAX) (default -1)
     none                         E..V.....
     spatial                      E..V.....
     temporal                     E..V.....
     auto                         E..V.....
  -slice-max-size            E..V..... Limit the size of each slice in bytes (from -1 to INT_MAX) (default -1)
  -stats                  E..V..... Filename for 2 pass stats
  -nal-hrd                   E..V..... Signal HRD information (requires vbv-bufsize; cbr not allowed in .mp4) (from -1 to INT_MAX) (default -1)
     none                         E..V.....
     vbr                          E..V.....
     cbr                          E..V.....
  -avcintra-class            E..V..... AVC-Intra class 50/100/200 (from -1 to 200) (default -1)
  -me_method                 E..V..... Set motion estimation method (from -1 to 4) (default -1)
     dia                          E..V.....
     hex                          E..V.....
     umh                          E..V.....
     esa                          E..V.....
     tesa                         E..V.....
  -motion-est                E..V..... Set motion estimation method (from -1 to 4) (default -1)
     dia                          E..V.....
     hex                          E..V.....
     umh                          E..V.....
     esa                          E..V.....
     tesa                         E..V.....
  -forced-idr            E..V..... If forcing keyframes, force them as IDR frames. (default false)
  -coder                     E..V..... Coder type (from -1 to 1) (default default)
     default                      E..V.....
     cavlc                        E..V.....
     cabac                        E..V.....
     vlc                          E..V.....
     ac                           E..V.....
  -b_strategy                E..V..... Strategy to choose between I/P/B-frames (from -1 to 2) (default -1)
  -chromaoffset              E..V..... QP difference between chroma and luma (from INT_MIN to INT_MAX) (default -1)
  -sc_threshold              E..V..... Scene change threshold (from INT_MIN to INT_MAX) (default -1)
  -noise_reduction           E..V..... Noise reduction (from INT_MIN to INT_MAX) (default -1)
  -x264-params            E..V..... Override the x264 configuration using a :-separated list of key=value parameters

H.264编码举例:
x264 --full help
  • 1. 编码器预设参数设置preset
      --preset        Use a preset to select encoding settings [medium]
                                  Overridden by user settings.
                                  - ultrafast:
                                    --no-8x8dct --aq-mode 0 --b-adapt 0
                                    --bframes 0 --no-cabac --no-deblock
                                    --no-mbtree --me dia --no-mixed-refs
                                    --partitions none --rc-lookahead 0 --ref 1
                                    --scenecut 0 --subme 0 --trellis 0
                                    --no-weightb --weightp 0
                                  - superfast:
                                    --no-mbtree --me dia --no-mixed-refs
                                    --partitions i8x8,i4x4 --rc-lookahead 0
                                    --ref 1 --subme 1 --trellis 0 --weightp 1
                                  - veryfast:
                                    --no-mixed-refs --rc-lookahead 10
                                    --ref 1 --subme 2 --trellis 0 --weightp 1
                                  - faster:
                                    --no-mixed-refs --rc-lookahead 20
                                    --ref 2 --subme 4 --weightp 1
                                  - fast:
                                    --rc-lookahead 30 --ref 2 --subme 6
                                    --weightp 1
                                  - medium:
                                    Default settings apply.
                                  - slow:
                                    --direct auto --rc-lookahead 50 --ref 5
                                    --subme 8 --trellis 2
                                  - slower:
                                    --b-adapt 2 --direct auto --me umh
                                    --partitions all --rc-lookahead 60
                                    --ref 8 --subme 9 --trellis 2
                                  - veryslow:
                                    --b-adapt 2 --bframes 8 --direct auto
                                    --me umh --merange 24 --partitions all
                                    --ref 16 --subme 10 --trellis 2
                                    --rc-lookahead 60
                                  - placebo:
                                    --bframes 16 --b-adapt 2 --direct auto
                                    --slow-firstpass --no-fast-pskip
                                    --me tesa --merange 24 --partitions all
                                    --rc-lookahead 60 --ref 16 --subme 11
                                    --trellis 2
  • ultrafast:最快的编码方式
  • superfast:超级快速的编码方式
  • veryfast:非常快速的编码方式
  • faster:稍微快速的编码方式
  • fast:快速的编码方式
  • medium:折中的编码方式
  • slow:慢的编码方式
  • slower:更慢的编码方式
  • veryslow:非常慢的编码方式
  • placebo:最慢的编码方式

不同的预设参数对转码效率的影响不同。
设置ultrafast参数:

ffmpeg -i 222.mp4 -vcodec libx264 -preset ultrafast -b:v 2000k 333.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '222.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.19.100
  Duration: 00:00:30.32, start: 0.000000, bitrate: 397 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x208 [SAR 16:15 DAR 32:13], 261 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x7fc2f500fc00] using SAR=16/15
[libx264 @ 0x7fc2f500fc00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7fc2f500fc00] profile Constrained Baseline, level 2.1
[libx264 @ 0x7fc2f500fc00] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=abr mbtree=0 bitrate=2000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, mp4, to '333.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.20.100
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 480x208 [SAR 16:15 DAR 32:13], q=-1--1, 2000 kb/s, 25 fps, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc58.35.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/2000000 buffer size: 0 vbv_delay: -1
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc58.35.100 aac
frame=  149 fps=0.0 q=16.0 size=    1536kB time=00:00:06.08 bitrate=2068.4kbits/frame=  288 fps=287 q=13.0 size=    2816kB time=00:00:11.60 bitrate=1987.0kbits/frame=  452 fps=301 q=11.0 size=    4608kB time=00:00:18.18 bitrate=2076.3kbits/frame=  613 fps=306 q=11.0 size=    5888kB time=00:00:24.61 bitrate=1959.7kbits/frame=  756 fps=309 q=-1.0 Lsize=    7582kB time=00:00:30.30 bitrate=2049.7kbits/s speed=12.4x

如上,转码速度为12.4倍速。
设置medium参数:

ffmpeg -i 222.mp4 -vcodec libx264 -preset medium -b:v 2000k 333.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '222.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.19.100
  Duration: 00:00:30.32, start: 0.000000, bitrate: 397 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x208 [SAR 16:15 DAR 32:13], 261 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x7fd62c809200] using SAR=16/15
[libx264 @ 0x7fd62c809200] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7fd62c809200] profile High, level 2.1
[libx264 @ 0x7fd62c809200] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate=2000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '333.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.20.100
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 480x208 [SAR 16:15 DAR 32:13], q=-1--1, 2000 kb/s, 25 fps, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc58.35.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/2000000 buffer size: 0 vbv_delay: -1
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc58.35.100 aac
frame=  116 fps=0.0 q=9.0 size=     512kB time=00:00:04.73 bitrate= 885.5kbits/sframe=  190 fps=187 q=7.0 size=    1280kB time=00:00:07.70 bitrate=1360.2kbits/sframe=  273 fps=179 q=6.0 size=    2048kB time=00:00:11.02 bitrate=1521.2kbits/sframe=  347 fps=170 q=7.0 size=    3072kB time=00:00:13.97 bitrate=1800.4kbits/sframe=  424 fps=167 q=8.0 size=    3840kB time=00:00:17.06 bitrate=1843.2kbits/sframe=  497 fps=163 q=8.0 size=    4608kB time=00:00:19.96 bitrate=1890.4kbits/sframe=  571 fps=161 q=8.0 size=    5376kB time=00:00:22.91 bitrate=1921.7kbits/sframe=  647 fps=160 q=7.0 size=    6144kB time=00:00:25.98 bitrate=1937.1kbits/sframe=  718 fps=157 q=8.0 size=    6912kB time=00:00:28.81 bitrate=1965.0kbits/sframe=  756 fps=152 q=-1.0 Lsize=    7652kB time=00:00:30.30 bitrate=2068.6kbits/s speed= 6.1x

如上,转码速度为6.1倍速,但图像质量要比ultrafast高。

  • 2. H.264编码优化参数tune
      --tune          Tune the settings for a particular type of source
                              or situation
                                  Overridden by user settings.
                                  Multiple tunings are separated by commas.
                                  Only one psy tuning can be used at a time.
                                  - film (psy tuning):
                                    --deblock -1:-1 --psy-rd :0.15
                                  - animation (psy tuning):
                                    --bframes {+2} --deblock 1:1
                                    --psy-rd 0.4: --aq-strength 0.6
                                    --ref {Double if >1 else 1}
                                  - grain (psy tuning):
                                    --aq-strength 0.5 --no-dct-decimate
                                    --deadzone-inter 6 --deadzone-intra 6
                                    --deblock -2:-2 --ipratio 1.1
                                    --pbratio 1.1 --psy-rd :0.25
                                    --qcomp 0.8
                                  - stillimage (psy tuning):
                                    --aq-strength 1.2 --deblock -3:-3
                                    --psy-rd 2.0:0.7
                                  - psnr (psy tuning):
                                    --aq-mode 0 --no-psy
                                  - ssim (psy tuning):
                                    --aq-mode 2 --no-psy
                                  - fastdecode:
                                    --no-cabac --no-deblock --no-weightb
                                    --weightp 0
                                  - zerolatency:
                                    --bframes 0 --force-cfr --no-mbtree
                                    --sync-lookahead 0 --sliced-threads
                                    --rc-lookahead 0
  • film
  • animation
  • grain
  • stillimage
  • psnr
  • ssim
  • fastdecode
  • zerolatency

如上,可使用以上几个参数进行调优编码,如使用zerolatency会提升效率,因为它降低了因编码导致的延迟。

  • 3. H.264的profile(档次)与level(等级)设置

使用baseline profile编码一个H.264视频,不会包含B Slice:

ffmpeg -i input.mp4 -vcodec libx264 -profile:v baseline -level 3.1 -s 352x288 -an -y -t 10 output_baseline.ts

使用ffprobe查看output_baseline.ts中每帧包含B帧的情况:

aiputekiMacBook-Pro:bin aipu$ ffprobe -v quiet -show_frames -select_streams v output_baseline.ts |grep "pict_type=B"|wc -l
       0

使用high profile编码一个H.264视频,包含B Slice:

ffmpeg -i input.mp4 -vcodec libx264 -profile:v high -level 3.1 -s 352x288 -an -y -t 10 output_high.ts

使用ffprobe查看output_high.ts中每帧包含B帧的情况:

aiputekiMacBook-Pro:bin aipu$ ffprobe -v quiet -show_frames -select_streams v output_high.ts |grep "pict_type=B"|wc -l
     173

如上,baseline profile包含0个B帧,high profile包含173个B帧。

  • 4. 控制场景切换关键帧插入参数sc_threshold

不使用ffmpeg命令控制编码时的GOP大小:

ffmpeg -i input.mp4 -c:v libx264 -g 50 output.mp4
FFmpeg转码(1)_第1张图片

如上,使用工具查看GOP间隔不是均匀的。

使用ffmpeg命令控制编码时的GOP大小:

ffmpeg -i input.mp4 -c:v libx264 -g 50 -sc_threshold 0 -y output.mp4
FFmpeg转码(1)_第2张图片

如上,使用工具查看GOP间隔是均匀的。

  • 5. 设置x264内部参数x264opts

该参数可以控制帧的顺序及出现频率

设置GOP为50帧,并且不出现B帧:

ffmpeg -i input.mp4 -c:v libx264 -x264opts "bframes=0" -g 50 -sc_threshold 0 output.mp4
FFmpeg转码(1)_第3张图片

如图,使用工具查看output.mp4中并不包含B帧。

设置GOP中每2个P帧之间存放3个B帧:

ffmpeg -i input.mp4 -c:v libx264 -x264opts "bframes=3:b-adapt=0" -g 50 -sc_threshold 0 output.mp4
FFmpeg转码(1)_第4张图片

如图,使用工具查看output.mp4中每2个P帧之间存放了3个B帧。

  • 6. CBR恒定码率设置参数nal-hrd

设置VBR(可变码率)、CBR(恒定码率),控制码率的波动,以及设置buffer的大小:

ffmpeg -i input.mp4 -c:v libx264 -x264opts "bframes=10:b-adapt=0" -b:v 1000k -maxrate 1000k -minrate 1000k -bufsize 50k -nal-hrd cbr -g 50 -sc_threshold 0 output.ts

以上命令的具体分析如下:

  • 设置B帧的个数,并且是每2个P帧之间包含10个B帧
  • 设置视频码率为1000kbit/s
  • 设置最大码率为1000kbit/s
  • 设置最小码率为1000kbit/s
  • 设置编码的buffer大小为50KB
  • 设置H.264的编码HRD信号形式为CBR
  • 设置GOP为50帧
  • 设置场景切换不强行插入关键帧

你可能感兴趣的:(FFmpeg转码(1))