映像の黒枠を検出して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 -
画像の場合はloopを有効にしないとログが流れない。
ffmpeg -loop 1 -i img.jpg -vf cropdetect -vframes 2 -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 Filters Documentation : cropdetect
オプション
- limit[float]
黒枠の検出閾値の上限指定。0 から 1 まではビット深度に応じての割合になる。つまり 0 指定にすると真っ黒(0)以外は検出しない
既定値:0.0941176
範囲:0 から 65535 まで - 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 まで
メタデータ
以下の値が指定できる。
- lavfi.cropdetect.x1
- lavfi.cropdetect.x2
- lavfi.cropdetect.y1
- lavfi.cropdetect.y2
- lavfi.cropdetect.w
- lavfi.cropdetect.h
- lavfi.cropdetect.x
- lavfi.cropdetect.y
コマンド例。
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 | ニコラボで検索。