Javaでmp3ファイルのID3タグを解析するライブラリを色々と調べてみました。参考になれば幸いです。
以下に挙げていくコードは、特に意味のあることをしている訳ではありませんが、 どうやってタグの読み込み、書き換えを行えるかというサンプルとして参考にして頂ければと思います。
以下に挙げていくコードは、特に意味のあることをしている訳ではありませんが、 どうやってタグの読み込み、書き換えを行えるかというサンプルとして参考にして頂ければと思います。
MyID3: a Java ID3 Tag Library
import java.io.File;
import java.io.IOException;
import org.cmc.music.common.MusicMetadata;
import org.cmc.music.myid3.MusicMetadataSet;
import org.cmc.music.myid3.MyID3;
/**
* {@link http://www.fightingquaker.com/myid3/}
*/
public class MyID3Tester {
public static void main(String[] args) throws IOException {
File mp3File = new File("C:\\temp\\music.mp3");
MyID3 id3 = new MyID3();
MusicMetadataSet src_set = id3.read(mp3File); // read metadata
if (src_set == null){
System.out.println("could not read data");
return;
}
// You can extract simplified information
MusicMetadata metadata = src_set.getSimplified();
System.out.println(metadata.getArtist());
System.out.println(metadata.getAlbum());
// this doesn't work for me somehow :(
System.out.println(metadata.getSongTitle());
System.out.println(metadata.getComment());
}
}
JID3 - A Java ID3 Class Library Implementation
import java.io.File;
import org.blinkenlights.jid3.ID3Exception;
import org.blinkenlights.jid3.ID3Tag;
import org.blinkenlights.jid3.MP3File;
import org.blinkenlights.jid3.MediaFile;
import org.blinkenlights.jid3.v1.ID3V1Tag;
import org.blinkenlights.jid3.v2.ID3V2_3_0Tag;
/**
* {@link http://jid3.blinkenlights.org/}
*/
public class JID3Tester
{
public static void main(String[] args) throws Exception {
// the file we are going to modify
File oSourceFile = new File("C:\\temp\\music.mp3");
// create an MP3File object representing our chosen file
MediaFile mediaFile = new MP3File(oSourceFile);
// you can check artist name from v1 tag if the tag is available
ID3V1Tag tag1 = mediaFile.getID3V1Tag();
if (tag1 != null) {
System.out.println(tag1.getArtist());
}
// print tags
System.out.println("****Original tag****");
printTags(mediaFile.getTags());
// remove all tags from **file** e.g. persisted
mediaFile.removeTags();
// create a v2.3.0 tag object, and set some frames
// if you want put v1 tag you can achieve similarly
// ID3V1_1Tag newTag = new ID3V1_1Tag();
ID3V2_3_0Tag newTag = new ID3V2_3_0Tag();
newTag.setArtist("new artist");
mediaFile.setID3Tag(newTag);
// persist netags to file
mediaFile.sync();
// again print tags
System.out.println("\n****New tag****");
printTags(mediaFile.getTags());
}
// some utility methods etc for your understanding.... hope these are helpful :)
private static final String album = "Album: ";
private static final String artist = "Artist: ";
private static final String title = "Title: ";
private static final String year = "Year: ";
private static final String genre = "Genre: ";
private static final String trackNumber = "Track Number: ";
private static final String totlaTracks = "Total Tracks: ";
private static void printTags(ID3Tag[] tags) throws ID3Exception {
for (int i = 0; i < tags.length; i++) {
ID3Tag tag = tags[i];
if (tag instanceof ID3V1Tag) {
System.out.println("---ID3V1---");
JID3TagUtils.print((ID3V1Tag) tag);
} else if (tag instanceof ID3V2_3_0Tag) {
System.out.println("---ID3V2_3_0---");
JID3TagUtils.print((ID3V2_3_0Tag) tag);
}
}
}
public static void clearID3V1Tag(MediaFile mediaFile) throws ID3Exception {
ID3V1Tag tag = new ID3V1_1Tag();
mediaFile.setID3Tag(tag);
mediaFile.sync();
}
public static void clearTag(MediaFile mediaFile) throws ID3Exception {
ID3Tag[] tags = mediaFile.getTags();
for (int i = 0; i < tags.length; i++) {
ID3Tag tag = tags[i];
if (tag instanceof ID3V1Tag) {
ID3V1Tag tag1 = (ID3V1Tag) tag;
tag1.setAlbum("");
tag1.setArtist("");
tag1.setComment("");
tag1.setGenre(null);
tag1.setYear("");
} else if (tag instanceof ID3V2_3_0Tag) {
ID3V2_3_0Tag tag2 = (ID3V2_3_0Tag) tag;
tag2.setAlbum("");
tag2.setArtist("");
tag2.setComment("");
tag2.setGenre("");
tag2.setYear(0);
}
}
}
public static void print(ID3V1Tag tag) {
System.out.println(album + tag.getAlbum());
System.out.println(artist + tag.getArtist());
System.out.println(title + tag.getTitle());
System.out.println(year + tag.getYear());
System.out.println(genre + tag.getGenre());
}
public static void print(ID3V2Tag tag) throws ID3Exception {
System.out.println(album + tag.getAlbum());
System.out.println(artist + tag.getArtist());
System.out.println(title + tag.getTitle());
System.out.println(genre + tag.getGenre());
try {
System.out.println(totlaTracks + tag.getTotalTracks());
} catch (ID3Exception e) {
// simply ignore because the library throw exception when the tag is
// simply missing
}
try {
System.out.println(trackNumber + tag.getTrackNumber());
} catch (ID3Exception e) {
}
try {
System.out.println(year + tag.getYear());
} catch (ID3Exception e) {
}
}
}
Java ID3 Tag Library
import java.io.File;
import java.io.IOException;
import org.farng.mp3.MP3File;
import org.farng.mp3.TagConstant;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.AbstractID3v2;
import org.farng.mp3.id3.AbstractID3v2Frame;
import org.farng.mp3.id3.FrameBodyTIT2;
import org.farng.mp3.id3.ID3v2_2;
/**
* {@link http://javamusictag.sourceforge.net/}
*/
public class JavaID3TagTester {
public static void main(String[] args) throws IOException, TagException {
File sourceFile = new File("C:\\temp\\music.mp3");
MP3File mp3file = new MP3File(sourceFile);
if(mp3file.hasID3v2Tag()){
AbstractID3v2 tag = mp3file.getID3v2Tag();
System.out.println("Has ID3v2 tag");
// get song title
System.out.println("title:" + tag.getSongTitle());
AbstractID3v2 tag2 = mp3file.getID3v2Tag();
System.out.println("comment: " + tag2.getSongComment());
if(tag instanceof ID3v2_2){
// override new title
AbstractID3v2Frame frame = tag2.getFrame("TIT2");
if(frame != null){
((FrameBodyTIT2) frame.getBody()).setText("New Title");
}
// persist to file
mp3file.save(TagConstant.MP3_FILE_SAVE_OVERWRITE);
}
}
if(mp3file.hasID3v1Tag()){
System.out.println("Has ID3v1 tag");
}
}
}
コメント