#!/bin/bash

#   FILE: mpegize -- 
# AUTHOR: W. Michael Petullo <mike@flyn.org>
#   DATE: 21 October 2001
#
# Copyright (C) 2001 W. Michael Petullo <mike@flyn.org>
# All rights reserved.
#
# 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

MPEG_AUDIO=`mktemp -u /tmp/sound.XXXXXX`
mkfifo $MPEG_AUDIO
MPEG_VIDEO=`mktemp -u /tmp/video.XXXXXX`
mkfifo $MPEG_VIDEO
trap 'rm -f $MPEG_AUDIO $MPEG_VIDEO' SIGHUP SIGINT SIGTERM EXIT

IN=foo.mov

USAGE="-i <input> [OPTION]...

  -h, -?   print this message"

while :;
	do case "$1" in
		-h | "-?" )	
			echo -e usage: ${0##*/} "$USAGE" >&2
			exit 1 ;;
		-i )
			IN=$2
			shift ;;
		-?* )
			echo "${0##*/}: unrecognised option: $1" >&2
			exit 1 ;;
		* )
			break ;;
	esac
	shift
done

if [ ! -f $IN ]; then
	echo "${0##*/}: $IN does not exist" >&2
	exit 1
fi

OUT=${IN%.*}.mpg
lav2yuv +n -n 2 -d 3 $IN | mpeg2enc -F 3 -s -b 2500 -V 500 -q 16 -g 9 -G 18 -f 3 -o $MPEG_VIDEO
lav2wav +n $IN | mp2enc -V -o $MPEG_AUDIO
mplex -f 3 -b 500 $MPEG_AUDIO $MPEG_VIDEO -o $OUT
