Solved

Detect FLV duration with PHP

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
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.

743 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

11 Experts available now in Live!

Get 1:1 Help Now