#!/bin/sh

# if ffmpeg is not installed, exit with an error message
if ! command -v ffmpeg >/dev/null 2>&1; then
  printf "Can't find ffmpeg. Please install it (or check your path).\n"
  exit 1
fi

pngSeq='pngs/frame-%5d.png'

cmd=$(basename "$0")

usage() {
  printf "Create a video from a PNG sequence.\n\n"
  printf "Usage:\n"
  printf "    %s [gif|mp4|webm] framerate width basename [max-gif-colors]\n\n" "${cmd}"
  printf "The input is expected to be a PNG sequence relative to the current\ndirectory with the format 'pngs/frame-%%5d.png'\n"
}

makeGIF() {
  framerate=$1
  width=$2
  basename=$3
  maxColors=$5

  # GIFs of 60fps videos have to be 50fps
  if [ "${framerate}" = "60" ]; then
    framerate=50
  fi

  if [ "${maxColors}" = "" ]; then
    maxColors=256
  fi

  ffmpeg \
    -framerate "${framerate}" \
    -i "${pngSeq}" \
    -vf "fps=${framerate},scale=${width}:-1:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors=${maxColors}[p];[s1][p]paletteuse" \
    "${basename}".gif
}

makeMP4() {
  framerate=$1
  width=$2
  basename=$3

  ffmpeg \
    -framerate "${framerate}" \
    -i "${pngSeq}" \
    -vcodec libx264 \
    -pix_fmt yuv420p \
    -an \
    -crf 20 \
    -filter:v scale="${width}":-1 \
    "${basename}.mp4"
}

makeWebM() {
  framerate=$1
  width=$2
  basename=$3

  ffmpeg -framerate "${framerate}" \
    -i "${pngSeq}" \
    -pix_fmt yuv420p \
    -an \
    -crf 30 \
    -b:v 0 \
    -filter:v scale="${width}":-1 \
    "${basename}.webm"
}

if [ "$#" -lt 4 ] || [ "$1" = "-h" ]; then
  usage
  exit 1
fi

case "$1" in
  gif)
    if [ "$#" -lt 5 ]; then
      exit 1
    fi
    makeGIF "$2" "$3" "$4" "$5"
    ;;
  mp4)
    makeMP4 "$2" "$3" "$4"
    ;;
  webm)
    makeWebM "$2" "$3" "$4"
    ;;
  *)
    usage
    ;;
esac
