Solved

Detect FLV duration with PHP

Posted on 2006-10-24
4
2,658 Views
Last Modified: 2010-01-20
Is there a way to detect the duration of an FLV file with PHP assuming that it contains the correct MetaData? Thanks.
0
Comment
Question by:rbichon
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
RobotWorx earned 250 total points
ID: 17814042
The file data of an FLV has to be viewed in HEX format. In essence, what you want to do is find the last time stamp in the FLV file which will be the duration in milliseconds. To do that, you must understand a bit about the structure of an flv file. The main body of an FLV is split up into tags. The last tag contains the timestamp that you are interested in. The last three characters, or UI32, of the file represents the length of the final tag. If you seek backwards in the file by that number, you will come to the beginning of the tag. The first three characters, or UI32, of the tag represents the timestamp in milliseconds that you are interested in. Here is the function I came up with.

function GetFLVDuration($file){
      // get contents of a file into a string
      if (file_exists($file)){
            $handle = fopen($file, "r");
            $contents = fread($handle, filesize($file));
            fclose($handle);
            
            if (strlen($contents) > 3){
                  if (substr($contents,0,3) == "FLV"){
                        $taglen = hexdec(bin2hex(substr($contents,strlen($contents)-3)));
                        if (strlen($contents) > $taglen){
                              $duration = hexdec(bin2hex(substr($contents,strlen($contents)-$taglen,3)));
                              return $duration;
                        }
                  }
            }
      }
      return false;
}

This has been tested and should work on Flash 7 and 8 FLV's.
0
 
LVL 1

Expert Comment

by:RobotWorx
ID: 17814093
By the way, PHP usually has a memory limit of 16MB. If you are using FLV files larger than that, you have to change the memory limit in the php.ini file. I set mine to memory_limit=32M which will allow for 32MB files. You will get an error if you try to use this function on a file that is larger than your memory limit.
0
 
LVL 1

Author Comment

by:rbichon
ID: 17814107
Thanks. I'll give it a try.
0
 
LVL 1

Author Comment

by:rbichon
ID: 17814159
Looks like it works! Thanks a lot for the code!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question