PHP获取mp4视频时长
使用getid3获取mp4视频时长(同样适用于mp3等其他媒体文件)
    $getid3 = new getID3;
    $file = './test.mp4';
    $info = $getid3->analyze($file);
    $playtime_seconds = $info['playtime_seconds'];
    $duration = gmdate("H:i:s", $playtime_seconds); //将秒换算为分钟
PHP获取m3u8视频时长
读取m3u8文件获取视频时长
     $file_url = 'https://example.com/test.m3u8'; //m3u8视频地址
     $vtime = $this->getDuration($file_url); //获取文件时长
     $duration = gmdate("H:i:s", $vtime); //将秒换算为分钟
     function getDuration($file_url) {
         try {
            // 忽略ssl证书验证
            $stream_opts = [
                "ssl" => [
                    "verify_peer" => false,
                    "verify_peer_name" => false,
                ]
            ];
            $res = file_get_contents($file, false, stream_context_create($stream_opts));
        } catch (\ErrorException $e) {
            return 0;
        }
        // preg_match_all("/\EXTINF:(.*?)\,/i", $res, $arr);
        // $res = array_sum($arr[1]);
        preg_match_all('/\d+[.]\d+/', $res, $arr);
        $res = array_sum($arr[0]);
        return (int)$res;
     }
 
    
     
				