Skip to content

Instantly share code, notes, and snippets.

@istro
Last active November 14, 2018 19:43
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 istro/d229e88ac1444ee303b08f82efb3f26a to your computer and use it in GitHub Desktop.
Save istro/d229e88ac1444ee303b08f82efb3f26a to your computer and use it in GitHub Desktop.
A shell wrapper for `ffmpeg` command to compress video files to mp4 with h264 codec.
#!/bin/bash/
# Shell wrapper for converting to mp4 with h264 codec with sensible defaults optimized for small file size.
while getopts ":h" opt; do
case $opt in
h)
echo "Usage: 'tomp4 INPUT OUTPUT SCALE_FACTOR'" >&2
echo "Only INPUT is required, just hit enter for default values when prompted." >&2
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
if [ -z $1 ]; then
echo "Which file do you want to convert?"
echo "usage: 'tomp4 INPUT OUTPUT SCALE_FACTOR'"
echo "Only INPUT is required, for others you can just hit enter for default values when prompted."
exit 1
fi
###### helper functions
floatToInt() {
printf "%.0f\n" "$@"
}
nearestEven() {
if (( $1 % 2 == 0 )); then
echo $1
else
echo $(( $1 + 1 ))
fi
}
just_compress() {
echo '*************************'
echo '*** Just compressing. ***'
echo '*************************'
ffmpeg -i $1 -c:v libx264 -preset slow -crf 22 -c:a copy -r 30 $2
}
resize_and_compress() {
echo '******************************'
echo '*** Scaling & compressing. ***'
echo '******************************'
ffmpeg -i $1 -c:v libx264 -preset slow -crf 22 -c:a copy -vf scale=$3 -r 30 $2
}
scale() {
FACTOR=$1
INPUT=$2
OUTPUT=$3
ORIGINAL_WIDTH=`mediainfo --language=raw --inform="Video;%Width%" $INPUT`
TARGET_WIDTH=$(floatToInt $(bc <<< $ORIGINAL_WIDTH*$FACTOR ))
DIMENSIONS="$(nearestEven $TARGET_WIDTH):-2"
resize_and_compress $INPUT $OUTPUT $DIMENSIONS
}
target_width() {
WIDTH=$1
INPUT=$2
OUTPUT=$3
DIMENSIONS="$(nearestEven $WIDTH):-2"
resize_and_compress $INPUT $OUTPUT $DIMENSIONS
}
target_height() {
HEIGHT=$1
INPUT=$2
OUTPUT=$3
DIMENSIONS="-2:$(nearestEven $HEIGHT)"
resize_and_compress $INPUT $OUTPUT $DIMENSIONS
}
###### processing input params
INPUT=$1
if [ -z $2 ]; then
echo "Output file name (defaults to [orignal_file_name]_scaled.mp4)"
read OUTPUT
[[ $OUTPUT ]] && OUTPUT="$OUTPUT.mp4" || OUTPUT="[$INPUT]_scaled.mp4"
else
OUTPUT="$2.mp4"
fi
if [ -z $3 ]; then
echo "Choose scaling option: by a factor (f), specific output width (w) or height (h)"
read SCALING_OPTION
case $SCALING_OPTION in
"f")
echo "Enter scaling factor (e.g. '.5' for 2x smaller, '2' for 2x larger)"
read SCALING_FACTOR
if [ -z $SCALING_FACTOR ]; then
just_compress $INPUT $OUTPUT
else
scale $SCALING_FACTOR $INPUT $OUTPUT
fi
;;
"w")
echo "Enter target width in pixels"
read TARGET_WIDTH
if [ -z $TARGET_WIDTH ]; then
just_compress $INPUT $OUTPUT
else
target_width $TARGET_WIDTH $INPUT $OUTPUT
fi
;;
"h")
echo "Enter target height in pixels"
read TARGET_HEIGHT
if [ -z $TARGET_HEIGHT ]; then
just_compress $INPUT $OUTPUT
else
target_height $TARGET_HEIGHT $INPUT $OUTPUT
fi
;;
*)
just_compress $INPUT $OUTPUT
exit 0
;;
esac
else
scale $3 $INPUT $OUTPUT
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment