1

我有以下脚本用于读取id3mp3 文件的标签。我手动检查id3了 mp3 文件中是否存在标签,但我的输出总是返回一些文件: MP3 file does not have any ID3 tag!

我正在从 ffmpeg 转换这些文件。当我为原始文件运行以下代码时,它显示了id3标签,但是当我为转换后的文件运行脚本时(通过 ffmpeg),它没有显示任何id3标签。我已经下载了原始文件和转换后的文件并检查了它们,发现这两个文件具有完全相同的标签,但是下面的代码仍然给出了错误消息。

这是代码:

 <?php
 $mp3 = "4.mp3"; //The mp3 file.

 $filesize = filesize($mp3);
 $file = fopen("4.mp3", "r");

 fseek($file, -128, SEEK_END); // It reads the 

 $tag = fread($file, 3);

 if($tag == "TAG")
 {
     $data["title"] = trim(fread($file, 30));

     $data["artist"] = trim(fread($file, 30));

     $data["album"] = trim(fread($file, 30));

     $data["year"] = trim(fread($file, 4));

      $data["genre"] = trim(fread($file, 1));



   }
   else
     die("MP3 file does not have any ID3 tag!");

   fclose($file);

   while(list($key, $value) = each($data))
   {
     print("$key: $value<br>\r\n");    
  }
 ?>    
4

2 回答 2

1

它需要正确的id3版本。我已经用这段代码解决了这个问题:

<?

print_r( tagReader ("4.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;
}

?>
于 2012-10-02T04:23:57.187 回答
0

有一个 PECL 扩展已经解决了 ID3 数据的解析。您可以在 PHP 手册中阅读它:http ://www.php.net/manual/en/book.id3.php 。

于 2012-10-02T04:49:41.710 回答