Open8

FFmpegで画像から動画を作るときのパフォーマンス

atsushiatsushi

目的

大量の画像を圧縮して保存したときの圧縮率とパフォーマンスについて把握する。
コーデック、ハードウェアアクセラレーションの有無とかでどのくらい変わるか?

環境

  • M1 Macbook Air
    • Sonoma 14.2.1
    • 16GB
  • FFmpeg 6.1.1

画像

  • FHDの 100枚のjpeg
  • 合計 13.8MB

今のところ結果サマリ

  • medium
codec hardware acceleration 変換後ファイルサイズ CPU負荷ピーク 処理速度 メモ
h.265 No 1.6MB 80% 6.76sec 重い
h.265 Yes - - - 今のところ動かせない
h.264 No 1.8MB 68% 4.66sec まあいける
h.264 Yes 2.7MB - 1.15sec パラメータ調整できない? videotoolboxの縛り?
atsushiatsushi

h.265 ソフトエンコ

ffmpeg -f concat -safe 0 -i input.txt -codec:v libx265 output.mp4
  • 結果
    • medium
      • 13.8MB → 1.6MB
      • 6.76sec
      • CPU 80%
    • veryfast
      • 13.8MB → 1.8MB
      • 5.86sec
      • CPU 80%

medium

frame=  100 fps= 15 q=32.7 Lsize=    1602kB time=00:00:03.88 bitrate=3383.4kbits/s speed=0.574x
x265 [info]: frame I:      3, Avg QP:28.51  kb/s: 16381.33
x265 [info]: frame P:     47, Avg QP:30.64  kb/s: 5312.96
x265 [info]: frame B:     50, Avg QP:35.42  kb/s: 567.18

veryfast

frame=  100 fps= 17 q=31.7 Lsize=    1804kB time=00:00:03.88 bitrate=3808.4kbits/s speed=0.662x
x265 [info]: frame I:      3, Avg QP:28.55  kb/s: 16178.07
x265 [info]: frame P:     21, Avg QP:28.86  kb/s: 7990.65
x265 [info]: frame B:     76, Avg QP:34.65  kb/s: 2001.32
x265 [info]: Weighted P-Frames: Y:0.0% UV:0.0%
atsushiatsushi

h.265 ハードウェアエンコ

なんかエラーになるので後回し。

/ffmpeg -f concat -safe 0 -i input.txt -codec:v hevc_videotoolbox  output.mp4
[swscaler @ 0x7faf38060000] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 0x7faf10108000] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 0x7faf38060000] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 0x7faf10108000] deprecated pixel format used, make sure you did set range correctly
[hevc_videotoolbox @ 0x7faf30f0bd00] Error: cannot create compression session: -12908
[hevc_videotoolbox @ 0x7faf30f0bd00] Try -allow_sw 1. The hardware encoder may be busy, or not supported.
[vost#0:0/hevc_videotoolbox @ 0x7faf30f0b940] Error while opening encoder - maybe incorrect parameters such as bit_rate, rate, width or height.
Error while filtering: Generic error in an external library
[out#0/mp4 @ 0x7faf30f0ab80] Nothing was written into output file, because at least one of its streams received no packets.
atsushiatsushi

h.264 ソフトエンコ

ffmpeg -f concat -safe 0 -i input.txt -codec:v libx264 output.mp4
  • 結果
    • medium
      • 13.8MB → 1.8MB
      • 4.66sec
      • CPU 68%
    • veryfast
      • 13.8MB → 1.9MB
      • 1.8sec
      • CPU 53%

medium

[out#0/mp4 @ 0x7fad28908240] video:1863kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.100494%
frame=  100 fps= 21 q=-1.0 Lsize=    1865kB time=00:00:03.88 bitrate=3937.1kbits/s speed=0.831x
[libx264 @ 0x7fad2890a680] frame I:7     Avg QP:20.78  size:108145
[libx264 @ 0x7fad2890a680] frame P:49    Avg QP:22.01  size: 20581
[libx264 @ 0x7fad2890a680] frame B:44    Avg QP:23.31  size:  3214

veryfast

[out#0/mp4 @ 0x7f93c6f065c0] video:1968kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.092628%
frame=  100 fps= 53 q=-1.0 Lsize=    1970kB time=00:00:03.88 bitrate=4159.7kbits/s speed=2.07x
[libx264 @ 0x7f93c6f06e00] frame I:7     Avg QP:17.44  size:108456
[libx264 @ 0x7f93c6f06e00] frame P:51    Avg QP:19.19  size: 22638
[libx264 @ 0x7f93c6f06e00] frame B:42    Avg QP:22.39  size:  2409
atsushiatsushi

veryfastでcrfを変化させたとき。

  • 0−51で指定できる
  • 0が最高画質
  • 17-28が推奨範囲
  • デフォルトは23
crf エンコード時間 ファイルサイズ
23 1.8sec 1.9MB
25 2.12sec 1.7MB
26 2.12sec 1.6MB
28 1.94sec 1.4MB

当然crfをあげてくと見た目でわかるレベルで画質が劣化していく。

atsushiatsushi

h.264 ハードウェアエンコ

ffmpeg -f concat -safe 0 -i input.txt -codec:v h264_videotoolbox output.mp4
  • 結果
    • medium
      • 13.8MB → 2.7MB
      • 1.15sec
    • veryfast
      • 13.8MB → 2.7MB
      • 1.09sec

medium

[out#0/mp4 @ 0x7fee56a052c0] video:2727kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.046988%
frame=  100 fps= 86 q=-0.0 Lsize=    2728kB time=00:00:03.96 bitrate=5643.4kbits/s speed=3.42x

veryfast

[out#0/mp4 @ 0x7fa4a5b05380] video:2727kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.046988%
frame=  100 fps= 92 q=-0.0 Lsize=    2728kB time=00:00:03.96 bitrate=5643.4kbits/s speed=3.64x
atsushiatsushi

h264_videotoolboxではcrfを指定しても画質やファイルサイズが変わらなかった。
video tool boxが対応していない?

ffmpeg -f concat -safe 0 -i input.txt -codec:v h264_videotoolbox -crf 48 output.mp4
atsushiatsushi

profileやlevelを変えてもファイルサイズが増える方向にしかいかない。

 -profile           <int>        E..V....... Profile (from -99 to INT_MAX) (default -99)
     baseline        66           E..V....... Baseline Profile
     constrained_baseline 578          E..V....... Constrained Baseline Profile
     main            77           E..V....... Main Profile
     high            100          E..V....... High Profile
     constrained_high 612          E..V....... Constrained High Profile
     extended        88           E..V....... Extend Profile
  -level             <int>        E..V....... Level (from 0 to 52) (default 0)
     1.3             13           E..V....... Level 1.3, only available with Baseline Profile
     3.0             30           E..V....... Level 3.0
     3.1             31           E..V....... Level 3.1
     3.2             32           E..V....... Level 3.2
     4.0             40           E..V....... Level 4.0
     4.1             41           E..V....... Level 4.1
     4.2             42           E..V....... Level 4.2
     5.0             50           E..V....... Level 5.0
     5.1             51           E..V....... Level 5.1
     5.2             52           E..V....... Level 5.2