2011-12-29 6 views
13

tôi có một lớp mp3 để đọc các tệp mp3 cho trang web của mình. (Tôi không thể cài đặt mô-đun kể từ khi nó được chia sẻ lưu trữ). tôi tải lên mp3 và sau đó hệ thống đọc nó bằng cách sử dụng lớp của tôi và chèn vào mysql của tôi tên tệp và vị trí, và thẻ cơ bản (nghệ sĩ, tên bài hát, album). chèn mysql là ok nhưng tôi có vấn đề với các chuỗi tôi đang chèn.lỗi chuỗi mp3 mp3

đây là mã của tôi: http://pastebin.com/fXsm0c3T

<?php 



class Id3 { 



    private $tags = array(

     'TALB' => 'album', 'TCON' => 'genre', 'TENC' => 'encoder', 

    'TIT2' => 'title', 'TPE1' => 'artist', 'TPE2' => 'ensemble', 'TYER' => 'year', 'TCOM' => 'composer', 

     'TCOP' => 'copyright', 'TRCK' => 'track', 'WXXX' => 'url', 

     'COMM' => 'comment' 

    ); 



    private $genre = array(

     0 => 'Blues', 1 => 'Classic Rock', 2 => 'Country', 3 => 'Dance', 4 => 'Disco', 5 => 'Funk', 6 => 'Grunge', 7 => 'Hip-Hop', 8 => 'Jazz', 9 => 'Metal', 10 => 'New Age', 11 => 'Oldies', 12 => 'Other', 

    13 => 'Pop', 14 => 'R&B', 15 => 'Rap', 16 => 'Reggae', 17 => 'Rock', 18 => 'Techno', 19 => 'Industrial', 20 => 'Alternative', 21 => 'Ska', 22 => 'Death Metal', 23 => 'Pranks', 24 => 'Soundtrack', 25 => 'Euro-Techno', 26 => 'Ambient', 27 => 'Trip-Hop', 28 => 'Vocal', 

     29 => 'Jazz+Funk', 30 => 'Fusion', 31 => 'Trance', 32 => 'Classical', 33 => 'Instrumental', 34 => 'Acid', 35 => 'House', 

     36 => 'Game', 37 => 'Sound Clip', 38 => 'Gospel', 39 => 'Noise', 40 => 'Alternative Rock', 41 => 'Bass', 42 => 'Soul', 43 => 'Punk', 44 => 'Space', 45 => 'Meditative', 46 => 'Instrumental Pop', 47 => 'Instrumental Rock', 48 => 'Ethnic', 

      49 => 'Gothic', 50 => 'Darkwave', 51 => 'Techno-Industrial', 52 => 'Electronic', 53 => 'Pop-Folk', 54 => 'Eurodance', 55 => 'Dream', 56 => 'Southern Rock', 57 => 'Comedy', 58 => 'Cult', 59 => 'Gangsta', 60 => 'Top 40', 61 => 'Christian Rap', 62 => 'Pop/Funk', 63 => 'Jungle', 64 => 'Native US', 65 => 'Cabaret', 66 => 'New Wave', 67 => 'Psychadelic', 68 => 'Rave', 69 => 'Showtunes', 70 => 'Trailer', 71 => 'Lo-Fi', 72 => 'Tribal', 73 => 'Acid Punk', 74 => 'Acid Jazz', 75 => 'Polka', 76 => 'Retro', 77 => 'Musical', 78 => 'Rock & Roll', 79 => 'Hard Rock', 80 => 'Folk', 81 => 'Folk-Rock', 82 => 'National Folk', 83 => 'Swing', 84 => 'Fast Fusion', 85 => 'Bebob', 86 => 'Latin', 

     87 => 'Revival', 88 => 'Celtic', 89 => 'Bluegrass', 90 => 'Avantgarde', 91 => 'Gothic Rock', 92 => 'Progressive Rock', 93 => 'Psychedelic Rock', 94 => 'Symphonic Rock', 95 => 'Slow Rock', 96 => 'Big Band', 97 => 'Chorus', 98 => 'Easy Listening', 99 => 'Acoustic', 

      100 => 'Humour', 101 => 'Speech', 102 => 'Chanson', 103 => 'Opera', 104 => 'Chamber Music', 105 => 'Sonata', 106 => 'Symphony', 107 => 'Booty Bass', 108 => 'Primus', 109 => 'Porn Groove', 110 => 'Satire', 111 => 'Slow Jam', 112 => 'Club', 

    113 => 'Tango', 114 => 'Samba', 115 => 'Folklore', 116 => 'Ballad', 117 => 'Power Ballad', 118 => 'Rhytmic Soul', 119 => 'Freestyle', 120 => 'Duet', 121 => 'Punk Rock', 122 => 'Drum Solo', 123 => 'Acapella', 124 => 'Euro-House', 

     125 => 'Dance Hall', 126 => 'Goa', 127 => 'Drum & Bass', 128 => 'Club-House', 129 => 'Hardcore', 130 => 'Terror', 131 => 'Indie', 132 => 'BritPop', 133 => 'Negerpunk', 134 => 'Polsk Punk', 135 => 'Beat', 136 => 'Christian Gangsta Rap', 137 => 'Heavy Metal', 138 => 'Black Metal', 139 => 'Crossover', 140 => 'Contemporary Christian', 141 => 'Christian Rock', 142 => 'Merengue', 143 => 'Salsa', 144 => 'Trash Metal', 145 => 'Anime', 146 => 'Jpop', 147 => 'Synthpop' 

     ); 



    public function __construct() { 

     $this->info = ''; 

    } 



    private function getId3() { 

    $handle = fopen($this->file, 'r'); 

      $head = fread($handle,10); 

      $head = unpack("a3signature/c1version_major/c1version_minor/c1flags/Nsize", $head); 

     $result = array(); 

    for ($i = 0; $i<5; $i++){ 

      $tag = trim(fread($handle, 6)); 

      if (!isset($this->tags[$tag])) continue;   

     $size = fread($handle, 2); 

      $size = unpack('n', $size); $size = $size[1]+2; 

     $value = fread($handle, $size); 

     $this->info[$this->tags[$tag]] = $value; 

     }  fclose($handle); 

    } 



    public function load($file) { 

     $this->file = $file; $this->getId3(); } 

} 





$id3 = new Id3(); 

    $id3->load('mp3.mp3'); 

print_R($id3->info); 

bây giờ, khi tôi đọc các giá trị i có được điều này:

Array 
(
    [genre] => ��Rock 
    [title] => ��Walk 
    [artist] => ��Foo Fighters 
    [album] => ��Wasting Light 
) 

gì là sai với mã của tôi?

+3

gì chars lạ? –

+0

Có thể bạn đang sử dụng thuật toán sai. Nên có mã exisiting cho vấn đề của bạn, chỉ cần sử dụng một cái gì đó mà làm việc, có lẽ bắt đầu ở đây: http://php.net/manual/en/ref.id3.php – hakre

+3

như tôi đã nói trong câu hỏi của tôi, những id3 không có sẵn và tôi không thể thêm chúng –

Trả lời

17

dựa trên mã của bạn, giá trị $ cần được cắt hoặc chuyển đổi thành UTF (tôi thích chuyển sang UTF). tôi đã thêm chức năng sẽ kiểm tra UTF là mp3 và phân tích nó một cách chính xác:

$value = $this->filter($value, $tag); // convert to UTF or else that is why you have weird chars 

Dưới đây là một readable code.

<?php 

class Id3 { 

    private $tags = array(
    'TALB' => 'album', 
    'TCON' => 'genre', 
    'TENC' => 'encoder', 
    'TIT2' => 'title', 
    'TPE1' => 'artist', 
    'TPE2' => 'ensemble', 
    'TYER' => 'year', 
    'TCOM' => 'composer', 
    'TCOP' => 'copyright', 
    'TRCK' => 'track', 
    'WXXX' => 'url', 
    'COMM' => 'comment' 
); 

    private $genre = array(
    0 => 'Blues', 
    1 => 'Classic Rock', 
    2 => 'Country', 
    3 => 'Dance', 
    4 => 'Disco', 
    5 => 'Funk', 
    6 => 'Grunge', 
    7 => 'Hip-Hop', 
    8 => 'Jazz', 
    9 => 'Metal', 
    10 => 'New Age', 
    11 => 'Oldies', 
    12 => 'Other', 
    13 => 'Pop', 
    14 => 'R&B', 
    15 => 'Rap', 
    16 => 'Reggae', 
    17 => 'Rock', 
    18 => 'Techno', 
    19 => 'Industrial', 
    20 => 'Alternative', 
    21 => 'Ska', 
    22 => 'Death Metal', 
    23 => 'Pranks', 
    24 => 'Soundtrack', 
    25 => 'Euro-Techno', 
    26 => 'Ambient', 
    27 => 'Trip-Hop', 
    28 => 'Vocal', 
    29 => 'Jazz+Funk', 
    30 => 'Fusion', 
    31 => 'Trance', 
    32 => 'Classical', 
    33 => 'Instrumental', 
    34 => 'Acid', 
    35 => 'House', 
    36 => 'Game', 
    37 => 'Sound Clip', 
    38 => 'Gospel', 
    39 => 'Noise', 
    40 => 'Alternative Rock', 
    41 => 'Bass', 
    42 => 'Soul', 
    43 => 'Punk', 
    44 => 'Space', 
    45 => 'Meditative', 
    46 => 'Instrumental Pop', 
    47 => 'Instrumental Rock', 
    48 => 'Ethnic', 
    49 => 'Gothic', 
    50 => 'Darkwave', 
    51 => 'Techno-Industrial', 
    52 => 'Electronic', 
    53 => 'Pop-Folk', 
    54 => 'Eurodance', 
    55 => 'Dream', 
    56 => 'Southern Rock', 
    57 => 'Comedy', 
    58 => 'Cult', 
    59 => 'Gangsta', 
    60 => 'Top 40', 
    61 => 'Christian Rap', 
    62 => 'Pop/Funk', 
    63 => 'Jungle', 
    64 => 'Native US', 
    65 => 'Cabaret', 
    66 => 'New Wave', 
    67 => 'Psychadelic', 
    68 => 'Rave', 
    69 => 'Showtunes', 
    70 => 'Trailer', 
    71 => 'Lo-Fi', 
    72 => 'Tribal', 
    73 => 'Acid Punk', 
    74 => 'Acid Jazz', 
    75 => 'Polka', 
    76 => 'Retro', 
    77 => 'Musical', 
    78 => 'Rock & Roll', 
    79 => 'Hard Rock', 
    80 => 'Folk', 
    81 => 'Folk-Rock', 
    82 => 'National Folk', 
    83 => 'Swing', 
    84 => 'Fast Fusion', 
    85 => 'Bebob', 
    86 => 'Latin', 
    87 => 'Revival', 
    88 => 'Celtic', 
    89 => 'Bluegrass', 
    90 => 'Avantgarde', 
    91 => 'Gothic Rock', 
    92 => 'Progressive Rock', 
    93 => 'Psychedelic Rock', 
    94 => 'Symphonic Rock', 
    95 => 'Slow Rock', 
    96 => 'Big Band', 
    97 => 'Chorus', 
    98 => 'Easy Listening', 
    99 => 'Acoustic', 
    100 => 'Humour', 
    101 => 'Speech', 
    102 => 'Chanson', 
    103 => 'Opera', 
    104 => 'Chamber Music', 
    105 => 'Sonata', 
    106 => 'Symphony', 
    107 => 'Booty Bass', 
    108 => 'Primus', 
    109 => 'Porn Groove', 
    110 => 'Satire', 
    111 => 'Slow Jam', 
    112 => 'Club', 
    113 => 'Tango', 
    114 => 'Samba', 
    115 => 'Folklore', 
    116 => 'Ballad', 
    117 => 'Power Ballad', 
    118 => 'Rhytmic Soul', 
    119 => 'Freestyle', 
    120 => 'Duet', 
    121 => 'Punk Rock', 
    122 => 'Drum Solo', 
    123 => 'Acapella', 
    124 => 'Euro-House', 
    125 => 'Dance Hall', 
    126 => 'Goa', 
    127 => 'Drum & Bass', 
    128 => 'Club-House', 
    129 => 'Hardcore', 
    130 => 'Terror', 
    131 => 'Indie', 
    132 => 'BritPop', 
    133 => 'Negerpunk', 
    134 => 'Polsk Punk', 
    135 => 'Beat', 
    136 => 'Christian Gangsta Rap', 
    137 => 'Heavy Metal', 
    138 => 'Black Metal', 
    139 => 'Crossover', 
    140 => 'Contemporary Christian', 
    141 => 'Christian Rock', 
    142 => 'Merengue', 
    143 => 'Salsa', 
    144 => 'Trash Metal', 
    145 => 'Anime', 
    146 => 'Jpop', 
    147 => 'Synthpop' 
); 
    private $file; 
    private $info; 

    public function __construct() { 
    $this->info = array(
     'genre' => 'unknow', 
     'title' => 'unknow', 
     'artist' => 'unknow', 
     'album' => 'unknow', 
    ); 
    } 

    private function filter($tag, $type) { 
    if ($type == 'COMM') { 
     $tag = substr($tag, 0, 3) . substr($tag, 10); 
    } 
    if(ord($tag[2]) == 0) { 
     return iconv('UTF-8', 'ISO-8859-1', substr($tag, 3)); 
    } 
    elseif(ord($tag[2]) == 1) { 
     return iconv('UTF-16LE', 'UTF-8', substr($tag, 5)); 
    } 
    elseif(ord($tag[2]) == 2) { 
     return iconv('UTF-16BE', 'UTF-8', substr($tag, 5)); 
    } 
    elseif(ord($tag[2]) == 3) { 
     return substr($tag, 3); 
    } 
    return false; 
    } 

    private function getId3() { 
    $handle = fopen($this->file, 'rb'); 
    $head = fread($handle, 10); 
    $head = unpack("a3signature/c1version_major/c1version_minor/c1flags/Nsize", $head); 
    if (!$head['signature'] == 'ID3') { 
     fclose($handle); 
     return false; 
    } 
    $result = array(); 
    for ($i = 0; $i < 22; $i++) { //22 most popular tags, you had 5 therefore only the first five. 
     $tag = rtrim(fread($handle, 6)); 
     if (!isset($this->tags[$tag])) { 
     continue; 
     } 
     $size = fread($handle, 2); 
     $size = unpack('n', $size); 
     $size = $size[1]+2; 
     $value = fread($handle, $size); 
     $value = $this->filter($value, $tag); // convert to UTF or else that is why you have weird chars 
     $this->info[$this->tags[$tag]] = $value; 
    } 
    fclose($handle); 
    } 

    public function load($file) { 
    $this->file = $file; 
    $this->getId3(); 
    } 

    public function getInfo() { 
    return $this->info; 
    } 

} 

$id3 = new Id3(); 
$id3->load('mp3.mp3'); 
print_R($id3->getInfo()); 

Để biết thêm thông tin về thẻ, đọc bài viết wiki này: http://en.wikipedia.org/wiki/ID3

+1

hoạt động này nhờ –

+0

bạn được chào đón –