Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php mp3 string error

i have a mp3 class to read mp3s for my site. (i cannot install the module since its a shared hosting). i upload the mp3 and then the system read it using my class and insert into my mysql the file name and location, and the basic tag (artist, song name, album). the mysql insert is ok but i have problem with the strings i am inserting.

here's my code: 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);

now, when i read the value i get this:

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

what is wrong with my code?

like image 429
mario bros Avatar asked Dec 29 '11 02:12

mario bros


1 Answers

based on your code, the $value needs to be trim or converted to UTF (I prefer converting to UTF). I added function that will check which UTF is the mp3 and parse it correctly:

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

Here's a 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());

To get more information about tags, read this wiki article: http://en.wikipedia.org/wiki/ID3

like image 88
Book Of Zeus Avatar answered Oct 23 '22 05:10

Book Of Zeus