#!/bin/sh

## 
## makempg.sh
##
## Create DVD-compliant MPEG-2 files from MythTV NUV files created by
## Hauppauge PVR-xxx hardware MPEG-2 encoders
##
## Copyright (C) 2005, Mike Frisch <mfrisch@isurfer.ca>
##
## !!! USE AT YOUR OWN RISK !!!
##
## usage: ./makempg.sh <source NUV file> [output file]
##
## If the output file name is not specified, an MPG file is created with
## the same file name as the source NUV file.
##
## Output is to the current directory.
##
## TODO:
##
##   * better error checking
##   * more flexibility
##   * automatically create ISO file
##   * convert multiple mpg files in one pass
##

NUV_FILENAME=$1
OUT_FILENAME=$2

OUTDIR=.

if [ -z "$NUV_FILENAME" ]
then
	exit
fi

if [ -z "$OUT_FILENAME" ]
then
	OUT_FILENAME=`basename $NUV_FILENAME .nuv`.mpg
fi

echo Using source file $NUV_FILENAME
echo Output file: $OUTDIR/$OUT_FILENAME

PX_BASE_FILENAME=`basename $NUV_FILENAME .nuv`

# Split 'er up
projectx $NUV_FILENAME -out $OUTDIR

# Re-multiplex
#
mplex -f 8 $PX_BASE_FILENAME.m2v $PX_BASE_FILENAME.mp2 -o $OUT_FILENAME

# Cleanup
#
M2V=m2v
MP2=mp2
LOG_SUFFIX=_log.txt

MPEG2_VIDEO_FILENAME=$PX_BASE_FILENAME.$M2V
MPEG2_AUDIO_FILENAME=$PX_BASE_FILENAME.$MP2
PX_LOG_FILENAME=$PX_BASE_FILENAME$LOG_SUFFIX

rm $MPEG2_VIDEO_FILENAME $MPEG2_AUDIO_FILENAME $PX_LOG_FILENAME
