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

Forums

/

How To Get Video Duration With FFMPEG and PHP

31 replies [Last post]
Reply

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)));
}

Reply

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

Reply

sadhu

Reply

Nice code it runnung fine.
Thanks

Reply

What "ob_start();" do ?

Reply
Reply

muchas gracias!!

Reply

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];
?>

Reply

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

Reply

<?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;

?>

Reply

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

Reply

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

Reply

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

Reply

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

Reply

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

Reply

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.

Reply

 
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.

Reply

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

Reply

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.

Reply

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.

Reply

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];

Reply

here's an example that works on windows well and returns the seconds instead of the 00:00:00 format:

<?php
$videofile
="c:\\webs\\band\\bunny.flv";
ob_start();
passthru("C:\\ffmpeg.rev12665\\ffmpeg.exe -i \"{$videofile}\" 2>&1");
$duration = ob_get_contents();
ob_end_clean();
$search='/Duration: (.*?),/';
$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);
$duration = $matches[1][0];
//echo $duration."<BR>";
$timearray = explode(":", $duration);
$min = 60*$timearray[1];
$sec = $timearray[2];
$ttime = $min+$sec;
if(
$dot = strpos($ttime, ".")){
  
$ttime = substr($ttime, 0, 3);
}  
echo
$ttime;

?>
Reply

i want get duration video a use ffmpeg java ?
who can help me... please?

Reply
$percent = 100;
$text = "Seems stream 0 codec frame rate differs from container frame rate: 50.00 (50/1) -> 25.00 (25/1)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/opt/lampp/htdocs/split_video/test.mp4':
  Duration: 01:44:13.68, start: 0.000000, bitrate: 718 kb/s
    Stream #0.0(und): Video: h264, yuv420p, 512x384, 25 tbr, 25 tbn, 50 tbc
    Stream #0.1(und): Audio: aac, 44100 Hz, mono, s16
At least one output file must be specified";

$preg = '|Duration: (.*),|siU';
preg_match_all($preg, $text, $matches, PREG_PATTERN_ORDER);
if(isset($matches[1][0])){
$duration = trim($matches[1][0]);
$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)));
print $time;
}

Reply

hello, my friends

I am new to PHP. I use xampp and Window XP. I want to know how to convert video files to flv file type using ffmpeg. I find this information everywhere. But I can not find. Now here I find. Your
informations are so cool. So can you guide me how to convert movies to flv file type using ffmpeg on XAMPP.

Reply

You should really go to the FFmpeg site for that info. It's a bit too serverside/technical for the kind of support we provide here.

Reply

Just in case anyone was wondering .. you can also get the video width and height if you wanted to set the video to its original width and height or work out if its widescreen so you can set the video settings to show widescreen. (I am new to PHP by the way)

$videofile="/home/sim2k/Tezco/Vids/802000129.MTS";
ob_start();
passthru("ffmpeg -i \"{$videofile}\" 2>&1");
$Size = ob_get_contents();
$full = ob_get_contents();
ob_end_clean();
$search='/Video: (.*?), (.*?), (.*?),/';
$Size=preg_match($search, $Size, $matches, PREG_OFFSET_CAPTURE, 0);
$Size = $matches[3][0];
echo $Size."<BR>";

if (preg_match("/16:9/", $Size, $matches)) {
  echo "This is wide screen <br />";
  echo $matches[0]."<br><br>";
}

$search='/(.*?) (.*?) (.*?) (.*?) (.*?)/';
$Size=preg_match($search, $Size, $matches, PREG_OFFSET_CAPTURE, 0);
$Size = $matches[1][0];
echo $Size."<BR>";
$Size = explode("x", $Size);
echo $Size[0]." - Width<br>";
echo $Size[1]." - Height<br><br>";
echo $full."<br><br>File stream Done";

Reply

<?php
$videofile="http://localhost/spadesocial/code/ch_videos/2843business_video.flv";

ob_start();

passthru("/usr/bin/ffmpeg -i \"{$videofile}\" 2>&1");

$duration = ob_get_contents();

print_r($duration);
ob_end_clean();

$search='/Duration: (.*?),/';
print_r($search);
$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);

print_r($duration);

//TEST ECHO
//echo $matches[1][0];
?>
please confirm me

Reply

<?php
$videofile="http://localhost/spadesocial/code/ch_videos/2843business_video.flv";

ob_start();

passthru("/usr/bin/ffmpeg -i \"{$videofile}\" 2>&1");

$duration = ob_get_contents();

print_r($duration);
ob_end_clean();

$search='/Duration: (.*?),/';
print_r($search);
$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);

print_r($duration);

//TEST ECHO
//echo $matches[1][0];
?>
please confirm me

Reply

Confirm what?

Reply

 
This seems to work fine for me. This script is most useful when you can call it with the filename of a video file and it returns the duration.

 

<?php

// call with:  http://www.mydomain.com/path/duration.php?videofile=filename.ext

$videofile = isset($_GET["videofile"]) ? strval($_GET["videofile"]) : "default";

//...external access
$videofile = "http://localhost/path/" . $videofile;

//...internal access
//$videofile = "/path/" . $videofile;

ob_start();

passthru("\"/path/ffmpeg\" -i \"{$videofile}\" 2>&1");

$duration = ob_get_contents();

ob_end_clean();

$search = "/Duration: (.*?),/";

$duration = preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);

//return $matches[1][0];

echo "Duration of " . $videofile . ": " . $matches[1][0];

?>
 

Reply

If you have full access to bash app you could use the following script:
$ /usr/bin/ffmpeg -i /path/to/file.flv 2>&1 |grep Duration |awk '{print $2}' |awk -F: '{print ($1 * 3600) + ($2 * 60 ) + $3}'

This command will return the time in seconds with milli precision.
If you like, you could replace the math formula and do what you want (eg return the time expressed in hh:mm:ss)

Cheers

Post new comment

  • Allowed HTML tags: <code> <blockquote> <em> <strong> <strike> <ul> <li> <ol>
  • You may post code using <code>...</code> .
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options