Reputation: 352
I have FFMpeg installed and I know it's functional, but i'm trying to get the duration time from a flv video through PHP but when I use this code:
function mbmGetFLVDuration($file){
/*
* Determine video duration with ffmpeg
* ffmpeg should be installed on your server.
*/
//$time = 00:00:00.000 format
$ffmpeg = "../ffmpeg/ffmpeg";
$time = exec("$ffmpeg -i $file 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//");
$duration = explode(":",$time);
$duration_in_seconds = $duration[0]*3600 + $duration[1]*60+ round($duration[2]);
return $duration_in_seconds;
}
and:
$duration = mbmGetFLVDuration('http://www.videoaddsite.com/videos/intro.flv'); echo $duration;
I get an output of 220. THe video is 3:40. Can any help me on what i'm doing wrong, or if there's something else I can use?
Upvotes: 3
Views: 5581
Reputation: 1594
The following code works for me
$file='http://techslides.com/demos/sample-videos/small.mp4';
$dur = shell_exec("ffmpeg -i ".$file." 2>&1");
if(preg_match("/: Invalid /", $dur)){
return false;
}
preg_match("/Duration: (.{2}):(.{2}):(.{2})/", $dur, $duration);
if(!isset($duration[1])){
return false;
}
$hours = $duration[1];
$minutes = $duration[2];
$seconds = $duration[3];
echo $seconds + ($minutes*60) + ($hours*60*60);
Upvotes: 0
Reputation: 583
$ffmpeg = "../ffmpeg/ffmpeg";
$time = exec("$ffmpeg -i $file 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//");
$duration = explode(":", $time);
$duration_in_seconds = ($duration[0] * 3600) + ($duration[1] * 60) + round($duration[2]);
return $duration_in_seconds;
Upvotes: 0
Reputation: 4241
I dont see a problem. 220 seconds are 3:40.
To get minutes and seconds use this conversion:
<?php
$seconds = 220;
$minutes = $seconds/60;
$real_minutes = floor($minutes);
$real_seconds = round(($minutes-$real_minutes)*60);
?>
$real_minutes
will be 3 and $real_seconds
will be 40.
Upvotes: 3