Perl function that returns info about a video (uses FFMPEG)

This program contains a Perl function I wrote to extract data about a given video.

It also shows how to parse information from program output and organize it usefully.

This has been used a few times in production systems.

If you use this, please drop a comment! It would be fun to know.

Share and enjoy!

#!/usr/bin/perl -w
# get_video_info.pl
# by Andrew Ault, www.andrewault.net
# Please drop me a note it you use this.

use strict;
use warnings;

use IPC::Open3;

# example
my $filename  = "yourvideoFilenameHere.mp4";
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) {

		# 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!

5 thoughts on “Perl function that returns info about a video (uses FFMPEG)

  1. Very nice script. But I would like have the same script which runs in windows. Can you send me that script which runs in windows?

  2. Very useful, Thank you , but sometimes I find there is little problem.

    See I got an audio string : “Stream #0.0: Audio: cook, 44100 Hz, stereo, s16, 96 kb/s ” ,
    no channels !

  3. Thanks for that, it works really well.
    I used it in a script I wrote on Linux, but I’m also using it in Windows with ActivePerl and it works just fine there too. I had to tweak it a bit because the version of ffmpeg I have produces a slightly different output.

Comments are closed.