一、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,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
83 ImageJpeg ($newimg,$this->dstimg);
84 }
85
86
87 /* 裁图 宽度优先 */
88 if($ratio<$resize_ratio) {
89 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
90 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
91 ImageJpeg ($newimg,$this->dstimg);
92 }
93 } else {
94
95 /* 不裁图 */
96 if($ratio>=$resize_ratio) {
97 $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
98 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
99 ImageJpeg ($newimg,$this->dstimg);
100 }
101 if($ratio<$resize_ratio) {
102 $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
103 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
104 ImageJpeg ($newimg,$this->dstimg);
105 }
106 }
107 }
108
109 /* 初始化图象 */
110 function initi_img() {
111 if($this->type=="jpg") {
112 $this->im = imagecreatefromjpeg($this->srcimg);
113 }
114
115 if($this->type=="gif") {
116 $this->im = imagecreatefromgif($this->srcimg);
117 }
118
119 if($this->type=="png") {
120 $this->im = imagecreatefrompng($this->srcimg);
121 }
122
123 if($this->type=="bmp") {
124 $this->im = $this->imagecreatefrombmp($this->srcimg);
125 }
126 }
127
128
129 /* 图象目标地址 */
130 function dst_img($dstpath) {
131 $full_length = strlen($this->srcimg);
132 $type_length = strlen($this->type);
133 $name_length = $full_length-$type_length;
134 $name = substr($this->srcimg,0,$name_length-1);
135 $this->dstimg = $dstpath;
136 }
137
138
139
140 function ConvertBMP2GD($src, $dest = false) {
141 if(!($src_f = fopen($src, "rb"))) {
142 return false;
143 }
144 if(!($dest_f = fopen($dest, "wb"))) {
145 return false;
146 }
147 $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f,14));
148 $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread($src_f, 40));
149
150 extract($info);
151 extract($header);
152
153 if($type != 0x4D42) { // signature "BM"
154 return false;
155 }
156
157 $palette_size = $offset - 54;
158 $ncolor = $palette_size / 4;
159 $gd_header = "";
160 // true-color vs. palette
161 $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
162 $gd_header .= pack("n2", $width, $height);
163 $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
164 if($palette_size) {
165 $gd_header .= pack("n", $ncolor);
166 }
167
168 $gd_header .= "\xFF\xFF\xFF\xFF";
169
170 fwrite($dest_f, $gd_header);
171
172 if($palette_size) {
173 $palette = fread($src_f, $palette_size);
174 $gd_palette = "";
175 $j = 0;
176 while($j < $palette_size) {
177 $b = $palette{$j++};
178 $g = $palette{$j++};
179 $r = $palette{$j++};
180 $a = $palette{$j++};
181 $gd_palette .= "$r$g$b$a";
182 }
183 $gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);
184 fwrite($dest_f, $gd_palette);
185 }
186
187 $scan_line_size = (($bits * $width) + 7) >> 3;
188 $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size &
189 0x03) : 0;
190
191 for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
192 // BMP stores scan lines starting from bottom
193 fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
194 $scan_line = fread($src_f, $scan_line_size);
195 if($bits == 24) {
196 $gd_scan_line = "";
197 $j = 0;
198 while($j < $scan_line_size) {
199 $b = $scan_line{$j++};
200 $g = $scan_line{$j++};
201 $r = $scan_line{$j++};
202 $gd_scan_line .= "\x00$r$g$b";
203 }
204 }
205 else if($bits == 8) {
206 $gd_scan_line = $scan_line;
207 }
208 else if($bits == 4) {
209 $gd_scan_line = "";
210 $j = 0;
211 while($j < $scan_line_size) {
212 $byte = ord($scan_line{$j++});
213 $p1 = chr($byte >> 4);
214 $p2 = chr($byte & 0x0F);
215 $gd_scan_line .= "$p1$p2";
216 }
217 $gd_scan_line = substr($gd_scan_line, 0, $width);
218 }
219 else if($bits == 1) {
220 $gd_scan_line = "";
221 $j = 0;
222 while($j < $scan_line_size) {
223 $byte = ord($scan_line{$j++});
224 $p1 = chr((int) (($byte & 0x80) != 0));
225 $p2 = chr((int) (($byte & 0x40) != 0));
226 $p3 = chr((int) (($byte & 0x20) != 0));
227 $p4 = chr((int) (($byte & 0x10) != 0));
228 $p5 = chr((int) (($byte & 0x08) != 0));
229 $p6 = chr((int) (($byte & 0x04) != 0));
230 $p7 = chr((int) (($byte & 0x02) != 0));
231 $p8 = chr((int) (($byte & 0x01) != 0));
232 $gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
233 }
234 $gd_scan_line = substr($gd_scan_line, 0, $width);
235 }
236 fwrite($dest_f, $gd_scan_line);
237 }
238 fclose($src_f);
239 fclose($dest_f);
240 return true;
241 }
242
243 function imagecreatefrombmp($filename) {
244 $tmp_name = tempnam("/tmp", "GD");
245 if($this->ConvertBMP2GD($filename, $tmp_name)) {
246 $img = imagecreatefromgd($tmp_name);
247 unlink($tmp_name);
248 return $img;
249 }
250 return false;
251 }
252
253
254 }
? 4.2 软件下载
FFmpeg官方下载:http://ffmpeg.org/download.html
?
http://www.cnblogs.com/wgw8299/archive/2011/12/12/2284656.html