The following is two methods to get the duration either as a colon separated value, or in seconds, of a given media file using FFmpeg.
/** * Get duration of media file from ffmpeg * @param $file * @return bool|string */ function getDuration($file){ $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; } return $duration[1].":".$duration[2].":".$duration[3]; }
/** * Get duration in seconds of media file from ffmpeg * @param $file * @return bool|string */ function getDurationSeconds($file){ $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]; return $seconds + ($minutes*60) + ($hours*60*60); }
the argument for the function, what data type would it be?
It should be a string which is the absolute path to the file that you want to get the length of.
Cool, thanks!
Thanks a lot.