ffmpeg
FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。
功能
多媒体视频处理工具FFmpeg有非常强大的功能包括视频采集功能、视频格式转换、视频抓图、给视频加水印等。
.exe下载
Download FFmpeg for Windows
地址:https://ffmpeg.zeranoe.com/builds/
下载好后放到你的php目录:
进入你的laravel项目目录 Shift+鼠标右键 在此打开命令窗口:
命令窗口中输入composer命令,前提你已经安装了composer,没有安装的参考我博客安装方法。
$ composer require php-ffmpeg/php-ffmpeg
安装成功
在你的config目录建立一个你的xxx.php(自己定义)存放FFMpeg的路径,修改起来方便:
<?phpreturn ['ffmpegpath' =>'C:/xampp/php/ffmpeg/bin/ffmpeg.exe -i "%s" 2>&1','ffmpeg' =>'C:/xampp/php/ffmpeg/bin/ffmpeg.exe','ffprobe' =>'C:/xampp/php/ffmpeg/bin/ffprobe.exe', ];
使用
伪代码:use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Config;$ffmpeg_path = Config::get("element.ffmpeg");//ffmpeg运行的路径$ffprobe_path = Config::get("element.ffprobe");//ffprobe运行路径$ffmpeg = \FFMpeg\FFMpeg::create(array('ffmpeg.binaries' => $ffmpeg_path,'ffprobe.binaries' => $ffprobe_path,'timeout' => 30000, // The timeout for the underlying process'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use ));$default = ini_get('max_execution_time');//获取php的超时设置的值set_time_limit(-1);//这里设置成不超时$video = $ffmpeg->open($path);//这里的路径填写你要转码的视频的绝对路径$format = new \FFMpeg\Format\Video\X264('libmp3lame', 'libx264');$format->on('progress', function ($video, $format, $percentage) {//echo " 进度:$percentage%";if ($percentage == 100) {//1.修改数据库文件路径信息DB::table('elements')->where('id', $this->id)->update(['path' => $this->chakge_path, 'trans_status' => '1']);}});$format->setKiloBitrate(1000)->setAudioChannels(2)->setAudioKiloBitrate(256);$video->save($format, $save_path);//这里保存转码后的视频地址set_time_limit($default);//还原php的超时设置的值
返回视频或图片详细信息的方法:
/*** 返回视频或音频文件的信息*/public function getVideoInfo($file){// 定义ffmpeg路径及命令常量//define('FFMPEG_CMD', '/usr/local/bin/ffmpeg -i "%s" 2>&1');$ffmpeg = Config::get("element.ffmpegpath");//这里去conf里读define('FFMPEG_PATH', $ffmpeg);$command = sprintf(FFMPEG_PATH, $file);ob_start();passthru($command);$info = ob_get_contents();ob_end_clean();$data = array();if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {$data['duration'] = $match[1]; //播放时间$arr_duration = explode(':', $match[1]);$data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数$data['start'] = $match[2]; //开始时间$data['bitrate'] = $match[3]; //码率(kb) }if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {$data['vcodec'] = $match[1]; //视频编码格式$data['vformat'] = $match[2]; //视频格式$data['resolution'] = $match[3]; //视频分辨率if (strpos($match[3], 'x')) {$arr_resolution = explode('x', $match[3]);$data['resolution'] = $match[3];$data['width'] = $arr_resolution[0];$data['height'] = $arr_resolution[1];}}if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {$data['acodec'] = $match[1]; //音频编码$data['asamplerate'] = $match[2]; //音频采样频率 }if (isset($data['seconds']) && isset($data['start'])) {$data['play_time'] = $data['seconds'] + $data['start']; //实际播放时间 }$data['size'] = filesize($file); //文件大小return $data;}
秒传:MD5文件算法
/*** 新式获取md5的方法* @param string $file 文件的绝对路径*/public static function md5($file){$str = "";$tmp_file = "";@$fp = fopen($file, 'r');if ($fp) {$size = filesize($file);if (1048576 > $size) // 1024*1024 {$str = $file;} else {fseek($fp, 0); // 1 - 2012$str .= fread($fp, 2012);fseek($fp, $size / 2 - 1999); // filesize/2 - 1999$str .= fread($fp, 1999);fseek($fp, -2010, SEEK_END); // -2010$str .= fread($fp, 2010);$tmp_file = sprintf("%s/%s_%s", public_path(), time(), basename($file));$tfp = fopen($tmp_file, "wb");if ($tfp) {fwrite($tfp, $str);fclose($tfp);$str = $tmp_file;}}fclose($fp);}@$md5 = md5_file($str);@unlink($tmp_file);return $md5;}
检查文件类型:
/*** 检查文件类型*/public function typeCheck($file){//获取文件扩展名$ext = strtolower($file->getClientOriginalExtension());$mime_type = strtolower($file->getClientMimeType());//上传文件类型约束列表$uptypes = array('gif', 'jpg', 'jpeg', 'png', 'mp3', 'mp4', 'mov', 'mkv', 'avi');if (!in_array($ext, $uptypes)) {return false;}//图片if (in_array($ext, array('gif', 'jpg', 'jpeg', 'png')) && str_contains($mime_type, 'image')) {return 'image';}//视频if (in_array($ext, array('mp4', 'mkv', 'mov', 'avi')) || str_contains($mime_type, 'video')) {return 'video';}//音频if (in_array($ext, array('mp3')) && str_contains($mime_type, 'audio')) {return 'audio';}return false;}