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

 



srijithbhandary
Novice

Apr 15, 2011, 5:05 AM

Post #1 of 8 (32501 views)
Print file information using ffmpeg in perl Can't Post

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

Post #2 of 8 (32480 views)
Re: [srijithbhandary] Print file information using ffmpeg in perl [In reply to] Can't Post

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

Post #3 of 8 (32270 views)
Re: [miller] Print file information using ffmpeg in perl [In reply to] Can't Post

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

Post #4 of 8 (32267 views)
Re: [srijithbhandary] Print file information using ffmpeg in perl [In reply to] Can't Post

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

Post #5 of 8 (32251 views)
Re: [miller] Print file information using ffmpeg in perl [In reply to] Can't Post

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

Post #6 of 8 (32219 views)
Re: [srijithbhandary] Print file information using ffmpeg in perl [In reply to] Can't Post

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

Post #7 of 8 (32181 views)
Re: [miller] Print file information using ffmpeg in perl [In reply to] Can't Post

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

Post #8 of 8 (32180 views)
Re: [miller] Print file information using ffmpeg in perl [In reply to] Can't Post

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

 
 


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

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