语音转写(讯飞开放平台)工具类
https://www.xfyun.cn/doc/asr/lfasr/Java-SDK.html#_1%E3%80%81%E7%AE%80%E4%BB%8B
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iflytek.msp.lfasr.LfasrClient;
import com.iflytek.msp.lfasr.model.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* 语音转写工具类
* @author MWX
* @date 2021/1/19 16:11
*/
@Slf4j
@Repository
public class PhoneticTranscriptionUtil{
//讯飞平台申请(语音转写)不过每天有限制
public static final String APP_ID = "";
public static final String SECRET_KEY = "";
/**
* 标准语音转写
* @param localPath 本地文件路径
* //音频文件路径
* //1、绝对路径:D:\......\demo-3.0\src\main\resources\audio\lfasr.wav
* //2、相对路径:./resources/audio/lfasr.wav
* //3、通过classpath:
* @throws InterruptedException
*/
private static String standard(String localPath) throws Exception {
//1、创建客户端实例
LfasrClient lfasrClient = LfasrClient.getInstance(APP_ID, SECRET_KEY);
//2、上传
Message task = lfasrClient.upload(localPath);
String taskId = task.getData();
log.info("转写任务 taskId{}",taskId);
//3、查看转写进度
int status = 0;
while (status != 9) {
Message message = lfasrClient.getProgress(taskId);
JSONObject object = JSON.parseObject(message.getData());
status = object.getInteger("status");
System.out.println(message.getData());
TimeUnit.SECONDS.sleep(10);
}
//4、获取结果
Message result = lfasrClient.getResult(taskId);
JSONArray jsonArray=JSON.parseArray(result.getData());
String str = "";
for (int i = 0 ; i < jsonArray.size();i++){
JSONObject rowData = new JSONObject(jsonArray.getJSONObject(i));
String ret = rowData.getString("onebest");
str= str.concat(ret);
}
return str;
}
//java 通过url下载录音文件保存到本地
public static String download(String urlString) throws Exception {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流(输出到项目根目录)
String filename = "./" + UUID.randomUUID() + ".wav"; //下载路径及下载图片名称
File file = new File(filename);
FileOutputStream os = new FileOutputStream(file, true);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
String result = standard(filename);
if(file.isFile() && file.exists()){
boolean delete = file.delete();
}
return result;
}
// public static void main(String[] args) {
// try {
// long startTime = System.currentTimeMillis(); // 获取开始时间
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
// System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
// download("http://sfc-audio-test.oss-cn-shenzhen.aliyuncs.com/sfc-audio%2F20210119_225424.opus?OSSAccessKeyId=LTAI4G7FTdwrxSw13sDddjV7&Expires=1611117983&Signature=a0eYBLPHgDhIQpluQakpmy69%2Bp8%3D");
// long endTime = System.currentTimeMillis(); // 获取结束时间
// System.out.println("程序总运行时间: " + (endTime - startTime) + "ms");//opus 129394ms //103799ms //123282ms //wav 331109ms
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
}