#!/bin/bash
#
#   mpgprobe - check MPEG stream for video and audio characteristics
#
#   Copyright (C) 2004-2008 W. Wershofen <itconsult at wershofen.de>
#   Copyright (C) 2009-2011 Joo Martin <joomart2009 at users.sf.net>
#
#   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 3 of the License, or
#   (at your option) any later version.
#
#   This package 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, see <http://www.gnu.org/licenses/>.
#
#   2004-01-26  Wolfgang Wershofen
#               * initial version
#   2004-10-29  Stefan Becker
#               * recognize pcm und dts audio tracks
#   2008-12-12  Joo Martin
#               * update for newer ffmpeg ('fps' goes to 'tb')
#   2009-02-25  Joo Martin
#               * update for newer ffmpeg (more tb variables)
#   2009-09-10  Joo Martin
#               * change reading audio codec
#
# -------------------------------------------------------------------------

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
#
rcfile="@DW_RC_FILE@"
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 ffmpeg on given mpeg file
#
# example of ffmpeg 2007 output:
#Stream #0.0[0x1e0]: Video: mpeg2video, yuv420p, 720x576 [PAR 16:15 DAR 4:3], 6400 kb/s, 25.00 fps(r)
# example of ffmpeg 2008 output:
#Stream #0.0[0x1e0]: Video: mpeg2video, yuv420p, 720x576 [PAR 16:15 DAR 4:3], 6400 kb/s, 25.00 tb(r)
# example of ffmpeg 2009 output:
#Stream #0.0[0x1e0]: Video: mpeg2video, yuv420p, 720x576 [PAR 16:15 DAR 4:3], 9500 kb/s, 25 tbr, 90k tbn, 25 tbc

vstrfull="$(LANG=C ffmpeg -i "$@" 2>&1 |\
	grep 'Stream #' | grep 'Video:' |\
	sed -e 's#\ fps#\ tb#' | sed -e 's#\ tb(r)# tbr#')"
vstring="$(echo "$vstrfull" | \
	sed -e 's#^.*\ \([0-9]\+x[0-9]\+\).*DAR\ \([0-9]\+:[0-9]\+\).*\ \([0-9]\+\.\?[0-9]\+\)\ tbr.*$#\1 \2 \3#g')"
echo "Video:${vstring}"

#
# Extract Audio Attributes (possible results are 'ac3' and 'mp2' for dvd standard)
#
echo -n "Audio:"
for i in $(LANG=C ffmpeg -i "$@" 2>&1 | grep 'Stream #' | grep 'Audio:' | \
		sed -e 's/liba52/ac3/g' -e 's/^.*Audio:\ //' | cut -d',' -f1); 
do
	echo -n "$i "
done 
echo ""

exit 0
