提取 ID3 音频标签时允许 unicode

Allow unicode when extract ID3 audio tags

我使用下面的 PHP 代码从 MP3 文件中提取元数据。它工作正常。

但是当Title标签包含特殊字符时,标题会被自动删除。

如何使其能够使用 unicode。标题标签示例:áåðÐáýúíó ?

<?php
$mp3 = 'example.mp3';
$myResult = tagReader($mp3);
print_r($myResult);
function tagReader($file){
    $id3v23 = array ("TIT2","TALB","TPE1","TRCK","TDRC","TLEN","USLT", "TT2");
    $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 = "";
            }
        }
    }

    return $result;
}

?>

解法:

<?php
$mp3 = 'example.mp3';
$myResult = tagReader($mp3);
print_r($myResult);
function unichr($i) {
    return iconv('UCS-4LE', 'UTF-8', pack('V', $i));
}

function tagReader($file){
    $id3v23 = array ("TIT2","TALB","TPE1","TRCK","TDRC","TLEN","USLT", "TT2");
    $fsize = filesize($file);
    $fd = fopen($file,"r");
    $tag = fread($fd,$fsize);
    $tmp = "";
    fclose($fd);
    if (substr($tag,0,3) == "ID3") {
        $result['FileName'] = $file;
        $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'] = utf8_encode(substr($tmp,13));
                }
                $tmp = "";
            }
        }
    }
    return $result;
}
?>