#!/bin/bash -e

#####################################################
#
# This command will re-encode a video into an MP4
# container with H264 video and AAC audio encoded to
# 96K. Each of the arguments are explained as follows:
#
# -i "${INPUT}"
# Sets the first argument, saved as variable "$INPUT"
# as the input file that ffmpeg will re-encode. Must 
# go at the beginning of your command.
#
# -codec:video libx264
# Sets the video codec to libx264, which is the library
# ffmpeg uses to encode H264 video streams.
#
# -preset slow
# Sets the preset of the H264 encoding process to run
# slow, which can yield higher quality imagery at the
# expense of speed. Available options:
# ultrafast, superfast, veryfast, faster, medium (default),
# slow, slower, veryslow, placebo
#
# -crf 22
# Sets the Constant Rate Factor of the video stream,
# which determines the data rate. The range of -crf
# values range from 0 to 51, with 0 being visually
# lossless and 51 being extremely compressed.
#
# -codec:audio aac
# Sets the codec of the output audio stream to AAC.
#
# -b:a
# Sets the data rate of the audio to 192kbit/s.
#
# "${OUTPUT}"
# The output video file, which should be the last
# argument you provide.
#
# Usage: bash ffmpeg/basic/reencode.sh <input video> <output video>
#
#####################################################

INPUT="${1}"
OUTPUT="${2}"

# Check if input and output arguments are supplied
if [ "${INPUT}" == "" ] || [ "${OUTPUT}" == "" ]; then
	echo "Please provide input and output files as your first and second arguments"
	exit 1
fi

# Check if file extension is .mp4
if [[ "${OUTPUT}" != *".mp4" ]]; then
	echo "Please use an .mp4 extension on your output file argument"
	exit 2
fi

# Command to re-encode the input video into H264/AAC
ffmpeg -i "${INPUT}" -c:v libx264 -preset slow -crf 22 -c:a aac -b:a 192k "${OUTPUT}"