#!/bin/bash
##########################
## CHECK FOR SOME TOOLS ##
##########################
if [ -f $(which mplayer) ]; then
echo "We have $(which mplayer) good..."
else echo "We don't have $(which mplayer) binary in your PATH." && exit 1
fi
if [ -f $(which mencoder) ]; then
echo "We have $(which mencoder) good..."
else echo "We don't have $(which mencoder) binary in your PATH." && exit 1
fi
if [ -f $(which x264) ]; then
echo "We have $(which x264) good..."
else echo "We don't have $(which x264) binary in your PATH." && exit 1
fi
if [ -f $(which neroAacEnc) ]; then
echo "We have $(which neroAacEnc) good..."
else echo "We don't have $(which neroAacEnc) binary in your PATH." && exit 1
fi
#################################
## Setup some global variables ##
#################################
declare -x audiopipe="audiopipe"
declare -x videopipe="videopipe"
declare -x br="900"
declare -x fps="23.976"
declare -x basedir="$(pwd)"
declare -x res="720x480"
declare -x message0="USAGE: $0 <Bitrate> <resolution> <final mp4 name> <video to encode>..."
declare -x message1="The defaults are:<$br> <$fps> <$finalmp4> <$thevideo>"
declare -x message2="Arguments cannot be skipped. Type no args for all defaults, or all args"
declare -x vidstream="x264_tmp.mp4"
declare -x m4astream="audiostream.m4a"
declare -x finalmp4="finalmp4.mp4"
declare -x thevideo="title.vob"
################################
## Setup some DEFAULT Values ##
################################
BR=${1:-"$br"}
RES=${2:-"$res"}
FINALMP4=${3:-"$finalmp4"}
THEVIDEO=${4:-"$thevideo"}
################################
## Check the number of ARGS ##
################################
if [ $# -ne 4 ]; then
echo "$message0" && echo && echo "$message1" && echo "$message2"
fi
cd "$basedir"
# Make the pipes
mkfifo "$audiopipe" 2> /dev/null
mkfifo "$videopipe" 2> /dev/null
#Encode the audio to HC/AAC
if [ -f "$m4astream" ]; then
rm "$m4astream"
fi
rm *.bz2 2> /dev/null
neroAacEnc -ignorelength -br 48000 -he -if "$audiopipe" -of "$m4astream" 2> neroAacEnc.log & mplayer -nocorrect-pts -vo null -vc null -ao pcm:file="$audiopipe":fast "$THEVIDEO" 2> audiorip.err > audiorip.log
rm "$audiopipe"
# Join the stderr files and stdout files then compress with bzip2
cat neroAacEnc.log audiorip.err audiorip.log | bzip2 -9c > audiostream.log.bz2
rm neroAacEnc.log audiorip.err audiorip.log
#Encode the video
if [ -f "$vidstream" ]; then
rm "$vidstream"
fi
# Start Pass 1
x264 --pass 1 --threads 3 --bitrate "$BR" --fps "$fps" --ref 3 --bframes 3 --direct auto --analyse all --8x8dct --subme 7 --me umh --trellis 1 --mixed-refs -q 30 --crf 20 --output "$vidstream" "$videopipe" "$RES" &> myx264_pass1.log & mencoder -vf format=i420 -nosound -ovc raw -of rawvideo -ofps "$fps" -o "$videopipe" "$THEVIDEO" 2> mencoder_raw.err > mencoder_raw.log
x264 --pass 2 --threads 3 --bitrate "$BR" --fps "$fps" --ref 3 --bframes 3 --direct auto --analyse all --8x8dct --subme 7 --me umh --trellis 1 --mixed-refs --output "$vidstream" "$videopipe" "$RES" &> myx264_pass2.log & mencoder -vf format=i420 -nosound -ovc raw -of rawvideo -ofps "$fps" -o "$videopipe" "$THEVIDEO" 2> mencoder_raw-pass2.err > mencoder_raw-pass2.log
# Archive log files for future analysis...
cat *.log x264_2pass* *.err | bzip2 -9c > videostream.log.bz2
rm *.log x264_2pass* *.err 2> /dev/null
# Build MP4 container
MP4Box -add "$vidstream"#video "$FINALMP4" &> container_vid.log
MP4Box -add "$m4astream"#audio "$FINALMP4" &> container_audio.log
cat container* | bzip -9c > MP4Box.log.bz2
rm container*
rm "$videopipe"
if [ -f mkx264_sh.scriptoutput ]; then
cat mkx264_sh.scriptoutput | bzip2 -9c > mkx264_sh.scriptoutput.bz2 && rm mkx264_sh.scriptoutput
fi