MP3 Tag java library Quick Start


True to the title, this is quick. There are many more read/write/edit options if you go through the JavaDocs or through the code.

Introduction to tags

There are three types of tags found in an MP3 file found in this order:
  1. ID3v2
  2. <MP3 Data>
  3. Lyrics3
  4. ID3v1
In addition, there are different versions for each tag:
  1. ID3v2
  2. Lyrics3
  3. ID3v1

Compiling:

If you have ant, there is a build.xml. Type "ant help" for build options.
If you get from CVS, you can use IntelliJ and read jid3lib.ipr

Reading:

    File sourceFile;
    MP3File mp3file = new MP3File(sourceFile);
You can also read specific tags:
    ID3v1_1 tag = new ID3v1_1(sourceFile);
    ID3v1 tag = new ID3v1(sourceFile);
    ID3v2_4 tag = new ID3v2_4(sourceFile);
    ID3v2_3 tag = new ID3v2_3(sourceFile);
    ID3v2_2 tag = new ID3v2_2(sourceFile);
    Lyrics3v2 tag = new Lyrics3v2(sourceFile);
    Lyrics3v1 tag = new Lyrics3v1(sourceFile);

Creating:

    MP3File mp3file = new MP3File();
    TagOptionSingleton.getInstance().setDefaultSaveMode(TagConstant.MP3_FILE_SAVE_OVERWRITE);

    // setup id3v1
    id3v1.setAlbum("albumTitle");

    // setup id3v2
    AbstractID3v2Frame frame;
    AbstractID3v2FrameBody frameBody;
    frameBody = new FrameBodyTALB((byte) 0, "albumTitle");
    frame = new ID3v2_4Frame(frameBody);
    id3v2.setFrame(frame);

    // setup lyrics3v2
    Lyrics3v2Field field;
    AbstractLyrics3v2FieldBody fieldBody;
    fieldBody = new FieldBodyEAL("albumTitle");
    field = new Lyrics3v2Field(fieldBody);
    lyrics3.setField(field);

    // setup filename tag
    frameBody = new FrameBodyTALB((byte) 0, "albumTitle");
    frame = new ID3v2_4Frame(frameBody);
    filenameId3.setFrame(frame);
    TagOptionSingleton.getInstance().setFilenameTagSave(true);
Things to note:

Editing Part 1:

There are convience methods defined in AbstractMP3Tag to edit common data fields. Not all tags have all fields listed here.
    public abstract String getSongTitle();
    public abstract String getLeadArtist();
    public abstract String getAlbumTitle();
    public abstract String getYearReleased();
    public abstract String getSongComment();
    public abstract String getSongGenre();
    public abstract String getTrackNumberOnAlbum();
    public abstract String getSongLyric();
    public abstract String getAuthorComposer();
    public abstract void setSongTitle(String songTitle);
    public abstract void setLeadArtist(String leadArtist);
    public abstract void setAlbumTitle(String albumTitle);
    public abstract void setYearReleased(String yearReleased);
    public abstract void setSongComment(String songComment);
    public abstract void setSongGenre(String songGenre);
    public abstract void setTrackNumberOnAlbum(String trackNumberOnAlbum);
    public abstract void setSongLyric(String songLyrics);
    public abstract void setAuthorComposer(String authorComposer);

Editing Part 2:

If the field you want is not listed above, you can use these methods.
    id3v1 = mp3file.getID3v1Tag();
    id3v2 = mp3file.getID3v2Tag();
    lyrics3 = mp3file.getLyrics3Tag();
ID3v1 tags have fixed fields and use accessor methods to change it's properties.

ID3v2 tags have multiple frames. Use this to set the title of the tag.

    frame = id3v2.getFrame("TIT2");
    ((FrameBodyTIT2) frame.getBody()).setText("New Title");

Lyrics3 tags have multiple fields. Use this to set the title of the tag.

    field = lyrics3.getField("ETT");
    ((FieldBodyETT) field.getBody()).setTitle("New Title");

Writing:

    mp3file.save();
You can also save each individual tag through each tags' save() method.

Reference:

Here is a mapping of the commonly used fields

Field Name ID3v1 ID3v1.1 ID3v2.2 ID3v2.3 ID3v2.4 Lyrics3v1 Lyrics3v2
Song Title title title TT2 TIT2 TIT2 -- ETT
Lead Artist artist artist TP1 TPE1 TPE1 -- EAR
Album Title album album TAL TALB TALB -- EAL
Year Released year year TYE TYER TDRC -- --
Comment comment comment COM COMM COMM -- INF
Song Genre genre genre TCO TCON TCON -- --
Track number on album -- track TRK TRCK TRCK -- --
Lyrics -- -- SYL or ULT SYLT or USLT SYLT or USLT lyric LYR
Author / Composer -- -- TCM TCOM TCOM -- AUT