#!/bin/bash

SECONDS=0
in=$1
bin=$(basename "$in")	
mp4=".mp4"
out=${in/.mov/$mp4}
out=${out/.mxf/$mp4}
out=${out/.mkv/$mp4}
out=${out/.webm/$mp4}
out=${out/.3gp/$mp4}
out=${out/.wmv/$mp4}
out=${out/.m4v/$mp4}
out=${out/.mp4/$mp4}
out=${out/.mpg/$mp4}

if [[ "$in" == *".mp4"* ]]; then
echo "in: $in"
  x='x'
  in="$in$x"
  mv "$out" "$in"
fi

st=`date '+%H:%M:%S'`

echo "$st - Processing $in to $out..."

#exit

if test -f "$out"; then
    rm -f "$in" 
    sp=`date '+%H:%M:%S'`
    echo "$sp - $bin already converted to mp4 in $SECONDS seconds"
else 
#   ffmpeg -y -loglevel quiet -i "$in" -vcodec h264 -acodec mp2 "$out"
	ffmpeg -y -loglevel quiet -i "$in" -vcodec h264 "$out"
	touch -r "$in" "$out"

   if test -f "$out"; then
       rm -f "$in" 
       sp=`date '+%H:%M:%S'`
       echo "$sp - $bin converted to mp4 in $SECONDS seconds"
   else
       sp=`date '+%H:%M:%S'`
       echo "$sp - FAILED TO CONVERT $in to mp4 in $SECONDS seconds"
   fi
fi

