·您当前的位置:首页 > 技术教程 > FFmpeg技术 >

[FFmpeg]用ffmpeg-php做视频换(源代码)

时间:2014-07-11 08:35酷播
[FFmpeg]用ffmpeg-php做视频换(源代码)ffmpeg安装包,php装ffmpeg

FLV 是FLASH VIDEO的简称,FLV流媒体格式是随着Flash MX的推出发展而来的视频格式。由于它形成的文件极小、加载速度极快,使得网络观看视频文件成为可能,它的出现有效地解决了视频文件导入Flash后,使 导出的SWF文件体积庞大,不能在网络上很好的使用等缺点。
现在的网站应用中,经常会用到flv的流媒体,而需要上传的视频格式可能是各种各样的,如avi,mpeg。
本文章介绍了通过php调用ffmpeg和mencode两个软件,将多种格式视频转为flv格式。
详细使用请参考:ffmpeg-php
Ffmpeg快速命令使用
MEncoder的基础用法
以下是一个参考类,平台使用windows。

  1. <?   
  2. define("ROOT_DIR",dirname(__FILE__));   
  3. class EcodeFlv {   
  4.   var $fromFile;   //CuPlayer.com上传来的文件   
  5.   var $toFilePath;  //CuPlayer.com保存文件路径   
  6.   var $toPicPath;  //CuPlayer.com保存图片路径   
  7.   var $mpeg; //CuPlayer.comffmpeg程序文件的路径   
  8.   var $mencode; //mencode程序文件的路径   
  9.   var $cmdToFile; //CuPlayer.com转换文件命令   
  10.   var $cmdToPic;  //CuPlayer.com转换图片命令   
  11.   var $toFileName; //CuPlayer.com转换后的文件名   
  12.   var $mpegComm; //ffmpeg.exe的转换命令   
  13.   var $mencodeComm; //mencode.exe的命令   
  14.   var $mpegType;   
  15.   var $mencodeType;   
  16.   var $midi; //mdi.exe的路径   
  17.   var $cmdMidi; //mdi.exe的命令   
  18.   //CuPlayer.com初始化类   
  19.   function EcodeFlv($fromFile,$toFilePath,$toPicPath,$mpeg,$mencode,$midi) {   
  20.     $this->mpegComm = false;   
  21.     $this->mencodeComm = false;   
  22.     $this->fromFile = $fromFile;   
  23.     $this->toFilePath = $toFilePath;   
  24.     $this->toPicPath = ROOT_DIR."/".$toPicPath;   
  25.     $this->mpeg = ROOT_DIR.$mpeg;   
  26.     $this->mencode = ROOT_DIR.$mencode;   
  27.     $this->midi = ROOT_DIR.$midi;   
  28.     $this->mpegType=array (   
  29.     "audio/x-mpeg"=>".mp3",   
  30.     "video/mpeg"=>".mpeg",   
  31.     "video/3gpp"=>".3gp",   
  32.     "video/x-ms-asf"=>".asf",   
  33.     "video/x-msvideo"=>".avi"   
  34.     );   
  35.     $this->mencodeType = array(   
  36.     "application/vnd.rn-realmedia"=>".rm",   
  37.     "audio/x-pn-realaudio"=>".rmvb",   
  38.     "audio/x-ms-wmv"=>".wmv",   
  39.     );   
  40.   } //CuPlayer.com检查文件类型   
  41.      
  42.   function checkType() {   
  43.     if(function_exists(mime_content_type)){   
  44.       return false;   
  45.     }else{   
  46.       //$contentType = mime_content_type($this->fromFile);   
  47.       $exe = "D:\server\php\extras\magic";   
  48.       $handel = new finfo(FILEINFO_MIME, $exe);   
  49.       $contentType =  $handel->file($this->fromFile);   
  50.     }   
  51.     foreach($this->mpegType as $index=>$key){   
  52.       if($contentType == $index){   
  53.         $name = md5(date("Ymd").tiime());   
  54.         $this->toFileName = $name;   
  55.         $this->$mpegComm = true;   
  56.         return true;   
  57.       }   
  58.     }   
  59.     foreach($this->mencodeType as $index=>$key){   
  60.       if($contentType == $index){   
  61.         $name = md5(date("Ymd").time());   
  62.         $this->toFileName = $name;   
  63.         $this->mencodeComm = true;   
  64.         return true;   
  65.       }else{   
  66.         return false;   
  67.       }   
  68.     }   
  69.   }   
  70.      
  71.   //CuPlayer.com设置文件,图片大小   
  72.   function setSize($flvSize,$picSize) {   
  73.     $flvWidth = $flvSize[0];   
  74.     $flvHeight = $flvSize[1];   
  75.     $picWidth = $picSize[0];   
  76.     $picHeight = $picSize[1];   
  77.     $picName = $this->toPicPath.$this->toFileName.".jpg";   
  78.     $flvName = $this->toFilePath.$this->toFileName.".flv";   
  79.     $toMdi = ROOT_DIR."/".$flvName;   
  80.     $size = $picWidth."x".$picHeight;   
  81.     if($this->mpegComm){   
  82.       $this->cmdToFile"$this->mpeg -i $this->fromFile -y -ab 56 -ar 22050 -b 500 -r 15 -s $flvWith*$flvHeight $flvName";   
  83.     }   
  84.     elseif($this->mencodeComm){   
  85.       $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";   
  86.     }   
  87.     $this->cmdToPic = "$this->mpeg -i $toMdi -y -f image2 -ss 8 -t 0.003 -s $size $picName";   
  88.     $this->cmdMidi = "$this->midi $toMdi /k";   
  89.     echo $this->cmdToPic;   
  90.   }   
  91.      
  92.   //CuPlayer.com开始转换   
  93.   function toEcode() {   
  94.     set_time_limit(0);   
  95.     exec($this->cmdToFile,$flvStatus);   
  96.     exec($this->cmdToPic,$picStatus);   
  97.     exec($this->cmdMidi,$mStatus);   
  98.   }   
  99.    
  100. }   

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器