映像の黒枠を検出してcropフィルタで使う引数を返すcropdetectフィルタの使い方。

基本コマンド

ffmpeg -i input -vf cropdetect -an -f null -
ffplay -i input -vf cropdetect

metadataフィルタを使ってテキストに出力する方法もある。
ffmpeg -i input -vf cropdetect,metadata=mode=print:file=foo.txt -an -f null -

1枚画像のときはskip=0を指定する。
ffmpeg -i img.jpg -vf cropdetect=skip=0 -f null -

運用例。
ffplay -f lavfi -i color=white -vf fillborders=0:0:32:16:2,cropdetect
ffplay -f lavfi -i color=white -vf fillborders=0:0:32:16:2,crop=320:192:0:32

以下のようなログが1フレーム毎に流れる。

x1:0 x2:319 y1:32 y2:223 w:320 h:192 x:0 y:32 pts:2 t:0.080000 crop=320:192:0:32
  • x1, x2 は黒枠を除外した横座標の開始と終了ピクセルで、0ピクセル開始
  • y1, y2 は黒枠を除外した縦座標の開始と終了ピクセル、0ピクセル開始
  • w, h, x, y はcropフィルタで指定する引数

埋め込まれたビデオ領域を見つけ、事前にモーション ベクトルを生成する。
ffmpeg -i input -vf mestimate,cropdetect=mode=mvedges,metadata=mode=print -an -f null -

埋め込まれたビデオ領域を見つけ、デコーダからのベクトルを使用する。
ffmpeg -flags2 +export_mvs -i input -vf cropdetect=mode=mvedges,metadata=mode=print -an -f null -

関連フィルタ

公式ドキュメント:FFmpeg Filters Documentation : cropdetect

オプション

  • limit[float]
    黒枠の検出閾値の上限指定。0 から 1 まではビット深度に応じての割合になる。つまり 0 指定にすると真っ黒(0)以外は検出しない
    既定値:0.0941176
    範囲:0 から 65535 まで
  • skip[int]
    スキップする最初のフレーム数
    既定値:2
    範囲:0 から INT_MAX まで
  • round[int]
    指定した値の倍数で縦横の検出ピクセルを丸める。検出した値でcropフィルタできれいにクロップできないときはこの値を小さくする
    既定値:16
    範囲:0 から INT_MAX まで
  • reset_count, reset[int]
    検出エリアを指定フレーム毎に再計算する。黒枠がフレームで異なるときに指定
    既定値:0
    範囲:0 から INT_MAX まで
  • max_outliers[int]
    Threshold count of outliers
    既定値:0
    範囲:0 から INT_MAX まで
  • mode[int]
    検出モードの設定
    • 0, balck:周囲にある黒ピクセルを検出。既定値
    • 1, mvedges:周囲にあるモーション、エッジ検出
  • high[float]
    エッジ検出のしきい値上限
    既定値:0.0980392
    範囲:0 から 1 まで
  • low[float]
    エッジ検出のしきい値下限
    既定値:0.0588235
    範囲:0 から 1 まで
  • mv_threshold[int]
    ウインドウサイズの推定するモーションベクトルしきい値
    既定値:8
    範囲:0 から 100 まで

メタデータ出力内容。

lavfi.cropdetect.x1
lavfi.cropdetect.x2
lavfi.cropdetect.y1
lavfi.cropdetect.y2
lavfi.cropdetect.w
lavfi.cropdetect.h
lavfi.cropdetect.x
lavfi.cropdetect.y
lavfi.cropdetect.limit

コマンド例。
ffplay -f lavfi -i color=white -vf fillborders=0:0:32:16:2,cropdetect,drawtext=fontfile=C\\://WINDOWS/Fonts/arial.ttf:text='"y1\:"%{metadata\:lavfi.cropdetect.y1}':fontsize=32:borderw=2:bordercolor=white:y=3
ffprobe -v error -f lavfi -i color=white,fillborders=0:0:32:16:2,cropdetect -select_streams v:0 -show_entries packet_tags=lavfi.cropdetect.y1 -of csv=p=0 -read_intervals "%+#3"
ffprobe -v error -f lavfi -i movie=input.mp4,fillborders=0:0:32:16:2,cropdetect -select_streams v:0 -show_entries packet_tags=lavfi.cropdetect.y1,lavfi.cropdetect.y2 -read_intervals "%+#3" > cropdetect.y1.y2.txt

ffprobe の使い方
packet_tags で使えるフィルタは meta | ニコラボで検索。

コメントを残す

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

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