/**
* AudioUtils.java File
*/
package co.cc.enlightensoft.util;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This utils class would provide all Audio operations like convert Flv To Mp3
* etc.
*
* @author Pankil Patel
*
*/
public class AudioUtils {
private static Log LOG = LogFactory.getLog(FileUtils.class);
/**
*
* @param directory
* @return
* @throws IOException
*/
public static boolean convertFlvToMp3(String flvFile, String mp3Folder,
String mp3FileName) throws IOException {
String cmd = “ffmpeg -title \”" + mp3FileName + “\” -i \”" + flvFile
+ “\” -acodec mp3 -ac 2 -ab 128 -vn -y \”" + mp3Folder
+ mp3FileName + “.mp3\”";
LOG.info(“converFlvToMp3 cmd: ” + cmd);
Runtime.getRuntime().exec(cmd.split(” “));
System.out.println(“converFlvToMp3: Done………………”);
// Runtime.getRuntime().exec(cmd.split(” “));
return true;
}
/**
*
* @param directory
* @return
* @throws IOException
*/
public static boolean convertFlvToWav(String flvNameFileWithFullPath,
String wavFileNameWithFullPath) throws IOException {
String cmd = “ffmpeg -i ” + flvNameFileWithFullPath + ” -y “
+ wavFileNameWithFullPath;
LOG.info(“convertFlvToWav cmd: ” + cmd);
RuntimeUtils.executeCommand(cmd.split(” “));
System.out.println(“convertFlvToWav: Done………………”);
return true;
}
/**
*
* @param directory
* @return
* @throws IOException
*/
public static boolean convertWavToMP3(String wavNameFileWithFullPath,
String mp3FileNameWithFullPath) throws IOException {
String cmd = “lame -V2 ” + wavNameFileWithFullPath + ” “
+ mp3FileNameWithFullPath;
LOG.info(“convertWavToMP3 cmd: ” + cmd);
RuntimeUtils.executeCommand(cmd.split(” “));
System.out.println(“convertWavToMP3: Done………………”);
return true;
}
/**
* @param wavFileFolderPath
* @param genderCommand
* @return
* @throws IOException
*/
public static boolean convertWavToPM(String wavFileFullPath,
String genderCommand, String pmGeneratorPath) throws IOException {
String cmd = pmGeneratorPath + genderCommand + ” ” + wavFileFullPath;
LOG.info(“convertWavToPM cmd: ” + cmd);
RuntimeUtils.executeCommand(cmd.split(” “));
System.out.println(“convertWavToPM: Done………………”);
return true;
}
}
/**
* RuntimeUtils.java File
*/
package co.cc.enlightensoft.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author Pankil Patel
*
*/
public class RuntimeUtils {
private static final Log LOG = LogFactory.getLog(RuntimeUtils.class);
/**
* Executes the given command and return the results as a list
*
* @param cmd
* The command that is being executed.
* @return List The list of the out put
*/
public static List<String> executeCommand(String… cmd) {
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
LOG.info(e.getMessage() + e);
e.printStackTrace();
}
BufferedReader localBufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String str;
ArrayList<String> localArrayList = null;
try {
str = localBufferedReader.readLine();
localArrayList = new ArrayList<String>();
while (str != null) {
localArrayList.add(str);
str = localBufferedReader.readLine();
}
} catch (IOException e) {
LOG.info(e.getMessage() + e);
e.printStackTrace();
}
return localArrayList;
}
/**
* Executes the given command and return the results as a list
*
* @param cmd
* The command that is being executed.
* @return List The list of the out put
*/
public static List<String> executeCommand(String cmd) {
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);
try {// For Printing Error
process.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
String line = “”;
while ((line = buf.readLine()) != null) {
System.out.println(line);
}
} catch (InterruptedException e) {
LOG.info(e.getMessage() + e);
e.printStackTrace();
}
} catch (IOException e) {
LOG.info(e.getMessage() + e);
e.printStackTrace();
}
BufferedReader localBufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String str;
ArrayList<String> localArrayList = null;
try {
str = localBufferedReader.readLine();
localArrayList = new ArrayList<String>();
while (str != null) {
localArrayList.add(str);
str = localBufferedReader.readLine();
}
} catch (IOException e) {
LOG.info(e.getMessage() + e);
e.printStackTrace();
}
return localArrayList;
}
}