java 调用lame.exe 将 wav 进行MP3压缩的一些有关问题

java 调用lame.exe 将 wav 进行MP3压缩的一些问题
首先下载附件 lame.exe copy到d 盘,因为代码默认是去d盘找,当然可以修改

注意singleShell 里面的2个线程 ,如果注释调就能发现,程序运行阻塞了。这2个线程在于情况当前现成的IO操作,使得线程变成正常状态。


package xm.createpkg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class AudioImpress {
	/**
	 * 转化单个文件
	 * @param path
	 * @return
	 */
	public static String singleShell(String path) {
		String newName = path.replace(".wav", ".mp3");
		String command = "d:\\lame -V2  " + path + " " + newName;
		try {
			String cmd = createBat(command);
			Process process = Runtime.getRuntime().exec(cmd);
			
		            final InputStream is1 = process.getInputStream();  
		            final InputStream is2 = process.getErrorStream();  
		            new Thread() {  
		                public void run() {  
		                    BufferedReader br = new BufferedReader(  
		                            new InputStreamReader(is1));  
		                    try {  
		                        String lineB = null;  
		  
		                        while ((lineB = br.readLine()) != null) {  
		                            if (lineB != null) System.out.println(lineB);  
		  
		                        }  
		                    } catch (IOException e) {  
		                        e.printStackTrace();  
		                    }  
		                }  
		            }.start();  
		  
		            new Thread() {  
		                public void run() {  
		                    BufferedReader br2 = new BufferedReader(  
		                            new InputStreamReader(is2));  
		                    try {  
		  
		                        String lineC = null;  
		                        while ((lineC = br2.readLine()) != null) {  
		  
		                            if (lineC != null) System.out.println(lineC);  
		                        }  
		                    } catch (IOException e) {  
		                        e.printStackTrace();  
		                    }  
		                }  
		            }.start();  
		            process.waitFor();  
			
			
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return newName;
	}
	/**
	 * 转化一个文件下所以文件,包括n层文件夹
	 * @param strList
	 */
	private static void callShell(List<String> strList) {
		try {
			for (int i = 0; i < strList.size(); i++) {
				String newName = strList.get(i).replace(".wav", ".mp3");
				String command = "d:\\lame -V2  " + strList.get(i) + " "
						+ newName;
				String cmd = createBat(command);
				Process process = Runtime.getRuntime().exec(cmd);
			}

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

	private static String createBat(String command) throws IOException {
		String dir = "d:\\lame.bat";

		byte[] b = command.getBytes();
		File file = new File(dir);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream os = new FileOutputStream(file);
		os.write(b);
		os.close();

		return dir;

	}

	private static List<String> getFileName(String path, List<String> strList) {
		File file = new File(path);
		FileFilter ff = new FileFilter() {
			@Override
			public boolean accept(File dir) {
				if (dir.getName().endsWith(".wav")) {
					return true;
				}
				return false;
			}
		};
		if (file.isFile()) {
			System.out.println("必须是文件夹");
		} else {
			File[] fileList = file.listFiles(ff);
			for (File f : fileList) {
				if (f.isFile()) {
					strList.add(f.getAbsolutePath());
				} else {
					getFileName(f.getAbsolutePath(), strList);
				}
			}
		}
		return strList;
	}

	public static void main(String[] args) throws IOException {

		String path = "G:\\本人文档存放\\研发中心任务文档\\自动分析html\\colorblock-音效文件\\audio\\20114822174851.wav";
		// change(path);
		//AudioImpress ai  = new AudioImpress();
		singleShell(path);
		//CreatePkg pkg = new CreatePkg();
		//pkg.audioImpress(path);
	}

	private static void change(String path) {
		List<String> strList = new ArrayList<String>();
		getFileName(path, strList);
		callShell(strList);
	}
}