?
Solved

Detect FLV duration with PHP

Posted on 2006-10-24
4
Medium Priority
?
2,667 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 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
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.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
Suggested Courses

764 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