Process MP3 tags Tag(s): IO OpenSource


I like to have my MP3 tags organized and cleaned. To do that I am using the jaudiotagger library to detect unwanted tags (like COMMENT or WWW link) 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.

Detect if COMMENT is found in a MP3 files in a directory and sub-directories.

package rgagnon.mp3;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
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 org.jaudiotagger.tag.images.Artwork;

/**
 * Detect if comment is present.
 * using http://www.jthink.net/jaudiotagger/
 *
 */
public class Mp3Comment {
   public static void main(String[] args) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
      LogManager.getLogManager().reset();   // jaudiotagger can generating many warnings when analyzing a MP3 file,
                                            // this will suppress unwanted output.
      String path = "\\\\MYSERVER\\Public\\_Music\\MP3\\byartist (E)\\W";
      System.out.println("Process " + path);

      List<String> mp3 = Mp3Comment.process(path);
      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())) {
                     // System.out.println("No COMMENT for " + s);
                  }
                  else if (comment.length() > 0) {
                     System.out.println("COMMENT found in " + s + " : " + comment);
                  }
               }
               else {
                  System.out.println("No tag found for " + s);
               }

            }
            catch (KeyNotFoundException e) {
               System.out.println("COMMENT key not found in " + s);
            }
          }
         }
      System.out.println("Done.");
   }

   private static List <String> process(String path) throws IOException {
      String result, _result[];

      if ( (new File(path)).exists() ) {
         result = recurseInDirFrom(path);
         _result = result.split("\\|");
         return Arrays.asList(_result);
      }
      else {
         throw new IOException("Path not found : " + path);
      }
   }

   private static String recurseInDirFrom(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 + "|"
             + recurseInDirFrom(dir + File.separatorChar + list[i]);
        }
      return result;
   }
}

Detect if MP3 file contains multiple artworks.

package rgagnon.mp3;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

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.TagException;
import org.jaudiotagger.tag.images.Artwork;

/**
 * Detect if multiple artworks or no artwork defined
 * using http://www.jthink.net/jaudiotagger/
 *
 */
public class Mp3MultipleArtwork {
   public static void main(String[] args) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
      LogManager.getLogManager().reset();   // jaudiotagger can generating many warnings when analyzing a MP3 file,
                                            // this will suppress unwanted output.
      String path = "\\\\MYSERVER\\Public\\_Music\\MP3\\byartist (E)\\W";

      System.out.println("Process " + path);
      List<String> mp3 = Mp3MultipleArtwork.process(path);

      for(String s : mp3) {
         if (s.toLowerCase().endsWith(".mp3")) {
            MP3File mp3file = (MP3File) AudioFileIO.read(new File(s));
            List<Artwork> arts = mp3file.getTag().getArtworkList();

            if (arts.size() == 0) {
               System.out.println("No art for " + s);

            }
            else if (arts.size() > 1) {
               System.out.println("Multiple art for " + s);
            }
          }
         }
      System.out.println("Done.");
   }

   private static List <String> process(String path) throws IOException {
      String result, _result[];

      if ( (new File(path)).exists() ) {
         result = recurseInDirFrom(path);
         _result = result.split("\\|");
         return Arrays.asList(_result);
      }
      else {
         throw new IOException("Path not found : " + path);
      }
   }

   private static String recurseInDirFrom(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 + "|"
             + recurseInDirFrom(dir + File.separatorChar + list[i]);
        }
      return result;
   }
}

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.


blog comments powered by Disqus