#! /bin/bash

# TODO: - Check why multithreading does not work on many MPEG-xs
#       - Use AVIDEMUX for ac3 files, as ffmpeg copy seems not to work for Humax...

while getopts “hi:o:avf” OPTION
do
  case $OPTION in
  i)
    oldfile=$OPTARG
    ;;
  o)
    newfile=$OPTARG
    ;;
  a)
    fa=1
    ;;
  v)
    fv=1
    ;;
  f)
    f=1
    ;;
  h|[?])
    echo 1>&2 "Usage: $0 -i input -o output [-f]"
    echo 1>&2 "Will transcode input to AVI iwht name output"
    echo 1>&2 "-a will force audio copying (instead of transcoding to stereo MP3)"
    echo 1>&2 "-v will force video copying (instead of transcoding to XViD)"
    echo 1>&2 "-f will force overwriting target name (but MUST still be different than source!)"
    exit 1
    ;;
  esac
done

if [ -z "$oldfile" ]
then
  # Minimal input not found, show usage
  `"$0" -h`
    exit 1
fi

echo

basename=${oldfile%.*}

if [ -z "$newfile" ]
then
  newfile=${basename}.avi
fi
if [ "$newfile" == "$oldfile" ]
then
  echo 1>&2 "output file MUST be different than input file!"
  exit 255
fi
if [ -e "$newfile" -a -z "$f" ]
then
  echo 1>&2 "Output file \"$newfile\" already exists!"
  exit 255
fi
date
echo "Transcoding \"$oldfile\" into \"$newfile\""

if [[ ( "$oldfile" == *VTS_* ) || ( "$oldfile" == *VIDEO_TS.* ) ]]
then
  echo 1>&2 "??? Seems like DVD, are you sure to transcode ???"
  exit 255
fi

#echo Testing codecs
acodec=`mplayer -vo null -ao null -frames 0 -identify "$oldfile" 2>/dev/null |grep -i "ID_AUDIO_CODEC" | cut -f 2 -d "="`
anch=`mplayer -vo null -ao null -frames 0 -identify -channels 8 "$oldfile" 2>/dev/null |grep -i "ID_AUDIO_NCH" | tail -1 |cut -f 2 -d "="`
vcodec=`mplayer -vo null -nosound -frames 0 -identify "$oldfile" 2>/dev/null |grep -i "ID_VIDEO_CODEC" | cut -f 2 -d "="`
if [ -z "$acodec" -o -z "$vcodec" ]
then
  echo 1>&2 "No valid multimedia file! (both audio and video track required)"
  exit 255
fi
echo "Current codecs: video: $vcodec    audio: $acodec ($anch channels)"
if [[ ( "$anch" -gt 2 ) && ( -z "$fa" ) ]]
then
  # transcoding multichannel to stereo MP3
  echo 1>&2 "Source file is multi-channel that will be removed!!!"
fi

echo "Running advanced cropdetect"

width=`mplayer -vo null -nosound -frames 0 -identify "$oldfile" 2>/dev/null |grep -i "ID_VIDEO_WIDTH" | cut -f 2 -d "="`
height=`mplayer -vo null -nosound -frames 0 -identify "$oldfile" 2>/dev/null |grep -i "ID_VIDEO_HEIGHT" | cut -f 2 -d "="`
echo "detected resolution: $width x $height"

length=`mplayer -vo null -nosound -frames 0 -identify "$oldfile" 2>/dev/null |grep -i "ID_LENGTH" | cut -f 2 -d "=" | cut -f 1 -d "."`
#echo detected length:$length

vstart=$(($length / 2))
#echo playing from $vstart during 10 (I) frames

mplayer -vo null -nosound -vf framestep=I -ss $vstart -frames 10 -vf cropdetect "$oldfile" 2> /dev/null | \
grep -i "crop area:" | tail -1 | cut -f 2 -d\( |
(
  read vf cropparam
  cropparam=${cropparam/)/}
  cropparam=${cropparam/./}
#  echo detected cropparam:$cropparam

  vx=$(echo $cropparam | cut -d= -f2 | cut -d: -f1)
  vy=$(echo $cropparam | cut -d= -f2 | cut -d: -f2)
  vxo=$(echo $cropparam | cut -d= -f2 | cut -d: -f3)
  vyo=$(echo $cropparam | cut -d= -f2 | cut -d: -f4)

  bxl=$vxo
  bxr=$(($width - $vx - $vxo))
  byb=$(($height - $vy - $vyo))
  # create vertical border size from bottom side
  byt=$byb

  # apply the vertical symmetry
  vyo=$byt
  vy=$(($height - $byt - $byb))

if [ "$bxl" -eq 0 -a "$bxr" -eq 0 -a "$byt" -eq 0 -a "$byb" -eq 0 ]; 
then
  # No cropping, thus possibly no video transcoding
  echo "No cropping"
  cropping=""
else
  # Cropping, thus video transcoding
  echo "X cropping from 0 till $vxo [$bxl pixels] and from $(($vxo+$vx)) till $width  [$bxr pixels]"
  echo "Y cropping from 0 till $vyo [$byt pixels] and from $(($vyo+$vy)) till $height [$byb pixels]"
  cropping="-croptop $byt -cropleft $bxl -cropright $bxr -cropbottom $byb"
fi

if [[ ( -n "$fa" ) || ( "$acodec" == *mp3* ) ]];
then
  echo "No audio transcoding"
  audio="-acodec copy"
else
  echo "Audio transcoding (to MP3)"
  audio="-acodec libmp3lame -ab 128k -ac 2"
fi
if [[ ( -n "$fv" ) || ( "$vcodec" == *xvid* ) || ( "$vcodec" == *divx* ) ]];
then
  echo "No video transcoding"
  video="-vcodec copy"
else
  echo "Video transcoding (2 pass to XViD)"
  video="-deinterlace -vcodec libxvid -vb 2048k"
  vt=1;
fi

echo "Encoding AVI..."
if [ -n "$vt" ]
then
#  ffmpeg -threads 4 -y -i "$oldfile" $video $cropping -an    -pass 1 "$newfile" 2>/dev/null
  ffmpeg -y -i "$oldfile" $video $cropping -an    -pass 1 "$newfile" 2>/dev/null
  echo "Pass 2..."
#  ffmpeg -threads 4 -y -i "$oldfile" $video $cropping $audio -pass 2 "$newfile" 2>/dev/null
  ffmpeg -y -i "$oldfile" $video $cropping $audio -pass 2 "$newfile" 2>/dev/null
  rm ffmpeg*.log
else
  # no video transcoding required: single pass
#  ffmpeg -threads 4 -y -i "$oldfile" $video $audio "$newfile" 2>/dev/null
  ffmpeg -y -i "$oldfile" $video $audio "$newfile" 2>/dev/null
fi
echo "Done."
date
)

