Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get flv video length

Tags:

php

video

flv

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

like image 849
Sujeet Avatar asked Jun 02 '11 13:06

Sujeet


2 Answers

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

like image 78
wework4web Avatar answered Nov 06 '22 12:11

wework4web


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.

like image 1
SirDarius Avatar answered Nov 06 '22 12:11

SirDarius