Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
ID3 reader - encoding problem
Old 06-02-2009, 08:05 PM ID3 reader - encoding problem
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
I came across some PHP code that reads Id3 tag information from .mp3 files. When returning the values of the specified Id3 tag information, it adds the following characters which I suspect to be an encoding problem: ÿþ.

A screen shot:



The three pages that make up the entire code:


Page 1 - id3.class.php

PHP Code:
<?php
/***********************************************************
* Class:       ID3
* Version:     1.0
* Date:        Janeiro 2004
* Author:      Tadeu F. Oliveira
* Contact:     tadeu_fo@yahoo.com.br
* Use:         Extract ID3 Tag information from mp3 files
***********************************************************
Exemple

    require('error.inc.php');
    $nome_arq  = 'Blind Guardian - Bright Eyes.mp3';
     $myId3 = new ID3($nome_arq);
     if ($myId3->getInfo()){
         echo('<HTML>');
         echo('<a href= "'.$nome_arq.'">Clique para baixar: </a><br>');
         echo('<table border=1>
               <tr>
                  <td><strong>Artista</strong></td>
                  <td><strong>Titulo</strong></font></div></td>
                  <td><strong>Trilha</strong></font></div></td>
                  <td><strong>Album/Ano</strong></font></div></td>
                  <td><strong>G&ecirc;nero</strong></font></div></td>
                  <td><strong>Coment&aacute;rios</strong></font></div></td>
               </tr>
               <tr>
                  <td>'. $myId3->getArtist() . '&nbsp</td>
                  <td>'. $myId3->getTitle()  . '&nbsp</td>
                  <td>'. $myId3->getTrack()  . '&nbsp</td>
                  <td>'. $myId3->getAlbum()  . '/'.$myId3->getYear().'&nbsp</td>
                  <td>'. $myId3->getGender() . '&nbsp</td>
                  <td>'. $myId3->tags['COMM']. '&nbsp</td>
               </tr>
            </table>');
         echo('</HTML>');
       }else{
        echo($errors[$myId3->last_error_num]);
   }

*/


class ID3{

   var 
$file_name=''//full path to the file
                         //the sugestion is that this path should be a
                      //relative path
   
var $tags;   //array with ID3 tags extracted from the file
   
var $last_error_num=0//keep the number of the last error ocurred
   
var $tags_count 0// the number of elements at the tags array
   /*********************/
   /**private functions**/
   /*********************/

   
function hex2bin($data) {
   
//thankz for the one who wrote this function
   //If iknew your name I would say it here
      
$len strlen($data);
      for(
$i=0;$i<$len;$i+=2) {
         
$newdata .= pack("C",hexdec(substr($data,$i,2)));
      }
   return 
$newdata;
   }
   
   function 
get_frame_size($fourBytes){
      
$tamanho[0] = str_pad(base_convert(substr($fourBytes,0,2),16,2),7,0,STR_PAD_LEFT);
      
$tamanho[1] = str_pad(base_convert(substr($fourBytes,2,2),16,2),7,0,STR_PAD_LEFT);
      
$tamanho[2] = str_pad(base_convert(substr($fourBytes,4,2),16,2),7,0,STR_PAD_LEFT);
      
$tamanho[3] = str_pad(base_convert(substr($fourBytes,6,2),16,2),7,0,STR_PAD_LEFT);
      
$total =    $tamanho[0].$tamanho[1].$tamanho[2].$tamanho[3];
      
$tamanho[0] = substr($total,0,8);
      
$tamanho[1] = substr($total,8,8);
      
$tamanho[2] = substr($total,16,8);
      
$tamanho[3] = substr($total,24,8);
      
$total =    $tamanho[0].$tamanho[1].$tamanho[2].$tamanho[3];
        
$total base_convert($total,2,10);
       return 
$total;
    }
    
   function 
extractTags($text,&$tags){
      
$size = -1;//inicializando diferente de zero para não sair do while
       
while ((strlen($text) != 0) and ($size != 0)){
      
//while there are tags to read and they have a meaning
       //while existem tags a serem tratadas e essas tags tem conteudo
            
$ID    substr($text,0,4);
          
$aux   substr($text,4,4);
         
$aux   bin2hex($aux);
         
$size  $this->get_frame_size($aux);
         
$flags substr($text,8,2);
         
$info  substr($text,11,$size-1);
         if (
$size != 0){
            
$tags[$ID] = $info;
            
$this->tags_count++;
         }
         
$text substr($text,10+$size,strlen($text));
       }
   }
   
   
/********************/
   /**public functions**/
   /********************/
   /**Constructor**/

   
function ID3($file_name){
      
$this->file_name $file_name;
      
$this->last_error_num 0;
   }
   
   
/**Read the file and put the TAGS
   content on $this->tags array**/
   
function getInfo(){
        if (
$this->file_name != ''){
            
$mp3 = @fopen($this->file_name,"r");
           
$header = @fread($mp3,10);
         if (!
$header) {
             
$this->last_error_num 2;
            return 
false;
            die();
         }
           if (
substr($header,0,3) != "ID3"){
             
$this->last_error_num 3;
            return 
false;
              die();
           }
           
$header bin2hex($header);
           
$version base_convert(substr($header,6,2),16,10).".".base_convert(substr($header,8,2),16,10);
           
$flags base_convert(substr($header,10,2),16,2);
           
$flags str_pad($flags,8,0,STR_PAD_LEFT);
           if (
$flags[7] == 1){
               
//echo('with Unsynchronisation<br>');
           
}
           if (
$flags[6] == 1){
               
//echo('with Extended header<br>');
           
}
           if (
$flags[5] == 1){//Esperimental tag
            
$this->last_error_num 4;
            return 
false;
              die();
           }
           
$total $this->get_frame_size(substr($header,12,8));
         
$text = @fread($mp3,$total);
           
fclose($mp3);
         
$this->extractTags($text,$this->tags);
      }
      else{
         
$this->last_error_num 1;//file not set
         
return false;
          die();
      }
       return 
true;
   }
   
   
/*************
   *   PUBLIC
   * Functions to get information
   * from the ID3 tag
   **************/
   
function getArtist(){
      if (
array_key_exists('TPE1',$this->tags)){
          return 
$this->tags['TPE1'];
      }else{
          
$this->last_error_num 5;
         return 
false;
      }
   }
   
   function 
getTrack(){
      if (
array_key_exists('TRCK',$this->tags)){
          return 
$this->tags['TRCK'];
      }else{
          
$this->last_error_num 5;
         return 
false;
      }
   }
   
   function 
getTitle(){
      if (
array_key_exists('TIT2',$this->tags)){
          return 
$this->tags['TIT2'];
      }else{
          
$this->last_error_num 5;
         return 
false;
      }
   }
   
   function 
getAlbum(){
      if (
array_key_exists('TALB',$this->tags)){
          return 
$this->tags['TALB'];
      }else{
          
$this->last_error_num 5;
         return 
false;
      }
   }
   
   function 
getYear(){
      if (
array_key_exists('TYER',$this->tags)){
          return 
$this->tags['TYER'];
      }else{
          
$this->last_error_num 5;
         return 
false;
      }
   }
   
   function 
getGender(){
      if (
array_key_exists('TCON',$this->tags)){
          return 
$this->tags['TCON'];
      }else{
          
$this->last_error_num 5;
         return 
false;
      }
   }
   
}
?>
Page 2 - sample.php

PHP Code:
<?php
    
require('error.inc.php');
    require(
'id3.class.php');
    
$nome_arq  '../music/files/the all-american rejects - dirty little secret.mp3';
     
$myId3 = new ID3($nome_arq);
     if (
$myId3->getInfo()){
         echo(
'<HTML>');
         echo(
'<a href= "'.$nome_arq.'">Click to download: </a><br>');
         echo(
'<table border=1>
               <tr>
                  <td><strong>Artist</strong></td>
                  <td><strong>Title</strong></font></div></td>
                  <td><strong>Track</strong></font></div></td>
                  <td><strong>Album/Year</strong></font></div></td>
                  <td><strong>Genre</strong></font></div></td>
                  <td><strong>Comments</strong></font></div></td>
               </tr>
               <tr>
                  <td>'
$myId3->getArtist() . '&nbsp</td>
                  <td>'
$myId3->getTitle()  . '&nbsp</td>
                  <td>'
$myId3->getTrack()  . '&nbsp</td>
                  <td>'
$myId3->getAlbum()  . '/'.$myId3->getYear().'&nbsp</td>
                  <td>'
$myId3->getGender() . '&nbsp</td>
                  <td>'
$myId3->tags['COMM']. '&nbsp</td>
               </tr>
            </table>'
);
         echo(
'</HTML>');
       }else{
        echo(
$errors[$myId3->last_error_num]);
   }
?>
Page 3 - error.inc.php

PHP Code:
<?php

$errors
[0] = '';//means no error. (Change it and things can become very strange)
$errors[1] = 'File Name not set';
$errors[2] = 'Unable to open MP3 file';
$errors[3] = 'ID3v2 Tag not found on this file';
$errors[4] = 'TAG not Supported';
$errors[5] = 'Tag not found(maybe you need to call getInfo() first?)';

/*$errors[0] = '';//sem erro. (Mude isto e as coisas podem ficar BEM estranhas)
$errors[1] = 'Nome do Arquivo não definido';
$errors[2] = 'Impossível encontrar arquivo .MP3';
$errors[3] = 'Tag ID3v2 Não encontrada neste arquivo';
$errors[4] = 'TAG não suportada';
$errors[5] = 'Tag não encontrada(talvez vc tenha esqquecido de chamar getInfo() antes?)';
*/


?>
Sorry about some of it being Portuguese. I converted most of it to English.

Thanks a lot!!
Moatist
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>

Last edited by moatist; 06-02-2009 at 08:13 PM..
moatist is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-02-2009, 08:15 PM Re: ID3 reader - encoding problem
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
You could use str_replace(http://uk3.php.net/manual/en/function.str-replace.php) to remove the extra thing, for example:

PHP Code:
str_replace('ÿþ'''$input); // this looks for ÿþ in the $input and replaces it with nothing. 
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 06-03-2009, 09:12 AM Re: ID3 reader - encoding problem
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
I think your offsets in the parser are probably just off.
I've got some info for reading/parsing ID3 tags on my site:
http://willcode4beer.com/parsing.jsp?set=mp3ID3

It's java but, the part you would care about is essentially the same.

Instead of using substr to find the parts in the file, just iterate through the bytes. It will be much more reliable.
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to ID3 reader - encoding problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13361 seconds with 12 queries