#!/usr/bin/env bash
#
#   mpgprobe 
#     - check MPEG stream for video and audio characteristics
#
#   Copyright (c) 2004-2008 W. Wershofen <itconsult at wershofen.de>
#   Copyright (c) 2010-2011 Markus Kohm <kohm at users.sf.net>
#   Copyright (c) 2009-2012 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/>.
#
# Changes:
#
#   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
#   2010-06-21  Markus Kohm
#               * renamed dvdwizardrc into dvdwizard_common
#               * moved some functions to dvdwizard_common
#               * usage of gettext features for multilanguage ui
#                 NOTE: Only -h, -v and error output should use this,
#                       because the default output will be parsed by 
#                       DVDwizard and should be machine-readable for this!
#
# -------------------------------------------------------------------------

#
# i18n
#
export TEXTDOMAIN=dvdwizard
export TEXTDOMAINDIR="@LOCALEDIR@"

# We need some sub-routines from dvdwizard_common.
# This file must be found at PATH.
#
. dvdwizard_common || {
    echo $"FATAL ERROR: could not execute common function file \`dvdwizard_common'!
You have to install \`dvdwizard_common' somewhere at PATH." >&2
    exit 1
}

usagetext=$"
Usage:	${thisscript} mpeg-file
	${thisscript} -h|--help
	${thisscript} -v|--version

specify the mpeg-file to be probed
"

# ------------------------------
# Main Processing
#

#
# Is help wanted?
#
test_versionhelp "$@"

#
# Check for needed tools
#
check_tools

#
# Check if MPEG-File was specified and if, check if it is valid
#
if [ -z "$@" ]; then
    error_help $"No input file specified."
fi
if [ ! -e "$@" ]; then
    error_help $"Specified MPEG-file \`%s' not found" "$*"
fi

#
# 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:" # DO NOT REPLACE THIS BY $"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
