#!/bin/sh
DEFAULTSIZE=720:600
if [ $# -lt 1 ] ; then
    echo "Usage: $0 <videofile> [x:h]"
    exit 1
fi
if [ ! -e "$1" ] ; then
    echo "$0: No such file $1"
    exit 1
fi
if [ "$3" = "" ] ; then
    SIZE=$DEFAULTSIZE
else
    SIZE="$3"
fi
NAMEADD=`echo $SIZE | sed 's/:/_/'`
if [ "$SIZE" = "$NAMEADD" ] ; then
    echo "Suspicious size $SIZE.  Should be width:height."
    exit 1
fi

set -x
EXT=`echo $1 | sed 's/.*\.\([Mm][PpTt][4Ss]\)/\1/' | tr 'A-Z' 'a-z'`
echo "$EXT"
if [ "$EXT" = "mp4" ] ; then
  output=`echo $1 | sed 's/^\(.*\)\.[Mm][Pp]4/\1/'`_${NAMEADD}.webm
else
  if [ "$EXT" = "mts" ] ; then
    output=`echo $1 | sed 's/^\(.*\)\.[Mm][Tt][Ss]/\1/'`_${NAMEADD}.webm
  else
    echo "$0: Warning: Extension of $1 is unknown"
    output="$1"_${NAMEADD}.webm
  fi
fi
rm -f "$output"
ffmpeg -i $1 -acodec libvorbis -vf scale=$SIZE -aq 5 -ac 2 -qmax 25 -threads 2 $output
touch -r "$1" "$output"

exit
