tags:

views:

86

answers:

5

Hello,

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

+2  A: 

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.

ariawan
thanks man I will try it. I know that ffmpeg is installed.
ciprian
I'm not sure how to do this. I created a variable $videoPath = 'skittles.flv'; and I called the $time variable with print_r (time); The result is Array()
ciprian
try ffmpeg -i /path/path/videofile.flv. is it running?
ariawan
nope ... the file is in the same directory as php file. I created a wordpress template file. Does that matter? This is the path that I used $cmd = "ffmpeg -i /wp-tube/wptube/skittles.flv" . $videoPath . " 2>
ciprian
if the ffmpeg is running. the code should return the duration. where is the location of your ffmpeg? if it's installed in /usr/bin/ffmpeg then the $cmd should be like this.. $cmd ="/user/bin/ffmpeg -i /wp-tube/wptube/skittles.flv"
ariawan
$cmd = "/usr/local/bin/ffmpeg -i /wp-tube/wptube/skittles.flv" . $videoPath . " 2> this is what I have. For some reason it s still not working.
ciprian
I got it to work. Thank you very much! I am getting the duration with print_r ($time[0]); The duration is in this format 00:00:45. How do I only get the minutes and seconds? 00:45
ciprian
yaay! cool.. ;) you can use substr() to get the 00:45 only.. or $str = explode() with delimiter ':' and display only the $str[1] and $str[2]
ariawan
It worked in the end. I had a small issue with my sql query. Thank you very much.
ciprian
+4  A: 

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.

David Norris
then divide by 60 to get the minutes and modulo 60 to get the remaining seconds
David Norris
cool ... i will try this option also. Thank you very much!
ciprian
+2  A: 

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'];
Max
thanks man. I appreciate your help. But I ended up using the second suggestion. Any idea on how to grab the thumbnail?
ciprian
+1 for getid3 because it has no dependencies
Pekka
@ciprian how to grab the thumbnail should be a separate question.
Pekka
@Pekka Ok ... sorry I just figured that this information should be in the same place.
ciprian
+1  A: 

Thank you for all your help. I would also like to generate a thumbnail from the flv file. I am currently trying to accomplish this using this code. But I seem to be doing something wrong.

$cmd = "ffmpeg -i /home/web1/wpmu.site.com/htdocs/wp-content/blogs.dir/7/files/2010/10/4973_3.flv -f image2 -vframes 1 -ss 00:00:01 -y -s 100x100 /home/web1/wpmu.site.com/htdocs/wp-content/blogs.dir/7/files/2010/10/4973_3.png";

exec($cmd, $output, $return);

I included it on a test.php page. It should run just by refreshing the page right? I added the path to the png file myself. What can be wrong with this ?

Thanks

ciprian
i already add another answer to your generate thumbnail question. i attached the code also, but the option i used on ffmpeg is jpeg.
ariawan
+1  A: 

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

ariawan
ariawan, thank you so much for your help man. I am able to get the image in the folder, but it is empty 0 bytes. this is what i have:
ciprian
I m not sure what happend, but the server I was working on was restarted, and everything works fine now. Thanks a lot man!
ciprian
okay.. great...
ariawan