Skip to content

Instantly share code, notes, and snippets.

@frederickk
Created April 24, 2020 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frederickk/d91751e88780ca0939e6bb344ad4dfcf to your computer and use it in GitHub Desktop.
Save frederickk/d91751e88780ca0939e6bb344ad4dfcf to your computer and use it in GitHub Desktop.
Simple bash script to add border and/or rotate a video
#!/bin/bash
# https://gist.github.com/frederickk/d91751e88780ca0939e6bb344ad4dfcf
# Usage
# ./video-border-rotate.sh -i foo.mp4 -x 100 # Add 100px border on left and right
# ./video-border-rotate.sh -i foo.mp4 -y 100 # Add 100px border on top and bottom
# ./video-border-rotate.sh -i foo.mp4 -x 100 -y 150 # Add 100px border on left and right and 150px border on top and bottom
sides
# ./video-border-rotate.sh -i foo.mp4 -x 100 -y 150 -c "red" # Add 100px red border on left and right and 150px red border on top and bottom
# ./video-border-rotate.sh -i foo.mp4 -r 1 # Rotate video 90 degrees clockwise
# ./video-border-rotate.sh -i foo.mp4 -r 2 # Rotate video 90 degrees anti-clockwise
# ./video-border-rotate.sh -i foo.mp4 -r 1 -x 396 -y 264 # Rotate video 90 degrees clockwise, with a 396px border on top and bottom and 364px border on left and right (because the video gets rotated, after adding border)
# Defaults
X_WIDTH=0
Y_WIDTH=0
ROTATE=0
COLOR="white"
while getopts ":i:x:y:r:c:" opt; do
case $opt in
i) SRC=${OPTARG};;
x) X_WIDTH=${OPTARG};;
y) Y_WIDTH=${OPTARG};;
r) ROTATE=${OPTARG};;
c) COLOR=${OPTARG};;
esac
done
echo $SRC
if [ -z "$SRC" ]; then
echo "⛔️ Error source file '-i foo.mp4' required"
else
OUTSRC=${SRC%.*}_B$COLOR$X_WIDTH$Y_WIDTH
ffmpeg -i $SRC -filter_complex "[0]pad=w=iw+($X_WIDTH*2):h=ih+($Y_WIDTH*2):x=$X_WIDTH:y=$Y_WIDTH:color=$COLOR" $OUTSRC.mp4
if [[ "$ROTATE" == 1 || "$ROTATE" == 2 ]]; then
# https://www.ostechnix.com/how-to-rotate-videos-using-ffmpeg-from-commandline/
if [ $ROTATE == 1 ]; then
ROTATE_DEG="90"
elif [ $ROTATE == 2 ]; then
ROTATE_DEG="-90"
fi
ffmpeg -i $OUTSRC.mp4 -vf "transpose=$ROTATE" $OUTSRC$ROTATE_DEG.mp4
rm $OUTSRC.mp4
OUTSRC=$OUTSRC$ROTATE_DEG.mp4
fi
echo "🎥 All done! $OUTSRC"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment