LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script that prints AVI file info using mplayer output (https://www.linuxquestions.org/questions/programming-9/script-that-prints-avi-file-info-using-mplayer-output-574260/)

Marel 08-02-2007 12:23 PM

Script that prints AVI file info using mplayer output
 
I have made some script, so I want to share it with others. It prints some information on multimedia file entered as an argument, parsing output of "mplayer -identify". You can use any file playable by mplayer, but I have tested it with avi and mp3. I tested avi files with video Divx, Xvid, x264, WMV and audio MP3, Ogg, AC3. Here is the script (I call it vfinfo):

Code:

#!/bin/bash

if [ "$1" = "-g" ]; then
        OUT=">>/tmp/vfinfo.out"
        FILENAME="$2"
else
        FILENAME="$1"
fi

if [ ! -r "$FILENAME" -o ! -f "$FILENAME" ] ; then
        echo "$0: error accessing file '$FILENAME'"
        exit 1
fi

mplayer -identify -channels 6 -endpos 0 -vc dummy -vo null -ao pcm:file="/dev/null" "$FILENAME" &> /tmp/tmpinfo
supported=`grep file\ format\ detected /tmp/tmpinfo`
if [ -z "$supported" ] ; then
        echo "$0: file "$FILENAME" is not supported by mplayer"
        exit 2
fi

LENGTH=`grep ID_LENGTH /tmp/tmpinfo | cut -d '=' -f 2`
LENGTH=`echo $LENGTH|cut -d '.' -f 1`
SUBSTRACT_ABR=0

software=`grep " Software:" /tmp/tmpinfo | cut -d ' ' -f 3-`
VIDEO_FORMAT=`grep ID_VIDEO_FORMAT /tmp/tmpinfo | cut -d '=' -f 2`
case "$VIDEO_FORMAT" in
        "avc1" )               
                VIDEO_FORMAT="x264"
                ;;
        "DIV3" )
                VIDEO_FORMAT="DivX 3"
                ;;
        "DIV4" )
                VIDEO_FORMAT="DivX 4"
                ;;
        "DX50" )
                VIDEO_FORMAT="DivX 5"
                ;;
        "XVID" )
                VIDEO_FORMAT="XviD"
                ;;
esac

VIDEO_BITRATE=`grep ID_VIDEO_BITRATE /tmp/tmpinfo | cut -d '=' -f 2`
if [ $VIDEO_BITRATE ] ; then
        VIDEO_BITRATE=$[$VIDEO_BITRATE/1000]
fi
if [ "$VIDEO_BITRATE" = "0" ] ; then
        FILE_SIZE=`ls -l "$1" | awk '{print $5}'`
        FILE_SIZE=$[$FILE_SIZE/125]
        VIDEO_BITRATE=$[$FILE_SIZE/$LENGTH]
        SUBSTRACT_ABR=1
fi


VIDEO_FPS=`grep ID_VIDEO_FPS /tmp/tmpinfo | head -1 | cut -d '=' -f 2`
VIDEO_WIDTH=`grep ID_VIDEO_WIDTH /tmp/tmpinfo | head -1 | cut -d '=' -f 2`
VIDEO_HEIGHT=`grep ID_VIDEO_HEIGHT /tmp/tmpinfo | head -1 | cut -d '=' -f 2`

AUDIO_FORMAT=`grep ID_AUDIO_FORMAT /tmp/tmpinfo | cut -d '=' -f 2`
case "$AUDIO_FORMAT" in
        "85" )
                AUDIO_FORMAT="MP3"
                ;;
        "8192" )
                AUDIO_FORMAT="AC3"
                ;;
        "vrbs" )
                AUDIO_FORMAT="Vorbis"
                ;;
        "353" )
            AUDIO_FORMAT="WMA"
                ;;
        "28781" )
            AUDIO_FORMAT="AAC"
                ;;
esac
AUDIO_BITRATE=`grep ID_AUDIO_BITRATE /tmp/tmpinfo | head -1 | cut -d '=' -f 2`
if [ "$AUDIO_BITRATE" = "0" ] ; then
        AUDIO_BITRATE=`grep ID_AUDIO_BITRATE /tmp/tmpinfo | tail -1 | cut -d '=' -f 2`
fi
AUDIO_BITRATE=$[$AUDIO_BITRATE/1000]
if [ "$SUBSTRACT_ABR" = "1" ] ; then
        VIDEO_BITRATE=$[$VIDEO_BITRATE-$AUDIO_BITRATE]
fi

AUDIO_NCH=`grep ID_AUDIO_NCH /tmp/tmpinfo | tail -1 | cut -d '=' -f 2`
AUDIO_RATE=`grep ID_AUDIO_RATE /tmp/tmpinfo | tail -1 | cut -d '=' -f 2`

eval echo Filename: \"`basename "$FILENAME"`\" $OUT
eval echo Software: \"$software\" $OUT

if [ "$VIDEO_FORMAT" ] ; then
        eval echo Video: $VIDEO_FORMAT ${VIDEO_BITRATE}kb/s, ${VIDEO_FPS}fps, ${VIDEO_WIDTH}x${VIDEO_HEIGHT} $OUT
fi
if [ "$AUDIO_FORMAT" ] ; then
        eval echo Audio: $AUDIO_FORMAT ${AUDIO_BITRATE}kb/s, $AUDIO_NCH channels, ${AUDIO_RATE}Hz $OUT
fi

hours=$[$LENGTH/3600]
minutes=$[($LENGTH-3600*$hours)/60]
seconds=$[$LENGTH-3600*$hours-60*$minutes]
eval echo "Duration: $hours:$minutes:$seconds" $OUT

if [ "$OUT" ]; then
        if [ -x /usr/bin/Xdialog ]; then
                Xdialog --textbox /tmp/vfinfo.out 12 50
        elif [ -x /usr/bin/zenity ]; then
                zenity --text-info --filename /tmp/vfinfo.out
        elif [ -x /usr/bin/kdialog ]; then
                kdialog --textbox /tmp/vfinfo.out 12 50
        fi
        rm /tmp/vfinfo.out
fi

rm /tmp/tmpinfo

UPDATE 2007-08-03: Added checks if file exists and is supported by mplayer.
UPDATE 2007-08-04: Added support for graphical output via -g option.
UPDATE 2007-10-10: Fixed bug about error message when filename or software string contains special characters.

nilleso 08-03-2007 08:50 PM

very cool, good for you for thinking to share with the LQ community!

cheers :)


All times are GMT -5. The time now is 10:44 AM.