#! /bin/sh -x
## usage : moviespeed.sh inputfile speed

IFS="
"
NO_COLOR=true

FPSC=20000
FPSM=1001

if [ $# -le 1 ]; then
    echo "Please Set inputfile and speed."
    exit 1
fi
if [ -f "$1" ]; then
    IN="$1"
    str=${IN##*/}
    FILENAME=${str%.*}
else
    echo "Input file is not found."
    exit 1
fi
if [ $# = 3 ]; then
    if [ ! -d $3 ]; then
        mkdir -p $3
    fi
    FILENAME=${3}/${FILENAME}
fi

NFPS=$(echo "(${FPSC}*$2)" | bc)

SCREEN=$(ffmpeg -i $IN 2>&1 1>/dev/null | grep -F " Video:" | cut -d',' -f3 | sed 's/^[^0-9]*\([0-9x]*\).*/\1/' )
#SCREEN=$(echo $str | cut -d',' -f1 | sed 's/^ *\(.*\) *$/\1/')
#FPS=$(echo $str | cut -d',' -f2 | cut -d' ' -f1)

ffmpeg -i $IN -s $SCREEN -r $FPSC/$FPSM  $FILENAME.yuv $FILENAME.wav

#ffmpeg -s $SCREEN -r $NFPS/$FPSM -i $FILENAME.yuv -vcodec copy -an ${FILENAME}_t.avi
sox $FILENAME.wav ${FILENAME}_t.wav tempo -s $2

rm $FILENAME.wav

ffmpeg -s $SCREEN -r $NFPS/$FPSM -i $FILENAME.yuv -i ${FILENAME}_t.wav -vcodec mpeg4 -b 1M -acodec libfaac -f mp4 ${FILENAME}.mp4
# bitrate value should be configured according to the default bitrate.
rm ${FILENAME}_t.wav ${FILENAME}.yuv
