我正在努力从我的媒体服务器上获取一些id3标签。我找到了一种从mp3的
中挖掘ID3标签的方法<?php
// From here: http://www.autistici.org/ermes/index.php?pag=1&post=15
// and fixed here: http://www.barattalo.it
// ------------------------------
// example:
// print_r( tagReader ("myfile.mp3") );
// ------------------------------
function tagReader($file){
$id3v23 = array("TIT2","TALB","TPE1","TRCK","TDRC","TLEN","USLT");
$id3v22 = array("TT2","TAL","TP1","TRK","TYE","TLE","ULT");
$fsize = filesize($file);
$fd = fopen($file,"r");
$tag = fread($fd,$fsize);
$tmp = "";
fclose($fd);
if (substr($tag,0,3) == "ID3") {
$result['FileName'] = $file;
$result['TAG'] = substr($tag,0,3);
$result['Version'] = hexdec(bin2hex(substr($tag,3,1))).".".hexdec(bin2hex(substr($tag,4,1)));
}
if($result['Version'] == "4.0" || $result['Version'] == "3.0"){
for ($i=0;$i<count($id3v23);$i++){
if (strpos($tag,$id3v23[$i].chr(0))!= FALSE){
$pos = strpos($tag, $id3v23[$i].chr(0));
$len = hexdec(bin2hex(substr($tag,($pos+5),3)));
$data = substr($tag, $pos, 9+$len);
for ($a=0;$a<strlen($data);$a++){
$char = substr($data,$a,1);
if($char >= " " && $char <= "~") $tmp.=$char;
}
if(substr($tmp,0,4) == "TIT2") $result['Title'] = substr($tmp,4);
if(substr($tmp,0,4) == "TALB") $result['Album'] = substr($tmp,4);
if(substr($tmp,0,4) == "TPE1") $result['Author'] = substr($tmp,4);
if(substr($tmp,0,4) == "TRCK") $result['Track'] = substr($tmp,4);
if(substr($tmp,0,4) == "TDRC") $result['Year'] = substr($tmp,4);
if(substr($tmp,0,4) == "TLEN") $result['Lenght'] = substr($tmp,4);
if(substr($tmp,0,4) == "USLT") $result['Lyric'] = substr($tmp,7);
$tmp = "";
}
}
}
if($result['Version'] == "2.0"){
for ($i=0;$i<count($id3v22);$i++){
if (strpos($tag,$id3v22[$i].chr(0))!= FALSE){
$pos = strpos($tag, $id3v22[$i].chr(0));
$len = hexdec(bin2hex(substr($tag,($pos+3),3)));
$data = substr($tag, $pos, 6+$len);
for ($a=0;$a<strlen($data);$a++){
$char = substr($data,$a,1);
if($char >= " " && $char <= "~") $tmp.=$char;
}
if(substr($tmp,0,3) == "TT2") $result['Title'] = substr($tmp,3);
if(substr($tmp,0,3) == "TAL") $result['Album'] = substr($tmp,3);
if(substr($tmp,0,3) == "TP1") $result['Author'] = substr($tmp,3);
if(substr($tmp,0,3) == "TRK") $result['Track'] = substr($tmp,3);
if(substr($tmp,0,3) == "TYE") $result['Year'] = substr($tmp,3);
if(substr($tmp,0,3) == "TLE") $result['Lenght'] = substr($tmp,3);
if(substr($tmp,0,3) == "ULT") $result['Lyric'] = substr($tmp,6);
$tmp = "";
}
}
}
return $result;
}
和子请求调用
print_r(tagReader("/mnt/data/downloads/music/Arctic Monkeys/Who The F Are Arctic Monkeys_ - EP/05 Who The F Are Arctic Monkeys_.mp3"));
我将此文件复制到一个更简单的路径(即使是空格),并且该方法可以正常工作。它似乎不适用于长文件名。我尝试使用\
转义空格,但这不起作用。
答案 0 :(得分:1)
整洁的小脚本!我自己为一个流行的媒体共享网站用PHP做了一些MP3的媒体处理,我们一直依靠命令行工具来读取ID3标签。我从来没有得过一个脚本来在PHP中读取这些标签。
无论如何,不需要逃避路径。如果没有它,PHP会将它正确地传递给底层操作系统。
我重新创建了您提供的/mnt/data/...
路径,并在其中放置了我自己的MP3文件,其文件名与您提供的完全相同。我还通过在脚本顶部添加error_reporting(E_ALL);
来启用错误报告。该脚本没有错误,它在我的MP3文件中正确显示了ID3标签。
在我看来,该文件不在您认为的路径上,或者您的/mnt/data
挂载点可能存在问题。你检查过这些东西了吗?您可以使用以下脚本检查PHP是否能够查看和读取该文件:
<?php
$path = "/mnt/data/downloads/music/Arctic Monkeys/Who The F Are Arctic Monkeys_ - EP/05 Who The F Are Arctic Monkeys_.mp3";
if (!is_file($path)) {
throw new Exception("No such file");
} else if (!is_readable($path)) {
throw new Exception("Path exists, but is not readable");
}
?>
答案 1 :(得分:0)
我实际上在上面发表了评论,我最终开启了错误报告。出现错误,我得到了Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 13484377 bytes) in /opt/share/www/hi.php on line 16
所以我添加了ini_set("memory_limit","20M");
并修复了它