2

Hello everyone,

!WARNING! this function should only be use, WHEN ffmpeg PHP CANNOT be installed in the server.

I just got some free time from tedious and hectic school schedules. About two months ago, I saw a question about "how to get the duration of a given video file?". Of course, the common response to this question is to utilize ffmpeg php.

Using the ffmpeg php extension, we can pretty much grab anything from all sorts of video extensions. For example, we can implement simple codes below to get the duration of a video file..

$thisVideoFile = new ffmpeg_movie("video.ext");

echo $thisVideoFile->getDuration();

The code above is that all we need to get and display duration of the video. Pretty easy huh? "Wait a minute, I don't have any ffmpeg installed on my server, what shall I do?" one developer screamed with disperation. Well, in response to the screaming developer's question is not that hard to guess??? Of course, it can be done with pure php, and this is how I did it...

Once again, I would like to WARN everyone that this equation or formula is sometimes off by 0.001 micro seconds. This is not an ultimate solution, but rather an easy fix when there is no FFMPEG php installed on the server.

function getDuration($file){

if (file_exists($file)){
 ## open and read video file
$handle = fopen($file, "r");
## read video file size
$contents = fread($handle, filesize($file));

fclose($handle);
$make_hexa = hexdec(bin2hex(substr($contents,strlen($contents)-3)));

if (strlen($contents) > $make_hexa){

$pre_duration = hexdec(bin2hex(substr($contents,strlen($contents)-$make_hexa,3))) ;
$post_duration = $pre_duration/1000;
$timehours = $post_duration/3600;
$timeminutes =($post_duration % 3600)/60;
$timeseconds = ($post_duration % 3600) % 60;
$timehours = explode(".", $timehours);
$timeminutes = explode(".", $timeminutes);
$timeseconds = explode(".", $timeseconds);
$duration = $timehours[0]. ":" . $timeminutes[0]. ":" . $timeseconds[0];}
return $duration;

}
else {

return false;
}
}

I am not going to explain the function line by line. However, just by glancing (if you will) at the codes above, we can easily see that the function is just reading the file size of the video, and from these data, we can safely approximate its duration by treating the file as string( the term string is USE for explanation purposes ONLY)..

Major PHP function used: hexdec, bin2hex, strlen, and explode.

How to use the function above?

## first, define video file and location

$video_file = "somedirectory/video.flv";

## call out the function 

echo getDuration($video_file);

There you have it.. Happy scripting.... :)

Some CREDIT dues and apologies: My appreciation to my Brother Michael D, and the wonderful people of adobe developement, for entertaining some of my highly caffeinated questions.. My sincere apology, I did not mean to ask questions that will get you off guard. Sometimes, the most dificult questions are coming from a kid :)...Don't worry, I won't tell anyone that one of you fell asleep while this topic was brought up in the classroom.

Edited by veedeoo: more info. added.

Votes + Comments
Nice Work!
great work!
3
Contributors
6
Replies
15
Views
5 Years
Discussion Span
Last Post by ifile168
0

@veedeoo

I assume this thread is not a question but a tutorial or code snippet that you intend to shared.

I try the code but nothing happend?

It's blank?

I attached an image of how it looks on my system.

252

1

Hi LastMitch,

The script may only work on flv, avi, mp4, but it has some limitations though. Some video files like the h264 that hasn't been process with mp4box, the PHP function may not be able to read the time duration, because of the position of the moov atom.

For h264 videos, the moov atom is located in the end of the file, while the flv ,avi, and divx, the moov atoms are all in the beginning of the stream file. Actually, when it comes to flv it is called metadata..

Here is the demo.. I loaded the video on the flash player, and then below the player I used the function to reflect the time as seen by the PHP script. The video durations shown in the player and outputted by the PHP script are pretty close to each other.

The limitation of the function above relies heavily on the reliability and integrity of the PHP function fread().. I strongly believe that as the video file gets bigger in size, the script will eventually heads for failure. Most standard video file size for web streaming is little under 200MB. I have not had the chance to test the function above or beyond the 30MB video, because most of my test videos are all lower than 30MB.

Edited by veedeoo: info added

Votes + Comments
Thanks for the demo! I understand how it works now!
0

@veedeoo

The script may only work on flv, avi, mp4, but it has some limitations though. Some video files like the h264 that hasn't been process with mp4box, the PHP function may not be able to read the time duration, because of the position of the moov atom.

I understand how it works now!

Thanks for the link of the demo. It was very helpful!

I didn't know that I need to embedded an video player in order to work.

Plus I need to find a video that is less than 30mb to work too.

Thanks. I appreciated!

1

@lastMitch,

not necessarily.. I just included the player for duration comparison.

Here is the demo of the script unable to read the proper duration. The video loaded on the player is an h264/mp4 not process by MP4Box or any moov atom application. The reason it is streaming, before the entire file has been loaded by the browser because of the floyplayer javascript stremaing module file.

I installed the ffmpeg, mencoder, flvtool2,mp4box, and ffmpeg-php on this server to demonstrate how the ffmpeg-php can read the video duration regardless of the file types or codecs.

Results;

Player duration readings : 10:17
The php function above  :  2:3:55 

// because of the increase in file size and h264 codec in the mix, the php function above failed . It's approximation is way out of the ten minutes. This is probably due to its inability to dig into the video file using fread, so by default the approximation was solely based on the file size.

FFMPEG PHP : accurately read the video duration in seconds  **617.68371582031**

Php code used for the ffmpeg-php

$video = new ffmpeg_movie($videoFile);

echo 'Duration in Seconds: ' $video->->getDuration();

Even the html5 player will have a hard time playing the h264/mp4 video files with the moov atom located in the end of the file. This will create a tremendous amount bandwidth usage. So, for people who are trying to have a video site with medium traffic, flv or ogg should be the codec of choice. The reason is that it is a lot easier to pseudo stream flv than h264/mp4 videos. Another alternative for the web video streaming is to acuquire a DiVX player license and stream the video as DiVX with the mKV or Matroska wrapper.

This is how my hosting account gets cancel all the time... excessive testing of my application while in the development. A common i-nodes usage is almost more than 48000, and this is just for 10 videos streaming simultaneously.

Edited by veedeoo: info added

0

This is an interesting topic that you created by using PHP with Multimedia.

I really like this very much. I never knew used can used PHP with Multimedia.

I mean I play around with Adobe Flash/Flex but never knew you can used a script like a PHP to present a video with the data like that.

I learn something new. Thanks for sharing!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.