23

I think it has a "blackness" video filter, which can determine if a picture sequence is black. Maybe it also has a filter to determine crop values automatically to remove black borders form edges of a video. Or maybe it is possible somehow to write a script using the "blackness" filter.

Konstantin
  • 653
  • 2
  • 12
  • 22

3 Answers3

36

Yes, it is possible.

First play your video to see if it is OK:

ffplay -i YourMovie.mp4 -vf "cropdetect=24:16:0"

The cropdetect filter values are:

cropdetect=limit:round:reset

limit = black threshold (default 24)
round = output resolution must be divisible to this
reset = after how many frames the detection process will start over

If it looks OK, crop it:

ffmpeg -i YourMovie.mp4 -vf "crop=640:256:0:36" YourCroppedMovie.mp4

Source and more info: René Calles blog renevolution.com

JoeNyland
  • 276
  • 1
  • 10
Cornelius
  • 2,754
  • 1
  • 16
  • 26
  • 11
    It might be helpful to put a start point (-ss 00:05:00) in when using cropdetect (before input -i) as some video will have a solid black screen when starting. –  Jul 04 '15 at 13:45
  • I would like to use this to crop `python` `matplotlib.animation` videos, which have a white border. Is it possible to alter the color of the border being detected? – ryanjdillon Oct 08 '15 at 11:51
  • 1
    Can you specify something else than black as border color? White for example? – DanMan Mar 08 '18 at 13:03
  • This does not answer the question as you still need to do manual action – Freedo Sep 08 '19 at 08:32
  • Hey @Cornelius can you please help me to find one command - i want to crop video.mp4 from Square to Round using ffmpeg - not want to use overlay command for this – Adil Sep 13 '19 at 07:08
  • 1
    Your ffplay cropdetect command played my video but still had black on the right and bottom sides. Then I saw https://superuser.com/a/1035399/74576. And it occurs to me that maybe https://ffmpeg.org/ffmpeg-filters.html#cropdetect can't help at all for videos that have black sides of asymmetrical thicknesses. The documentation is not great. – Ryan May 24 '20 at 13:47
  • A manual crop worked for my case: https://ffmpeg.org/ffmpeg-filters.html#crop `ffplay -i "myvid.mp4" -vf "crop=w=1104:h=830:x=0:y=0"` – Ryan May 24 '20 at 13:57
19

From: https://stackoverflow.com/questions/17265381/ffmpeg-get-value-from-cropdetect

ffmpeg -i input -t 1 -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1
Jannes
  • 290
  • 2
  • 4
  • 1
    ok, this will generate the "crop values" to use with ffmpeg in a simple and very clear way :), thx! – Aquarius Power Apr 13 '16 at 00:11
  • 1
    This fails miserably if there's a bit of black at the beginning of the video, such as in an intro. This is solved by adding `-ss 5` before `-i`, to run `cropdetect` after skipping the first 5 seconds. – Hashim Aziz Apr 05 '21 at 19:11
  • 1
    Save yourself a fork and get `awk` to only print the last matching field. `awk '/crop=/ {a=$NF} END{print a}'` – Cliff Jul 06 '23 at 22:50
5

Putting the other two answers together into a script:

#!/bin/sh
#ffmpeg_zoom ver 20180128202453
I="$@";X=${I##*.};O=${I%.*}_zoomed.${X};f=$(which ffmpeg 2>/dev/null)
if [ ! "$f" ]||[ "$f" = '' ];then echo "Install ffmpeg";exit 1;fi
C=$($f -i "$I" -t 1 -vf cropdetect -f null - 2>&1|awk '/crop/{print $NF}'|tail -n1)
echo $f -i "$I" -vf "$C" "$O"; $f -i "$I" -vf "$C" "$O"

This question has some relevant ffmpeg examples

Alexx Roche
  • 820
  • 7
  • 15