1、文件上傳,使用springmvc一直不想,后來看到別人有一樣的情況改成了serverlet就可以了
2、因為要進行語音識別成文字,上傳的語音文件是silk格式,需要用到訊飛的語音識別所以必須轉成wav,用到了kn007大神的這個工具https://github.com/kn007/silk-v3-decoder才搞定過程比較坎坷
3、還想把返回結果轉換成語音文件給小程序進行播放,結果...........silk是有了,但是一直播放不了,因為小程序的silk似乎需要特定的參數格式才行,最后轉了個war給前端,讓它播放
4、期間使用ffmpeg進行各種格式轉換,ffmpeg的參數設置也挺惡心的,下面留一個可用的例子
-
public static void convertAudio(String sourcePath,int sourceHZ,String targetPath,int targetHZ){
-
Properties props=System.getProperties(); //獲得系統屬性集
-
String osName = props.getProperty("os.name"); //操作系統名稱
-
String command = null;
-
if(osName.contains("Windows")){
-
// ffmpeg -y -f s16le -ar 24000 -ac 1 -i "$1.pcm" -f wav -ar 16000 -b:a 16 -ac 1 "${1%.*}.$2"
-
command = "C:\\ffmpeg.exe -y -f s16le -ar "+sourceHZ+" -i "+sourcePath+" -f wav -ar "+targetHZ+" -b:a 8 -ac 1 "+targetPath;
-
}else{
-
command = "/usr/local/ffmpeg/bin/ffmpeg -y -f s16le -ar "+sourceHZ+" -ac 1 -i "+sourcePath+" -f wav -ar "+targetHZ+" -b:a 8 -ac 1 "+targetPath;
-
}
-
System.out.println("格式轉換:"+command);
-
ShellExec.execShell(command);
-
}
-
-
public static void pcmToSilk(String pcmPath,String silkPath) throws InterruptedException{
-
//首先 pcm轉換成8000的wav,然后wav轉成silk
-
// ShellExec.convertAudio(pcmPath, 16000, pcmPath+".wav", 16000);
-
// Thread.sleep(1000);
-
// ShellExec.convertAudio(pcmPath+".wav", 16000, silkPath, 8000);
-
-
-
ShellExec.convertAudio(pcmPath, 16000, silkPath, 8000);
-
}
-
-
public static void pcmToWav(String pcmPath,String wavPath){
-
Properties props=System.getProperties(); //獲得系統屬性集
-
String osName = props.getProperty("os.name"); //操作系統名稱
-
String command = null;
-
if(osName.contains("Windows")){
-
command = "C:\\ffmpeg.exe -f s16le -ar 16000 -i "+pcmPath+" -f wav -ar 16000 -b:a 8 -ac 1 "+wavPath;
-
}else{
-
command = "/usr/local/ffmpeg/bin/ffmpeg -f s16le -ar 16000 -i "+pcmPath+" -f wav -ar 16000 -b:a 16 -ac 1 "+wavPath;
-
}
-
System.out.println("格式轉換:"+command);
-
ShellExec.execShell(command);
-
}
-
-
public static void silkToWav(String silkPath,String wavPath){
-
Properties props=System.getProperties(); //獲得系統屬性集
-
String osName = props.getProperty("os.name"); //操作系統名稱
-
String command = null;
-
if(osName.contains("Windows")){
-
// ffmpeg -y -f s16le -ar 24000 -ac 1 -i "$1.pcm" -f wav -ar 16000 -b:a 16 -ac 1 "${1%.*}.$2"
-
command = "C:\\ffmpeg.exe -y -f s16le -ar 8000 -ac 1 -i "+silkPath+" -f wav -ar 16000 -b:a 16 -ac 1 "+wavPath;
-
}else{
-
command = "/usr/local/ffmpeg/bin/ffmpeg -y -f s16le -ar 8000 -ac 1 -i "+silkPath+" -f wav -ar 16000 -b:a 16 -ac 1 "+wavPath;
-
}
-
System.out.println("格式轉換:"+command);
-
ShellExec.execShell(command);
-
-
}