PHP做视频网站,让程序自动实现视频格式转换、设置视频大小、生成视频缩略图...

news/2024/5/8 22:16:16/文章来源:https://blog.csdn.net/dianzongliao7933/article/details/101390296

一、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 图片处理工具类如下

ImageHelper.class.php
  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

 

转载于:https://www.cnblogs.com/wgw8299/archive/2011/12/12/2284656.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_868701.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

几种常用的网站在线客服风格

第一种&#xff1a;企业QQ客服网站 效果截图&#xff1a; 关键JS代码&#xff1a; lastScrollY0; function heartBeat(){ var diffY; if (document.documentElement && document.documentElement.scrollTop) diffY document.documentElement.scrollTop; else if (doc…

网站社会化评论插件推荐之“畅言”

“畅言”...你畅个试试&#xff0c;..半夜查你水表&#xff01;页面添加评论回复功能&#xff0c;对比了很多&#xff0c;这个“畅言”还算灵活&#xff0c;简单的js调用&#xff0c;当然&#xff0c;如果使用WordPress&#xff0c;还可以安装它的插件。不过像我们这种追求速度…

URL访问网站的网络传输全过程

引 打开浏览器&#xff0c;在地址栏输入URL&#xff0c;回车&#xff0c;出现网站内容。这是我们几乎每天都在做的事&#xff0c;那这个过程中到底是什么原理呢&#xff1f;HTTP、TCP、DNS、IP这些耳熟能详的名词都在什么时候起着什么作用呢&#xff1f;在这里整体梳理一遍。 …

妄谈大型网站技术架构

引 网络架构这个问题&#xff0c;我认为不是一个后台、架构师等等才需要考虑的问题&#xff0c;不管是前端也好&#xff0c;移动端也好&#xff0c;都应该多考虑考虑这个层面的问题&#xff0c;包括之后公司对你的要求也是这样的&#xff0c;不是说你会写业务会写功能就很ok&a…

各种大型网站技术架构

引言近段时间以来&#xff0c;通过接触有关海量数据处理和搜索引擎的诸多技术&#xff0c;常常见识到不少精妙绝伦的架构图。除了每每感叹于每幅图表面上的绘制的精细之外&#xff0c;更为架构图背后所隐藏的设计思想所叹服。个人这两天一直在搜集各大型网站的架构设计图&#…

网站单点登录实现

单点登录在现在的系统架构中广泛存在&#xff0c;他将多个子系统的认证体系打通&#xff0c;实现了一个入口多处使用&#xff0c;而在架构单点登录时&#xff0c;也会遇到一些小问题&#xff0c;在不同的应用环境中可以采用不同的单点登录实现方案来满足需求。我将以我所遇到的…

不知道这些网站还当什么程序员啊!

今天我就来总结一些程序员必备的网站&#xff0c;囊括开源项目、解决bug、技术分享、一线资源和自我提升的网站&#xff0c;希望能对广大程序猿有所帮助&#xff0c;赶紧给我收藏起来&#xff0c;下次刷不到了可别说我没提醒你。 我们首先来看一下国内比较流行的程序员社区&am…

关于前端spa项目seo优化改造方案(预渲染,ssr,nuxt比较)

目前的的前端项目为基于vuecli3搭建的spa项目&#xff0c;由于需求提出需要对首页&#xff0c;部分内容页面做seo优化&#xff0c;涉及到前端项目的框架和部分页面的改造。 目录 SEO简介关于收录关于链接 一、预渲染二、ssr服务端渲染三、nuxt SEO简介 SEO&#xff08;Search…

spa应用seo优化方案(arm服务器)

最近公司需要对网页进行seo优化&#xff0c;奈何项目为vue的spa应用&#xff0c;没那么方便改造&#xff0c;所以记录一下优化经历 目录 一、非SSR项目进行seo优化有以下五种方案&#xff1a;二、文件编写三、arm环境中nodejs环境搭建四、arm环境下安装谷歌或火狐浏览器五、ng…

ASP.NET网站版本自动更新程序及代码[转]

1、自动更新程序主要负责从服务器中获取相应的更新文件&#xff0c;并且把这些文件下载到本地&#xff0c;替换现有的文件。达到修复Bug&#xff0c;更新功能的目的。用户手工点击更新按钮启动更新程序。已测试。2、环境VS2008&#xff0c;采用C#.NET和ASP.NET实现。3、服务器&…

WebApi托管静态网站(Owin 自托管静态网站)

我们在使用WebApi对外提供简单Api的时候&#xff0c;有时候往往需要同步提供一些简单的静态页面给用户&#xff0c;例如OAuth认证服务&#xff0c;提供一个授权界面等。如果我们单独架设网站&#xff0c;将会导致调用我们自己的接口出现跨域访问&#xff0c;出现IE8及以下浏览器…

那些网站够安全吗?

近日&#xff0c; CSDN 社区网站数据库泄露 &#xff0c;近 600 万用户真实账号密码外泄。该事件横扫整个中文互联网&#xff0c;并且随后又爆出 多玩游戏 800 万用户资料被泄露 &#xff0c;另有传言人人网、开心网、天涯社区、世纪佳缘、百合网等社区都有可能成为黑客下一个目…

5个在线的网站测试和验证工具

网站上线前的测试和验证是非常重要的一个环节&#xff0c;验证的意思是检查网站的页面和其他数据是否符合标准规范&#xff0c;设计规范的网站在各种浏览器上表现会一致而且良好。 为了帮助你执行这些测试和验证&#xff0c;今天我们列表了5个在线的工具。 Pingdom Tools Ping…

使用GitHub建立个人网站

使用GitHub建立个人网站 1 Git简介 2 为什么使用Github Pages 3 创建Github Pages 3.1 安装git工具. 3.2 两种pages模式 3.3 创建步骤 3.4 常用命令 4 使用Jekyll搭建博客 4.1 什么是jekyll 4.2 jekyll本地环境搭建 4.3 jekyll目录结构 4.4 Jekyll-Bootstrap创建博客 4.5 Je…

cnzz统计网站

cnzz是由国际著名风险投资商IDG投资的网络技术服务公司&#xff0c;是中国互联网目前最有影响力 CNZZ网站首页的免费流量统计技术服务提供商&#xff0c;专注于为互联网各类站点提供专业、权威、独立的第三方数据统计分析。同时&#xff0c;CNZZ拥有全球领先的互联网数据采集、…

网站防止攻击

1、什么是XSS XSS又叫CSS (Cross Site Script) &#xff0c;跨站脚本攻击。它指的是恶意攻击者往Web页面里插入恶意html代码&#xff0c;当用户浏览该页之时&#xff0c;嵌入其中Web里面的html代码会被执行&#xff0c;从而达到恶意用户的特殊目的。XSS属于被动式的攻击&#…

如何防止你的网站被攻击?

避免网站被攻击&#xff0c;其实是可以提前预防的&#xff0c;那么要如何预防呢&#xff1f; 1、关闭不必要的端口和服务 2、安装杀毒软件或者是防火墙来抵御攻击。 3、定期修改账户密码&#xff0c;尽量设置的复杂些&#xff0c;不要使用弱密码。 4、日常维护的时候要注意&…

网站前端开发--css篇

Ⅰ 全局&#xff1a;global.css 全局样式为全站公用&#xff0c;为页面样式基础&#xff0c;页面中必须包含。 结构&#xff1a;layout.css 页面结构类型复杂&#xff0c;并且公用类型较多时使用。多用在首页级页面和产品类页面中。 私有&#xff1a;style.css 独立页面所使用的…

scrapy爬取途牛网站旅游数据

描述&#xff1a;采取了scrapy框架对途牛网旅游数据进行了爬取&#xff0c;刚开始练手&#xff0c;所以只爬了四个字段用作测试&#xff0c;分别是景点名称、景点位置、景点开放时间、景点描述&#xff0c;爬取结果存的是json格式。 部分数据&#xff1a; 部分代码&#xf…

网站数据统计分析之一:日志收集原理及其实现

网站数据统计分析工具是网站站长和运营人员经常使用的一种工具&#xff0c;比较常用的有谷歌分析、百度统计 和 腾讯分析等等。所有这些统计分析工具的第一步都是网站访问数据的收集。目前主流的数据收集方式基本都是基于javascript的。本文将简要分析这种数据收集的原理&#…