MP3结构简单介绍
有时我们会发现MP3文件在电脑上可以显示出封面图,艺术家,标题,唱片集等等信息,这是因为MP3中有ID3标签信息。
MP3文件大体上分为三个部分:ID3V2+音频数据+ID3V1
, ID3V2在MP3文件的头部,ID3V1在尾部,都是用来补充说明这个MP3文件的
mp3文件构成
名称 | 说明 |
---|---|
ID3V2 | 位于MP3文件首部,包含作者、作曲、专辑等信息、长度不固定 |
音频数据 | 1.音频数据由一系列的数据帧构成 2.每个帧的长度可能固定可能不固定,由位率决定 3.每个帧都包括帧头、数据实体两部分 4.帧头记录着mp3的位率、采样率、版本等信息 5.如果启用CRC校验,则帧头后跟随2字节CRC校验,之后可能存在32字节的附加信息,也可能无附加信息 |
ID3V1 | Mp3文件末尾的固定128字节,包含作者作曲专辑等 |
说明信息 | 有些MP3文件可能会有一些额外的说明信息(不重要) |
java获取MP3文件的ID3信息
要求jdk1.8及以上
引入Maven依赖
<dependency>
<groupId>com.mpatric</groupId>
<artifactId>mp3agic</artifactId>
<version>0.9.1</version>
</dependency>
获取ID3v1信息
Mp3File mp3file = new Mp3File("SomeMp3File.mp3");
if (mp3file.hasId3v1Tag()) {
ID3v1 id3v1Tag = mp3file.getId3v1Tag();
System.out.println("Track: " + id3v1Tag.getTrack());
System.out.println("Artist: " + id3v1Tag.getArtist());
System.out.println("Title: " + id3v1Tag.getTitle());
System.out.println("Album: " + id3v1Tag.getAlbum());
System.out.println("Year: " + id3v1Tag.getYear());
System.out.println("Genre: " + id3v1Tag.getGenre() + " (" + id3v1Tag.getGenreDescription() + ")");
System.out.println("Comment: " + id3v1Tag.getComment());
}
获取ID3v2信息
Mp3File mp3file = new Mp3File("SomeMp3File.mp3");
if (mp3file.hasId3v2Tag()) {
ID3v2 id3v2Tag = mp3file.getId3v2Tag();
System.out.println("Track: " + id3v2Tag.getTrack());
System.out.println("Artist: " + id3v2Tag.getArtist());
System.out.println("Title: " + id3v2Tag.getTitle());
System.out.println("Album: " + id3v2Tag.getAlbum());
System.out.println("Year: " + id3v2Tag.getYear());
System.out.println("Genre: " + id3v2Tag.getGenre() + " (" + id3v2Tag.getGenreDescription() + ")");
System.out.println("Comment: " + id3v2Tag.getComment());
System.out.println("Lyrics: " + id3v2Tag.getLyrics());
System.out.println("Composer: " + id3v2Tag.getComposer());
System.out.println("Publisher: " + id3v2Tag.getPublisher());
System.out.println("Original artist: " + id3v2Tag.getOriginalArtist());
System.out.println("Album artist: " + id3v2Tag.getAlbumArtist());
System.out.println("Copyright: " + id3v2Tag.getCopyright());
System.out.println("URL: " + id3v2Tag.getUrl());
System.out.println("Encoder: " + id3v2Tag.getEncoder());
byte[] albumImageData = id3v2Tag.getAlbumImage();
if (albumImageData != null) {
System.out.println("Have album image data, length: " + albumImageData.length + " bytes");
System.out.println("Album image mime type: " + id3v2Tag.getAlbumImageMimeType());
}
}
删除ID3信息,保存到新的文件
public static void main(String[] args) throws Exception {
Mp3File mp3file = new Mp3File("C:\\Users\\wangz\\Desktop\\去除ID3前.mp3");
System.out.println("Length of this mp3 is: " + mp3file.getLengthInSeconds() + " seconds");
System.out.println("Bitrate: " + mp3file.getBitrate() + " kbps " + (mp3file.isVbr() ? "(VBR)" : "(CBR)"));
System.out.println("Sample rate: " + mp3file.getSampleRate() + " Hz");
System.out.println("Has ID3v1 tag?: " + (mp3file.hasId3v1Tag() ? "YES" : "NO"));
System.out.println("Has ID3v2 tag?: " + (mp3file.hasId3v2Tag() ? "YES" : "NO"));
System.out.println("Has custom tag?: " + (mp3file.hasCustomTag() ? "YES" : "NO"));
if (mp3file.hasId3v1Tag()) {
mp3file.removeId3v1Tag();
}
if (mp3file.hasId3v2Tag()) {
mp3file.removeId3v2Tag();
}
if (mp3file.hasCustomTag()) {
mp3file.removeCustomTag();
}
// 将ID3信息去除,并保存到一个新文件
mp3file.save("C:\\Users\\wangz\\Desktop\\去除ID3后.mp3");
}
更多ID3操作api请访问:mp3agic