·您当前的位置:首页 > 技术教程 > FFmpeg技术 >

[FFmpeg]php基于ffmpeg的应用:视频转换/视频截图/生成缩略图

时间:2014-07-21 08:54酷播
[FFmpeg]php基于ffmpeg的应用:视频转换/视频截图/生成缩略图

[FFmpeg]php基于ffmpeg的应用:视频转换/视频截图/生成缩略图

  1. include("ImageResize.class.php") 
  2.  //转视频 
  3.  $cmd="ffmpeg.exe -i starwar.avi -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 1.flv"
  4.  exec($cmd); 
  5.  //视频截图 
  6.  $cmd="ffmpeg.exe -i starwar.avi -f image2 -ss 10 -s 400*300 -vframes 1 1.jpg";  
  7.  exec($cmd); 
  8.  //生成缩略图 
  9.  $thumbnail = new ImageResize(); 
  10.  $thumbnail->resizeimage("1.jpg", 30,30, 0, "small1.jpg"); 
  11.  
  12.  
  13. class ImageResize { 
  14.     
  15.     //图片类型 
  16.     var $type; 
  17.     
  18.     //实际宽度 
  19.     var $width; 
  20.     
  21.     //实际高度 
  22.     var $height; 
  23.     
  24.     //改变后的宽度 
  25.     var $resize_width; 
  26.     
  27.     //改变后的高度 
  28.     var $resize_height; 
  29.     
  30.     //是否裁图 
  31.     var $cut; 
  32.     
  33.     //源图象 
  34.     var $srcimg; 
  35.     
  36.     //目标图象地址 
  37.     var $dstimg; 
  38.     
  39.     //临时创建的图象 
  40.     var $im; 
  41.  
  42. function resizeimage($img, $wid, $hei,$c,$dstpath) { 
  43.         $this->srcimg = $img; 
  44.         $this->resize_width = $wid; 
  45.         $this->resize_height = $hei; 
  46.         $this->cut = $c; 
  47.         
  48.         //图片的类型 
  49.         $this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); 
  50.         
  51.         //初始化图象 
  52.         $this->initi_img(); 
  53.         
  54.         //目标图象地址 
  55.         $this -> dst_img($dstpath); 
  56.         
  57.         //-- 
  58.         $this->width = imagesx($this->im); 
  59.         $this->height = imagesy($this->im); 
  60.         
  61.         //生成图象 
  62.         $this->newimg(); 
  63.         
  64.         ImageDestroy ($this->im); 
  65.     } 
  66.  
  67. function newimg() { 
  68.  
  69. //改变后的图象的比例 
  70.         $resize_ratio = ($this->resize_width)/($this->resize_height); 
  71.  
  72. //实际图象的比例 
  73.         $ratio = ($this->width)/($this->height); 
  74.  
  75. if(($this->cut)=="1") { 
  76.             //裁图 高度优先 
  77.             if($ratio>=$resize_ratio){ 
  78.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
  79.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height); 
  80.                 ImageJpeg ($newimg,$this->dstimg); 
  81.             } 
  82.             
  83.             //裁图 宽度优先 
  84.             if($ratio<$resize_ratio) { 
  85.                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
  86.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio)); 
  87.                 ImageJpeg ($newimg,$this->dstimg); 
  88.             } 
  89.         } else { 
  90.             //不裁图 
  91.             if($ratio>=$resize_ratio) { 
  92.                 $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); 
  93.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height); 
  94.                 ImageJpeg ($newimg,$this->dstimg); 
  95.             } 
  96.             if($ratio<$resize_ratio) { 
  97.                 $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); 
  98.                 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height); 
  99.                 ImageJpeg ($newimg,$this->dstimg); 
  100.             } 
  101.         } 
  102.     } 
  103.  
  104. //初始化图象 
  105.     function initi_img() { 
  106.         if($this->type=="jpg") { 
  107.             $this->im = imagecreatefromjpeg($this->srcimg); 
  108.         } 
  109.         
  110.         if($this->type=="gif") { 
  111.             $this->im = imagecreatefromgif($this->srcimg); 
  112.         } 
  113.         
  114.         if($this->type=="png") { 
  115.             $this->im = imagecreatefrompng($this->srcimg); 
  116.         } 
  117.         
  118.         if($this->type=="bmp") { 
  119.             $this->im = $this->imagecreatefrombmp($this->srcimg); 
  120.         } 
  121.     } 
  122.  
  123. //图象目标地址 
  124.     function dst_img($dstpath) { 
  125.         $full_length  = strlen($this->srcimg); 
  126.         $type_length  = strlen($this->type); 
  127.         $name_length  = $full_length-$type_length; 
  128.         $name = substr($this->srcimg,0,$name_length-1); 
  129.         $this->dstimg = $dstpath; 
  130.         //echo $this->dstimg; 
  131.     } 
  132.     
  133.     function ConvertBMP2GD($src, $dest = false) { 
  134.         if(!($src_f = fopen($src, "rb"))) { 
  135.             return false; 
  136.         } 
  137.         if(!($dest_f = fopen($dest, "wb"))) { 
  138.             return false; 
  139.         } 
  140.         $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f,14)); 
  141.         $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread($src_f, 40)); 
  142.         
  143.         extract($info); 
  144.         extract($header); 
  145.         
  146.         if($type != 0x4D42) { // signature "BM" 
  147.             return false; 
  148.         } 
  149.         
  150.         $palette_size = $offset - 54; 
  151.         $ncolor = $palette_size / 4; 
  152.         $gd_header = ""
  153.         // true-color vs. palette 
  154.         $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF"; 
  155.         $gd_header .pack("n2", $width, $height); 
  156.         $gd_header .= ($palette_size == 0) ? "\x01" : "\x00"; 
  157.         if($palette_size) { 
  158.             $gd_header .pack("n", $ncolor); 
  159.         } 
  160.         // no transparency 
  161.         $gd_header ."\xFF\xFF\xFF\xFF"
  162.  
  163. fwrite($dest_f, $gd_header); 
  164.  
  165. if($palette_size) { 
  166.             $palette = fread($src_f, $palette_size); 
  167.             $gd_palette = ""
  168.             $j = 0
  169.             while($j < $palette_size) { 
  170.                 $b = $palette{$j++}; 
  171.                 $g = $palette{$j++}; 
  172.                 $r = $palette{$j++}; 
  173.                 $a = $palette{$j++}; 
  174.                 $gd_palette ."$r$g$b$a"
  175.             } 
  176.             $gd_palette .str_repeat("\x00\x00\x00\x00", 256 - $ncolor); 
  177.             fwrite($dest_f, $gd_palette); 
  178.         } 
  179.  
  180. $scan_line_size = (($bits * $width) + 7) >> 3; 
  181.         $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 
  182.         0x03) : 0; 
  183.  
  184. for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) { 
  185.             // BMP stores scan lines starting from bottom 
  186.             fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l)); 
  187.             $scan_line = fread($src_f, $scan_line_size); 
  188.             if($bits == 24) { 
  189.                 $gd_scan_line = ""
  190.                 $j = 0
  191.                 while($j < $scan_line_size) { 
  192.                     $b = $scan_line{$j++}; 
  193.                     $g = $scan_line{$j++}; 
  194.                     $r = $scan_line{$j++}; 
  195.                     $gd_scan_line ."\x00$r$g$b"
  196.                 } 
  197.             } 
  198.             else if($bits == 8) { 
  199.                 $gd_scan_line = $scan_line; 
  200.             } 
  201.             else if($bits == 4) { 
  202.                 $gd_scan_line = ""
  203.                 $j = 0
  204.                 while($j < $scan_line_size) { 
  205.                     $byte = ord($scan_line{$j++}); 
  206.                     $p1 = chr($byte >> 4); 
  207.                     $p2 = chr($byte & 0x0F); 
  208.                     $gd_scan_line ."$p1$p2"
  209.                 } 
  210.                 $gd_scan_line = substr($gd_scan_line, 0, $width); 
  211.             } 
  212.             else if($bits == 1) { 
  213.                 $gd_scan_line = ""
  214.                 $j = 0
  215.                 while($j < $scan_line_size) { 
  216.                     $byte = ord($scan_line{$j++}); 
  217.                     $p1 = chr((int) (($byte & 0x80) != 0)); 
  218.                     $p2 = chr((int) (($byte & 0x40) != 0)); 
  219.                     $p3 = chr((int) (($byte & 0x20) != 0)); 
  220.                     $p4 = chr((int) (($byte & 0x10) != 0)); 
  221.                     $p5 = chr((int) (($byte & 0x08) != 0)); 
  222.                     $p6 = chr((int) (($byte & 0x04) != 0)); 
  223.                     $p7 = chr((int) (($byte & 0x02) != 0)); 
  224.                     $p8 = chr((int) (($byte & 0x01) != 0)); 
  225.                     $gd_scan_line ."$p1$p2$p3$p4$p5$p6$p7$p8"
  226.                 } 
  227.                 $gd_scan_line = substr($gd_scan_line, 0, $width); 
  228.             } 
  229.             fwrite($dest_f, $gd_scan_line); 
  230.         } 
  231.         fclose($src_f); 
  232.         fclose($dest_f); 
  233.         return true; 
  234.     } 
  235.  
  236. function imagecreatefrombmp($filename) { 
  237.         $tmp_name = tempnam("/tmp", "GD"); 
  238.         if($this->ConvertBMP2GD($filename, $tmp_name)) { 
  239.             $img = imagecreatefromgd($tmp_name); 
  240.             unlink($tmp_name); 
  241.             return $img; 
  242.         } 
  243.         return false; 
  244.     } 
  245.     

源代码下载链接:http://download.csdn.net/detail/toss156/3830291

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器