#!/bin/sh
# converts multiple avi files to mpeg2
#
# uses same quantzer as input file
#
#
# see http://www.ehow.com/how_6054318_convert-avi-mpeg2-linux.html
# see mpeg2, Aufloesung etc. wikipedia 
# see "Was ist MPEG-4? Technische Hintergründe" http://www.camgaroo.com/nc/artikel/single/details/was-ist-mpeg-4-technische-hintergruende/
# instead of ffmpeg use avconv
#
# 120606, 130201, 130331

# type (Aufloesung):	-target ntsc-svcd (480 × 480)
#			-target pal (544 × 576)
#			-target ntsc-dvd (704 × 480)
#			-target pal-dvb (768 × 576)
#			-target pal (768 × 576)
#
# see: 	http://de.wikipedia.org/wiki/Videoauflösung, 
#	http://de.wikipedia.org/wiki/Bildauflösung, 
#	http://de.wikipedia.org/wiki/480p
#

if test $# -lt 1; then
   echo "Err: use one or more parameter/s (avi filename/s)"
   exit
fi

opts=$*

DATUM=`/bin/date '+%Y%m%d-%H%M'`
  LOG_file="LOG-${DATUM}.log"
echo "Begin ---   $DATUM" | tee $LOG_file

for i in $opts; do
   if test -e $i; then
      newname=$i
      newname=${newname%.*}-sameq
      if test ! -f $newname.mpeg; then
#         echo  "Converting file $i to $newname.mpeg" | tee -a $LOG_file
#         ffmpeg -i $i -target ntsc-svcd -ps 2000000000 -aspect 4:3 $newname.mpeg
#         ffmpeg -i $i -target ntsc-svcd -ps 2000000000 -aspect 16:9 $newname.mpeg
#         ffmpeg -i $i -target dvd -ps 2000000000 -aspect 16:9 $newname.mpeg
#         ffmpeg -i $i -s 640x360 -aspect 16:9 $newname.mpeg
#         ffmpeg -i $i -qmin 10 -qmax 20 -aspect 16:9 $newname.mpeg
#         command="ffmpeg -i $i -sameq -aspect 16:9 $newname.mpeg"
         command="avconv -i $i -ac 2 -r 25 $newname.mpeg"    #  eqal to "-qmin 1", 6 audio channels to 2 only (ok), frame rate to 25 (incorrect)
#         command="avconv -i $i $newname.mpeg"    #  eqal to "-qmin 1"  # err: incorrect params o.ae.
         echo  "Command: $command" | tee -a $LOG_file
         $command
      else
         echo "File $newname.mpeg just exists. Next!"| tee -a $LOG_file
      fi
   else
      echo "File $i does not exist !!!" | tee -a $LOG_file
   fi
done

DATUM=`/bin/date '+%Y%m%d-%H%M'`
echo "End ---     $DATUM" | tee -a $LOG_file
echo >> $LOG_file

exit

#-----------------
# the same by mencoder:
# mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=DVD:tsaf -vf scale=720:480,harddup \
#  -srate 48000 -af lavcresample=48000 -lavcopts \
#  vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:keyint=18:vstrict=0:acodec=ac3:abitrate=192:aspect=16/9 \
#  -ofps 30000/1001 -o output.mpg original.avi"



