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

本文介绍了一种使用LAME编码器将WAV文件转换为MP3文件的方法,并提供了批量处理不同层级文件夹内所有WAV文件的功能,同时涉及创建批处理文件以简化操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先下载附件 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);
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值