<?php

/**
 * VideoTools class
 * 
 * 
 */

class VideoTools
{
    const 
FFMPEG '/usr/bin/ffmpeg';
    const 
FLVTOOL2 '/usr/bin/flvtool2';
    
/**
  * exportToFlv ($file, $destination, $export_thumb=false, $thumbnail_destination=null)
  */
  
public static function exportToFlv ($file$destination$export_thumb=false$thumbnail_destination=null)
  {
      
// export flv
      
exec(sprintf('%s -i %s -ar 11025 -ab 28k -aspect 4:3 -b 100k -r 6 -f flv -s 320x240 -y %s | %s -U stdin %s > /dev/null 2>&1'self::FFMPEG$file$destinationself::FLVTOOL2$destination));

      
// export thumb
      
if($export_thumb)
      {
          
sleep(1); // sleep for a little while | lol
          // calculate export time frame
          
$video_file = new ffmpeg_movie($file);
        
$video_length self::get_time_frame(ceil($video_file->getDuration()));
        
self::exportVideoThumb ($file$thumbnail_destinationfalse$video_length);
      }
  }
  
  
/**
  * exportVideoThumb ($file, $destination, $calculate_size=true, $video_length='00:00:01')
  */
  
public static function exportVideoThumb ($file$destination$calculate_size=true$video_length='00:00:01')
  {
      if(
$calculate_size)
      {
        
$video_file = new ffmpeg_movie($file);
          
// calculate export time frame
          
$video_length self::get_time_frame(ceil($video_file->getDuration()));
      }
        
// export thumbnail
      
exec(sprintf('%s -i %s -an -ss 1 -t %s -vcodec mjpeg -vframes 1 -an -f rawvideo -y -s 320x240 %s > /dev/null 2>&1'self::FFMPEG$file$video_length$destination));
  }
  
  
/**
  * get_time_frame($t)
  * 
  * return video time frame 
  */
  
public static function get_time_frame($t)
    {
      if((int) 
$t 60) return '00:00:50'// bigger than a minute
      
else if((int) $t 30) return '00:00:25'// bigger than 30 seconds
      
else if((int) $t 15) return '00:00:10'// bigger than 15 seconds
      
else return '00:00:01'// opps!
    
}
}