Solved

Detect FLV duration with PHP

Posted on 2006-10-24
4
2,650 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
To view Flash technology content error 4 6,489
Youtube video error 30 742
File drag and drop 6 132
Cannot install Adove Flash player on Windows 2012R2 6 96
Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
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…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now