Getting FLV duration with php

duration, flv, php

Solution

Here is my code to grab a frame and generate the image from the video...

// get the duration and a random place within that
$cmd = "ffmpeg -i " . $videoPath . " 2>&1";
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
   $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
   $second = rand(1, ($total - 1));
}
exec($cmd);

// get the screenshot
exec("ffmpeg -i " . $videoPath . " -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg " . $imageOutput . " 2>&1");

$second variable is random number between 0 and total duration. and the second exec() is to create a image file from selected frame.

$imageOutput is absolute path location to the generated image. eg: /home/ariawan/image-generated.jpg

Problem

I have an flv file uploaded to a server. I would like to display its duration in the following format "minutes:seconds". Can anybody help me with this ? Thank you

Original source