LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2007, 01:23 PM   #1
Marel
Member
 
Registered: May 2005
Location: Serbia
Distribution: Debian, Ubuntu, Red Hat, Gentoo
Posts: 64

Rep: Reputation: 15
Smile 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.

Last edited by Marel; 10-10-2007 at 11:09 AM.
 
Old 08-03-2007, 09:50 PM   #2
nilleso
Member
 
Registered: Nov 2004
Location: ON, CANADA
Distribution: ubuntu, RHAS, and other unmentionables
Posts: 372

Rep: Reputation: 31
very cool, good for you for thinking to share with the LQ community!

cheers
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
One avi file not getting played my mplayer. ShaanAli Linux - Software 3 05-01-2006 09:24 PM
MPlayer cant play AVI file janesfelcs Linux - Software 9 12-20-2005 06:56 AM
MPlayer unable to find appropriate audio codec for avi file hbbtstar Linux - Software 1 05-23-2005 05:51 PM
mplayer only plays first 5 secs. amx of downloaded avi file LavaDevil94 Linux - Software 0 04-28-2004 05:49 PM
mplayer Can't play AVI file breathless Linux - Newbie 7 04-06-2004 12:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration