Thumbnail creation from FLV using ffmpeg

seek to the middle of the file an output a JPEG
 
 
<?php
 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 0;
 }
 
 // get duration and divide by 2 to get middle
 $duration = number_format((GetFLVDuration('output.flv') / 1000) / 2);
 
 // check for error
 if($duration<0){
  $duration = 0;
 }
 
 //save out thumbnail
 $output2 = exec('ffmpeg -y -i output.flv -r 1 -vframes 1 -sameq -f image2 -ss ' . $duration . ' -an output.jpg');
?>
-----------------
 
To improve quality, try increasing the frame rate via the -r option:

 $output = exec ("ffmpeg -i source.ext -s qvga -r 24 -ab 128 -y output.flv");

or

 $output = exec ("ffmpeg -i source.ext -s qvga -r 30 -ab 128 -y output.flv");

Below is a solution to finding the total number of frames in a file, which you could use to determine the middle frame:

http://lists.mplayerhq.hu/pipermail/ffmpeg-user/2006-August/003884.html (Note that the language is not PHP, but you should be able to get the general idea).

Comments