介绍
mp3agic是一个纯Java库。用于读取mp3文件和读取/操作ID3标签(ID3v1和ID3v2.2到ID3v2.4),可以使用 ID3 标签中的详细信息重命名 mp3 文件、重新标记 mp3 文件、将图像附加到 mp3 文件以及从 mp3 文件中提取图像等等任务。
mp3文件尾部标签介绍
mp3音乐文件的有三种尾部标签,分别是:ID3v1,ID3v2,APEv2这三种类型
-
ID3v1位于文件尾部,不支持封面,不支持一些特殊字符,长度短,但兼容老设备。
-
ID3v2是ID3v1的后继替代者,位于文件头部,支持封面和特殊字符,长度任意。
-
APEv2位于文件尾部(同时与ID3v1存在时,在ID3v1之前),同样支持封面和特殊字符,但不推荐用于mp3。
mp3agic依赖
<dependency><groupId>com.mpatric</groupId><artifactId>mp3agic</artifactId><version>0.9.1</version>
</dependency>
官方维护地址
https://github.com/mpatric/mp3agic
使用示例
打开mp3文件
Opening an mp3 file
Mp3File mp3file = new Mp3File("eguid.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"));
保存MP3文件
mp3file.save("MyMp3File.mp3");
删除ID3和自定义标签
Mp3File mp3file = new Mp3File("eguid.mp3");
if (mp3file.hasId3v1Tag()) {mp3file.removeId3v1Tag();
}
if (mp3file.hasId3v2Tag()) {mp3file.removeId3v2Tag();
}
if (mp3file.hasCustomTag()) {mp3file.removeCustomTag();
}
mp3file.save("eguidMp3FileWithoutTags.mp3");
获取ID3v1的值
Mp3File mp3file = new Mp3File("eguid.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());
}
设置ID3v1的值
Mp3File mp3file = new Mp3File("SomeMp3File.mp3");
ID3v1 id3v1Tag;
if (mp3file.hasId3v1Tag()) {id3v1Tag = mp3file.getId3v1Tag();
} else {// mp3 does not have an ID3v1 tag, let's create one..id3v1Tag = new ID3v1Tag();mp3file.setId3v1Tag(id3v1Tag);
}
id3v1Tag.setTrack("5");
id3v1Tag.setArtist("An Artist");
id3v1Tag.setTitle("The Title");
id3v1Tag.setAlbum("The Album");
id3v1Tag.setYear("2001");
id3v1Tag.setGenre(12);
id3v1Tag.setComment("Some comment");mp3file.save("MyMp3File.mp3");
获取 ID3v2专辑封面
Mp3File mp3file = new Mp3File("eguid.mp3");
if (mp3file.hasId3v2Tag()) {ID3v2 id3v2Tag = mp3file.getId3v2Tag();byte[] imageData = id3v2Tag.getAlbumImage();//获取封面图像if (imageData != null) {String mimeType = id3v2Tag.getAlbumImageMimeType();//获取封面图像mine-type扩展名// 写出图像到文件 - 可以从 MIME 类型确定适当的文件扩展名RandomAccessFile file = new RandomAccessFile("album-artwork", "rw");file.write(imageData);file.close();}
}
设置ID3v2字段的值
Mp3File mp3file = new Mp3File("eguid.mp3");
ID3v2 id3v2Tag;
if (mp3file.hasId3v2Tag()) {id3v2Tag = mp3file.getId3v2Tag();
} else {// mp3 does not have an ID3v2 tag, let's create one..id3v2Tag = new ID3v24Tag();mp3file.setId3v2Tag(id3v2Tag);
}
id3v2Tag.setTrack("5");
id3v2Tag.setArtist("An Artist");
id3v2Tag.setTitle("The Title");
id3v2Tag.setAlbum("The Album");
id3v2Tag.setYear("2001");
id3v2Tag.setGenre(12);
id3v2Tag.setComment("Some comment");
id3v2Tag.setLyrics("Some lyrics");
id3v2Tag.setComposer("The Composer");
id3v2Tag.setPublisher("A Publisher");
id3v2Tag.setOriginalArtist("Another Artist");
id3v2Tag.setAlbumArtist("An Artist");
id3v2Tag.setCopyright("Copyright");
id3v2Tag.setUrl("http://foobar");
id3v2Tag.setEncoder("The Encoder");
mp3file.save("eguid-mp3.mp3");
获取ID3v2字段的值
Mp3File mp3file = new Mp3File("eguid.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("是否有专辑图数据, length: " + albumImageData.length + " bytes");System.out.println("专辑图MIME类型的文件扩展名: " + id3v2Tag.getAlbumImageMimeType());}
}