windows系统是需要jar包的,下面贴链接可供下载。
/**
* 将amr格式转成mp3格式
* @param amrFilePath amr文件
* @return mp3文件路径
*/
public static String convertAmr2Mp3(String amrFilePath) {
File source = new File(amrFilePath);
String extension = amrFilePath.substring(amrFilePath.lastIndexOf("."));
String targetFilename = amrFilePath.replace(extension, ".wav");
String os = System.getProperties().getProperty("os.name").toLowerCase();
if (os.startsWith("win")) { //windows系统转为mp3格式
File target = new File(targetFilename);
Encoder encoder = new Encoder();
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
attrs.setAudioAttributes(audio);
try {
encoder.encode(source, target, attrs);
} catch (Exception e) {
e.printStackTrace();
LOGGER.debug("win convert amr to wav error", e.getMessage());
}
} else { //linux转为wav格式
String command = "ffmpeg -i " + amrFilePath + " "+ targetFilename;
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
LOGGER.debug("linux convert amr to wav error", e.getMessage());
}
}
return targetFilename;
}