#!/bin/sh -e

vertical="$1"
input="$2"
output="$3"

if [ -z "${vertical}" ] || [ -z "${input}" ]; then
  echo "Usage: $(basename "$0") <vertical-resolution> <input> [<output>]" >&2
  exit 1
fi

if [ -z "${output}" ]; then
  d="$(dirname "${input}")"
  f="$(basename "${input}")"
  ext="${f##*.}"
  noext="${f%.*}"
  output="${d}/${noext}_${vertical}p.${ext}"
fi

exec ffmpeg -y \
  -i "${input}" -vf "scale=-1:${vertical}" -c:v libx264 -c:a copy -vsync 0 "${output}"
