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.