]]>MP3鐨勪竴浜涜祫鏂?/title>http://m.tkk7.com/liltos/articles/9503.html鏉庡父榛?/dc:creator>鏉庡父榛?/author>Sun, 07 Aug 2005 02:28:00 GMThttp://m.tkk7.com/liltos/articles/9503.htmlhttp://m.tkk7.com/liltos/comments/9503.htmlhttp://m.tkk7.com/liltos/articles/9503.html#Feedback0http://m.tkk7.com/liltos/comments/commentRss/9503.htmlhttp://m.tkk7.com/liltos/services/trackbacks/9503.html
ID3v1 Tags
## 鍥犱負 ID3v2 姣旇緝澶嶆潅錛岃繛FOOBAR閮戒笉鏀寔瀹冿紝鎴戞兂鍐欑殑涓滆タ涔熷氨涓嶆敮鎸佺畻浜嗭紝鍏嶅緱楹葷儲 ## 鎬庝箞鎰熻榪欓噷涓嶆槸榪欐牱鍛紝涓婃鐪嬪埌鐨勬槸 ID3v1 鏄湪鏂囦歡鐨勫ご閮ㄧ殑錛岀敤UE鎵撳紑MP3濂藉儚涔熸槸鍦ㄥご閮?/EM> The ID3v1 tag is found at the end of an MP3 file and has a fixed number of fields and size. The structure of the tag is as follows:
Private Type MP3ID3V1Tag
Tag As String * 3 '-- 03 = "TAG"
Title As String * 30 '-- 33
Artist As String * 30 '-- 63
Album As String * 30 '-- 93
Year As String * 4 '-- 97
Comment As String * 30 '-- 127
Genre As Byte '-- 128
End Type
Note that v1.1 of the ID3v1 specification allows the last two bytes of the Comment tag to be used to store the track number. Byte 29 is always set to 0 in this case, and Byte 30 stores the track number itself.
Checking if an MP3 file contains an ID3v1 tag is a simple matter of rewinding 128 bytes from the end and checking if the bytes read "TAG". If they do, then the rest of the bytes are the tag itself. Reading and writing the tag is simple: if its already there, then you just fill in the structure and then write it over the last 128 bytes of the file. If the tag isn't there, then just append a structure to the end of the file.
The TAG is used to describe the MPEG Audio file. It contains information about artist, title, album, publishing year and genre. There is some extra space for comments. It is exactly 128 bytes long and is located at very end of the audio data. You can get it by reading the last 128 bytes of the MPEG audio file.
## 鍦ㄦ槰澶╁啓鐨勯偅涓▼搴忎腑錛屽鏋滄妸璇昏繘鏉ョ殑 String tag 閮芥墦鍗板嚭鏉ョ殑璇濓紝鍙互鐪嬪埌涓浜涙晥鏋滐紝褰撶劧錛岄偅浜涗笢瑗挎槸澶嶅埗涓嶅嚭鏉ョ殑錛屽彧鍙互鑷繁榪愯鍑烘潵鐪?FONT size=5>
Tag identification. Must contain 'TAG' if tag exists and is correct.
B
30
(3-32)
Title
C
30
(33-62)
Artist
D
30
(63-92)
Album
E
4
(93-96)
Year
F
30
(97-126)
Comment
G
1
(127)
Genre
## 榪欓噷鎻愪簡錛屽畠鐢ㄧ殑緙栫爜涓庢眽瀛楃殑緙栫爜鏄笉涓鏍鋒淮錛屾墍浠ユ垜鏄ㄥぉ鍐欑殑閭d釜紼嬪簭娌℃嬁甯︿腑鏂囩殑鏍囩鏉ュ仛瀹為獙錛屽懙鍛碉紝闇瑕佽漿鎹竴涓?BR>The specification asks for all fields to be padded with null character (ASCII 0). However, not all applications respect this (an example is WinAmp which pads fields with , ASCII 32).
There is a small change proposed in ID3v1.1 structure. The last byte of the Comment field may be used to specify the track number of a song in an album. It should contain a null character (ASCII 0) if the information is unknown.
Genre Bytes
鍐嶅姞涓浜涘叾瀹冪殑璧勬枡錛屽彲鑳藉彧鏄瘮杈冩湁鍒╀簬闃呰鑰屼互 Genre is a numeric field which may have one of the following values:
/**//** * Created on 2005-8-6 5:10:29 * @author 緋婃秱楝?BR>*/ publicclass ReadMp3ID3v1Info { privatestatic final int TAG_SIZE =128; privatestatic final int TITLE_SIZE =30; privatestatic final int ARTIST_SIZE =30; privatestatic final int ALBUM_SIZE =30; privatestatic final int YEAR_SIZE =4; privatestatic final int COMMENT_SIZE =29; privatestatic final int TRACK_LOCATION =126; privatestatic final int GENRE_LOCATION =127; privatestatic final int MAX_GENRE =255; privatestatic final int MAX_TRACK =255; privatestatic final String ENC_TYPE ="Cp437"; privatestatic final String TAG_START ="TAG"; publicstaticvoid main(String[] args){ try{ File mp3 =new File("F:/闊充箰/MP3/鑻辨枃濂蟲瓕鎵?Madonna - Music.mp3"); RandomAccessFile raf =new RandomAccessFile( mp3, "r" ); raf.seek(raf.length() - TAG_SIZE); byte[] buf =newbyte[TAG_SIZE]; raf.read(buf, 0, TAG_SIZE); String tag =new String(buf, 0, TAG_SIZE, "Cp437"); int start = TAG_START.length(); System.out.println("鏂囦歡鍚? "+ mp3.getName()); System.out.println("鏍囬 錛?nbsp;"+ tag.substring(start, start += TITLE_SIZE).trim()); System.out.println("鑹烘湳瀹訛細 "+ tag.substring(start, start += ARTIST_SIZE).trim()); raf.close(); System.out.println("===================================="); File mp32 =new File("F:/闊充箰/MP3/鑻辨枃緇勫悎/blue - you make me wanna.mp3"); raf =new RandomAccessFile( mp32, "r" ); raf.seek(raf.length() -128); raf.read(buf, 0, 128); tag =new String(buf, 0, 128, "Cp437"); start = TAG_START.length(); System.out.println("鏂囦歡鍚? "+ mp32.getName()); System.out.println("鏍囬 錛?nbsp;"+ tag.substring(start, start +=30).trim()); System.out.println("鑹烘湳瀹訛細 "+ tag.substring(start, start +=30).trim()); raf.close(); }catch (Exception e) { } } }
榪愯鐨勭粨鏋滄槸錛?/FONT> 鏂囦歡鍚? Madonna - Music.mp3 鏍囬 錛?Music 鑹烘湳瀹訛細 Madonna ==================================== 鏂囦歡鍚? blue - you make me wanna.mp3 鏍囬 錛?U Make Me Wanna 鑹烘湳瀹訛細 Blue