Techno

ffmpeg video thumbnail / screenshot class [ technoslab ]

Mar 25th, 2011
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.76 KB | None | 0 0
  1. <?php
  2.  
  3. # usage : http://technoslab.blogspot.com
  4. #
  5. # tecShot::genThumb('e:\video\b.avi',9,'e:\video\ffmpeg.exe');
  6. #
  7. # Class
  8.  
  9. final class tecShot{
  10.  
  11.     private function error($msg){
  12.         exit("<b>tecShot</b>: $msg");
  13.     }
  14.    
  15.     public function genThumb($video_path,$ss_no,$ffmpeg_path="/usr/local/bin/ffmpeg"){
  16.  
  17.     if(!is_file($video_path)){
  18.             $this->error("'$video_path' contains no such video.");
  19.         }
  20.        
  21.         if($ss_no<=0){
  22.             $this->error("'$ss_no' is less that or equal to 0.");
  23.         }
  24.        
  25.         if($ss_no>50){
  26.             $this->error("You can't generate more than 50 thumbs at a time.");
  27.         }
  28.        
  29.         if(!is_dir("./thumb")){
  30.             $this->error("Create a folder with proper permissions and rename it to '<b>thumb</b>'.");
  31.         }
  32.        
  33.         ob_start();
  34.         passthru("{$ffmpeg_path} -i \"$video_path\" 2>&1");
  35.         $duration = ob_get_contents();
  36.         ob_end_clean();
  37.         $search='@Duration: ([^:]*):([^:]*):([^(:|.)]*)(.[^,]*|)@is';
  38.         preg_match($search, $duration, $matches);
  39.         $hr=$matches[1];
  40.         $min=$matches[2];
  41.         $sec=$matches[3];
  42.         $tot = $hr * 3600 + $min * 60 + $sec;
  43.         $r=round($ss_no/$tot,2);
  44.         $name = "thumb/".basename($video_path)."_";
  45.         ob_start();
  46.         $exec="{$ffmpeg_path} -i \"{$video_path}\" -r $r -vframes $ss -an -y \"$name%d.png\" 2>&1";
  47.         passthru($exec);
  48.         $duration = ob_get_contents();
  49.         ob_end_clean();
  50.        
  51.         for($i=1;$i<=$ss_no;$i++){
  52.             $newName = $name.$i.".png";
  53.             echo "&nbsp;&nbsp;<img src='$newName' style='max-width:300px' />";
  54.             if($i%3==0)echo"<br />";
  55.            
  56.         }
  57.        
  58.     }
  59. }
  60.  
  61. ?>
Advertisement
Add Comment
Please, Sign In to add comment