#!/bin/sh

#version 1.1
#Changes 1-1.1
#Added aspect ratio.  There will be black bars on top and bottom to keep the resolution 
#at 320x240 to avoid software scaling on the Z.
#Fixed AV_DELAY

#This script should convert any mplayer playable video file (audio + video) to ogm format
#playable on the sharp zaurus 5600, 760, 860 using mplayer with Tremor codec support. 
#You need mencoder and mplayer of course, plus oggenc and ogmmerge.

#Variable assignment
ENCODE_RATE=44100
AUDIO_QUALITY=4
AV_DELAY=200

X_RESOLUTION=320
Y_RESOLUTION=240
VIDEO_BITRATE=400
OUTPUT_FPS=16

#Check the command line arguments
if [ -z "$1" ]; then
              echo "Usage is 'createogm  <inputfile> <outputfile>' "
              exit
              fi

if [ -z "$2" ]; then
              echo "Usage is 'createogm  <inputfile> <outputfile>' "
              exit
              fi

if [ -f "$2" ]; then
              echo "The file $2 exists. Aborting "
              exit
              fi

#Check the source audio rate
AUDIO_RATE=`mplayer -vo null -ao null -frames 0 -identify "$1" 2>/dev/null | grep "ID_AUDIO_RATE" | sed "s/ID_AUDIO_RATE=//g"`

if [ -z $AUDIO_RATE  ]; then
              echo "Cannot detect audio in source file. Aborting"
              exit
fi

#Delete log files from previous encodes
if [ -f divx2pass.log ]; then
rm divx2pass.log
fi
if [ -f audiotmp.wav ]; then
rm audiotmp.wav
fi
if [ -f videotmp.avi ]; then
rm videotmp.avi
fi
if [ -f audiotmp.ogg ]; then
rm audiotmp.ogg
fi

#Encode the video
#First Pass
nice -19 mencoder \
-nosound \
-ovc lavc \
-lavcopts threads=2\
:vcodec=mpeg4\
:vqmin=2\
:vqmax=31\
:vmax_b_frames=2\
:vme=4\
:mbd=2\
:v4mv\
:keyint=${OUTPUT_FPS}0\
:vb_strategy=1\
:vpass=1\
:vbitrate=$VIDEO_BITRATE\
:vqcomp=0.6\
:vlelim=-4\
:vcelim=7\
:vstrict=-1\
:vfdct=2\
:idct=7\
:lumi_mask=0.05\
:dark_mask=0.01\
:precmp=0\
:cmp=3\
:subcmp=3\
:predia=1\
:dia=2\
:trell\
:cbp\
:mv0\
:preme=2\
 \
-vf pp=fd/hb/vb/dr\
,scale=$X_RESOLUTION:-2\
,hqdn3d\
 \
-ofps $OUTPUT_FPS \
-zoom \
-sws 2 \
-o /dev/null "$1" 2> /dev/null
#Second Pass
# vb_strategy is pass one only
nice -19 mencoder \
-nosound \
-ovc lavc \
-lavcopts threads=2\
:vcodec=mpeg4\
:vqmin=2\
:vqmax=31\
:vmax_b_frames=2\
:vme=4\
:mbd=2\
:v4mv\
:keyint=${OUTPUT_FPS}0\
:vpass=2\
:vbitrate=$VIDEO_BITRATE\
:vqcomp=0.6\
:vlelim=-4\
:vcelim=7\
:vstrict=-1\
:vfdct=2\
:idct=7\
:lumi_mask=0.05\
:dark_mask=0.01\
:precmp=0\
:cmp=3\
:subcmp=3\
:predia=1\
:dia=2\
:trell\
:cbp\
:mv0\
:preme=2\
 \
-vf pp=fd/hb/vb/dr\
,scale=$X_RESOLUTION:-2\
,hqdn3d\
 \
-ofps $OUTPUT_FPS \
-zoom \
-sws 2 \
-o ./videotmp.avi "$1" 2> /dev/null

#Extract the audio
nice -n 19 mplayer -vc dummy -vo null -ao pcm -aofile ./audiotmp.wav "$1" 2>/dev/null

#Convert to ogg
if [ $AUDIO_RATE -eq $ENCODE_RATE ]; then
   nice -n 19 oggenc -r -C 2 -R $AUDIO_RATE -B 16 -q $AUDIO_QUALITY -o audiotmp.ogg  audiotmp.wav
else
   nice -n 19 oggenc -r -C 2 -R $AUDIO_RATE -B 16 -q $AUDIO_QUALITY --resample $ENCODE_RATE -o audiotmp.ogg  audiotmp.wav
fi

#Multiplex the audio and video streams
nice -n 19 ogmmerge -o $2 videotmp.avi --sync $AV_DELAY audiotmp.ogg

#Delete the temporary files
rm -f audiotmp.ogg
rm -f audiotmp.wav
rm -f videotmp.avi
rm -f divx2pass.log

