Order Now AdSolution Sign Up | Login » Bits on the Run Sign Up | Login »

How To Get Video Duration With FFMPEG and PHP

20 replies [Last post]

i would like to use PHP and FFMPEG to get the DURATION of the VIDEO so i can save into the Database.. I found some examples but could not get to work

my path to my FFMPEG is

/usr/local/bin/ffmpeg

here is the example of of snippet but it looks to be local for the ffmpeg??

so much help appreciated if you can fix the php code and fix the proper path i provided above to grab the DURATION in a php file.. thanks for any help to get this properly working as i do not want to have to use ffmpeg-php

if (isset($_GET['percent']))
{
$percent = $_GET['percent'];

ob_start();
passthru("ffmpeg-9260.exe -i \"". $videofile . "\" 2>&1");
$duration = ob_get_contents();
ob_end_clean();

preg_match('/Duration: (.*?),/', $duration, $matches);
$duration = $matches[1];
$duration_array = split(':', $duration);
$duration = $duration_array[0] * 3600 + $duration_array[1] * 60 + $duration_array[2];
$time = $duration * $percent / 100;
$time = intval($time/3600) . ":" . intval(($time-(intval($time/3600)*3600))/60) . ":" . sprintf("%01.3f", ($time-(intval($time/60)*60)));
}

First, you should change the script to:passthru("ffmpeg -i \"{$videofile}\" 2>&1");

Then try ffmpeg from the command line in the directory where the script is, to confirm that it works:ffmpeg -i /path/path/videofile.flv

sadhu

Nice code it runnung fine.
Thanks

What "ob_start();" do ?

http://www.google.com/search?hl=en&q=PHP+ob_start%28%29&aq=f&oq=

muchas gracias!!

This one worked for me:

<?php
$videofile="/var/video/user_videos/partofvideo.avi";
ob_start();
passthru("/usr/bin/ffmpeg -i \"{$videofile}\" 2>&1");
$duration = ob_get_contents();
ob_end_clean();

$search='/Duration: (.*?),/';
$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);
//TEST ECHO
echo $matches[1][0];
?>

thanks as i will try to implement this into my php code..

as users were having issue ffmpeg-php..

anyone have any other experience on any other ways to the duration with any other php files.. ID3Tag, etc

<?php

$file= ".../movie.flv"

//ffmpeg-php method without the same output as mine:
//$movie = new ffmpeg_movie($file);
//echo $movie->getDuration();

ob_start();
//$whereis ffmpeg: change with /usr/local/bin/ffmpeg
passthru("/usr/local/bin/ffmpeg -i ".$file." 2>&1");
$duration = ob_get_contents();
ob_end_clean();

//the full output:
//echo $duration."<br/>";

$search='/Duration: (.*?)[.]/';
$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE);
$duration = $matches[1][0];

//i suppose that our video hasn't duration of a day+ :
list($hours, $mins, $secs) = split('[:]', $duration);

echo "Hours: ".$hours." Minutes: ".$mins." Seconds: ".$secs;

?>

hy,

when i convert any media file into flv through ffmpeg.
orignal file have metadata but the converted flv file have not complete metadata which some information lost.
such as duration.which i need.
for copy metadata input file to output file this command is used (-map_meta_data output file:input file) but this parameter does not work.
which always movie file duration show 00:00:00.
please help me.
thanks

Rana Muhammad Saleem

hy,
when i used this code
ob_start();
passthru("ffmpeg-9260.exe -i \"". $videofile . "\" 2>&1");
$duration = ob_get_contents();
ob_end_clean();

preg_match('/Duration: (.*?),/', $duration, $matches);
$duration = $matches[1];

in which movie file duration show different from the orignal file duration which is showing by any player.
please help me why this code show wrong different file duration.
i have experienced with multiple file.
please help
thanks
Rana muhammad Saleem

hay,

when i convert any media file into flv through ffmpeg.
orignal file have metadata but the converted flv file have not complete metadata which some information lost.
such as duration.which i need.
for copy metadata input file to output file this command is used (-map_meta_data output file:input file) but this parameter does not work.
which always movie file duration show 00:00:00.
please help me.
thanks

i am using this commang

exec("ffmpeg -i $fileToFlv -ar 44100 -sameq -an $fileFlv");

after conversion, size of file increase too large and duration metadata missing.
Please help me

Somebody knows how obtain the duration with vb6.0 thanks...

There's a Ruby tool that manipulates metadata; ffmpeg is a powerful tool, but it's a bit of overkill just to get the duration of an flv no?

http://rubyforge.org/frs/?group_id=1096&release_id=9694

It will even output the data as xml, very useful.

 
ffmpeg is lightining fast!

That's why it's the best tool to use for video.

Not that it's needed here, but ffmpeg will transcode at 3~4 times the framerate, making real-time transcoding very usable.

ffmpeg will also serve "chunks" of FLV video without regard to keyframes.

It's the best tool there is.

Ok so how do i parse this into the player (script) then ?

for example, my player script has :-

<?php
ob_start();
//$whereis ffmpeg: change with /usr/local/bin/ffmpeg
passthru("/usr/local/bin/ffmpeg -i ".$file." 2>&1");
$duration = ob_get_contents();
ob_end_clean();

//the full output:
//echo $duration."<br/>";

$search='/Duration: (.*?)[.]/';
$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE);
$duration = $matches[1][0];

//i suppose that our video hasn't duration of a day+ :
list($hours, $mins, $secs) = split('[:]', $duration);

echo $duration;
?>

then in the java script section (to invoke the player), i have

so.addVariable('duration','<?php echo $duration; ?>');

Since the duration string is derived from the first bit of php script it should work but it does not.

How do i get this to work ?

Thanks Rich

at the moment to get my non flv videos to work with some sort of progress bar i have to cheat with :-

so.addVariable('duration','10800');

basic say that my media files are 3 hours long.

I managed to fix my own issue

now what ever video format ffmpeg supports, on file selection the player file will then interogate the file for duration then parse this out the the jw player under $duration_in_seconds string

code -

<?php
$path = '/mnt/data/video/films/';
$filename = htmlspecialchars($_GET["file"]);
$ext=strrchr($filename, ".");
$file = $path . $filename;

ob_start();
passthru("/usr/local/bin/ffmpeg -i \"". $file . "\" 2>&1");
$duration = ob_get_contents();
ob_end_clean();

preg_match('/Duration: (.*?),/', $duration, $matches);
$duration = $matches[1];
list($hr,$m,$s) = explode(':', $duration);
$duration_in_seconds = ( (int)$hr*3600 ) + ( (int)$m*60 ) + (int)$s;

// a work around for now - need to remove when above works
//$duration_in_seconds = 10800
//}
?>

then further down the same php script i have the players code -

<script type='text/javascript'>
var so = new SWFObject('player-viral.swf','ply','640','360','9','#ffffff');
so.addParam('allowfullscreen','true');
so.addVariable('smoothing','false');
so.addVariable('deblocking','0');
so.addParam('allowscriptaccess','always');
so.addParam('wmode','opaque');
so.addVariable('frontcolor','EEEEEE');
so.addVariable('backcolor','333333');
so.addVariable('provider','video');
so.addVariable('bufferlength','20');
so.addVariable('duration','<?php echo $duration_in_seconds; ?>');
so.addVariable('stretching','exactfit');
so.addVariable('autostart','true');
so.addVariable('file','/filmstreamencoder.php?file=<?php echo $_GET["file"]; ?>&position=0');
so.write('mediaspace');
</script>

well happy i finally nailed it.

Try this to get the width and height too (there's probably a swisher way!):

ob_start();
passthru("/usr/bin/ffmpeg -i \"{$videofile}\" 2>&1");
$resolution = ob_get_contents();
ob_end_clean();
$search='/Video: (.*?),(.*?),(.*?),/';
$resolution=preg_match($search, $resolution, $durmatches, PREG_OFFSET_CAPTURE, 3);
$resolution= $durmatches[3][0];
$resolutionsplit=array();

$resolutionsplit=explode("x",$resolution);
$videowidth=$resolutionsplit[0];
$videoheight=$resolutionsplit[1];

Post new comment

  • Allowed HTML tags: <code> <blockquote> <em> <strong> <strike> <ul> <li> <ol>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic).

More information about formatting options