brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ffmpeg -vf cropdetect finds the black bars you can crop away

Ffmpeg
~/tips/tip-ffmpeg-cropdetect-black-bars

ffmpeg -i file.mp4 -vf cropdetect,metadata=mode=print -f null -

Measures the black borders in a video so you know the exact crop to use.

Some videos come with black bars baked into the picture, above and below or at the sides. Instead of guessing the numbers for a crop, ffmpeg has a filter that measures the bars for you.

The cropdetect filter looks for the video area that is surrounded by black borders. The example in the ffmpeg filters documentation is this one:

ffmpeg -i file.mp4 -vf cropdetect,metadata=mode=print -f null -

It decodes the video, prints what it found and throws the output away (-f null -), so it creates no file. Look at the printed crop values near the end, the last lines are usually the most stable. Skip the first seconds if the opening is a fade from black.

The crop filter then takes four values, w:h:x:y: width, height and the top left corner of the area to keep. The docs show crop=100:100:12:34 as a 100×100 area at position (12,34). Plug in the numbers you got:

ffmpeg -i file.mp4 -vf crop=1920:800:0:140 cropped.mp4

The numbers here are made up, use your own. Because a filter changes the picture, the video has to be re-encoded, so -c copy cannot be combined with it.

The same page shows two other crop forms: crop=100:100 takes the central area, and crop=in_w-2*10:in_h-2*20 removes 10 pixels from the sides and 20 from the top and bottom, which is useful when the border is a fixed size.

My caveat is that detection measures dark pixels, so a dark scene can fool it and give a crop that is too tight. I would check values on a few stretches of the film, not only the start, and I would watch the cropped file around bright and dark scenes before I delete the original.

Further reading: ffmpeg filters documentation: crop and cropdetect

More Ffmpeg tips ffmpeg -stream_loop 2 -i in.mp4 -c copy out.mp4 ffmpeg -i in.mp4 \ ffmpeg -i video.mp4 -i video.en.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:0 language=eng video-subs.mp4 ffmpeg -i film.mkv -map 0:v -map 0:a:0 -c:v copy -c:a aac -b:a 192k -ac 2 film-stereo.mkv ffmpeg -i talk.mp4 -vn -c:a copy talk.m4a All Ffmpeg tips (38) All topics

Link