Question

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

Was it helpful?

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

OTHER TIPS

There is also a FFMPEG PHP extension ie. apt-get install php5-ffmpeg then

$movie = new ffmepg_movie("path/to/movie.flv");
$duration_in_seconds = $movie->getDuration();

This has worked for me previously. The extension is good for retrieving meta-data and testing if an uploaded file is an FLV, etc.

I´d use the getID3 PHP library, written in plain old PHP without any dependencies.

It not only gives you the duration of the .flv movie in seconds, but converts it to minute:seconds format already. Here is a code sample with v. 1.7.9 (latest stable version of getid3):

<?php

// getId3 library uses deprecated eregi_* functions 
// which generate errors under PHP 5.3 - so I excluded them
error_reporting(E_ALL ^ E_DEPRECATED);

// just for debugging/sample
header('Content-Type: text/plain');

// include the getid3 base class in order to use the lib
require_once('./lib/getid3.php');

// path to your .flv file
$filename = './sample.flv';

$getID3 = new getID3();
$fileInfo = $getID3->analyze($filename);

// echoes something like 127.8743
print 'Playtime in seconds: ' . $fileInfo['playtime_seconds']; 

print chr(10);

// echoes something like: 2:07
print 'Playtime in minute:seconds format: ' . $fileInfo['playtime_string'];

i am using php and ffmpeg to get duration of the video.

    $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];
    }
    exec($cmd);

print_r() the $time variable to see. make sure ffmpeg installed on your machine.. hope this will help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top