1.先裝一個ffmpeg環境,安裝方法請自行百度。
2.建一個類FfmpegTest
public class FfmpegTest {
public static void main(String[] args) {
String sPath = "d:\\music\\test.wav";
String tPath = "c:\\music\\test.pcm";
try {
new FfmpegTest().changeAmrToMp3(sPath,tPath);
} catch (Exception e) {
e.printStackTrace();
}
}
public void changeAmrToMp3(String sourcePath, String targetPath) throws Exception {
String webroot = "c:\\ffmpeg\\bin";
Runtime run = null;
try {
run = Runtime.getRuntime();
long start=System.currentTimeMillis();
System.out.println(new File(webroot).getAbsolutePath());
//執行ffmpeg.exe,前面是ffmpeg.exe的地址,中間是需要轉換的文件地址,後面是轉換後的文件地址。-i是轉換方式,意思是可編碼解碼,mp3編碼方式採用的是libmp3lame
//wav轉pcm
//Process p=run.exec(new File(webroot).getAbsolutePath()+"/ffmpeg -y -i "+sourcePath+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetPath);
//mp3轉pcm
Process p=run.exec(new File(webroot).getAbsolutePath()+"/ffmpeg -y -i "+sourcePath+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetPath);
//釋放進程
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();
p.waitFor();
long end=System.currentTimeMillis();
System.out.println(sourcePath+" convert success, costs:"+(end-start)+"ms");
} catch (Exception e) {
e.printStackTrace();
}finally{
//run調用lame解碼器最後釋放內存
run.freeMemory();
}
}
詳情參考百度語音音頻轉碼