CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Win32 Programming Help: Print file information using ffmpeg in perl: Edit Log



srijithbhandary
Novice

Apr 15, 2011, 5:05 AM


Views: 30207
Print file information using ffmpeg in perl

I am trying to print file information using ffmpeg tool in perl

Here is my code

Code
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 = 'C:\ffmpeg.exe';

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( "<C:\top.txt", ">C:\top.txt", \*ERPH,"$ffmpeg -i $file" ) or die "can't run $ffmpeg\n";
my @res = <ERPH>;

# 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;
}


I am getting the following output

Code
C:\Perl>perl listfiles.pl titanic.mov 
FFmpeg version Sherpya-r10707, Copyright (c) 2000-2007 Fabrice Bellard, et al.
libavutil version: 49.5.0
libavcodec version: 51.45.0
libavformat version: 51.14.0
built on Oct 11 2007 06:25:25, gcc: 4.2.1 [Sherpya]
titanic\.mov: no such file or directory
duration: 00:00:00.00
durationsecs: 0
bitrate: 0
vcodec:
vformat:
acodec:
asamplerate: 0
achannels: 0

If you see this , you can read "titanic\.mov: no such file or directory"

From where is it getting "\" ?
1)Anyone know how to remove it and make it work ?
2) Why is it showing 00:00:00:00 ?

please me help me guys..

Thanks


(This post was edited by srijithbhandary on Apr 15, 2011, 5:16 AM)


Edit Log:
Post edited by srijithbhandary (Novice) on Apr 15, 2011, 5:16 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives