#!/bin/sh

help(){
    cat <<END
Usage: $0 [options] <filename>.[mpg|m2t|m2ts|mts]
Options:
END
    exit 0
}

while getopts h name "$@"
do
    case $name in
*)
    help ;;
    esac
done
let shiftind=$OPTIND-1
shift $shiftind

if test "$#" != "1"
then
    help
fi
case $1 in
*.mts)
    input="$1"
    base=`echo $input | sed "s/\.mts$//"` ;;
*.m2ts)
    input="$1"
    base=`echo $input | sed "s/\.m2ts$//"` ;;
*.m2t)
    echo "Warning: sound/video synchonization may be lost if this m2t file"
    echo "was produced by dvgrab.  If this is the case, please remultiplex"
    echo "with Project X before proceding."
    echo ""
    input="$1"
    base=`echo $input | sed "s/\.m2t$//"` ;;
*.mpg)
    input="$1"
    base=`echo $input | sed "s/\.mpg$//"` ;;
*)
    help ;;
esac

echo 24PF $input to ${base}-24.mpg video...

ffmpeg -i $input -ab 384000 -f mp2 -y $base.m2a
ffmpeg -i $input -r 30000/1001 -f yuv4mpegpipe -pix_fmt yuv420p -y /dev/stdout |
    yuvkineco -F 1 |
    mpeg2enc --no-constraints -f3 -nn -a3 -Ktmpgenc -lh \
        -b24000 -V488 -r32 -G18 -D10 -q3 -s -o $base.m2v
mplex -f3 $base.m2a $base.m2v -r28000 -b800 -o $base-24.mpg
