用php将任何格式视频转为flv的代码
程序员文章站
2023-11-26 12:53:16
复制代码 代码如下: define("root_dir",dirname(__file__)); class ecodeflv { var $fromfile;...
复制代码 代码如下:
<?
define("root_dir",dirname(__file__));
class ecodeflv {
var $fromfile; //上传来的文件
var $tofilepath; //保存文件路径
var $topicpath; //保存图片路径
var $mpeg; //ffmpeg.exe文件的路径
var $mencode; //mencode.exe文件的路径
var $cmdtofile; //转换文件命令
var $cmdtopic; //转换图片命令
var $tofilename; //转换后的文件名
var $mpegcomm; //ffmpeg.exe的转换命令
var $mencodecomm; //mencode.exe的命令
var $mpegtype;
var $mencodetype;
var $midi; //mdi.exe的路径
var $cmdmidi; //mdi.exe的命令
//初始化类
function ecodeflv($fromfile,$tofilepath,$topicpath,$mpeg,$mencode,$midi) {
$this->mpegcomm = false;
$this->mencodecomm = false;
$this->fromfile = $fromfile;
$this->tofilepath = $tofilepath;
$this->topicpath = root_dir."/".$topicpath;
$this->mpeg = root_dir.$mpeg;
$this->mencode = root_dir.$mencode;
$this->midi = root_dir.$midi;
$this->mpegtype=array (
"audio/x-mpeg"=>".mp3",
"video/mpeg"=>".mpeg",
"video/3gpp"=>".3gp",
"video/x-ms-asf"=>".asf",
"video/x-msvideo"=>".avi"
);
$this->mencodetype = array(
"application/vnd.rn-realmedia"=>".rm",
"audio/x-pn-realaudio"=>".rmvb",
"audio/x-ms-wmv"=>".wmv",
);
}
//检查文件类型
function checktype() {
if(function_exists(mime_content_type)){
return false;
}else{
//$contenttype = mime_content_type($this->fromfile);
$exe = "d:\server\php\extras\magic";
$handel = new finfo(fileinfo_mime, $exe);
$contenttype = $handel->file($this->fromfile);
}
foreach($this->mpegtype as $index=>$key){
if($contenttype == $index){
$name = md5(date("ymd").tiime());
$this->tofilename = $name;
$this->$mpegcomm = true;
return true;
}
}
foreach($this->mencodetype as $index=>$key){
if($contenttype == $index){
$name = md5(date("ymd").time());
$this->tofilename = $name;
$this->mencodecomm = true;
return true;
}else{
return false;
}
}
}
//设置文件,图片大小
function setsize($flvsize,$picsize) {
$flvwidth = $flvsize[0];
$flvheight = $flvsize[1];
$picwidth = $picsize[0];
$picheight = $picsize[1];
$picname = $this->topicpath.$this->tofilename.".jpg";
$flvname = $this->tofilepath.$this->tofilename.".flv";
$tomdi = root_dir."/".$flvname;
$size = $picwidth."x".$picheight;
if($this->mpegcomm){
$this->cmdtofile= "$this->mpeg -i $this->fromfile -y -ab 56 -ar 22050 -b 500 -r 15 -s $flvwith*$flvheight $flvname";
}
elseif($this->mencodecomm){
$this->cmdtofile = "$this->mencode $this->fromfile -vf scale=$flvwidth:$flvheight -ffourcc flv1 -of lavf -ovc lavc -lavcopts vcodec=flv:vbitrate=70:acodec=mp3:abitrate=56:dia=-1 -ofps 25 -srate 22050 -oac mp3lame -o $flvname";
}
$this->cmdtopic = "$this->mpeg -i $tomdi -y -f image2 -ss 8 -t 0.003 -s $size $picname";
$this->cmdmidi = "$this->midi $tomdi /k";
echo $this->cmdtopic;
}
//开始转换
function toecode() {
set_time_limit(0);
exec($this->cmdtofile,$flvstatus)
exec($this->cmdtopic,$picstatus);
exec($this->cmdmidi,$mstatus);
}
}
?>