laravel5.4扩展FFmpeg windows安装使用教程

ffmpeg

FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。

功能

多媒体视频处理工具FFmpeg有非常强大的功能包括视频采集功能、视频格式转换、视频抓图、给视频加水印等。

.exe下载

地址:https://ffmpeg.zeranoe.com/builds/

 

 

下载好后放到你的php目录:

 

 进入你的laravel项目目录 Shift+鼠标右键 在此打开命令窗口:

 

命令窗口中输入composer命令,前提你已经安装了composer,没有安装的参考我博客安装方法。

$ composer require php-ffmpeg/php-ffmpeg

安装成功

在你的config目录建立一个你的xxx.php(自己定义)存放FFMpeg的路径,修改起来方便:

<?php
    return [
        '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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值