小程序录音文件上传与讯飞语音识别
package com.example.service.impl;
import com.example.service.XunFeiService;
import com.example.utils.FFMPEGUtil;
import com.example.utils.SR2Words;
import com.example.utils.SRTool;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* Created by songzs on 2017/12/12.
*/
@Service
public class XunFeiServiceImpl implements XunFeiService {
@Override
public Map
Map
UUID uuid = UUID.randomUUID();
String path = "/home/workspace/audio";
String fileName = uuid.toString()+".silk";
//临时silk文件
String tempFile = "/home/workspace/audio/"+uuid.toString()+".silk";
//中间过渡pcm文件
String pcmFile = "/home/workspace/audio/"+uuid.toString()+".pcm";
//可识别的wav文件
String wavFile = "/home/workspace/audio/"+uuid.toString()+".wav";
File file = new File(path,fileName);
try {
multi.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
/*移除临时silk文件首字节start*/
//标准silk文件
String silkFile = FFMPEGUtil.silk_remove_word(tempFile);
/*移除临时silk文件首字节end*/
//silk文件转换成pcm文件
String silk2Pcm = FFMPEGUtil.silk2Pcm(silkFile, pcmFile);
//pcm文件转换成wav文件
String pcm2Wav = FFMPEGUtil.pcm2Wav(silk2Pcm, wavFile);
//讯飞语音识别接口识别wav音频文件,转成文字返回
SRTool sr = new SRTool();
String words = null;
try {
words = sr.voice2words(pcm2Wav);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("讯飞识别的语音结果:"+words);
if("".equals(words)){
System.out.println("讯飞识别的语音结果:null");
map.put("status","error");
map.put("content","对不起,请您在描述一遍!");
return map;
}
String result = SR2Words.sr2words(words);
System.out.println("讯飞识别的语音结果:"+result);
map.put("status","success");
map.put("content",result);
return map;
}
}