PHP基于ffmpeg实现转换视频,截图及生成缩略图的方法
发布时间 - 2026-01-11 02:55:31 点击率:次本文实例讲述了PHP基于ffmpeg实现转换视频,截图及生成缩略图的方法。分享给大家供大家参考,具体如下:

这里把ffmpeg 和 生成缩略图整合了一下:
include("ImageResize.class.php")
//转视频
$cmd="ffmpeg.exe -i starwar.avi -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 1.flv";
exec($cmd);
//视频截图
$cmd="ffmpeg.exe -i starwar.avi -f image2 -ss 10 -s 400*300 -vframes 1 1.jpg";
exec($cmd);
//生成缩略图
$thumbnail = new ImageResize();
$thumbnail->resizeimage("1.jpg", 30,30, 0, "small1.jpg");
class ImageResize {
//图片类型
var $type;
//实际宽度
var $width;
//实际高度
var $height;
//改变后的宽度
var $resize_width;
//改变后的高度
var $resize_height;
//是否裁图
var $cut;
//源图象
var $srcimg;
//目标图象地址
var $dstimg;
//临时创建的图象
var $im;
function resizeimage($img, $wid, $hei,$c,$dstpath) {
$this->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
//图片的类型
$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
//初始化图象
$this->initi_img();
//目标图象地址
$this -> dst_img($dstpath);
//--
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//生成图象
$this->newimg();
ImageDestroy ($this->im);
}
function newimg() {
//改变后的图象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);
//实际图象的比例
$ratio = ($this->width)/($this->height);
if(($this->cut)=="1") {
//裁图 高度优先
if($ratio>=$resize_ratio){
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
//裁图 宽度优先
if($ratio<$resize_ratio) {
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
ImageJpeg ($newimg,$this->dstimg);
}
} else {
//不裁图
if($ratio>=$resize_ratio) {
$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio) {
$newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
}
}
//初始化图象
function initi_img() {
if($this->type=="jpg") {
$this->im = imagecreatefromjpeg($this->srcimg);
}
if($this->type=="gif") {
$this->im = imagecreatefromgif($this->srcimg);
}
if($this->type=="png") {
$this->im = imagecreatefrompng($this->srcimg);
}
if($this->type=="bmp") {
$this->im = $this->imagecreatefrombmp($this->srcimg);
}
}
//图象目标地址
function dst_img($dstpath) {
$full_length = strlen($this->srcimg);
$type_length = strlen($this->type);
$name_length = $full_length-$type_length;
$name = substr($this->srcimg,0,$name_length-1);
$this->dstimg = $dstpath;
//echo $this->dstimg;
}
function ConvertBMP2GD($src, $dest = false) {
if(!($src_f = fopen($src, "rb"))) {
return false;
}
if(!($dest_f = fopen($dest, "wb"))) {
return false;
}
$header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f,14));
$info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread($src_f, 40));
extract($info);
extract($header);
if($type != 0x4D42) { // signature "BM"
return false;
}
$palette_size = $offset - 54;
$ncolor = $palette_size / 4;
$gd_header = "";
// true-color vs. palette
$gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
$gd_header .= pack("n2", $width, $height);
$gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
if($palette_size) {
$gd_header .= pack("n", $ncolor);
}
// no transparency
$gd_header .= "\xFF\xFF\xFF\xFF";
fwrite($dest_f, $gd_header);
if($palette_size) {
$palette = fread($src_f, $palette_size);
$gd_palette = "";
$j = 0;
while($j < $palette_size) {
$b = $palette{$j++};
$g = $palette{$j++};
$r = $palette{$j++};
$a = $palette{$j++};
$gd_palette .= "$r$g$b$a";
}
$gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);
fwrite($dest_f, $gd_palette);
}
$scan_line_size = (($bits * $width) + 7) >> 3;
$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size &
0x03) : 0;
for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
// BMP stores scan lines starting from bottom
fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
$scan_line = fread($src_f, $scan_line_size);
if($bits == 24) {
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size) {
$b = $scan_line{$j++};
$g = $scan_line{$j++};
$r = $scan_line{$j++};
$gd_scan_line .= "\x00$r$g$b";
}
}
else if($bits == 8) {
$gd_scan_line = $scan_line;
}
else if($bits == 4) {
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = chr($byte >> 4);
$p2 = chr($byte & 0x0F);
$gd_scan_line .= "$p1$p2";
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
else if($bits == 1) {
$gd_scan_line = "";
$j = 0;
while($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = chr((int) (($byte & 0x80) != 0));
$p2 = chr((int) (($byte & 0x40) != 0));
$p3 = chr((int) (($byte & 0x20) != 0));
$p4 = chr((int) (($byte & 0x10) != 0));
$p5 = chr((int) (($byte & 0x08) != 0));
$p6 = chr((int) (($byte & 0x04) != 0));
$p7 = chr((int) (($byte & 0x02) != 0));
$p8 = chr((int) (($byte & 0x01) != 0));
$gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
fwrite($dest_f, $gd_scan_line);
}
fclose($src_f);
fclose($dest_f);
return true;
}
function imagecreatefrombmp($filename) {
$tmp_name = tempnam("/tmp", "GD");
if($this->ConvertBMP2GD($filename, $tmp_name)) {
$img = imagecreatefromgd($tmp_name);
unlink($tmp_name);
return $img;
}
return false;
}
}
附:完整实例代码点击此处本站下载。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《PHP数学运算技巧总结》
希望本文所述对大家PHP程序设计有所帮助。
# PHP
# ffmpeg
# 视频
# 截图
# 缩略图
# PHP中使用FFMPEG获取视频缩略图和视频总时长实例
# PHP使用FFmpeg获取视频播放总时长与码率等信息
# 利用Ffmpeg获得flv视频缩略图和视频时间的代码
# php使用FFmpeg接口获取视频的播放时长、码率、缩略图以及创建时间
# php 调用ffmpeg获取视频信息的简单实现
# php使用ffmpeg获取视频信息并截图的实现方法
# php利用ffmpeg提取视频中音频与视频画面的方法详解
# php使用ffmpeg向视频中添加文字字幕的实现方法
# php+ffmpeg如何获取视频缩略图、视频分辨率等相关信息
# 程序设计
# 操作技巧
# 相关内容
# 感兴趣
# 给大家
# 点击此处
# 更多关于
# 所述
# 面向对象
# 讲述了
# 整合了
# ImageDestroy
# newimg
# dst_img
# imagesx
# imagesy
# resize_ratio
# imagecopyresampled
# ImageJpeg
# imagecreatetruecolor
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel如何创建自定义中间件?(Middleware代码示例)
Laravel如何使用软删除(Soft Deletes)功能_Eloquent软删除与数据恢复方法
jimdo怎样用html5做选项卡_jimdo选项卡html5实现与切换效果【指南】
HTML5建模怎么导出为FBX格式_FBX格式兼容性及导出步骤【指南】
javascript事件捕获机制【深入分析IE和DOM中的事件模型】
JavaScript如何实现类型判断_typeof和instanceof有什么区别
Windows10如何删除恢复分区_Win10 Diskpart命令强制删除分区
Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】
Laravel怎么自定义错误页面_Laravel修改404和500页面模板
Laravel如何为API生成Swagger或OpenAPI文档
在centOS 7安装mysql 5.7的详细教程
宙斯浏览器怎么屏蔽图片浏览 节省手机流量使用设置方法
高性能网站服务器配置指南:安全稳定与高效建站核心方案
无锡营销型网站制作公司,无锡网选车牌流程?
网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?
Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)
如何快速搭建自助建站会员专属系统?
Laravel如何实现用户注册和登录?(Auth脚手架指南)
如何在景安服务器上快速搭建个人网站?
Laravel Debugbar怎么安装_Laravel调试工具栏配置指南
Laravel如何使用.env文件管理环境变量?(最佳实践)
Laravel如何实现事件和监听器?(Event & Listener实战)
在线制作视频的网站有哪些,电脑如何制作视频短片?
在Oracle关闭情况下如何修改spfile的参数
Laravel怎么调用外部API_Laravel Http Client客户端使用
如何在 Python 中将列表项按字母顺序编号(a.、b.、c. …)
零基础网站服务器架设实战:轻量应用与域名解析配置指南
香港服务器网站推广:SEO优化与外贸独立站搭建策略
html5源代码发行怎么设置权限_访问权限控制方法与实践【指南】
Windows10如何更改计算机工作组_Win10系统属性修改Workgroup
Laravel如何使用集合(Collections)进行数据处理_Laravel Collection常用方法与技巧
Laravel怎么实现前端Toast弹窗提示_Laravel Session闪存数据Flash传递给前端【方法】
javascript基于原型链的继承及call和apply函数用法分析
专业商城网站制作公司有哪些,pi商城官网是哪个?
uc浏览器二维码扫描入口_uc浏览器扫码功能使用地址
Google浏览器为什么这么卡 Google浏览器提速优化设置步骤【方法】
大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?
Windows10电脑怎么设置虚拟光驱_Win10右键装载ISO镜像文件
Javascript中的事件循环是如何工作的_如何利用Javascript事件循环优化异步代码?
如何在阿里云购买域名并搭建网站?
美食网站链接制作教程视频,哪个教做美食的网站比较专业点?
大型企业网站制作流程,做网站需要注册公司吗?
微信小程序 wx.uploadFile无法上传解决办法
Linux网络带宽限制_tc配置实践解析【教程】
如何在万网ECS上快速搭建专属网站?
*服务器网站为何频现安全漏洞?
如何用5美元大硬盘VPS安全高效搭建个人网站?
Laravel N+1查询问题如何解决_Eloquent预加载(Eager Loading)优化数据库查询
php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】
如何在云主机快速搭建网站站点?

