Last active
March 24, 2021 13:52
-
-
Save raspi/559e2af99401c3f87f666356a8668e06 to your computer and use it in GitHub Desktop.
Crop video with FFMPEG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Crop a part of a video with FFMPEG | |
if [[ $# -eq 0 ]] ; then | |
echo "Usage:" | |
echo " $0 <filename> <left> <right> <top> <bottom>" | |
echo "" | |
echo "Example:" | |
echo " $0 myvid.mp4 700 600 200 400" | |
echo "" | |
exit 0 | |
fi | |
if [[ $# -lt 4 ]] ; then | |
echo "No enough parameters" | |
exit 1 | |
fi | |
# File name to read from | |
FNAME=$1 | |
if [[ ! -f "$FNAME" ]]; then | |
echo "Not a file: $FNAME" | |
exit 1 | |
fi | |
declare LEFT=$2 | |
declare RIGHT=$3 | |
declare TOP=$4 | |
declare BOTTOM=$5 | |
shift | |
shift | |
shift | |
shift | |
shift | |
declare WIDTH=$(ffprobe -v error -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "$FNAME") | |
declare HEIGHT=$(ffprobe -v error -show_entries stream=height -of -of -of default=noprint_wrappers=1:nokey=1 "$FNAME") | |
declare WWIDTH=$(( $WIDTH - $LEFT - $RIGHT )) | |
declare WHEIGHT=$(( $HEIGHT - $TOP - $BOTTOM )) | |
PARAMS=crop=w=$WWIDTH:h=$WHEIGHT:x=$LEFT:y=$TOP | |
if [[ "$1" == "-vf" ]]; then | |
shift | |
PARAMS=${PARAMS},${1} | |
shift | |
fi | |
echo "$PARAMS" | |
if [[ "$PREVIEW" == "1" ]]; then | |
ffplay -hide_banner -volume 20 -loop 0 -vf "$PARAMS" "$@" "$FNAME" | |
exit 0 | |
fi | |
NEWNAME=${FNAME%.*}-crop-${WWIDTH}x${WHEIGHT}@${LEFT},${TOP}.${FNAME##*.} | |
ffmpeg -hide_banner -i "$FNAME" -vf "$PARAMS" -avoid_negative_ts make_zero -async 1 -strict -2 "$@" "$NEWNAME" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment