11 May, 2010

Get total length of Video in PHP


//Total Length of video can be userful at many cases. Following code sample shows
// how to get the total length or duration of any video file

//get the filename of video
$uploadVideoFile = $_FILE["fleVideo"]["name"];

//turn on the output buffering
ob_start();

//execute the the ffmpef command, where -i is the input
//the 2>&1 is done to redirect standard error (stderr) to standard output(stdout)
system("/path/to/ffmpeg -i $uploadVideoFile 2>&1");
//now get the contents
$totalDuration = ob_get_contents();

//clear the output buffer and turn off the output buffering done by ob_start()
ob_end_clean();

//perform a regular expression match
//check for the pattern Duration: ... in $duration and return matches
preg_match('/Duration: (.*?),/', $duration, $matches);

//found the matches
//this is the total length of video
$totalLength = $matches[1];
echo "Total Length of the video is:".$totalLength;
?>

1 comment:

Digital Knowledge said...

You need to define that this function need FFMPEG to be installed first :)

and if u have any tutz about ffmpeg installation on centos 5.5 64 bit it would be nice :)

regards