Perl script to get video details

This is a command line Perl script I made a while back to simply show details about a specified video. It is tested on Ubuntu.

You can modify it for your specific requirements or grab some code for another use.

#!/usr/bin/perl -w
use strict;
use warnings;
 
use IPC::Open3;
 
# example
my $filename  = $ARGV[0];
my %videoInfo = videoInfo($filename);
print "duration: " . $videoInfo{'duration'} . "\n";
print "durationsecs: " . $videoInfo{'durationsecs'} . "\n";
print "bitrate: " . $videoInfo{'bitrate'} . "\n";
print "vcodec: " . $videoInfo{'vcodec'} . "\n";
print "vformat: " . $videoInfo{'vformat'} . "\n";
print "acodec: " . $videoInfo{'acodec'} . "\n";
print "asamplerate: " . $videoInfo{'asamplerate'} . "\n";
print "achannels: " . $videoInfo{'achannels'} . "\n";
 
#
# returns media information in a hash
sub videoInfo {
	# ffmpeg command
	my $ffmpeg = '/usr/local/bin/ffmpeg';
 
	my %finfo = (
				  'duration'     => "00:00:00.00",
				  'durationsecs' => "0",
				  'bitrate'      => "0",
				  'vcodec'       => "",
				  'vformat'      => "",
				  'acodec'       => "",
				  'asamplerate'   => "0",
				  'achannels'       => "0", 
	);
 
	my $file = shift;
 
	# escaping characters
	$file =~ s/(\W)/\\$1/g;
 
	open3( "/dev/null", \*ERPH, "$ffmpeg -i $file" ) or die "can't run $ffmpeg\n";
	my @res = ;
 
	# parse ffmpeg output
	foreach (@res) {
        print;
 
		# duration
		if (m!Duration: ([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])!) {
			$finfo{'duration'} = $1;
		}
 
		# bitrate
		if (m!bitrate: (\d*) kb/s!) {
			$finfo{'bitrate'} = $1;
		}
 
		# vcodec and vformat
		if (/Video: (\w*), (\w*),/) {
			$finfo{'vcodec'}  = $1;
			$finfo{'vformat'} = $2;
		}
 
        # Stream #0.1(und): Audio: aac, 48000 Hz, 1 channels, s16, 64 kb/s
 
		# acodec, samplerate, stereo and audiorate
		if (m!Audio: (\w*), (\d*) Hz, (\d*)!) {
			$finfo{'acodec'}     = $1;
			$finfo{'asamplerate'} = $2;
			$finfo{'achannels'}     = $3;
		}
	}
 
	my $tenths  = substr( $finfo{'duration'}, 9, 2 );
	my $seconds = substr( $finfo{'duration'}, 6, 2 );
	my $minutes = substr( $finfo{'duration'}, 3, 2 );
	my $hours   = substr( $finfo{'duration'}, 0, 2 );
	$finfo{'durationsecs'} = ( $tenths * .01 ) + $seconds + ( $minutes * 60 ) + ( $hours * 360 );
 
	return %finfo;
}
Be Sociable, Share!

4 thoughts on “Perl script to get video details

  1. Hi I tried your code in ubuntu and its working great.
    But how can I make this work in windows ? Please help me because I am trying this from last 1 month.
    Please help me to execute this code in windows machine.
    I changed the path of ffmpeg.exe, Also I removed other information stuffs since I wanted only duration, bit-rate and the path.

    I Also have to generate all the above 3 data for each file and insert these data into mysql table.
    Is it possible to do ? Please help me in doing so.

    Thanks.

  2. Glad to hear it is work for you in Ubuntu.

    I don’t use Windows. Get a Perl script to run on Windows. Get FFMPEG to run on Windows. See if the output of FFMPEG is the same as on Ubuntu.

    Modify the path to FFMPEG in the script.

    It is quite possible to do this for a bunch of files and save the results to MySQL.

    It is beyond the scope of replying to my blog entry. Learn to use Perl to INSERT rows into a MySQL table. Use the videoInfo() function in a script to get the data and insert it into your table.

    Good luck with your development project! I’m sure you can learn to do this.

    Cheers, Andrew

  3. I need the perl script which stream video from server to any mobile device which contains that particular format please mail me a sample code for streaming a video on PC please email to prasad.gummadavelli@gmail.com.what ever you have code for streaming please share with me. Thanks, prasad

Comments are closed.