php做视频网站,让程序自动实现视频格式转换、设置视频大小、生成视频缩略图
一、php实现转换
在做视频网站的时候,最头痛的问题可能是格式转换、视频缩略图等。下面我将用php实现这一些功能。php是没有自带视频的函数,所以会用到第三方的软件工具来实现。
?
二、什么是ffmpeg
ffmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用lgpl或gpl许可证(依据你选择的组件)。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多codec都是从头开发的。
ffmpeg在linux平台下开发,但它同样也可以在其它操作系统环境中编译运行,包括windows、mac os x等。
这个项目最早由fabrice bellard发起,现在由michael niedermayer维护。许多ffmpeg的开发人员都来自mplayer项目,而且当前ffmpeg也是放在mplayer项目组的服务器上。项目的名称来自mpeg视频编码标准,前面的ff“代表fast forward“。更多详情》?/* 转视频 */$cmd=ffmpeg.exe -i tiwer_update_move.avi -ab 56 -ar 22050 -b 500 -r 15 -s 500x600 201112120089123.flv;? ?exec($cmd);? ?/*? 视频截图*/$cmd=ffmpeg.exe -itiwer_update_move.avi -f image2 -ss 10 -s 600*500 -vframes 1 201112120089123.jpg;
exec($cmd);
?三、生成缩略图
?
include(imagehelper.class.php);?/* 生成缩略图 */$thumbnail?= new?imagehelper();? $thumbnail->resizeimage(2012121208123.jpg, 30,30, 0, 2012121208123_small.jpg);?
?
?
四、工具类与软件下载
4.1 图片处理工具类如下
1 /** 2 * 图片处理工具 3 * 4 * project: bobo manage system 5 * this is not a freeware, use is subject to license terms! 6 * 7 * site: http://www.bobo123.cn 8 * 9 * $id: imagehelper.class.php 269 2011-03-08 00:44:01z wgw8299 $ 10 * 11 * copyright ? 2007-2012 bobo123.cn developer team. all rights reserved. 12 */ 13 class imagehelper { 14 15 16 var $type; 17 18 19 /* 实际宽度 */ 20 var $width; 21 22 /* 实际高度 */ 23 var $height; 24 25 /* 改变后的宽度 */ 26 var $resize_width; 27 28 /* 改变后的高度 */ 29 var $resize_height; 30 31 /* 是否裁图 */ 32 var $cut; 33 34 /* 源图象 */ 35 var $srcimg; 36 37 /* 目标图象地址 */ 38 var $dstimg; 39 40 /* 临时创建的图象 */ 41 var $im; 42 43 function resizeimage($img, $wid, $hei,$c,$dstpath) { 44 45 $this->srcimg = $img; 46 $this->resize_width = $wid; 47 $this->resize_height = $hei; 48 $this->cut = $c; 49 50 /* 图片的类型 */ 51 $this->type = strtolower(substr(strrchr($this->srcimg,.),1)); 52 53 /* 初始化图象 */ 54 $this->initi_img(); 55 56 /* 目标图象地址 */ 57 $this -> dst_img($dstpath); 58 59 60 $this->width = imagesx($this->im); 61 $this->height = imagesy($this->im); 62 63 /* 生成图象 */ 64 $this->newimg(); 65 66 imagedestroy ($this->im); 67 } 68 69 function newimg() { 70 71 /* 改变后的图象的比例 */ 72 $resize_ratio = ($this->resize_width)/($this->resize_height); 73 74 /* 实际图象的比例 */ 75 $ratio = ($this->width)/($this->height); 76 77 78 if(($this->cut)==1) { 79 /* 裁图高度优先 */ 80 if($ratio>=$resize_ratio){ 81 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 82 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,