php 常见的视频格式转换
本文章只支持windows 64 系统 服务器 Linux系统文件需自己另行下载。
文件下载百度云
mencoder链接: https://pan.baidu.com/s/1o8VFftw 密码: g3wmffmpeg链接: https://pan.baidu.com/s/1jJTvDwq 密码: iykj
class video{
/*******************
使用本类需要有mencoder.exe视频格式转换器
需要修改下面的绝对路径
支持的格式 rmvb,wmv,mkv,3gp,mp4,mpg,avi,mp3
该类需要结合两个视频转换软件 mencoder.exe 和 ffmpeg.exe
*******************/
private $mencoder_url = "D:wampmplayermencoder.exe";
private $ffmpeg_url = "D:wamp\ffmpegin\ffmpeg.exe";
private $savePath = "D:a"; //文件会保存在源文件目录
private $format = array("rmvb","wmv","mkv","3gp","mp4","mpg","avi","mp3");
public $err = "";
function video(){
}
public function run($fromfile,$to="flv",$w=720,$h=480,$n=1500){
//是否格式正确
if($this->is_format($fromfile,$to)==false) {
$this->err .= "extension not in format";
echo "extension not in format";
return false;
}
// echo 11;
//获取视频截图
$this->printscreen($fromfile,300,300);
//获取视频时长
// $b = $this->getTime($fromfile);
$ext = $this->getExt($fromfile);
echo $ext;
//转换格式
if($ext=="rmvb" || $ext=="rm" || $ext=="mp4"){
// echo $ext;
$this->convert($fromfile,$to,$w,$h,$n);
}else{
$this->convertF($fromfile,$to,$w,$h);
}
}
//获取后缀名
public function getExt($file){
$r = pathinfo($file);
return $r["extension"];
}
public function convertF($fromfile,$to,$w,$h){
$file = $this->ping($fromfile);
$tofile = $this->pingTo($fromfile,$to);
echo $file."<br>";
$code = "{$this->ffmpeg_url} -i {$file} -ab 56 -ar 22050 -b 1500 -qscale 1 -r 20 -s {$w}x{$h} {$tofile}";
echo $code;
echo exec($code,$e);
$this->err = $e;
}
public function convert($fromfile,$to="flv",$w,$h,$n){
$file = $this->ping($fromfile);
echo $file;
$tofile = $this->pingTo($fromfile,$to);
$ovc = "lavc";
// 关于mencoder的命令行参数小安将一些重要的参数记录下,可以供设置参考,他们直接影响了视频转换的好坏。 -oac //mp3lame 音频编码格式
// -ovc lavc 视频编码格式 我选择MP4的时候可能需要设置为 -ovc x264
// -lavcopts vcodec=mpeg4:vbitrate={$n} kbps 编码{
// 由于 Libavcodec 包含了多种视频编码,所以用 vcodec=mpeg4 来指定具体的使用 MPEG
// 目前用的flv
// }-vf 设置输出文件格式:默认为avi格式,mencoder的默认格式。 其它格式,可以用 lavf
// -oac 输出音频编码
// mp3lame -lameopts aq=7:vbr=2:q=6
// q , 质量(0 - 最好,9 - 最差)(仅用于VBR)
// aq,算法质量(0 - 最好/最慢,9 - 最差/最快)
// 编码方式包括有abr(按平均码率编码)、cbr(指固定码率编码)、vbr(按动态码率编码)三种
// br=128 则指定编码码率为 128kbps。
// -lavcopts vcodec=mpeg4
// dia越大品质越高,如果需要快速编码,设置为-1,设为4时已经很有利于品质了;
// cmp越大品质越高,默认值0,是最快速的,一般设到3,设为6已经只会细微提高品质了,但速度会慢
$code = "{$this->mencoder_url} {$file} -o {$tofile} -of lavf -oac mp3lame -lameopts abr:br=56 -ovc {$ovc} -lavcopts vcodec=flv:vbitrate={$n}:mbd=2:o=mpv_flags=+mv0:trell:v4mv:last_pred=1:dia=4:cmp=0:vb_strategy=1 -vf scale={$w}:{$h} -ofps 20 -srate 22050";
// $out=exec($code,$e);
proc_close(proc_open ($code, array(), $e));
pclose(popen ($code,"r"));
echo exec($code, $e);
$this->err = $e;
// echo $e;
}
//拼接完整来源地址
public function ping($fromFile){
$fromFile = str_replace("/","\",$fromFile);
// $path = $this->savePath.$fromFile;
// echo $fromFile."<br>";
return $fromFile;
}
//拼接完整的目标转换后的视频文件地址
public function pingTo($fromFile,$to){
$a = pathinfo($fromFile);
$a["dirname"] = str_replace("/","\",$a["dirname"]);
$path = $a["dirname"]."\".$a["filename"].".".$to;
// echo $path."<br>";
return $path;
}
//提取视频截图
public function printscreen($fromfile,$w=350,$h=240){
$file = $this->ping($fromfile);
$toImg = $this->pingTo($fromfile,"jpg");
$code = "{$this->ffmpeg_url} -i {$file} -y -f image2 -ss 8 -t 0.001 -s {$w}x{$h} {$toImg}";
exec($code,$e);
$this->err = $e;
}
//检查是否是其中格式
public function is_format($file,$to){
if(!file_exists($file)) return false;
$a = pathinfo($file);
//如果转换的格式和原格式相同
if($a["extension"]==$to) return false;
if(in_array($a["extension"],$this->format)) return true;
else return false;
}
//提取视频时长
public function getTime($fromfile){
$file = $this->ping($fromfile);
$code = "{$this->ffmpeg_url} -i {$file}";
exec($code,$e);
$this->err = $e;
}
}
$rr="./sun.mp4"; //源文件文位置
$v = new video();
$v->run($rr,"flv",480,480);
文件下载百度云
mencoder链接: https://pan.baidu.com/s/1o8VFftw 密码: g3wmffmpeg链接: https://pan.baidu.com/s/1jJTvDwq 密码: iykj
class video{
/*******************
使用本类需要有mencoder.exe视频格式转换器
需要修改下面的绝对路径
支持的格式 rmvb,wmv,mkv,3gp,mp4,mpg,avi,mp3
该类需要结合两个视频转换软件 mencoder.exe 和 ffmpeg.exe
*******************/
private $mencoder_url = "D:wampmplayermencoder.exe";
private $ffmpeg_url = "D:wamp\ffmpegin\ffmpeg.exe";
private $savePath = "D:a"; //文件会保存在源文件目录
private $format = array("rmvb","wmv","mkv","3gp","mp4","mpg","avi","mp3");
public $err = "";
function video(){
}
public function run($fromfile,$to="flv",$w=720,$h=480,$n=1500){
//是否格式正确
if($this->is_format($fromfile,$to)==false) {
$this->err .= "extension not in format";
echo "extension not in format";
return false;
}
// echo 11;
//获取视频截图
$this->printscreen($fromfile,300,300);
//获取视频时长
// $b = $this->getTime($fromfile);
$ext = $this->getExt($fromfile);
echo $ext;
//转换格式
if($ext=="rmvb" || $ext=="rm" || $ext=="mp4"){
// echo $ext;
$this->convert($fromfile,$to,$w,$h,$n);
}else{
$this->convertF($fromfile,$to,$w,$h);
}
}
//获取后缀名
public function getExt($file){
$r = pathinfo($file);
return $r["extension"];
}
public function convertF($fromfile,$to,$w,$h){
$file = $this->ping($fromfile);
$tofile = $this->pingTo($fromfile,$to);
echo $file."<br>";
$code = "{$this->ffmpeg_url} -i {$file} -ab 56 -ar 22050 -b 1500 -qscale 1 -r 20 -s {$w}x{$h} {$tofile}";
echo $code;
echo exec($code,$e);
$this->err = $e;
}
public function convert($fromfile,$to="flv",$w,$h,$n){
$file = $this->ping($fromfile);
echo $file;
$tofile = $this->pingTo($fromfile,$to);
$ovc = "lavc";
// 关于mencoder的命令行参数小安将一些重要的参数记录下,可以供设置参考,他们直接影响了视频转换的好坏。 -oac //mp3lame 音频编码格式
// -ovc lavc 视频编码格式 我选择MP4的时候可能需要设置为 -ovc x264
// -lavcopts vcodec=mpeg4:vbitrate={$n} kbps 编码{
// 由于 Libavcodec 包含了多种视频编码,所以用 vcodec=mpeg4 来指定具体的使用 MPEG
// 目前用的flv
// }-vf 设置输出文件格式:默认为avi格式,mencoder的默认格式。 其它格式,可以用 lavf
// -oac 输出音频编码
// mp3lame -lameopts aq=7:vbr=2:q=6
// q , 质量(0 - 最好,9 - 最差)(仅用于VBR)
// aq,算法质量(0 - 最好/最慢,9 - 最差/最快)
// 编码方式包括有abr(按平均码率编码)、cbr(指固定码率编码)、vbr(按动态码率编码)三种
// br=128 则指定编码码率为 128kbps。
// -lavcopts vcodec=mpeg4
// dia越大品质越高,如果需要快速编码,设置为-1,设为4时已经很有利于品质了;
// cmp越大品质越高,默认值0,是最快速的,一般设到3,设为6已经只会细微提高品质了,但速度会慢
$code = "{$this->mencoder_url} {$file} -o {$tofile} -of lavf -oac mp3lame -lameopts abr:br=56 -ovc {$ovc} -lavcopts vcodec=flv:vbitrate={$n}:mbd=2:o=mpv_flags=+mv0:trell:v4mv:last_pred=1:dia=4:cmp=0:vb_strategy=1 -vf scale={$w}:{$h} -ofps 20 -srate 22050";
// $out=exec($code,$e);
proc_close(proc_open ($code, array(), $e));
pclose(popen ($code,"r"));
echo exec($code, $e);
$this->err = $e;
// echo $e;
}
//拼接完整来源地址
public function ping($fromFile){
$fromFile = str_replace("/","\",$fromFile);
// $path = $this->savePath.$fromFile;
// echo $fromFile."<br>";
return $fromFile;
}
//拼接完整的目标转换后的视频文件地址
public function pingTo($fromFile,$to){
$a = pathinfo($fromFile);
$a["dirname"] = str_replace("/","\",$a["dirname"]);
$path = $a["dirname"]."\".$a["filename"].".".$to;
// echo $path."<br>";
return $path;
}
//提取视频截图
public function printscreen($fromfile,$w=350,$h=240){
$file = $this->ping($fromfile);
$toImg = $this->pingTo($fromfile,"jpg");
$code = "{$this->ffmpeg_url} -i {$file} -y -f image2 -ss 8 -t 0.001 -s {$w}x{$h} {$toImg}";
exec($code,$e);
$this->err = $e;
}
//检查是否是其中格式
public function is_format($file,$to){
if(!file_exists($file)) return false;
$a = pathinfo($file);
//如果转换的格式和原格式相同
if($a["extension"]==$to) return false;
if(in_array($a["extension"],$this->format)) return true;
else return false;
}
//提取视频时长
public function getTime($fromfile){
$file = $this->ping($fromfile);
$code = "{$this->ffmpeg_url} -i {$file}";
exec($code,$e);
$this->err = $e;
}
}
$rr="./sun.mp4"; //源文件文位置
$v = new video();
$v->run($rr,"flv",480,480);
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 根据各视频网站网址获取SWF链接的PHP实例仅供参考
- 下一篇: PHP版本-对应的新特性