crop video with ffmpeg

ffmpeg will detect black bars for cropping. i use this for recording retro video games from obs. i'd highly recommend disabling capture of the cursor when making recordings to be cropped this way, such that the letterboxing in the recording is fully black for the entire clip.

detect what would be the crop:

$ ffmpeg -i 2024-05-26_00-33-16.mkv -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1

perform the crop:

$ ffmpeg -i 2024-05-26_00-33-16.mkv -vf "crop=2880:2160:480:0" 2024-05-26_00-33-16--cropped.mkv

my script for perform automatic cropping:

#!/bin/bash

filename=$1
crop_filename="${filename%.*}_cropped.${filename##*.}"

crop=$(ffmpeg -i "$filename" -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
echo "cropping video with: $crop"
ffmpeg -i "$filename" -vf "$crop" "$crop_filename"
echo "done: $crop_filename"