get flv video length
Asked Answered
D

3

5

While i try to get length of a flv video file i get 0 second where as it only happens with some videos, else my function works fine.

below is my code.

<?php
function mbmGetFLVDuration($file){
    // read file
  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;
        }
      }
    }
  }
}
// not working video file
$result = ceil(mbmGetFLVDuration('not_working_copy.flv')/1000);
// working video file
//$result = ceil(mbmGetFLVDuration('working_copy.flv')/1000);
echo date('H:i:s',mktime(0,0,$result))
?>

i have attached both working and not working flv video in link below:

working video: http://blog.developeronhire.com/wp-content/uploads/downloads/2011/06/working_copy.flv

not working video: http://blog.developeronhire.com/wp-content/uploads/downloads/2011/06/not_working_copy.flv

any idea will be appreciated.

Thank you

Dakota answered 2/6, 2011 at 13:9 Comment(3)
possible duplicate #3282773Negress
@Mr. Black : sorry frient it's not duplicated. please read my problem first and comment it.Dakota
working on one file and on other it is not working, is that duplicate question for getting video length of flv file?Selfeducated
C
7

This type of problem occurs when the meta information of a video is partially or fully corrputed. In order to resolve this problem, use FFMPEG commnad line tool, to repair such corrupted file while uploading. below is a code snippet that extracts the video duration using FFMPEG.

<?php
     ob_start();
     passthru("ffmpeg -i working_copy.flv  2>&1");
     $duration = ob_get_contents();
     $full = ob_get_contents();
     ob_end_clean();
     $search = "/duration.*?([0-9]{1,})/";
     print_r($duration);
     $duration = preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);
     print_r('<pre>');
 print_r($matches[1][0]);
 print_r($full);
?>

to download FFMPEG go to http://www.ffmpeg.org

Cupulate answered 6/6, 2011 at 11:16 Comment(0)
I
1

First of all, I'm afraid your function might stop working at all, given a sufficiently big FLV video file, and hitting PHP's memory_limit

$contents = fread($handle, filesize($file));

because you are actually loading the entire file into memory.

Then, the non-working file also seems corrupted to me. flvmeta gives the following output:

$ flvmeta --check not_working_copy.flv
0x00488473: error E30013: unknown tag type 250
0x00488477: error E40023: timestamps are decreasing from 130543 to 0
2 error(s), 0 warning(s)

If you need to efficiently get the duration from a file that might be corrupted, or containing non-standard tags, I recommend you to use MediaInfo, which does a great job at handling even the most exotic video files, without altering them like ffmpeg would.

It can be called from PHP like any command-line program, and its output controlled via command line arguments:

$ MediaInfo --Inform="Video;%Duration%" not_working_copy.flv
130000

which displays the video duration in milliseconds.

Isidraisidro answered 14/6, 2011 at 15:12 Comment(1)
lots of thanks for the time. Ya your solution works fine. Thank you againDakota
S
0

You can extract meta-data of flv video, you'll find all the info like length, size etc. See link http://code.google.com/p/flv4php/ Good Luck sujeet

Sibie answered 2/6, 2011 at 17:40 Comment(1)
but is that for all kind of video files ??Ginnygino

© 2022 - 2024 — McMap. All rights reserved.