How to Square Images Without Cropping or Downscaling with FFmpeg

set -xef

input=$1

dirpath=$(dirname "$input")
file_name=$(basename "$input")
file_ext="${file_name##*.}"
file_name="${file_name%.*}"

blurred="$dirpath/blurred-$file_name.$file_ext"
output="$dirpath/output-$file_name.$file_ext"

width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=p=0 "$input")
height=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 "$input")

longer_side="$height"
diff_side=$(($height - $width))
# Determine longer side
if [ "$width" -gt "$height" ]; then
    longer_side="$width"
    diff_side=$(($width - $height))
fi

# Calculate canvas dimensions with padding
padding=$((50+$diff_side/2))
canvas_side=$((longer_side + 2 * padding))

ffmpeg -i "$input" -vf "scale=$(($canvas_side*2)):-1,gblur=sigma=15,crop=$canvas_side:$canvas_side" -y "$blurred"
ffmpeg -i "$blurred" -i "$input" -filter_complex "[0:v][1:v]overlay=(W-w)/2:(H-h)/2:format=auto" -y "$output"

rm "$blurred"

Comments

Popular posts from this blog