#!/bin/bash
# 2ogg.sh 0.01i - converts video files to Ogg format (Theora/Vorbis)
# Written by Timo Jyrinki <timo.jyrinki (at) iki.fi>
# Modifications by Joost Rohde
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    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

# Version history (dd/mm/yyyy):
# 07/05/2007: 0.01i - Without -speed 100 in video playback it was not possible 
#                     to encode faster than real time. Doh!
# 24/02/2007: 0.01h - Blah, add something to -vf for now. >0.01g needs that.
# 05/02/2007: 0.01g - Do scaling after the other filters, update -af comments
# 04/05/2005: 0.01f - Fixed to work with MPlayer 1.0pre7
# 17/04/2005: 0.01e - Fixed bug with video filters not working (sorry)
# 24/03/2005: 0.01d - Modifications by Joost Rohde: scaling
#                     Also, defaults changed again.
# 18/09/2004: 0.01c - Default video quality 5 (like it better)
# 17/09/2004: 0.01b - Add user settable parameters (to the script)
#                     ...just some things I've needed.
#                     Perhaps some other modifications (don't remember)
# 23/07/2004: 0.01a - Add detection of FAT/NTFS partitions.
#                     Default video quality 3, default audio quality 2.
#                     (thanks to Nilesh Bansal for suggestions)
#                     Add detection of mplayer and encoder_example.
#                     More verbose output.
# 22/07/2004: 0.01 - initial release

# User settable parameters


# Extra parameters to video processing, without -vf prefix
# eg. rotate=2 -> rotate picture left 90'
#     eq2=1.5,rotate=1 -> adjust gamma and rotate picture right 90'
#     hqdn3d -> reduce noise (increase compressibility)

v_extra="eq2=1"
# current version introduced a bug that you have to have something here... :)

# Extra parameters to audio processing
# eg. -af format=s16le -> convert audio to 16-bit (required by encoder)
#     -srate 44100 -> convert sample rate to 44.1kHz
# (note that you need that "-af")

a_extra=""

# Extra parameters to the encoder
# eg. -f 1000000 -F 66667 to fix some .wmv-problems (1000000/66667 -> 15fps)

e_extra=""

# -----------------------------------------------------

# FAT/NTFS detection
if [ \( `df . --exclude-type=ntfs | wc -l` -lt 2 \) -o \( `df . --exclude-type=vfat | wc -l` -lt 2 \) ]; then
  echo Sorry, mkfifo \(which is used\) doesn\'t work on FAT/NTFS partitions.
  echo \(if you get this message by mistake, you can remove the \"FAT/NTFS detection\"
  echo from the script code\)
  echo 
  exit
fi

# Check for MPlayer
mplayer > /dev/null 2>&1
if [ $? -ne 0 ]; then
  echo You need mplayer! I didn\'t find it, sorry...
  echo 
  exit
fi

# Check for encoder_example
theora_encoder_example > /dev/null 2>&1
if [ $? -ne 1 ]; then
  echo You need theora_encoder_example \(from libtheora-bin\)! Not found, sorry...
  echo 
  exit
fi

# Parameter parsing

if [ $# -le 0 ]; then
  echo 2ogg.sh 0.01i - converts video files to Ogg format \(Theora/Vorbis\)
  echo
  echo Usage: $0 videofile.ext outputfilename.ogg [vid_qty] [aud_qty] [scale]
  echo
  echo \* vid_qty: Video quality, 0-10 \(default: 6\)
  echo \* aud_qty: Audio quality, 0-10 \(default: 3\)
  echo \* scale: Scale in x direction, 0-3000 \(default: keep scale\)
  echo
  exit
fi

echo 2ogg.sh 0.01i - converts video files to Ogg format \(Theora/Vorbis\)
echo

if [ ! -f "$1" ]; then
  echo The file \'$1\' does not exist. Run ./2ogg.sh without parameters for usage.
  echo 
  exit
fi

if [ ! "$2" ]; then
  echo Please, give the name of the output filename, too!
  echo 
  exit
fi

if [ -f "$2" ]; then
  echo The output filename \'$2\' already exists. I don\'t want to overwrite it, sorry.
  echo
  exit
fi

# Parsing of $3
if [ $3 ]; then

numeric=1
case "$3" in
''|*[!0-9]*) numeric=0;;
esac

if [ $numeric -eq 0 ]; then
  echo Please use an integer between 0 and 10 for video quality. Thank you.
  echo 
  exit
fi

if [ \( $3 -lt 0 \) -o \( $3 -gt 10 \) ]; then
  echo Please use an integer between 0 and 10 for video quality. Thank you.
  echo 
  exit
fi

fi

# Parsing of $4
if [ $4 ]; then

numeric=1
case "$4" in
''|*[!0-9]*) numeric=0;;
esac

if [ $numeric -eq 0 ]; then
  echo Please use an integer between 0 and 10 for audio quality. Thank you.
  echo
  exit
fi

if [ \( $4 -lt 0 \) -o \( $4 -gt 10 \) ]; then
  echo Please use an integer between 0 and 10 for audio quality. Thank you.
  echo
  exit
fi

fi

# Parsing of $5
if [ -z $5 ]; then
	scalex=-1 # -1 = original
  	else
	numeric=1
	case "$5" in
	''|*[!0-9]*) numeric=0;;
	esac
	if [ $numeric -eq 0 ]; then
	  echo Please use an integer between 0 and 3000 for Scale in x direction. Thank you.
	  echo
	  exit
	fi
	if [ \( $5 -lt 0 \) -o \( $5 -gt 3000 \) ]; then
	  echo Please use an integer between 0 and 3000 for Scale in x direction. Thank you.
	  echo
	  exit
	fi
	scalex="$5"
fi

# Options
input_filename="$1"
output_filename="$2"
if [ $3 ]; then
  videoq=$3
else
  videoq=6
fi

if [ $4 ]; then
  audioq=$4
else
  audioq=3
fi

# Encoding
rm -f stream.yuv ; mkfifo -m 660 stream.yuv
rm -f stream.wav ; mkfifo -m 660 stream.wav

mplayer -quiet -ao pcm:file=stream.wav -vo null -vc null ${a_extra} "${input_filename}" &
mplayer -quiet -vo yuv4mpeg -ao null -ac null -nosound -vf ${v_extra},scale=${scalex}:-2 "${input_filename}" -speed 100 &

echo Input filename: ${input_filename}
echo Output filename: ${output_filename}
echo Video quality: ${videoq}
echo Audio quality: ${audioq}
echo 

theora_encoder_example -v ${videoq} -a ${audioq} ${e_extra} stream.wav stream.yuv > "${output_filename}"

rm -f stream.yuv ; rm -f stream.wav

