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

Saturday, November 11, 2017

Download songs from website using Java

Introduction

If you want to download mp3/song file from website its take time and it will popup adds, to avoid that you can use below program to download songs by one shot.
Create Songs Folder Location to store in Local drive "c:/tmp/songs/";
Place urls properties file in C:/tmp/songs/

DownloadSongsFromWeb



import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;

/** 
 * The standard class for downloading songs from given web url 
 * 
 */ 
public class DownloadSongsFromWeb {
 /*
  * Main Method
  */
 public static void main(String[] args) {

  String tgtLocation = "c:/tmp/songs/";
  String songsUrls = "C:/tmp/songs/urls.properties";

  try {
   File file = new File(songsUrls);
   FileReader fileReader = new FileReader(file);
   BufferedReader bufferedReader = new BufferedReader(fileReader);

   String line;
   while ((line = bufferedReader.readLine()) != null) {
    System.out.println("Processing : " + line);

    if (line.contains("&cpage")) {
     processSPage(line, tgtLocation);
    } else {
     getFileInfo(line, tgtLocation);
    }

    // System.out.println(line + ":Download Completed");
   }
   fileReader.close();

  } catch (IOException e) {
   e.printStackTrace();
  }

 }

 /*
  * It process the entire web url and find mp3 file or child url to get mp3 file 
  */
 private static void processSPage(String line, String tgtLocation) {
  
  try {
   URL url = new URL(line);

   URLConnection uc = url.openConnection();

   uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
   uc.connect();

   BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
   String inputLine="";

   String songsURL[];
   String songsSplitLine = "";
   String newURL = "";
   while ((inputLine = in.readLine()) != null) {

    songsURL = inputLine.split("page=ILaiyaraja Hits&");

    for (int i = 0; i < songsURL.length; i++) {
     songsSplitLine = songsURL[i];
     if (!songsSplitLine.isEmpty() && songsSplitLine.contains("spage=")) {
      
      newURL = songsSplitLine.substring(0, songsSplitLine.indexOf("\""));

      newURL = line.substring(0, line.indexOf("cpage")) + newURL;

      System.out.println(newURL);

      getFileInfo(newURL, tgtLocation);
     }
    }
   }
   in.close();

  } catch (MalformedURLException me) {
   System.out.println(me);

  } catch (IOException ioe) {
   System.out.println(ioe);
  }

 }

 /*
  * This method get the list of mp3 urls from child page and pass url to @downloadMP3 method
  */
 private static void getFileInfo(String urlPage, String tgtLocation) {

  try {
   URL url = new URL(urlPage);

   URLConnection uc = url.openConnection();

   uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
   uc.connect();

   BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
   String inputLine, songUrl = "";

   int count = 0;
   String songsURL[];
   String songsSplitLine = "";
   while ((inputLine = in.readLine()) != null) {

    songsURL = inputLine.split("href=");

    for (int i = 0; i < songsURL.length; i++) {
     songsSplitLine = songsURL[i];

     if (songsSplitLine.toUpperCase().contains(".MP3") && songsSplitLine.contains("http://")) {

      songUrl = songsSplitLine.substring(songsSplitLine.indexOf("http://"),
        songsSplitLine.indexOf(".mp3") + 4);

      downloadMP3(songUrl, tgtLocation);
     }

    }
   }
   in.close();

  } catch (MalformedURLException me) {
   System.out.println(me);

  } catch (IOException ioe) {
   System.out.println(ioe);
  }

 }

 /*
  * This method download mp3 file from give url
  */
 private static void downloadMP3(String songUrl, String tgtLocation) {
  try {
   String[] folderNames = songUrl.split("/");

   String folderName = folderNames[folderNames.length - 2];
   folderName = URLDecoder.decode(folderName, "UTF-8");

   String fileName = folderNames[folderNames.length - 1].replace("TamilWire.com", "");

   fileName = URLDecoder.decode(fileName, "UTF-8").replace(" ", "").replace("-.", ".");

   createFolder(tgtLocation + folderName);

   String tgtFile = tgtLocation + folderName + "/" + fileName;

   if (!new File(tgtFile).exists()) {

    songUrl = songUrl.replace(" ", "%20");

    URLConnection conn = new URL(songUrl).openConnection();

    conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    conn.connect();

    InputStream is = conn.getInputStream();

    OutputStream outstream = new FileOutputStream(new File(tgtFile));
    byte[] buffer = new byte[4096];
    int len;
    while ((len = is.read(buffer)) > 0) {
     outstream.write(buffer, 0, len);
    }
    outstream.close();

    System.out.println(tgtFile + " Downloaded");
   }

  } catch (Exception e) {
   System.err.println(e.getMessage());
  }

 }

 /*
  * It songs folder will be created based on url songs categorization 
  */
 private static void createFolder(String folderPath) {

  File file = new File(folderPath);
  if (!file.exists()) {
   if (file.mkdir()) {
    System.out.println("Directory is created!");
   }
  }
 }

urls.properties
You may provide multiple songs urls in properties file.


http://tamiltunes.live/vijayakanth-hits-59-tamil-songs.html
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