#!/bin/bash
#
# mpgprobe - check MPEG-Stream for Video and Audio Characteristics
#
# Version   0.01a written by Wolfgang Wershofen (mailto: itconsult at wershofen.de)
#
# See TOOLS in the dvdwizard tarball for info about needed tools and programs
#
# Anpassung 29.10.04/Stefan Becker: pcm und dts Audiotracks erkennen, 
#
usage()
{
 cat <<EOF
Usage:	`basename $0` mpeg-file
	`basename $0` -h|--help

specify the mpeg-file to be probed
EOF
exit 1
}

# ------------------------------
# Main Processing
#
#
# Is help wanted?
#
case "$1" in
  	-h|--help)
   	    usage
  	;;
esac

# We need some sub-routines from dvdwizardrc
# This file must reside in the same directory as the called script
# Should maybe go into a directory like /usr/local/share/dvdwizard
# but this would require an installation procedure - maybe in some
# later version.
#
rcfile="`dirname $0`/dvdwizardrc"
if [ -e "$rcfile" -a -r "$rcfile" ]; then
	. "$rcfile"
else
	echo "dvdwizardrc not found or is not readable. Aborting" >&2
    exit 1
fi

#
# Check for needed tools
#
check_tools

#
# Check if MPEG-File was specified and if, check if it is valid
#
if [ -z "$@" ]; then
 	echo "No input file specified. Aborting"
    echo "See $thisscript -h for more infos"
    exit 1
fi >&2
if [ ! -e "$@" ]; then
	echo "specified MPEG-file $@ not found. Aborting"
    echo "See $thisscript -h for more infos"
    exit 1
fi >&2

#
# Call tcprobe and write stdout to workfile
#
workFile="mpgprobe.tmp"
cat "$@" | tcprobe - 1>"$workFile" 2>/dev/null || error_out

#
# Extract Video attributes
#
fsize=$(grep "frame size:" $workFile | awk -F"-g " '{print $2}' | cut -d' ' -f 1)
aratio=$(grep "aspect ratio:" $workFile | awk -F": " '{print $2}' | cut -d' ' -f 1)
frate=$(grep "frame rate:" $workFile | awk -F"-f " '{print $2}' | cut -d' ' -f 1)
vstring="$fsize $aratio $frate"
echo "Video:$vstring"

#
# Extract Audio Attributes
#
astring=""
for i in $(grep "audio track:" $workFile | awk -F"-n " '{print $2}' | cut -d' ' -f1); do
	case "$i" in
   	"0x50")
      	astring="$astring mp2"
       ;;
    	"0x2000")
      	astring="$astring ac3"
       ;;
       	"0x1000f")
      	astring="$astring dts"
       ;;
    	"0x10001")
      	astring="$astring pcm"
       ;;
     	*)
      	astring="$astring ??"
       ;;
 	esac
done
echo "Audio:${astring:1}"

#
# Remove the work file
#
rm $workFile

exit 0
