Hi
I am trying to get a video duration and insert in into the database with the video.
But now I am working with extracting video duration using ffmpeg.
I am working on this for past 1 week.
I am able to make this work in linux.
But in windows its just not able to print the duration.
Here is the code.
use strict;
use warnings;
use IPC::Open3;
my @list = glob('*.mp3 *.mp4 *.mpg *.aac *.midi *m3u *.mpa *.dat *.avi');
foreach my $song (@list){
print "$song \n";
my $filename = $song;
my %videoInfo = videoInfo($filename);
print "duration: " . $videoInfo{'duration'}. "\n";
print "durationsecs: " . $videoInfo{'durationsecs'}. "\n\n";
}
sub videoInfo {
# ffmpeg command
my $ffmpeg = 'C:\ffmpeg.exe';
my %finfo = (
'duration' => "00:00:00.00",
'durationsecs' => "0"
);
my $file = shift;
# escaping characters
$file =~ s/(\W)/$1/g;
open3( "nul", "nul", \*ERPH, "$ffmpeg -i $file" ) or die "can't run $ffmpeg\n";
my @res = <ERPH>;
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;
}
}
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 installed ffmpeg in C:\ and given the path.
ffmpeg is working fine and is displaying bitrate, codec etc etc ( Here I have removed it since I only need Duration) But its jus not printing any duration, Its just printing 00:00:00.00.
I suspect its the search problem in the array.
Please help me in getting through this..
Here is the ouput when I run ffmpeg from command prompt
C:\>ffmpeg.exe -i cisco1.flv
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]
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cisco1.flv':
Duration: 00:08:37.0, start: 0.000000, bitrate: 421 kb/s
Stream #0.0(und): Audio: mpeg4aac, 44100 Hz, stereo
Stream #0.1(und): Video: h264, yuv420p, 960x720, 29.97 fps(r)
Must supply at least one output file
Here you can see the duration is fine.
If I add a
print;
Statement in the above code below line
foreach (@res) {
It will display the same ffmpeg output
You match with:
But the output is:
Notice that there is only one digit after the
..Try:
It will match if there is one or two digits after the
.(and you should escape.in the regular expression, otherwise it matches any single character).