Process MP3 tags Tag(s): IO OpenSource
I like to have my MP3 tags organized and cleaned.
With the help of the jaudiotagger library, we can detect unwanted tags (like COMMENT or WWW link), artwork too big or multiple artworks embedded in the MP3 file.
Jaudiotagger
Jaudiotagger (bitbucket repo)is the Audio Tagging library for tagging data in Audio files. It currently fully supports Mp3, Mp4 (Mp4 audio, M4a and M4p audio) Ogg Vorbis, Flac and Wma, there is limited support for Wav and Real formats.NOTE: The Utils class is used with these HowTo's.
package rgagnon.mp3;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class Utils {
private Utils() {}
private static final long KILOBYTE = 1024L;
public static long bytesToKb(long bytes) {
return bytes / KILOBYTE ;
}
public static void log(String s) {
System.out.println(s);
}
/**
* Build a file list from a folder and its subfolders
* @param path
* @return List
* @throws IOException
*/
public static List <String> processFolder(String path) throws IOException {
String result, _result[];
if ( (new File(path)).exists() ) {
result = recurseInFolderFrom(path);
_result = result.split("\\|");
return Arrays.asList(_result);
}
else {
throw new IOException("Path not found : " + path);
}
}
private static String recurseInFolderFrom(String dir) {
File file;
String list[], result;
result = dir;
file = new File(dir);
if (file.isDirectory()) {
list = file.list();
for (int i = 0; i < list.length; i++)
result = result + "|"
+ recurseInFolderFrom(dir + File.separatorChar + list[i]);
}
return result;
}
}Detect if COMMENT is found in a MP3 files in a folder and sub-folders.
This program deals with the COMMENT tag. You can also get the other tags, see this list. To get the title useFieldKey.TITLE instead of FieldKey.COMMENT for example.
package rgagnon.mp3;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.LogManager;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.KeyNotFoundException;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;
import static rgagnon.mp3.Utils.processFolder;
import static rgagnon.mp3.Utils.log;
/**
* Detect if comment is present.
* using https://www.jthink.net/jaudiotagger/
*
*/
public class DetectMp3Comment {
public static void main(String[] args) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
// NOTE :
// jaudiotagger can generate many warnings when analyzing a MP3 file,
// this will suppress unwanted output.
LogManager.getLogManager().reset();
String folder = "\\\\MYSERVER\\Public\\_Music\\MP3\\byartist (E)\\Weezer";
log("Process " + folder);
List<String> mp3 = processFolder(folder);
for(String s : mp3) {
if (s.toLowerCase().endsWith(".mp3")) {
MP3File mp3file = (MP3File) AudioFileIO.read(new File(s));
try {
Tag fileTag = mp3file.getTag();
if (fileTag != null) {
String comment = fileTag.getFirst(FieldKey.COMMENT);
if (comment == null || "".equals(comment.trim())) {
// log("No COMMENT for " + s);
}
else if (comment.length() > 0) {
log("COMMENT found in " + s + " : " + comment);
}
}
else {
log("No tag found for " + s);
}
}
catch (KeyNotFoundException e) {
log("COMMENT key not found in " + s);
}
}
}
log("Done.");
}
}
Detect if MP3 file contains multiple artworks.
To save disk space, it's not a bad idea to remove extra artwork embedded in the MP3 to keep only one.Folder and sub-folders are scanned.
package rgagnon.mp3;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.LogManager;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.images.Artwork;
import static rgagnon.mp3.Utils.processFolder;
import static rgagnon.mp3.Utils.log;
/**
* Detect if multiple artwork or no artwork defined
* using https://www.jthink.net/jaudiotagger/
*
*/
public class DetectMp3MultipleArtwork {
public static void main(String[] args) throws CannotReadException, IOException, TagException,
ReadOnlyFileException, InvalidAudioFrameException {
// NOTE :
// jaudiotagger can generate many warnings when analyzing a MP3 file,
// this will suppress unwanted output.
LogManager.getLogManager().reset();
String folder = "\\\\MYSERVER\\Public\\_Music\\MP3\\byartist (E)\\Weezer";
log("Process " + folder);
List<String> mp3 = processFolder(folder);
for(String s : mp3) {
if (s.toLowerCase().endsWith(".mp3")) {
MP3File mp3file = (MP3File) AudioFileIO.read(new File(s));
Tag tag = mp3file.getTag();
if (tag != null) {
List<Artwork> arts = mp3file.getTag().getArtworkList();
if (arts == null || arts.size() == 0 ) {
// log("No art for " + s);
}
else if (arts.size() > 1) {
log("More than 1 artwork for " + s);
}
}
else {
log("No tag found for " + s);
}
}
}
log("Done.");
}
}
Detect if MP3 artwork is bigger than a limit.
To save disk space, it's not bad idea to reduce some artwork size to something under 200K, especially if 1000's of MP3 files.This progam will produce a report to help you to achieve that goal.
Folder and sub-folders are scanned.
package rgagnon.mp3;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.LogManager;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.images.Artwork;
import static rgagnon.mp3.Utils.processFolder;
import static rgagnon.mp3.Utils.bytesToKb;
import static rgagnon.mp3.Utils.log;
/**
* Detect if MP3 artwork is bigger than a limit
* using https://www.jthink.net/jaudiotagger/
*
*/
public class DetectBigArtwork {
public static void main(String[] args) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
// NOTE :
// jaudiotagger can generate many warnings when analyzing a MP3 file,
// this will suppress unwanted output.
LogManager.getLogManager().reset();
String folder = "\\\\MYSERVER\\Public\\_Music\\MP3\\byartist (E)\\Weezer";
final long sizeLimit = 1024L * 150L; // artwork size limit 150K
log("Process " + folder +
" artwork size limit : " + bytesToKb(sizeLimit) +"k");
List mp3 = processFolder(folder);
for(String s : mp3) {
if (s.toLowerCase().endsWith(".mp3")) {
MP3File mp3file = (MP3File) AudioFileIO.read(new File(s));
Tag tag = mp3file.getTag();
if (tag != null) {
List arts = mp3file.getTag().getArtworkList();
if (arts == null || arts.size() == 0 ) {
log("No art for " + s);
}
else {
Artwork artwork = arts.get(0);
long actualSize = artwork.getBinaryData().length;
if (actualSize > sizeLimit) {
log(s + " *** BIG Artwork found : "
+ bytesToKb(artwork.getBinaryData().length) + "k");
}
}
}
else {
log("No tag found for " + s);
}
}
}
log("Done.");
}
}
While you can write tags into a MP3 file with Jaudiotagger library, it's easier to use the MP3TAG tool.
Mp3tag is a powerful and easy-to-use tool to edit metadata of audio files.
It supports batch tag-editing of ID3v1, ID3v2.3, ID3v2.4, iTunes MP4, WMA, Vorbis Comments and
APE Tags for multiple files at once covering a
variety of audio formats.
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com