PHP获取远程.mp3文件的元数据(从URL)
我正在尝试从远程.mp3文件PHP获取远程.mp3文件的元数据(从URL),php,mp3,metadata,Php,Mp3,Metadata,我正在尝试从远程.mp3文件获取歌曲名/艺术家名/歌曲长度/比特率等 我尝试过getID3脚本,但据我所知,它不适用于远程文件,因为我遇到了以下错误:“不支持远程文件-请先在本地复制文件” 此外,该代码: <?php $tag = id3_get_tag( "http://shiro-desu.com/scr/11.mp3" ); print_r($tag); ?> 也不起作用。 “致命错误:在第2行的/home4/shiro/public\u html/scr/index.p
获取
歌曲名
/艺术家名
/歌曲长度
/比特率
等我尝试过getID3脚本,但据我所知,它不适用于远程文件,因为我遇到了以下错误:“不支持远程文件-请先在本地复制文件”
此外,该代码:
<?php
$tag = id3_get_tag( "http://shiro-desu.com/scr/11.mp3" );
print_r($tag);
?>
也不起作用。“致命错误:在第2行的/home4/shiro/public\u html/scr/index.php中调用未定义的函数id3_get_tag()”由于您没有提到错误,我正在考虑一个常见错误案例
未定义函数
您得到的错误(未定义的函数)意味着您的PHP配置中未启用ID3扩展:
如果您没有Id3扩展名文件,请检查安装信息。首先,我没有创建此文件,我只是通过一个完整的示例使其易于理解
您可以在这里阅读更多内容,但这只是因为archive.org。
首先,从以下位置下载此库:
打开zip文件夹时,您将看到“getid3”。将该文件夹保存到您的工作文件夹中
接下来,在运行以下脚本的工作文件夹中创建一个名为“temp”的文件夹
基本上,它所做的是下载文件的前64k,然后从文件中读取元数据
我喜欢一个简单的例子。我希望这有帮助
<?php
require_once("getid3/getid3.php");
$url_media = "http://example.com/myfile.mp3"
$a=getfileinfo($url_media);
echo"<pre>";
echo $a['tags']['id3v2']['album'][0] . "\n";
echo $a['tags']['id3v2']['artist'][0] . "\n";
echo $a['tags']['id3v2']['title'][0] . "\n";
echo $a['tags']['id3v2']['year'][0] . "\n";
echo $a['tags']['id3v2']['year'][0] . "\n";
echo "\n-----------------\n";
//print_r($a['tags']['id3v2']['album']);
echo "-----------------\n";
//print_r($a);
echo"</pre>";
function getfileinfo($remoteFile)
{
$url=$remoteFile;
$uuid=uniqid("designaeon_", true);
$file="temp/".$uuid.".mp3";
$size=0;
$ch = curl_init($remoteFile);
//==============================Get Size==========================//
$contentLength = 'unknown';
$ch1 = curl_init($remoteFile);
curl_setopt($ch1, CURLOPT_NOBODY, true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_HEADER, true);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch1);
curl_close($ch1);
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$contentLength = (int)$matches[1];
$size=$contentLength;
}
//==============================Get Size==========================//
if (!$fp = fopen($file, "wb")) {
echo 'Error opening temp file for binary writing';
return false;
} else if (!$urlp = fopen($url, "r")) {
echo 'Error opening URL for reading';
return false;
}
try {
$to_get = 65536; // 64 KB
$chunk_size = 4096; // Haven't bothered to tune this, maybe other values would work better??
$got = 0; $data = null;
// Grab the first 64 KB of the file
while(!feof($urlp) && $got < $to_get) { $data = $data . fgets($urlp, $chunk_size); $got += $chunk_size; } fwrite($fp, $data); // Grab the last 64 KB of the file, if we know how big it is.
if ($size > 0) {
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RESUME_FROM, $size - $to_get);
curl_exec($ch);
}
// Now $fp should be the first and last 64KB of the file!!
@fclose($fp);
@fclose($urlp);
}
catch (Exception $e) {
@fclose($fp);
@fclose($urlp);
echo 'Error transfering file using fopen and cURL !!';
return false;
}
$getID3 = new getID3;
$filename=$file;
$ThisFileInfo = $getID3->analyze($filename);
getid3_lib::CopyTagsToComments($ThisFileInfo);
unlink($file);
return $ThisFileInfo;
}
?>
致命错误:在/home4/shiro/public\u html/scr/index.php的第2行调用未定义的函数id3\u get\u tag(),我将安装它,我在这里没有看到任何安装链接,所以我认为它不需要任何东西。你知道如何安装我下载的.zip吗?嗯,我正在尝试在实际购买的服务器上安装它,而不是WAMP,但我想这会帮到我很多人,我用它和getID3一起使用,你可以很容易地找到它