网址:https://www.getid3.org/
Packagist:james-heinrich/getid3
Github:JamesHeinrich/getID3
下载 https://github.com/JamesHeinrich/getID3

/**

 * 获取视频上传时长
 *
 * @param [type] $remotefilename 本地文件路径
 * @param integer $type 1获取MP4视频总时长(秒) 2获取文件大小
 * @return void
 */
function getid3($remotefilename,$type=1){
    require(EXTEND_PATH.'getID3/getid3/getid3.php');
    try{
        if ($fp_remote = fopen($remotefilename, 'rb')) {
            $localtempfilename = tempnam('/tmp', 'getID3');
            if ($fp_local = fopen($localtempfilename, 'wb')) {
                while ($buffer = fread($fp_remote, 8192)) {
                    fwrite($fp_local, $buffer);
                }
                fclose($fp_local);
                // Initialize getID3 engine
                $getID3 = new \getID3();
                $ThisFileInfo = $getID3->analyze($localtempfilename);
                // Delete temporary file
                unlink($localtempfilename);
            }
            fclose($fp_remote);
        }
        $res = $type == 1 ? $ThisFileInfo['playtime_seconds']:$ThisFileInfo['filesize'];
        return $res;
    }catch(\Exception $e){
       throw new BaseException($e->getMessage());
    }

}

输出结果:

[
"GETID3_VERSION" => "1.9.20-202006061653", #getid3版本
"filesize" => 7127824,                     # 文件大小 单位b
"filepath" => "D:/php/test",               # 文件路径
"filename" => "a.mp4",                     # 文件名称
"filenamepath" => "D:/php/test/a.mp4",     # 文件路径+名称
"fileformat" => "mp4",                     # 文件格式
"encoding"=> "UTF-8",                      # 编码
"mime_type" => "video/mp4",                # Mime 类型
"playtime_seconds" => 17.728,              # 播放时长 单位 s 秒
"bitrate" => 3201996.8411552,              # 比特率
"playtime_string" => "0:18"                # 播放时长格式化 HH:ii:ss

];


本文由 来鹏飞 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论