001package ca.bc.webarts.tools;
002
003import java.io.File;
004// not necessary but saves headaches later if classes move
005import ca.bc.webarts.widgets.Util;
006import ca.bc.webarts.tools.NativeAppLauncher;
007import ca.bc.webarts.tools.StreamGobbler;
008
009public class RecurseOgg2Mp3
010{
011  private static String ogg123 = "/usr/bin/ogg123";
012  private static String mp3Encode = "/usr/local/bin/lame";
013  private static String mp3OutDir = "/home/tgutwin/cdstage/filesToBurn";
014
015  /** recursive conversion of passed filename **/
016  public static void recursiveConvert(String filename)
017  {
018    File tmpFile = new File(filename);
019    String[] files = null;
020
021    //first check if it is a dir
022    if (tmpFile.exists() && tmpFile.isDirectory() )
023    {
024       files = tmpFile.list();
025      //loop through all the files in the dir
026      for (int i = 0; i < files.length; i++)
027        recursiveConvert(filename + File.separator + files[i]);
028    }
029    else if (tmpFile.exists() && filename.endsWith(".ogg") )
030         // not a dir... convert it.
031    {
032
033      String strippedFilename = filename.substring(0,filename.length()-4);
034      String strippedShortFilename =
035          tmpFile.getName().substring(0,tmpFile.getName().length()-4);
036      String wavFilename = strippedFilename+".wav";
037      String mp3Filename = mp3OutDir+File.separator+strippedShortFilename+".mp3";
038      String oggFilename = strippedFilename+".ogg";
039
040      // check that it hasn't been converted yet.
041      tmpFile = new File(mp3Filename);
042      System.out.println("Checking for " + mp3Filename);
043      if (!tmpFile.exists())
044      {
045        try
046        {
047          // 1st make sure the wav file does not exist
048          tmpFile = new File(wavFilename);
049          if (tmpFile.exists())
050            tmpFile.delete();
051
052          System.out.println("Converting " + filename + " to wav");
053          // convert to ogg-->wav
054          //String [] passedOggArgs =  {"-d", "wav", "-f", "\""+wavFilename+"\"", "\""+filename+"\""};
055          String [] passedOggArgs =  {"-d", "wav", "-f", wavFilename, filename};
056          new NativeAppLauncher(ogg123, passedOggArgs);
057
058          // convert wav-->mp3
059          System.out.println("Converting " + wavFilename + " to "+mp3Filename );
060          //String [] passedMp3Args =  { "'"+wavFilename+"'", "'"+mp3Filename+"'" };
061          String [] passedMp3Args =  { wavFilename, mp3Filename };
062          new NativeAppLauncher(mp3Encode, passedMp3Args);
063
064          // Now delete the wav
065          tmpFile = new File(wavFilename);
066          if (tmpFile.exists())
067            tmpFile.delete();
068
069          // move the mp3 to the specified mp3OutDir
070          //Util.moveFile(mp3Filename, mp3OutDir);
071        }
072        catch (Exception ex)
073        {
074          System.out.println("Error Deleting "+wavFilename);
075        }
076      }
077    }
078  }
079
080  /**
081   *  The main program method for this class
082   *
083   * @param  args  The command line arguments
084   */
085  public static void main(String[] args)
086  {
087    if (args.length>0)
088    {
089      for (int i = 0; i < args.length; i++)
090      {
091        recursiveConvert(args[i]);
092      }
093    }
094
095  }
096}
097