The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 05:50:18 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: TDs class to read id3v2 MP3 headers  (Read 2430 times)
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« on: April 24, 2007, 11:16:26 PM »

Found it Perk.

Code:
<?php
        
// Class to read id3v2 tags

        
class id3v2
        
{
                var 
$m_fd$m_artists$m_name$m_track$m_tag$m_bad_format false;

                
// Constructor
                
function id3v2$mp3_file )
                {
                        
$fd fopen$mp3_file'r' ); // Open MP3 file
                        
$header fread$fd10 ); // Read 10 byte header
                        
if( !stristr$header"ID3" ) )
                        {
                                
$this->m_bad_format true// Not MP3 or not V2 or corrupted header
                                
return;

                        }

                        
$this->m_tag["version"] = (int)bin2hex$header[3] ); // Fourth byte gives major version number
                        
$this->m_tag["revision"] = (int)bin2hex$header[4] ); // Fifth byte gives revision number
                        
$this->m_tag["flag"] = (int)bin2hex$header[5] ); // Sixth byte holds ID3v2 flag (if present)

                        // The last four bytes represent the size of the tag in bytes, only the 7 least significant bits count
                        // and the combination of the 28 bits is a binary representation of the size.
                        
$this->m_tag["size"] = $this->size$header[6].$header[7].$header[8].$header[9] );

                        
// Read in the entire tag
                        
$this->m_tag["tag_body"] = fread$fd$this->get_tag_size() );


                        
fclose$fd );

                }

                
// Do this better ****************************************
                
function error()
                {
                        return 
$this->m_bad_format;

                }

                
// Calculates size in decimal bytes from 28 bit binary string
                
function size$four_bytes )
                {
                        
// The following is an ugly kludge but....
                        
return bindecsprintf"%07b%07b%07b%07b"hexdecbin2hex$four_bytes[0] ) ),
                                                
hexdecbin2hex$four_bytes[1] ) ), hexdecbin2hex$four_bytes[2] ) ),
                                                
hexdecbin2hex$four_bytes[3] ) ) ) );

                }

                
// Returns major version number
                
function get_version()
                {
                        return 
$this->m_tag["version"];
                }

                
// Returns revision (minor version?) number
                
function get_revision()
                {
                        return 
$this->m_tag["revision"];
                }

                
// Returns size of ID3 tag - 10 byte header
                
function get_tag_size()
                {
                        return 
$this->m_tag["size"];
                }

                function 
get_frame$frame_id )
                {
                   
$holds_frame stristr$this->m_tag["tag_body"], $frame_id );

                   if( !
$holds_frame )
                        return 
false;

                   
$frame_header substr$holds_frame010 );

                   
// Read the frame skipping the encoding byte (I don't support unicode.... anybody?)
                   
$frame substr$holds_frame11$this->size$frame_header[4].$frame_header[5].$frame_header[6].
                                     
$frame_header[7] )-);
                   return 
$frame;
                }

                function 
get_artist()
                {
                   
$artist $this->get_frame"TPE1" );

                   return 
$artist;
                }

                function 
get_album()
                {
                   
$album $this->get_frame"TALB" );

                   return 
$album;
                }

                function 
get_track()
                {
                   
$track $this->get_frame"TRCK" );

                   return 
$track;
                }

                function 
get_title()
                {
                   
$title $this->get_frame"TIT2" );

                   return 
$title;
                }

                function 
get_bpm()
                {
                   
$beats_per_minute $this->get_frame"TBPM" );

                   return 
$beats_per_minute;
                }

        } 
// End id3v2 declaration

?>
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: April 25, 2007, 07:18:51 AM »

That's pretty cool TD ... I've not seen this done before with PHP - it's a great example as well as functional.

Thanks dude!

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!