說明:僅支持Windows系統下,amr、aud、slk、silk轉成mp3。轉換之前先下載轉換工具,點我下載,提取碼:2olq
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* 支持在windows系統下,amr、aud、slk、silk轉成mp3格式
*/
public class AudioConversion {
public static void main(String[] args) {
String audioType = "amr";//源音頻文件類型,amr、aud、slk、silk
String source = "E:\\audio\\audio." + audioType;//源音頻文件
String target = "E:\\audio\\audio.mp3";//目標音頻文件
String path = "E:\\audio\\";//音頻轉換工具存放路徑
File sourceFile = new File(source);
if(sourceFile.exists()) {
String pcmPath = path + "audio.pcm";
processPcm(path, source, pcmPath);//先轉成pcm格式
File file=new File(pcmPath);
if(file.exists()) {
processMp3(path, pcmPath, target);//再由pcm轉成mp3
} else {
processFfmpegMp3(path, source, target);//直接轉成mp3
}
} else {
System.out.println("源文件不存在");
}
}
// 調用sile_v3_decoder.exe,轉成pcm格式
private static void processPcm(String path, String source, String pcmPath){
List<String> commend = new ArrayList<String>();
commend.add(path+"silk_v3_decoder.exe");
commend.add(source);
commend.add(pcmPath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p=builder.start();
p=Runtime.getRuntime().exec("taskkill -f -t -im silk_v3_decoder.exe");
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
// 調用ffmpeg,pcm轉mp3
private static void processMp3(String path, String pcmPath, String target) {
//ffmpeg -y -f s16le -ar 24000 -ac 1 -i 源文件 目標文件
List<String> commend = new ArrayList<String>();
commend.add(path+"ffmpeg.exe");
commend.add("-y");
commend.add("-f");
commend.add("s16le");
commend.add("-ar");
commend.add("24000");
commend.add("-ac");
commend.add("1");
commend.add("-i");
commend.add(pcmPath);
commend.add(target);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p=builder.start();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
// 調用ffmpeg.exe,轉MP3
private static void processFfmpegMp3(String path, String source, String target){
List<String> commend = new ArrayList<String>();
commend.add(path+"ffmpeg.exe");
commend.add("-i");
commend.add(source);
commend.add(target);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p=builder.start();
p=Runtime.getRuntime().exec("taskkill -f -t -im ffmpeg.exe");
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}