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



srijithbhandary
Novice

Apr 15, 2011, 5:05 AM


Views: 43555
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)


miller
User

Apr 15, 2011, 11:06 AM


Views: 43534
Re: [srijithbhandary] Print file information using ffmpeg in perl

already answered in your cross-forum post.

This line is your problem:


Code
# escaping characters  
$file =~ s/(\W)/\\$1/g;



srijithbhandary
Novice

Apr 17, 2011, 8:17 PM


Views: 43324
Re: [miller] Print file information using ffmpeg in perl

well. did u try running this in ur windows ? do you have a solution ?

Actually this is working fine for me in Linux machine. So i don't think that is the problem.( with path to ffmpeg changed and after replacing C:\top.txt with /dev/null/)

Please give me a solution as today is the dew


miller
User

Apr 17, 2011, 8:56 PM


Views: 43321
Re: [srijithbhandary] Print file information using ffmpeg in perl

You're error said:


Code
"titanic\.mov: no such file or directory"


Well, your line below is adding the \ in front of the . and making the file unfindable:


Code
$file =~ s/(\W)/\\$1/g;


- Miller


srijithbhandary
Novice

Apr 18, 2011, 12:49 AM


Views: 43305
Re: [miller] Print file information using ffmpeg in perl

thanks... I resolved it, really ironic.. ut works in linux , but not in windows..

I am able to display all the values , but not duration. Actually I wanted only the duration and not others. But its not able to print the duration.

Is there a problem with the search patter?

Is searching thing difirent in windows from linux ?


miller
User

Apr 18, 2011, 9:27 AM


Views: 43273
Re: [srijithbhandary] Print file information using ffmpeg in perl

Without seeing the data you're trying to match against, can't say.

However, you can streamline your regex by using the \d character class.


Code
if (m/Duration: (\d\d:\d\d:\d\d\.\d\d)/) {



srijithbhandary
Novice

Apr 18, 2011, 8:56 PM


Views: 43235
Re: [miller] Print file information using ffmpeg in perl

ya thanks I got it right!!!
I looked into the output of ffmpeg carefully and finally able to do it.
my mistake..


srijithbhandary
Novice

Apr 18, 2011, 8:58 PM


Views: 43234
Re: [miller] Print file information using ffmpeg in perl

I am really new to regexps in perl.. IS there any document or book where I can start learning regexp from the beginning ?