次世代エンコーダVP9(webm、mp4)のエンコード設定についてffmpegで使う設定をまとめた。libvpx-vp9は外部ライブラリなので別途インストールする必要がある。さらに音声のエンコーダで使われるlibopusも外部ライブラリなのでこれもインストールする必要がある。

設定内容は多くあるがほとんどの設定は何も変えず、ビットレートの設定と品質・速度のトレードオフで考える。VP9は処理が遅いとよく言われるが、-threadsを設定することで速くなる。libx264のように自動設定してくれないので一番下の方にある解像度に併せて設定する。

libvpx-1.7.0から 10、12ビット深度に対応した。
ハードウェアのデコード、エンコード対応表:wiki – SoCs Supporting VP8/VP9

Downloads – Opus Codec
VP9コーデックメモ – Qiita

基本コマンド

-loglevel 48を追加するとvp9のパラメータ設定が見られる。
ffmpeg -i input -c:v libvpx-vp9 -crf 30 -b:v 0 -cpu-used 2 -threads 8 -row-mt 1 -c:a libopus -b:a 128k output.mp4

libopusをインストールしていない場合。libopusのほうが品質はよい。
ffmpeg -i input -c:v libvpx-vp9 -crf 30 -b:v 0 -cpu-used 2 -threads 8 -row-mt 1 -c:a opus -b:a 128k -strict -2 output.mp4

品質指定(CQ)の 2pass エンコード例。
ffmpeg -i input -c:v libvpx-vp9 -crf 30 -b:v 0 -cpu-used 4 -threads 8 -row-mt 1 -pass 1 -an -f null -
ffmpeg -i input -c:v libvpx-vp9 -crf 30 -b:v 0 -cpu-used 2 -threads 8 -row-mt 1 -pass 2 -auto-alt-ref 1 -c:a libopus -b:a 128k output.mp4

linux などの 1pass 目の例。
ffmpeg -i input -c:v libvpx-vp9 -crf 30 -b:v 0 -cpu-used 4 -threads 8 -row-mt 1 -pass 1 -an -f webm /dev/null

ビットレート指定の 2pass エンコード例。
ffmpeg -i input -c:v libvpx-vp9 -b:v 2000k -cpu-used 4 -threads 8 -row-mt 1 -pass 1 -an -f null -
ffmpeg -i input -c:v libvpx-vp9 -b:v 2000k -cpu-used 2 -threads 8 -row-mt 1 -pass 2 -auto-alt-ref 1 -c:a libopus -b:a 128k output.mp4

分割エンコードしてつなげるコマンド例。
ffmpeg -f concat -safe 0 -i input.txt -c copy output.mp4

input.txt の内容。

file output-0.webm
file output-1.webm

詳しくは 【ffmpeg】動画・音声を連結する concat の使い方 其の3

高ビット深度の場合は出力フォーマットを指定する。
ffmpeg -i input -c:v libvpx-vp9 -crf 18 -g 240 -b:v 0 -cpu-used 2 -threads 8 -row-mt 1 -pix_fmt yuv420p10le -c:a libopus -b:a 96k output.mp4

アルファチャンネル付きの動画で出力。出力フォーマットにアルファチャンネルを含める。出力コンテナは.mp4ではなく.webmにする。
ffmpeg -f lavfi -i testsrc2=d=10,chromakey=blue:.03:.1 -pix_fmt yuva420p -c:v libvpx-vp9 -crf 30 -b:v 0 -cpu-used 2 -threads 8 -row-mt 1 output.webm

RGB映像からYUV映像にエンコードする。
ffmpeg -i rgb -vf "scale=out_color_matrix=bt709:out_range=full" -c:v libvpx-vp9 -crf 30 -g 300 -b:v 0 -cpu-used 2 -threads 8 -row-mt 1 -pix_fmt yuv444p -colorspace bt709 -color_range pc -c:a libopus -b:a 96k output.mp4

ビットレート設定

以下の設定のどれかを必ず指定する。そうしないと-crf 32 -b:v 0でエンコードされる。-qmaxは特定の座標で品質の制御をするのに有効。

VP9 bitrate modes | Google Developers

  • 平均ビットレート(ABR、VBR)
    -c:v libvpx-vp9 -b:v 2000k
  • 上限と下限を指定した平均ビットレート(ABR、VBR)。シングルパスエンコードで使う
    -c:v libvpx-vp9 -b:v 2000k -minrate 1500k
    -c:v libvpx-vp9 -b:v 2000k -maxrate 2500k
    -c:v libvpx-vp9 -b:v 2000k -minrate 1500k -maxrate 2500k
  • 固定ビットレート。平均ビットレートと最小と最大の3つを一致させる(CBR)。シングルパスエンコードで使う
    -c:v libvpx-vp9 -b:v 2000k -minrate 2000k -maxrate 2000k
  • 品質に上限と下限を指定した平均ビットレート(VBR)
    ビットレート指定でも品質を制限する-qmin、-qmaxが使える
    -c:v libvpx-vp9 -b:v 2000k -qmin 10 -qmax 30
  • 品質指定(Constant Quality)
    (-1から63まで小さい値ほど高品質)-b:v 0は必須項目。ビットレートの上下幅は大きい
    -c:v libvpx-vp9 -crf 30 -b:v 0
  • 上限と下限を指定した品質指定(Constant Quality)
    (-1から63まで小さい値ほど高品質)-b:v 0は必須項目。品質の上下を指定。最小値を10にすることでビットレートを上げすぎなくする。-qmin-crf以下の値を、-qmax-crf以上の値を指定する。特定のシーンで品質が上がりすぎる、下がりすぎるときに指定すると効果的
    -c:v libvpx-vp9 -crf 30 -b:v 0 -qmin 10 -qmax 30
  • 品質指定にビットレートを指定(Constrained Quality)
    -crf 30でビットレート範囲の上限が2000kになる。-crf N -b:v 0の併用のビットレートを元に-b:vを指定しないと意図せずビットレートが高くなったり低くなったりする。crfエンコードからCBRエンコード寄りのレートコントロールになりビットレートの上下幅が狭くなりがち
    -c:v libvpx-vp9 -crf 30 -b:v 2000k
  • ロスレス(可逆圧縮)
    -c:v libvpx-vp9 -lossless 1

-bufsizeは例えば-maxrate 4350k -minrate 1500k -b:v 3000k -bufsize 5000kを指定するとエンコードログの最後にのるが、出力内容に変化がなく公式ドキュメントにも特に記載がない。

cpb: bitrate max/min/avg: 4350000/1500000/3000000 buffer size: 5000000 vbv_delay: N/A

-quality realtime-cpu_used 5以上のときは座標ごとに品質調整ができる。
映像の特定領域だけ品質を変える addroi

-crf-b:vの併用でのビットレート制限について

  • -qmaxを使うとqの上限制限がかかる
  • -crf指定単体で-b:v指定よりビットレートが高いときに-b:v併用すると-b:v指定した値が平均ビットレートの上限になる。つまり-b:vの値でビットレートが制限される
  • -crf指定単体で-b:v指定よりビットレートが低いときに-b:v併用すると-crf指定の平均ビットレートが上限になるときと、ならないときがある。つまり-b:vの値でビットレートが増えることもある

エンコード設定

Basic Encoding : developers.google.com

ffmpeg -h encoder=libvpx-vp9で見られるエンコード設定とエンコーダ全般に指定できる設定の2つがある。

Encoder libvpx-vp9 [libvpx VP9]:
General capabilities: dr1 delay threads
Threading capabilities: other
Supported pixel formats: yuv420p yuva420p yuv422p yuv440p yuv444p yuv420p10le yuv422p10le yuv440p10le yuv444p10le yuv420p12le yuv422p12le yuv440p12le yuv444p12le gbrp gbrp10le gbrp12le
libvpx-vp9 encoder AVOptions:
-lag-in-frames <int> E..V....... Number of frames to look ahead for alternate reference frame selection (from -1 to INT_MAX) (default -1)
-arnr-maxframes <int> E..V....... altref noise reduction max frame count (from -1 to INT_MAX) (default -1)
-arnr-strength <int> E..V....... altref noise reduction filter strength (from -1 to INT_MAX) (default -1)
-arnr-type <int> E..V....... altref noise reduction filter type (from -1 to INT_MAX) (default -1)
backward 1 E..V.......
forward 2 E..V.......
centered 3 E..V.......
-tune <int> E..V....... Tune the encoding to a specific scenario (from -1 to INT_MAX) (default -1)
psnr 0 E..V.......
ssim 1 E..V.......
-deadline <int> E..V....... Time to spend encoding, in microseconds. (from INT_MIN to INT_MAX) (default good)
best 0 E..V.......
good 1000000 E..V.......
realtime 1 E..V.......
-error-resilient <flags> E..V....... Error resilience configuration (default 0)
default E..V....... Improve resiliency against losses of whole frames
partitions E..V....... The frame partitions are independently decodable by the bool decoder, meaning that partitions can be decoded even though earlier partitions have been lost. Note that intra prediction is still done over the partition boundary.
-max-intra-rate <int> E..V....... Maximum I-frame bitrate (pct) 0=unlimited (from -1 to INT_MAX) (default -1)
-crf <int> E..V....... Select the quality for constant quality mode (from -1 to 63) (default -1)
-static-thresh <int> E..V....... A change threshold on blocks below which they will be skipped by the encoder (from 0 to INT_MAX) (default 0)
-drop-threshold <int> E..V....... Frame drop threshold (from INT_MIN to INT_MAX) (default 0)
-noise-sensitivity <int> E..V....... Noise sensitivity (from 0 to 4) (default 0)
-undershoot-pct <int> E..V....... Datarate undershoot (min) target (%) (from -1 to 100) (default -1)
-overshoot-pct <int> E..V....... Datarate overshoot (max) target (%) (from -1 to 1000) (default -1)
-ts-parameters <dictionary> E..V....... Temporal scaling configuration using a :-separated list of key=value parameters
-auto-alt-ref <int> E..V....... Enable use of alternate reference frames (2-pass only) (from -1 to 6) (default -1)
-cpu-used <int> E..V....... Quality/Speed ratio modifier (from -8 to 8) (default 1)
-lossless <int> E..V....... Lossless mode (from -1 to 1) (default -1)
-tile-columns <int> E..V....... Number of tile columns to use, log2 (from -1 to 6) (default -1)
-tile-rows <int> E..V....... Number of tile rows to use, log2 (from -1 to 2) (default -1)
-frame-parallel <boolean> E..V....... Enable frame parallel decodability features (default auto)
-aq-mode <int> E..V....... adaptive quantization mode (from -1 to 4) (default -1)
none 0 E..V....... Aq not used
variance 1 E..V....... Variance based Aq
complexity 2 E..V....... Complexity based Aq
cyclic 3 E..V....... Cyclic Refresh Aq
equator360 4 E..V....... 360 video Aq
-level <float> E..V....... Specify level (from -1 to 6.2) (default -1)
-row-mt <boolean> E..V....... Row based multi-threading (default auto)
-tune-content <int> E..V....... Tune content type (from -1 to 2) (default -1)
default 0 E..V....... Regular video content
screen 1 E..V....... Screen capture content
film 2 E..V....... Film content; improves grain retention
-corpus-complexity <int> E..V....... corpus vbr complexity midpoint (from -1 to 10000) (default -1)
-enable-tpl <boolean> E..V....... Enable temporal dependency model (default auto)
-min-gf-interval <int> E..V....... Minimum golden/alternate reference frame interval (from -1 to INT_MAX) (default -1)
-speed <int> E..V....... (from -16 to 16) (default 1)
-quality <int> E..V....... (from INT_MIN to INT_MAX) (default good)
best 0 E..V.......
good 1000000 E..V.......
realtime 1 E..V.......
-vp8flags <flags> E..V....... (default 0)
error_resilient E..V....... enable error resilience
altref E..V....... enable use of alternate reference frames (VP8/2-pass only)
-arnr_max_frames <int> E..V....... altref noise reduction max frame count (from 0 to 15) (default 0)
-arnr_strength <int> E..V....... altref noise reduction filter strength (from 0 to 6) (default 3)
-arnr_type <int> E..V....... altref noise reduction filter type (from 1 to 3) (default 3)
-rc_lookahead <int> E..V....... Number of frames to look ahead for alternate reference frame selection (from 0 to 25) (default 25)
-sharpness <int> E..V....... Increase sharpness at the expense of lower PSNR (from -1 to 7) (default -1)

この中で重要なのがエンコード速度と品質に関わる-cpu-used、-deadlineと、馴染みのある-crfである。-cpu-used、-deadlineはエンコード速度と品質に大きく影響する設定である。-cpu-used既定値は1で0にするとさらに半分以下にエンコード速度が遅くなるかわりに圧縮率が向上する。値を上げるとそれだけ速度が向上するが品質も悪くなる。品質と速度の面から2や3も候補になるが4、5の品質はよくない。マイナスも指定できるがプラスのときと出力内容は変わらない。さらに処理を速める-row-mt 1を併用する。

  • -cpu-used[int]
    -speedと同じ内容。マイナスも指定できるがプラスのときと出力内容は変わらない。0にすると最高品質だがものすごく遅くなる。大きな値ほど処理が速くビットレート当たりの品質が悪くなる。-deadlineの既定値であるgoodのときは-5から5までしか変化が無い。-deadline realtimeにすると-8から8まで変化がある。-cpu-used 3から-cpu-used 2で15%から25%程度遅くなる。少し下の「-deadline-cpu-usedを併用して処理速度をコントロールする。」を参照
    既定値:1
    範囲:-8から8まで
  • -deadline[int]
    -qualityと同じ内容。品質指定。-cpu-usedの設定を受け付けないbest(一番高品質一番低速度)、既定値のgood、realtimeの3つの指定が出来る。ライブ配信や速度重視の設定はrealtimeを指定する
    既定値:”good”
    指定値:best、good、realtime
  • -row-mt[boolean]
    マルチスレッド処理を有効化する。既定値は自動指定だが無指定より1を指定した方が処理が速くなる。品質にも影響を与える
    既定値:”auto”
  • -tile-columns[int]
    分割エンコード数で指定した値の2のべき乗になり、0なら分割しない、1なら2分割、2なら4分割になる。分割するほどエンコードとデコードは速くなるものの品質はわずかに低下する。設定できる値の範囲でも解像度によって影響を与える数値が異なる。複数同とき処理すると分割しないほうの処理速度差が縮まる。ffmpeg – VP9 encoding limited to 4 threads? – Stack Overflow
    既定値:-1(解像度別に指定できる値の最大値)
    範囲:-1から6まで

    解像度 -tile-columnsの数値で変化あり
    256×144 0
    512×288 0, 1
    768×432 0, 1
    1024×576 0, 1, 2
    1280×720 0, 1, 2
    1920×1080 0, 1, 2
    2560×1440 0, 1, 2, 3
    3840×2160 0, 1, 2, 3
    4096×2048 0, 1, 2, 3, 4
    8192×4096 0, 1, 2, 3, 4, 5
  • -crf[int]
    品質の数値指定。小さい値ほど高画質、高ビットレート。小数点以下は四捨五入して整数指定になる。libx264よりもcrfの変化によるビットレート変化は小さいような気がするが、映像によってビットレートが跳ね上がりやすい。AV1にも共通だがダーク、ブラウン、グレー、レッド、ブルー、パープルなどの背景の中に線があると品質を大きく上げないと線がぼやけやすい
    既定値:-1
    範囲:-1から63まで
  • -auto-alt-ref[int]
    パス指定の2パス目とき-pass 2のときに指定できる。。数値が大きいと高画質で容量も多く速度も遅くなる。映像によって最適値は変わるので基本は無指定のままにする
    既定値:-1
    範囲:-1から6まで
  • -lag-in-frames[int]
  • -rc_lookahead[int]
    -lag-in-frames-rc_lookaheadと同じ値になる。下げると暗部や細部の再現性が悪くなる。映像によって上限と下限近くで変化がなくなる
    既定値:25
    範囲:0から25まで
  • -aq-mode[int]
    映像によっては最適な値が変わるかもしれない
    • 0, none:既定値
    • 1, variance
    • 2, complexity
    • 3, cyclic
    • 4, equator360:360度映像用
  • -ts-parameters[dictionary]
    スケールときの設定内容の指定。key1=value1:key2=value2のように指定する。設定内容はvpx/vpx_encoder.hstruct vpx_codec_enc_cfgに書いてあり、レイヤー数やビットレートなどの指定ができる。参考記事draft-ietf-payload-vp9-16
    指定例:-ts-parameters ts_number_layers=3:ts_target_bitrate=250000,500000,1000000:ts_rate_decimator=4,2,1:ts_periodicity=4:ts_layer_id=0,2,1,2
    • ts_number_layers
      Number of temporal coding layers.
    • ts_target_bitrate
      Target bitrate for each temporal layer (in kbps). (bitrate should be inclusive of the lower temporal layer).
    • ts_rate_decimator
      Frame rate decimation factor for each temporal layer.
    • ts_periodicity
      Length of the sequence defining frame temporal layer membership.
    • ts_layer_id
      Template defining the membership of frames to temporal layers.
    • ts_layering_mode
      (optional) Selecting the temporal structure from a set of pre-defined temporal layering modes. Currently supports the following options.
      • 0
        No temporal layering flags are provided internally, relies on flags being passed in using metadata field in AVFrame with following keys.
        • vp8-flags
          Sets the flags passed into the encoder to indicate the referencing scheme for the current frame. Refer to function vpx_codec_encode in vpx/vpx_encoder.h for more details.
        • temporal_id
          Explicitly sets the temporal id of the current frame to encode.
      • 2
        Two temporal layers. 0-1…
      • 3
        Three temporal layers. 0-2-1-2…; with single reference frame.
      • 4
        Same as option “3”, except there is a dependency between the two temporal layer 2 frames within the temporal period.
  • -profile[int]
    入力ファイルに合わせて自動設定

    プロファイル ビット深度 クロマサブサンプリング
    0 8 bit/sample 4:2:0
    1 8 bit 4:2:2, 4:4:4
    2 10, 12 bit 4:2:0
    3 10, 12 bit 4:2:2, 4:4:4

-deadline-cpu-usedを併用して処理速度をコントロールする。
video – VP9 encoding with FFmpeg: relation between -speed and -deadline options – Super Userより。

  • best:ものすごく遅く非推奨
  • good:既定値で一般的にはこちらを使う。-cpu-usedを0から5までの6段階指定できる
    • -cpu-used 0:-deadline bestにとても近い結果になるが2倍速い
    • -cpu-used 1、-cpu-used 2:処理速度は大幅に向上するが品質やレートコントロールにも影響を与え始める
    • -cpu-used 4、-cpu-used 5:「レート歪みの最適化」をオフにするが、処理速度はさらに向上する
  • realtime:品質と処理速度のトレードオフ。-cpu-usedを-8から8までの17段階指定できるが、負の値と正の値の絶対値が同じだと結果に変化がない

公式ドキュメント:FFmpeg Codecs Documentation : libvpx

libvpx-vp9の設定以外に品質の上限下限の指定と、キーフレーム間隔、色空間などがある。

  • -g[int]
    group of picture sizeで、keyintのこと
    既定値:-1
  • -keyint_min[int]
    -force_key_frameも併用すると-force_key_frameが優先されるが、そもそも効果がないように見える
    既定値:-1
  • -force_key_frames[string]
    カンマ,区切りで任意のとき間指定か、exprでの指定ができる。フレーム指定(0開始)ではexpreq()をフレーム毎に+でつなげて指定する。10秒よりキーフレーム指定が離れるときは指定したフレーム区間の中央にIフレームが自動で挿入される。それを回避するには最大のキーフレーム幅の-gを指定すると、そのフレーム数毎にIフレームが挿入される
    例:-force_key_frames expr:eq(n,0)+eq(n,35)+eq(n,154)
    公式ドキュメント:FFmpeg Documentation : Advanced Video options
    ffmpeg – Maintain keyframes when transcoding and removing b-frames – Stack Overflow
  • -static-thresh
    エンコーダがスキップするブロックする変更のしきい値の指定
  • -slices [int]
    パーティションの総数となるスライス数の指定。log2(partitions)
  • -threads[int]
    x264のような品質の変化はない。下にある解像度に合わせて設定しないと速度が上がらない。指定値の2のべき乗になるので3の場合は下のグラフの8になる。CPUがスレッド数に対応していないと大きな値の効果は得られない
    既定値:-1(自動設定ではない)
  • -pass[:stream_specifier]
    2パスエンコードで使う。1、または2を指定。1パス目で解析して、2パス目で解析したログを使って指定したビットレート、品質にする。1パス目は動画を出力しなくてもよいので-an -f null –を併用する。stream_specifierは2トラック目以降の映像を指定するときに使う。1パスに-speedを遅い設定にしても2パスの出力に影響を与えないので処理の速い4を指定する。-cpu-used deadlineではログファイルを作らない。-crfエンコードではアニメやCG作品では複数解像度にリサイズするときも一番大きい解像度のパスログ、1080pなら2160pにアップスケールして流用する方がvmafスコアが高くなる傾向(Big Buck Bunnyで検証)にあった。2pass目のエンコードは通常のシングルパスのエンコードよりもかなり遅くなる。サブサンプリングではログは変わらず、ビット深度で変わる
  • -passlogfile[:stream_specifier] [string]
    -passで使うログファイルの指定。。1パス目では出力ログ名の指定。2パス目では入力ログ名の指定になる。記入内容はPREFIX-N.logのPREFIX部分になる。Nはstream_specifier
    既定値:ffmpeg2pass

公式ドキュメント:ffmpeg Documentation : Video Options

色の情報の設定

カラースペースなどの入力、出力オプションは以下の記事を参照。
色の情報の扱いについて

出力オプションの-colorspaceが効いてないのでビットストリームフィルタでcolorspaceを指定する。

HD未満
ffmpeg -i input -c:v libvpx-vp9 -crf 30 -b:v 0 -bsf:v vp9_metadata=color_space=bt601:color_range=tv -color_primaries smpte170m -color_trc smpte170m -c:a libopus -b:a 96k output.mp4

HD以上
ffmpeg -i input -c:v libvpx-vp9 -crf 30 -b:v 0 -bsf:v vp9_metadata=color_space=bt709:color_range=tv -color_primaries bt709 -color_trc bt709 -c:a libopus -b:a 96k output.mp4

カラースペースを明示する場合にffmpeg 4.1からは -bsf:v vp9_metadataからでも設定できる。カラープライマリー(別の言い方ではカラースペース、ホワイトポイント)とカラートランスファーが指定できないので上の出力オプションで指定するほうが他のコーデックでも共通なので使いやすい。

ビットストリームフィルタのまとめ

ffmpeg -h bsf=vp9_metadata

color_space[int]

  • unknown
  • bt601
  • bt709
  • smpte170
  • smpte240
  • bt2020
  • rgb

color_range[int]

  • tv
  • pc

色の情報が未指定のときは出力オプションとビットストリームフィルタの両方を指定してコーデックコピーするとメタデータを付与できる。

HD未満
ffmpeg -i input -c copy -bsf:v vp9_metadata=color_space=bt601:color_range=tv -color_primaries smpte170m -color_trc smpte170m output.mp4

HD以上
ffmpeg -i input -c copy -bsf:v vp9_metadata=color_space=bt709:color_range=tv -color_primaries bt709 -color_trc bt709 output.mp4

推奨ビットレートと品質、スレッド数の設定

Recommended Settings for VOD  |  Google Developersより
ビットレートの下限は指定ビットレートの50%、上限は145%としている。

Frame Size/Frame Rate Target Bitrate (VOD, kbps) Min Bitrate (50%) Max Bitrate (145%)
320x240p @ 24,25,30 150 75 218
640x360p @ 24,25,30 276 138 400
640x480p @ 24,25,30 512 (LQ), 750 (MQ) 256 (LQ) 375 (MQ) 742 (LQ) 1088 (MQ)
1280x720p @ 24,25,30 1024 512 1485
1280x720p @ 50,60 1800 900 2610
1920x1080p @ 24,25,30 1800 900 2610
1920x1080p @ 50,60 3000 1500 4350
2560x1440p @ 24,25,30 6000 3000 8700
2560x1440p @ 50,60 9000 4500 13050
3840x2160p @ 24,25,30 12000 6000 17400
3840x2160p @ 50,60 18000 9000 26100

縦解像度と品質指定(crf)

Frame Height Target Quality (CQ)
240 37
360 36
480 34 (LQ) or 33 (MQ)
720 32
1080 31
1440 24
2160 15

2パスエンコードのときには1パス2パスどちらも-deadline good-cpu-usedを以下の値にするとエンコードとき間と品質のバランスがよい。1パスに-cpu-usedを遅い設定にしてもパスログファイルの出力に影響を与えないので処理の速い4を指定している。

Frame Height Speed (First Pass) Speed (Second Pass)
240 4 1
360 4 1
480 4 1
720 4 2
1080 4 2
1440 4 2
2160 4 2

tile-columns と threads の指定。

Frame Size Number of tile-columns Number of threads
320x240p 1 (-tile-columns 0) 2
640x360p 2 (-tile-columns 1) 4
640x480p 2 (-tile-columns 1) 4
1280x720p 4 (-tile-columns 2) 8
1920x1080p 4 (-tile-columns 2) 8
2560x1440p 8 (-tile-columns 3) 16
3840x2160p 8 (-tile-columns 3) 16

こちらも参照
VP9 Bitrate Modes in Detail
Live Encoding

HDR エンコード

VP9 HDR Encoding  |  Google DevelopersよりYoutubeに投稿できるHDR動画のエンコード。SMPTE ST 2086, HDR10 compatibleのメタデータ書き込みに対応してないのでエンコードが終わったらmkvmergeを使う。mkvmergeはmkvtoolnixのアプリケーションの一つ。

HDRにかかわるmkvmergeの設定

サンプル動画:strobe_scientist.mkv

以下のサンプルコマンドはGoogleが2パス指定をしているのを参考にしているが、1パスの品質指定や固定ビットレートでYouTubeに投稿してもHDRのPQ形式にエンコードされる。どちらにしろ再エンコードされるのでビットレートをたくさん盛っておけば良さそう。

HLG

-color_primaries 9 -color_trc 18 -colorspace 9
-color_primaries bt2020 -color_trc arib-std-b67 -colorspace bt2020nc

PQ

-color_primaries 9 -color_trc 16 -colorspace 9
-color_primaries bt2020 -color_trc smpte2084 -colorspace bt2020nc

18Mbps 4k 2-pass
ffmpeg -report -i strobe_scientist.webm -b:v 18000000 -speed 4 -pass 1 -pix_fmt yuv420p10le -color_primaries 9 -color_trc 16 -colorspace 9 -color_range 1 -maxrate 26800000 -minrate 8040000 -profile:v 2 -vcodec libvpx-vp9 -an -f null -
ffmpeg -report -i strobe_scientist.webm -b:v 18000000 -pass 2 -pix_fmt yuv420p10le -color_primaries 9 -color_trc 16 -colorspace 9 -color_range 1 -maxrate 26800000 -minrate 8040000 -profile:v 2 -vcodec libvpx-vp9 strobe_scientist_18Mbps.webm

6Mbps 4k 2-pass
ffmpeg -report -i strobe_scientist.mkv -b:v 6000000 -speed 4 -pass 1 -pix_fmt yuv420p10le -color_primaries 9 -color_trc 16 -colorspace 9 -color_range 1 -maxrate 8000000 -minrate 4000000 -profile:v 2 -vcodec libvpx-vp9 -an -f null -
ffmpeg -report -i strobe_scientist.mkv -b:v 6000000 -pass 2 -pix_fmt yuv420p10le -color_primaries 9 -color_trc 16 -colorspace 9 -color_range 1 -maxrate 8000000 -minrate 4000000 -profile:v 2 -vcodec libvpx-vp9 2pass_HDR_strobe_scientist_6Mbps-static.webm

18Mbps 1080p 2-pass
ffmpeg -report -i strobe_scientist.mkv -b:v 18000000 -speed 4 -pass 1 -pix_fmt yuv420p10le -color_primaries 9 -color_trc 16 -colorspace 9 -color_range 1 -maxrate 20800000 -minrate 15040000 -profile:v 2 -vf scale=-2:1080 -vcodec libvpx-vp9 -an -f null -
ffmpeg -report -i strobe_scientist.mkv -b:v 18000000 -pass 2 -pix_fmt yuv420p10le -color_primaries 9 -color_trc 16 -colorspace 9 -color_range 1 -maxrate 20800000 -minrate 15040000 -profile:v 2 -vf scale=-2:1080 -vcodec libvpx-vp9 1080_2pass_HDR_strobe_scientist_18Mbps-static.webm

18Mbps 4k HDR to SDR with LUT。lut3dフィルタで色を指定する。
ffmpeg -i strobe_scientist.mkv -b:v 18000000 -speed 4 -pass 1 -pix_fmt yuv420p -color_primaries 1 -color_trc 1 -colorspace 1 -color_range 1 -maxrate 26800000 -minrate 8040000 -profile:v 0 -vf scale=-1:-1:in_color_matrix=bt2020,format=rgb48,lut3d=bt2020_to_bt709_example.cube,scale=-1:-1:out_color_matrix=bt709 -vcodec libvpx-vp9 -an -f null -
ffmpeg -i strobe_scientist.mkv -b:v 18000000 -pass 2 -pix_fmt yuv420p -color_primaries 1 -color_trc 1 -colorspace 1 -color_range 1 -maxrate 26800000 -minrate 8040000 -profile:v 0 -vf scale=-1:-1:in_color_matrix=bt2020,format=rgb48,lut3d=bt2020_to_bt709_example.cube,scale=-1:-1:out_color_matrix=bt709 -vcodec libvpx-vp9 SDR_strobe_scientist_18Mbps-static.webm

18Mbps 4k 2-Pass HLG。HLGの動画からVP9に変換する。

サンプル動画:strobe_scientist_hlg.mkv

ffmpeg -i strobe_scientist_hlg.mkv -b:v 18000000 -pass 1 -speed 4 -pix_fmt yuv420p10le -color_primaries 9 -color_trc 18 -colorspace 9 -color_range 1 -maxrate 26800000 -minrate 8040000 -profile:v 2 -vcodec libvpx-vp9 -an -f null -
ffmpeg -i strobe_scientist_hlg.mkv -b:v 18000000 -pass 2 -pix_fmt yuv420p10le -color_primaries 9 -color_trc 18 -colorspace 9 -color_range 1 -maxrate 26800000 -minrate 8040000 -profile:v 2 -vcodec libvpx-vp9 HLG_strobe_scientist_18Mbps-static.webm

MKVToolNix Downloads – Matroska tools for Linux/Unix and Windows
mkvmerge Doc — Merge multimedia streams into a Matroska file

mkvmerge -o HDR_strobe_scientist_18Mbps.mkv--colour-matrix 0:9 --colour-range 0:1 --colour-transfer-characteristics 0:16 --colour-primaries 0:9 --max-content-light 0:1000 --max-frame-light 0:300 --max-luminance 0:1000 --min-luminance 0:0.01 --chromaticity-coordinates 0:0.68,0.32,0.265,0.690,0.15,0.06 --white-colour-coordinates 0:0.3127,0.3290 HLG_HDR_strobe_scientist_18Mbps.webm

mkvmergeに任意のLUTをつけるとYoutubeのSDR変換にLUTが使われる。

--attachment-mime-type application/x-cube \
--attach-file *file-path-to-your-cube-LUT*

mimetypeの追加は.mkvコンテナに入れる。
ffmpeg -i input -attach hdr_to_sdr.cube -metadata:s:t mimetype=application/octet-stream -c copy output.mkv

attachment – FFMPEG attach file as metadata – Stack Overflow

アルファチャンネル付き動画を読み込む

yuv420p, yuva420p, yuv422p, yuv440p, yuv444p, gbrpと高ビット深度に対応しているが特にyuva420pのアルファチャンネルは注意が必要で ffmpeg ではデコーダにlibvpx-vp9、libvpxを指定しないとアルファチャンネルを読み込んでくれない。

アルファチャンネルのある PNG からのエンコード例(画像は連番のファイル名 img-001.png 形式)
ffmpeg -i img-%03d.png -vf colorspace=all=bt709:iall=bt601-6-625:range=tv:fast=1 -c:v libvpx-vp9 -crf 30 -b:v 0 -threads 8 -row-mt 1 -pix_fmt yuva420p output.webm

vp9のデコード例(入力ファイルの前にコーデックを指定する)
ffmpeg -c:v libvpx-vp9 -i output.webm output
ffplay -codec:v libvpx-vp9 -i output.webm

vp8のデコード例(入力ファイルの前にコーデックを指定する)
ffmpeg -c:v libvpx -i output.webm output
ffplay -codec:v libvpx -i output.webm

動画にアルファチャンネルが意図した通りに透過しているかを調べるコマンド例。アルファチャンネルだけ取り出してグレーフォーマットで出力。透過部分だけ黒くなる。
ffmpeg -c:v libvpx-vp9 -i output.webm -vf alphaextract -vframes 1 alphaextract.png
ffmpeg -c:v libvpx-vp9 -i output.webm -vf extractplanes=a -vframes 1 extractplanes.png

アルファチャンネルをグレーで出力する alphaextract
各映像チャンネルを分離する extractplanes

バイナリを読む

RAW ファイルコンテナには入れられないが lightweight IVFフォーマットに入れると32バイトグローバルヘッダとフレーム毎の12バイトヘッダで読みやすくなる。ffrprobe で見るときは動画とき間が長いと莫大なデータ量になる。
ffmpeg – How to extract raw VP9 stream from WebM file? – Stack Overflow
ffmpeg -i input.webm -c copy stream.ivf
ffprobe -show_packets -show_data -i stream.ivf > show_data.txt

ハードウェアの対応

エンコードでコードのハードウェアの対応。
wiki – SoCs Supporting VP8/VP9

VP9 の解説記事(英語)

2 thoughts on “VP9(libvpx-vp9)のエンコード設定について

  • 匿名

    stats fileの内容は、ファーストパスの -f が webm か null かに関係なく同じですね。
    試しに30秒のHD画質のクリップでこの記事のパラメータを使って実行時間を計測したところ、nullの方が4%ほど高速でした。

    • admin

      コメント、検証ありがとうございます。実際にエンコードしているときは基本-f nullで-f webmは使っていないので、-f webmの部分を削除しました。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)