ffmpeg calculate video duration php function

by Prakash 2013-07-01 16:35:06

The following is a php function which uses the software ffmpeg to calculate the time duration of a video :

function calculateDuration($filename){
$command = "ffmpeg -i '{$filename}' 2>&1";
ob_start();
passthru($command);
$content_grabbed = ob_get_contents();
ob_end_clean();
$search = '/Duration: (.*?)[.]/';
$duration = preg_match($search, $content_grabbed, $matches, PREG_OFFSET_CAPTURE);
$duration = $matches[1][0];
list($hours, $mins, $secs) = explode(':', $duration);
if($mins == '' && $secs == ''){
return false;
}
$hours = intval($hours);
if ($hours != 0){
$dur = $hours . ":" . $mins . ":" . $secs;
}
else {
$mins = intval($mins);
$dur = $mins . ":" . $secs;
}
return $dur;
}


Use the function as follows :


$filename = 'input.flv';
echo "duration is ".calculateDuration($filename);
963
like
0
dislike
0
mail
flag

You must LOGIN to add comments