#!/bin/sh -e
#
# optimize-video - converts given files (.mov, .gif) to .mp4
# dependencies: ffmpeg

input_file="$1"
output_file="$1.mp4"

if [ -z "$1" ]; then
  printf '%s\n' "$(basename "$0") [input file (.mov, .gif)]"
  exit 1
fi

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  cat <<EOF
Usage: $(basename "$0") [input file]

Convert given file (.mov, .gif) to .mp4 using ffmpeg.
Output will be stored in the same directory of input file.
EOF
  exit 0
fi

# verify dependencies
if ! command -v ffmpeg >/dev/null 2>&1; then
  printf '%s\n' "${0##*/}: ffmpeg not found in PATH!" >&2
  exit 1
fi

echo "[INFO] making mp4..."
echo "output file: ${output_file}"

/usr/bin/time ffmpeg \
  -hwaccel auto \
  -i "${input_file}" \
  -movflags faststart \
  -pix_fmt yuv420p \
  -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" \
  -hide_banner -loglevel error \
  "${output_file}"
