Solved

Detect FLV duration with PHP

Posted on 2006-10-24
4
2,665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to how to record live broadcast.

690 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