#!/bin/sh
#******************************************************************************\
#  encX264.sh 1.1 - Convert file to mpeg4 with ffmpeg and libx264              *
#  Copyright (C) 2006-2007 Rémy Guillemette <shaikadzari@gmail.com>            *
#                                                                              *
#  This program is free software; you can redistribute it and/or modify        *
#  it under the terms of the GNU General Public License version 2 only as      *
#  published by the Free Software Foundation.                                  *
#                                                                              *
#  This program is distributed in the hope that it will be useful,             *
#  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
#  GNU General Public License for more details.                                *
#                                                                              *
#  You should have received a copy of the GNU General Public License           *
#  along with this program; if not, write to the                               *
#  Free Software Foundation, Inc.,                                             *
#  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                   *
#******************************************************************************/

# Variables
options="-vcodec libx264 -b 768k -bf 3 -subq 6 -cmp 256 -refs 5 -qmin 10 \
           -qmax 51 -qdiff 4 -coder 1 -loop 1 -me hex -me_range 16 -trellis 1 \
           -flags +mv4 -flags2 +bpyramid+wpred+mixed_refs+brdo+8x8dct \
           -partitions parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -g 250 \
           -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71"
cdate=`date +%d-%m-%Y`
logs="./encX264.log"
fflogs="./tmpffmpeg.log"
defaultType=".flv"
banner="
-------------------------------------------------------
                     encX264.sh
     Convert file to mp4 using ffmpeg and libx264
-------------------------------------------------------
"
joinname="./output_$cdate.mpg"
joinfile=""
finalname="./output_$cdate.mp4"
reed="read -e"

showbanner() {
	echo "$banner";
}

writelogs() {
	echo "$1" >> $logs;
}

enctompeg() {
	nom=`basename $1 .$typefile`
	ffmpeg -i $1 -sameq -ab 128k -me_method full -pix_fmt rgb32 -threads 2 $nom.mpg &> $fflogs;
	cat $fflogs >> $logs;
	joinfile="${joinfile} $nom.mpg";
}

enctomp4() {
	writelogs "==================================";
	writelogs "Encoding $joinname.mpg to $finalname";
	echo "====>>> First pass..."; writelogs "===>>> First pass...";
	ffmpeg -y -i $joinname -an -pass 1 -threads 2 $options $finalname &> $fflogs;
	cat $fflogs >> $logs;
	
	echo "====>>> Second pass..."; writelogs "===>>> Second pass...";
	ffmpeg -y -i $joinname -acodec libfaac -ar 44100 -ab 128k -pass 2 -threads 2 $options $finalname &> $fflogs;
	cat $fflogs >> $logs;
}

main() {
	clear;
	showbanner
	echo "$banner" > $logs;
	writelogs "===>>> Starting encX264.sh at `date`";
	echo "Choose input video type:";
	echo "flv [default] | avi | wmv | mov" ; $reed typefile ; echo "";
	
	case "$typefile" in
		avi)
			typefile="avi"
		;;
		wmv)
			typefile="wmv"
		;;
		mov)
			typefile="mov"
		;;
		*)
			typefile="flv"
		;;
	esac

	writelogs "Choosed video type: $typefile";
	
	echo "Select all files from current directory?";
	echo "y [default] / n" ; $reed selAll ; echo "";
	writelogs "Selected all file : " ; writelogs "$selAll" ;

	case "$selAll" in
		n)
			echo "Select your file : " ; ls ; $reed selectfile ; echo "";
			if [ ! -f $selectfile ]; then
				echo "File not found!";
				exit 0;
			fi
			;;
		*)
			selectfile=`ls *.$typefile`
			;;
	esac

	echo "Starting encoding... ";
	writelogs "Starting encoding... " ;

	for f in $selectfile; do
		writelogs "##################################################################";
		writelogs "Convert $f to mpg...";
		echo "Convert $f to mpg...";
		enctompeg $f
		writelogs "##################################################################";
	done

	echo "Join file in one mpg..." ; writelogs "Join file in one mpg...";
	cat `echo "$joinfile"` > $joinname;
	
	echo "Convert .mpg to .mp4...";
	enctomp4
	writelogs " done."
	writelogs "===>>> Ending encX264.sh at `date`";
	echo " done."
}

main
