Wednesday, November 2, 2016

Metadata from mp3 files, Apache TIKA


Today's Discussion: Get Metadata ()Artist, Album, Creator) from MP3 files for Android Music Player

This Java program will retrieve all mp3 files from the given directory (inside nested directories or files) and display Album, Artist, 
Creator etc metadata (tags). The same can be used in sorting and display in Android Music Player for playing mp3 files. The same program 
will list out if there are Duplicate Files too in other directories. This uses "Apache Tika" Framework. 
Apache TIKA is used for:  Language detection mechanism and MIME detection mechanism. So it requires input as video (mp4, mkv etc), 
audio (mp3), pdf. It provides parser libraries which parses the input files and provides Metadata Extracts like:

mp3: Artist, Album, Title, Name, Creator etc.
mp4: FileTypes
pdf: Language (ge- german) etc.


   
//DEMO_AudioParser.java
package dmodi.mp3;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

//Jars required: tika-core-1.3.jar, tika-parsers-1.3.jar
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.mp3.Mp3Parser;
import org.xml.sax.ContentHandler;
import org.xml.sax.helpers.DefaultHandler;

public class DEMO_AudioParser {
    static String temp="";
    static int fileCount=1;
    static String fileLocation = "E:\\Personal\\Audio Songs";  //Provide your directory location
    static Set<String> albumSet = new HashSet<String>();
    static Map<String, Integer> fileMap = new HashMap<String, Integer>();

    public static void main(String[] args) {
        File folder = new File(fileLocation);
        listFilesForFolder(folder);
    }

    public static void listFilesForFolder(final File folder) {
        String fileName = "";
        String albumName = "";

        InputStream input = null;
        ContentHandler handler = new DefaultHandler();
        Metadata metadata = new Metadata();
        Parser parser = new Mp3Parser();
        ParseContext parseCtx=new ParseContext();


        for (final File fileEntry : folder.listFiles()) {  //File[] listOfFiles = folder.listFiles();
            if (fileEntry.isDirectory()) {
                //System.out.println("*************Reading files under the Directory: "+fileEntry);
                listFilesForFolder(fileEntry);
            } else {
                if(fileEntry.isFile()) {
                    temp = fileEntry.getName();
                    if((temp.substring(temp.lastIndexOf('.') + 1, temp.length()).toLowerCase()).equals("mp3")) {  //Will take only mp3 files
                        fileName = fileEntry.getName();
                        if(fileMap.containsKey(fileName)){   //Duplicate File Check
                            fileMap.put(fileName, fileMap.get(fileName)+1);
                            System.out.println("Duplicate folder: "+fileEntry+", fileName: "+fileName);
                        }
                        else
                            fileMap.put(fileName, 1);

                        try {
                            input = new FileInputStream(fileEntry);
                            parser.parse(input, handler, metadata, parseCtx);
                            input.close();

                            /*String[] metadataNames = metadata.names();
                            for(String name : metadataNames){
                                System.out.println(name + ": " + metadata.get(name));  //audioCompressor, releaseDate, title, author, album, creator, audio/mpeg etc.
                            }*/
                            albumName = metadata.get("xmpDM:album");    //This you can get from above metadata for loop
                            if(albumName!=null){
                                albumName = albumName.trim();
                            }
                            System.out.println("FileName: "+fileEntry+", Album : "+albumName+", Count: "+fileCount);
                            fileCount++;
                            albumSet.add(albumName);
                        }
                        catch(Exception e){
                            e.printStackTrace();
                            System.out.println("ERROR: fileName: "+fileName);
                        }
                    }
                }
            }
        }
    }
}

//Sample output:
FileName: E:\Personal\Audio Songs\MixSongs5\Kaddu Kaddu.mp3, Album : title, Count: 1
FileName: E:\Personal\Audio Songs\MixSongs5\Kagaz Ki Kashti.mp3, Album : Unknown Album (9/17/2005 10:26:39 PM), Count: 2
FileName: E:\Personal\Audio Songs\MixSongs5\KAHIN DEP JALE.mp3, Album : null, Count: 3
FileName: E:\Personal\Audio Songs\MixSongs5\Khalbali (Papuyaar[1].com).mp3, Album : Rang De Basanti @ Papuyaar.com, Count: 4
FileName: E:\Personal\Audio Songs\MixSongs5\khamoshi3(www.songs.pk).mp3, Album : Khamoshi, Count: 5
FileName: E:\Personal\Audio Songs\MixSongs5\khattameetha01(www.songs.pk).mp3, Album : Khatta Meetha, Count: 6

==============================END=============================