본문 바로가기

개발관련/이것저것

ID3TAG info

어디서 퍼 왔는데 어디서 가져왔는지는 기억이;;

TAG is name for data space in MP3 file where some text informations (song name, artist, album...) can be stored.


TAG ver.1

is old and simple. It takes always 128 Bytes at the very end of file (after the last audio frame).

Structure:

Bytes    Length    Content
0-2  3  Tag identifier. Must contain "TAG" string if Tag is valid.
3-32  30  Song Name
33-62  30  Artist
63-92  30  Album
93-96  4  Year
97-126  30  Comment
127  1  Genre

126. Byte can also be used as the number of song. Items should be padded with NULL (ASCII 0) or with a space (ASCII 32).

Values for Genre are predefined, you can find them eg. in WinAmp. Only the most important ones:
22    Death Metal
138  Black Metal
144  Thrash Metal
9  Metal
43  Punk
129  Hardcore

Problems with TAG v1 are obvious: you can store only a few items (you have a bad luck for eg. country of an artist, used codec, author of lyric...) and there is only a limited space for items (you also have a bad luck if song name has more than 30 chars).
But despite that is TAG v1 commonly used by most of "MP3 release" groups.

TAG ver.2


is newer, bigger and uncut. And... much more complicated.
Is placed at the very beginning of file (before all audio frames).


Structure:

TAG v2 contains of header and frames (dont mismatch it with audio frames and their headers!!!).


Header (10 Bytes)

Bytes    Content
0-2  TAG identifier. It contains of string "ID3"
3-4  TAG version. Can be eg. 03 00
5  Flags
6-9  Size of TAG

Flags Byte has this structure (in bits):
abc00000
where the first 3 bits indicate Unsynchronization, Extended header and Experimental indicator.
Flags normally dont have special meaning, can be set to 00.

Size of TAG is encoded into 4 Bytes. But not to be so easy, the most significant bit in each Byte is set to 0 and ignored. Only remaining 7 bits are used. The reason is to avoid mismatch with audio frame header which has the first synchro Byte FF).
Eg. TAG len 257 is encoded as 00 00 02 01.
Size of TAG doesnt contain header itself so total lenght of previous TAG is 257 + 10 Bytes.


Frames

Each frame is used for storing one information - eg. Artist or Album.
Frame consists of header and body.

Header(10 Bytes)

Bytes    Content
0-3  Frame identifier
4-7  Size
8-9  Flags

Frame identifier consist of four characters. There are many predefined values you can use. But eg. current version of WinAmp displays only these ones:

Iden.    Description
TRCK  Track number
TENC  Encoded By
WXXX  URL
TCOP  Frame identifier
TOPE  Original Artist
TCOM  Composer
TCON  Genre
COMM  Comments
TYER  Year
TALB  Album
TPE1  Artist
TIT2  Song name

You can freely define you own frame, eg. with identifier "CUNT" but you cant expect thet some player will be able to diaplay that. Anyway you can store any information into MP3 file without limits.

Size is stored from the most significant Byte to the least. It doesnt include frame header, so total length of frame is Size + 10. Warning: After the header always one Byte with value 00 follows and then begins frame body. Size has to include this Byte.

Flags in most cases are set to 00 00. But they have this structure (in bits):

abc00000 ijk00000        //why they are not stored in one Byte???

Flag    Description
a  TAG alter preservation
b  File alter preservation
c  Read only
i  Compression
j  Encryption
k  Grouping identity

Example of frame:
54 50 45 31 00 00 00 07 00 00 00 53 53 6C 61 79 65 72
T P E 1    7    S l a y e r

To be even more complicated, some frames can have special structure. Eg. COMM (comments) aslo contains language version which is not normally displayed:

43 4F 4D 4D 00 00 00 0C 00 00 00 65 6E 67 00 63 6F 6D 6D 65 6E 74
C O M M    0C    e n g  c o m m e n t

For TCON (genre or style) you can use predefined values as in TAG v1. Then frame body looks like "(144)Thrash Metal" and the number corresponds with TAG v1. Or you can simply write your own style and then body is "Brutal Black" and is stored like normal frame body.

Complete description of TAG v2 you can find here.

 어쨋든 내가 작업하는데 있어 가장 중요했던건 ID3TAG v1은 파일의 가장 마지막 128byte에 들어 있고 각각이 몇 byte이냐는 거였고

 ID3TAG v2는 파일의 맨 위에 있다는 것이였다..으흐

 그리고 ID3TAG v2의 경우에는 파일사이즈를 나타내는 4Byte가 각 Byte마다 상위 1bit는 다른 태그와 혼동을 피하기 위해 사용하지 않는 다는 것이다.

 따라서 실제 ID3TAG v2의 사이즈를 알아 내기 위해서는 4Byte를 읽어서
 int i_TagSize;
 char TAGv2[4];

ul_TagSize =  (t_TAGv2[0] << ( 3 * 8)) >> 3 |
                    (t_TAGv2[1] << ( 2 * 8)) >> 2 |
                    (t_TAGv2[2] << ( 1 * 8)) >> 1 |
                    (t_TAGv2[3]);

ul_TagSize += 10;    // default Tag info size is 10

을 해주어야 한다는 것이다.