Sunday, November 12, 2017

Add / Modify Songs Properties using Java & Jaudiotagger library

Introduction

Here I have provided peace of code to modify songs info, like album, artist, artwork, etc using Java & Jaudiotagger library

You can download jaudiotagger library


https://bitbucket.org/ijabz/jaudiotagger/downloads/jaudiotagger-2.2.6-SNAPSHOT.jar



Assume songs folder location c:/tmp/songs

RenameSongsInfo


import java.io.File;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;

/*
 *This program helps to add/modify songs properties 
 */
public class RenameSongsInfo {

 public static void main(final String[] args) {
  String songLocation = "C:/tmp/songs";

  File dir = new File(songLocation);

  traverseSongsInfo(dir);

 }

 static void traverseSongsInfo(File file) {

  String folderName = "";

  if (file.isDirectory()) {
   File[] subFiles = file.listFiles();

   if (subFiles.length == 0) {
    System.out.println("The directory is empty");
   } else {
    for (File subFile: subFiles) {
     if (subFile.isDirectory()) {
      traverseSongsInfo(subFile);
     } else {
      folderName = subFile.getParentFile().getName();
      if (subFile.getAbsolutePath().contains("Elaiyaraja Hits"))
       folderName = "Elaiyaraja Hits";

      setSongAuthor(folderName, subFile);
     }
    }
   }
  }
 }

 static void setSongAuthor(String folderName, File mp3File) {

  try {

   AudioFile f = AudioFileIO.read(mp3File);
   Tag tag = f.getTagOrCreateAndSetDefault();

   if (tag == null) {
    System.out.println("tag is null");
   } else {

    tag.setField(FieldKey.ARTIST, folderName);
    tag.setField(FieldKey.ALBUM_ARTIST, folderName);

    String album = tag.getFirst(FieldKey.ALBUM);

    //Here I am removing web site name from existing album name, you can provide custom name if you want
    album = album.replace(" - TamilWire.com", "");

    tag.setField(FieldKey.ALBUM, album);

    tag.deleteArtworkField();

    f.commit();
    System.out.println("Completed - " + mp3File.getAbsolutePath());
   }
  } catch (Exception e) {
   System.err.println(e);
  }

 }

}


I have tested with jdk1.8 in windows 10.
That's it!!


Copyright - There is no copyright on the code. You can copy, change and distribute it freely. Just mentioning this site should be fair
(C) November 2017, manivelcode

No comments: