Arrange music for iTunes, build albums automatically for music files that miss album name, artist name etc.

I did come across a problem with my Iphone and other Apple devices. Songs were not grouping properly in albums, and as all know apple group songs by all other tags nut the folder you keep them in. I have large music library, and I wanted to change album name to name of folder in which the songs were contained. Below is small Java program that will help you achieving that. Just change the directory paths and enjoy.

#Main Class
package com.gvt;


import java.io.File;

import java.io.FileFilter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;


import javax.swing.filechooser.FileNameExtensionFilter;


import org.jaudiotagger.audio.AudioFile;

import org.jaudiotagger.audio.AudioFileIO;

import org.jaudiotagger.audio.exceptions.CannotReadException;

import org.jaudiotagger.audio.exceptions.CannotWriteException;

import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;

import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;

import org.jaudiotagger.tag.FieldKey;

import org.jaudiotagger.tag.Tag;

import org.jaudiotagger.tag.TagException;


public class Main {

 private static List<String> permittedType = new ArrayList<String>();


 public static void main(String a[]) {

  permittedType.add("mp3");

  permittedType.add("ogg");

  permittedType.add("mpg");

  permittedType.add("wma");

  permittedType.add("wav");

  permittedType.add("wmv");

  permittedType.add("avi");


  a = new String[] { "/Users/vaibhavsingh/Music/songs" };

  if (a == null || a.length == 0) {

   printUsageSyntaxAndExit();

  }

  String dirPath = a[0];

  if (dirPath == null || "".equals(dirPath)) {

   printUsageSyntaxAndExit();

  }

  File baseDir = new File(dirPath);

  if (!baseDir.isDirectory()) {

   printUsageSyntaxAndExit();

  }

  for (File f : baseDir.listFiles()) {

   dirOperation(f);

  }

 }


 private static void fileOperation(File f) {

  String extension = f.getName().substring(f.getName().lastIndexOf(".") + 1);

  extension = extension.toLowerCase();

  if (permittedType.contains(extension)) {

   try {

    arrangeMusic(f);

   } catch (Exception e) {

    System.err.println("Error with file :" + f.getName() + " : " + e.getMessage());

   }

  }

 }


 private static void dirOperation(File f) {

  // System.out.println(f.getName());

  if (f.isDirectory()) {

   for (File file : f.listFiles()) {

    if (file.isDirectory())

     dirOperation(file);

    else

     fileOperation(file);

   }

  } else {

   fileOperation(f);

  }

 }


 private static void printUsageSyntaxAndExit() {

  System.err.println("No Music directory specfied");

  System.err.println("Usage is as follows");

  System.err.println("java -jar ArrangeMusic.jar <path of music directory>");

  System.exit(0);

 }


 private static void arrangeMusic(File file) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException, CannotWriteException {

  AudioFile f = AudioFileIO.read(file);

  Tag tag = f.getTag();

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

  if (album == null || "".equals(album.trim())) {

   String albumNameByDirectory = file.getParentFile().getName();

   tag.setField(FieldKey.ALBUM, albumNameByDirectory);

   MusicTags myTag = getMusicTags(tag);

   f.commit();

   System.out.println("Modified file details for album: <" + albumNameByDirectory + "> file: <" + file.getName() + "> : " + myTag.toString());

  }

 }


 private static MusicTags getMusicTags(Tag tag) {

  MusicTags myTags = new MusicTags();

  myTags.setArtist(tag.getFirst(FieldKey.ARTIST));

  myTags.setAlbum(tag.getFirst(FieldKey.ALBUM));

  myTags.setTitle(tag.getFirst(FieldKey.TITLE));

  myTags.setComment(tag.getFirst(FieldKey.COMMENT));

  myTags.setYear(tag.getFirst(FieldKey.YEAR));

  myTags.setTrack(tag.getFirst(FieldKey.TRACK));

  myTags.setDiscNo(tag.getFirst(FieldKey.DISC_NO));

  myTags.setComposer(tag.getFirst(FieldKey.COMPOSER));

  myTags.setArtistSort(tag.getFirst(FieldKey.ARTIST_SORT));

  return myTags;

 }


}



#FileNameFilter.java
package com.gvt;


import java.io.File;

import java.io.FileFilter;


public class FileNameFilter implements FileFilter {


 public FileNameFilter() {

  // TODO Auto-generated constructor stub

 }


 @Override

 public boolean accept(File pathname) {

  // TODO Auto-generated method stub

  return false;

 }
}





#MusicTags.java
package com.gvt;

public class MusicTags {


 private String artist;

 private String album;

 private String title;

 private String comment;

 private String year;

 private String track;

 private String discNo;

 private String composer;

 private String artistSort;

 public String getArtist() {

  return artist;

 }

 public void setArtist(String artist) {

  this.artist = artist;

 }

 public String getAlbum() {

  return album;

 }

 public void setAlbum(String album) {

  this.album = album;

 }

 public String getTitle() {

  return title;

 }

 public void setTitle(String title) {

  this.title = title;

 }

 public String getComment() {

  return comment;

 }

 public void setComment(String comment) {

  this.comment = comment;

 }

 public String getYear() {

  return year;

 }

 public void setYear(String year) {

  this.year = year;

 }

 public String getTrack() {

  return track;

 }

 public void setTrack(String track) {

  this.track = track;

 }

 public String getDiscNo() {

  return discNo;

 }

 public void setDiscNo(String discNo) {

  this.discNo = discNo;

 }

 public String getComposer() {

  return composer;

 }

 public void setComposer(String composer) {

  this.composer = composer;

 }

 

 public String getArtistSort() {

  return artistSort;

 }

 public void setArtistSort(String artistSort) {

  this.artistSort = artistSort;

 }

 @Override

 public String toString() {

  return "MusicTags [artist=" + artist + ", album=" + album + ", title=" + title + ", comment=" + comment + ", year=" + year + ", track=" + track + ", discNo=" + discNo

    + ", composer=" + composer + ", artistort=" + artistSort + "]";

 }
}


Comments

Popular posts from this blog

Caused by: java.sql.SQLTimeoutException: ORA-01013: user requested cancel of current operation

HashiCorp Vault Integration with Ansible Etower using approle

utility to extract date from text with java