2012-10-02 154 views
1

我有以下腳本來讀取mp3文件的id3標籤。我手動檢查過,id3標籤有MP3文件,但我的輸出總是返回幾個文件: MP3 file does not have any ID3 tag!從ffmpeg轉換的mp3文件中讀取id3標籤

我正在從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");  
    } 
?>  
+0

你確定他們是ID3v1標籤,而不是ID3v2標籤嗎? ID3v2是完全不同的。 – Brad

+0

@brad它們有時可能有時是1 2.在這種情況下它是2 – payal

+0

如果你想解析ID3v2,那麼你需要編寫一個ID3v2解析器! http://stackoverflow.com/questions/173257/writing-id3v2-tag-parsing-code-need-good-examples-to-test – Brad

回答

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; 
} 

?> 
+0

順便說一句,你的代碼包含一個錯字,它是'長度',而不是'長度'。 – nalply

+0

@nalply它的打字錯誤 – payal

0

有一個PECL擴展已經解決了ID3數據的解析。您可以在PHP手冊中閱讀它:http://www.php.net/manual/en/book.id3.php

+0

thx爲您的答案,請你告訴我爲什麼我的輸出是這樣的。 Array([FileName] => 4.mp3 [TAG] => ID3 [Version] => 3.0 [Title] =>&Bahut Khubsurat Ghazal(mr-Jatt.Com)[專輯] => 1Loverz Choice(Khubsurat Ghazal)( www.mzc.in)[作者] => DJ Badboy(mr-Jatt.Com)[Track] =>(www.mzc.in))我螞蟻整齊的輸出存儲在數據庫中,請告訴我如何可以存儲每個數據separetly在mysql – payal

+0

thx爲您的答案可以請告訴我爲什麼我的輸出合作,像這樣。 Array([FileName] => 4.mp3 [TAG] => ID3 [Version] => 3.0 [Title] =>&Bahut Khubsurat Ghazal(mr-Jatt.Com)[專輯] => 1Loverz Choice(Khubsurat Ghazal)( www.mzc.in)[作者] => DJ Badboy(mr-Jatt.Com)[Track] =>(www.mzc.in))我螞蟻整齊的輸出存儲在數據庫中,請告訴我如何可以存儲每個數據separetly在MySQL中 – payal