I'm building an mp3 tagging application using the JAudiotagger library. My application reads the mp3 metadata fine, and can write the metadata fine too, except for the artworks. So my problem is as follows:
When I add a number of artworks in the mp3 file, and save it, the file is getting bigger, which makes sense. But when I remove one or all the artworks, the file size doesn't get smaller.
The actual problem lies in the ID3v2 tag of my mp3 file. When I remove an artwork, it is actually removed from the tag, but the tag size itself doesn't shrink at all.
The method I'm using when deleting an artwork is this:
// Get the artworkList from the parentFrame.
List<Artwork> list = parentFrame.getArtworkList();
// Get the tag from the parentFrame's mp3File.
AbstractID3v2Tag tag = parentFrame.getTag();
// Get the index of the artwork the user is currently looking at (and
// wants to delete too).
int visibleArtworkIndex = parentFrame.getVisibleArtworkIndex();
// Remove it from the list.
list.remove(visibleArtworkIndex);
// Update the parentFrame's copy of the artworkList.
parentFrame.setArtworkList(list);
// Update the tag (delete its whole artwork field).
tag.deleteArtworkField();
// If the list has more artworks left, add them to the tag.
if (!list.isEmpty()) {
Iterator<Artwork> iterator = list.iterator();
while (iterator.hasNext()) {
try {
tag.addField(iterator.next());
} catch (FieldDataInvalidException e1) {
e1.printStackTrace();
}
}
}
, which actually removes an artwork from the list, and then updates the tag itself by deleting all of its artworks and copying them all over again from the updated list.
My attempts for a solution were:
To create a new tag from the updated old tag (after calling tag.deleteArtworkField()
), then adding the artworks to the new tag, but the new tag had the same size as the old one.
To trim the mp3 file just before saving it by using tag.adjustPadding(File fileToBeTrimmed, int sizeToStoreTagBeforeAudioInBytes, long audioStartByte)
, which adjusts the length of the padding at the beginning of the MP3 file.
The problem here is that I know the wrong tag size only and not the correct, so I can't trim the mp3 correctly and I end up losing audio data.
To illustrate the problem better I have included some images:
The mp3 file before:
The mp3 file after the removal of one artwork. Notice the tag kept its previous size although it has less artworks:
And how the file should be:
I hope anyone has any ideas. Thanks in advance.
This is fixed as of today.
By default jaudiotagger does not reclaim space when you make the metadata smaller, but now if you set
TagOptionSingleton.getInstance().setId3v2PaddingWillShorten(true);
before saving changes it will reclaim unnecessary padding to give the minimum file size possible.