从视频url在php中获得视频持续时间


Get video duration from video url in php

我需要在我的网站上显示视频的播放时间。

我尝试使用getid3,但它不工作,它显示警告,如:

preg_match()期望参数2为字符串,数组在C:'wamp'www'PE'getid3'getid3.php第262行
警告:is_readable()期望参数1是一个有效的路径,数组在C:'wamp'www'PE'getid3'getid3.php 271行
警告:file_exists()期望参数1是一个有效的路径,数组在C:'wamp'www'PE'getid3'getid3.php 271

这是警告等等,结果没有显示。

下面是我的代码:
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
ini_set('output_buffering','Off');
error_reporting(-1);
include_once("getid3/getid3.php");
$getID3 = new getID3;
$SongPath = pathinfo('http://localhost/PE/uploads/pe_discussion/videos/Wildlife.wmv');
set_time_limit(30);
$ThisFileInfo = $getID3->analyze($SongPath);
getid3_lib::CopyTagsToComments($ThisFileInfo);
echo 'File name: '.$ThisFileInfo['filenamepath'].'<br>';
echo 'Artist: '.(!empty($ThisFileInfo['comments_html']['artist'])
    ? implode('<BR>', $ThisFileInfo['comments_html']['artist'])
    : '&nbsp;').'<br>';
echo 'Title: '.(!empty($ThisFileInfo['comments_html']['title'])
    ? implode('<BR>', $ThisFileInfo['comments_html']['title'])
    : '&nbsp;').'<br>';
echo 'Bitrate: '.(!empty($ThisFileInfo['audio']['bitrate'])
    ? round($ThisFileInfo['audio']['bitrate'] / 1000).' kbps'
    : '&nbsp;').'<br>';
echo 'Play time: '.(!empty($ThisFileInfo['playtime_string'])
    ? $ThisFileInfo['playtime_string']
    : '&nbsp;').'<br>';
?>

给你一个快速的答案:文件必须先下载到你的服务器。

下面是一个简单的例子:

$SongPath = 'http://localhost/PE/uploads/pe_discussion/videos/Wildlife.wmv';
if($SongPath){
$filename = tempnam('/tmp','getid3');
if (file_put_contents($filename, file_get_contents($SongPath, false, null, 0, 300000))) {
   if (require_once('getid3/getid3.php')) {
      $getID3 = new getID3;
      $ThisFileInfo = $getID3->analyze($filename);
   }
   unlink($filename);
}
}
var_dump($ThisFileInfo['playtime_string']);

PS:只需将$SongPath替换为现有的外部url。