`Laravel`版`小丑路人社区`改版中,与`Hyperf版小丑路人社区`数据互动,此版本改版中……尚未彻底完结!

Q:

Ffmpeg获取文件的相关信息


class Ffmpeg
{
    /*******************
     *
     * 支持的格式 rmvb,wmv,mkv,3gp,mp4,mpg,avi,mp3
     *******************/
    protected $ffmpeg_path;

    public function __construct($ffmpeg_path)
    {
        $this->ffmpeg_path = $ffmpeg_path;
    }

    private $ffmpeg_url = ' -i "%s" 2>&1';
    public  $err        = '';

    //获得视频文件的具体信息

    function getContent($file)
    {
        //$file为视频上传目录
        $command = sprintf($this->ffmpeg_path . $this->ffmpeg_url, $file);//你的安装路径

        ob_start();

        passthru($command);

        $info = ob_get_contents();

        ob_end_clean();

        $data = [];

        if ( preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match) ) {
            $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]; //视频分辨率
//            $arr_resolution = explode('x', $match[3]);
//            $data['width'] = $arr_resolution[0];
//            // $data['height'] = $arr_resolution[1];
//        }

        // $regex_sizes = "/Video: ([^,]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/";
        // or : $regex_sizes = "/Video: ([^\r\n]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/";
        if ( preg_match("/Video: (.*?), (.*?), ([0-9]{1,4})x([0-9]{1,4})/", $info, $match) ) {
            $data['vcodec'] = $match[1]; //视频编码格式
            $data['vformat'] = $match[2]; //视频格式
            $data['resolution'] = $match[3] . 'x' . $match[4]; //视频分辨率
            $data['width'] = $match[3];
            $data['height'] = $match[4];
        }

        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']; //实际播放时间
        }

        // 如果是在线文件,那么请求获取头部信息
        if ($this->check_url($file)){
            $res = get_headers($file,true);
            $data['size'] = $res['Content-Length'];
        }else{
            $data['size'] = filesize($file); //文件大小
        }

        return $data;
    }

    function check_url(string $url) : bool
    {
        $str = "/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/";
        if ( !preg_match($str, $url) ) return false; else return true;
    }
}
技术分享
订阅

评论记录


评论/回复