MP3 Binary Interogation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

MP3 Binary Interogation

Post by JayBird »

I am looking to veryfy that an MP3 file upload to a server is *actually* an MP3 file

I want to do this by interogating the binary of the file.

I think i have found the file spec for MP3 files here - a little down the page.

I really have no idea where to start with this...could do with a few pointers on what i need to be checking for as i dont fully understand the file spec information, and how i will check this spec using PHP.

Any pointer appreciated.

Thanks
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I saw a code snippet on the zend web site earlier that would pull the tags from mp3 files, perhaps it could be modified. I'll try to find it again...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

getID3 can pull the ID3 information, but having ID3 tags does not make a MP3, unfortunately. You'd need to actually process each packet to ensure conformance to the specification(s) to thoroughly verify it's status. But then again, Pimptastic may not need such stringent verification.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

http://www.zend.com/codex.php?id=160&single=1

Also I know there is some code here in the forum someplace for detecting a files type be it's contents...
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Well, the file spec says that the header of the MP3 file should look like this

Code: Select all

AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM

Code: Select all

Sign(bits)      Length (bits)	Position	Description
A               11	           (31-21)	 Frame sync (all bits set)
B	        2                   (20,19)	  MPEG Audio version ID
                                                        00 - MPEG Version 2.5
                                                        01 - reserved
                                                        10 - MPEG Version 2 (ISO/IEC 13818-3)
                                                        11 - MPEG Version 1 (ISO/IEC 11172-3)
C	        2	            (18,17) 	  Layer description
                                                         00 - reserved
                                                         01 - Layer III
                                                        10 - Layer II
                                                        11 - Layer I
D	         1	            (16) 	   Protection bit
                                                        0 - Protected by CRC (16bit crc follows header)
                                                        1 - Not protected
etc. See link in fist post for more information


How would i check that the header bits match that? and would that be a good enough check to prove that it was an MP3 file
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pack() and unpack() (mostly the latter) can be used to pull out the information (sscanf() can be used too) along with some fun with math and bitmasking..

If you are looking for basic verification only, looking at the MP3 headers may do just fine.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

feyd wrote:pack() and unpack() (mostly the latter) can be used to pull out the information.
Holy smurf, after reading the manual, i have no idea how to use those functions :cry: :?
Post Reply