RE: [PHP] Find out how long an MP3 is
| From: | Lawrence dot Sheed at dfait-maeci dot gc dot ca | Date: | Wed, 22 Nov 2000 05:39:08 +0000 |
| Subject: | RE: [PHP] Find out how long an MP3 is | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-26583@lists.php.net to get a copy of this message | ||
There is no length indicator in an mp3 file - would seem logical in its
current usage, but mp3 was envisaged as a broadcast medium - ie no fixed
endpoint.
There are a couple of mp3 decoders with source code out there, do a search
on google.
You can find out more info about file formats at:
http://www.oreilly.com/catalog/mp3/chapter/ch02.html#52496
Position Purpose Length (in Bits)
A Frame sync 11
B MPEG audio version (MPEG-1, 2, etc.) 2
C MPEG layer (Layer I, II, III, etc.) 2
D Protection (if on, then checksum follows header) 1
E Bitrate index (lookup table used to specify bitrate for this MPEG version
and layer) 4
F Sampling rate frequency (44.1kHz, etc., determined by lookup table) 2
G Padding bit (on or off, compensates for unfilled frames) 1
H Private bit (on or off, allows for application-specific triggers) 1
I Channel mode (stereo, joint stereo, dual channel, single channel) 2
J Mode extension (used only with joint stereo, to conjoin channel data) 2
K Copyright (on or off) 1
L Original (off if copy of original, on if original) 1
M Emphasis (respects emphasis bit in the original recording; now largely
obsolete) 2
32 total header bits
ID3 Tag:
http://www.updatestage.com/previous/000501.html
MP3 files can store text information about the audio file along with the
data. The standard that specifies what type of data is stored at what
location in the file is called ID3. The first couple of versions of the ID3
specification, v1 and v1.1, stored information in 128 bytes at the very end
of the MP3 file. The more recent ID3 v2.* spec allows for the storage of
more detailed information at the beginning of the MP3 file. Since some older
players may not recognize the new ID3 info, and the "cost" to include the
trailer is only 128 bytes, most MP3's store song info in both formats. This
article and the example movie (80K,ZIP), read and write song information
using the older v1 and v1.1 format.
The spec for ID31 is simply 128 bytes at the end of the file divided into
the following fields:
ID3v1
Set Position To Length Description Data type
0 3 Always "TAG" String
3 30 Title String
33 30 Artist String
63 30 Album String
93 4 Year String
97 30 Comments String
126 1 CD Track (v1.1 - only if Comments fields is 28 chars or less) Unsigned
char (integer)
127 1 Genre Unsigned char (integer)
String fields are padded with character 0 to the length of the field, if the
string data is shorter than the field. Genre is a number lookup to a table
of standard genre names.
The only difference between 1.0 and 1.1, is that 1.1 checks to see if the
entire comments field has been used. If byte 29 of the comments field is
character 0, the padding character, meaning that the comments only took up
28 bytes or less of the field, then byte 30 of "Comments" is checked for a
non-zero value. If found, this value is interpreted as the CD Track.
You can use BinaryIO Xtra to read the information in the trailer by getting
the length of the file and subtracting 128 from it (the length of the
trailer) to position the read head at the start of the trailer.
theFile = new(xtra "binaryio")
openFile(theFile, 1, "C:\FOLDER\SOMESONG.MP3")
size = getFileSize(theFile)
setFilePosition(theFile, size - 128)
You are now positioned before the first byte of the trailer and can
determine if the file has a trailer by reading the next 3 bytes. If the file
has a trailer the bytes should contain the string "TAG"
tag = readbytes(theFile,3)
if tag <> "TAG" then
closeFile(theFile)
return("No trailer information")
end if
If the tag is there, the following code reads the data in each field into a
variable.
title = readbytes(theFile,30)
artist = readbytes(theFile,30)
album = readbytes(theFile,30)
year = readbytes(theFile,4)
comments = readbytes(theFile,30)
-- Go backwards to check for CD track
setFilePosition(theFile,getFilePosition(theFile) - 2)
byte29 = readunsignedchar(theFile)
-- If byte 29 is 0 then byte 30 might contain
-- the CD track
if byte29 = 0 then
possiblyCDtrack = readunsignedchar(theFile)
if possiblyCDtrack > 0 then
CDtrack = possiblyCDtrack
-----Original Message-----
From: Derek Sivers [mailto:list@hitmedia.com]
Sent: November 22, 2000 11:56 AM
To: Thomas Deliduka; PHP List
Subject: Re: [PHP] Find out how long an MP3 is
>I guess there's no way, at least that I know of, of finding the length of
>the mp3 without simply playing it.
There must be SOME way, because Napster, Gnutella and others list the
length and bitrate of all the MP3 files you find in a search.
I'm sure they're not playing every track on every hard drive in the world.
It's out there somewhere...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net