引入依赖
<!--讯飞-->
<dependency>
<groupId>cn.xfyun</groupId>
<artifactId>websdk-java-speech</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>cn.xfyun</groupId>
<artifactId>websdk-java-nlp</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>cn.xfyun</groupId>
<artifactId>websdk-java-ocr</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>cn.xfyun</groupId>
<artifactId>websdk-java-face-detector</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>cn.xfyun</groupId>
<artifactId>websdk-java-spark</artifactId>
<version>2.0.3</version>
</dependency>
package com.ruoyi.ai.utils;
import cn.xfyun.api.IatClient;
import cn.xfyun.api.TtsClient;
import cn.xfyun.model.response.TtsResponse;
import cn.xfyun.model.response.iat.IatResponse;
import cn.xfyun.model.response.iat.IatResult;
import cn.xfyun.service.iat.AbstractIatWebSocketListener;
import cn.xfyun.service.tts.AbstractTtsWebSocketListener;
import com.ruoyi.common.core.utils.uuid.UUID;
import okhttp3.Response;
import okhttp3.WebSocket;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.security.SignatureException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@Component
public class XfTalkUtil {
private static final String appId = "dfb";
private static final String apiKey = "876d6ff";
private static final String apiSecret = "Ym";
//win
// String ffmpegPath = "I:\\ffmpeg-7.1-full_build\\bin\\ffmpeg.exe";
//String ffmpegPath = "E:\\tool\\ffmpeg\\bin\\ffmpeg.exe";
//liunx
static String ffmpegPath = "/opt/ffmpeg/bin/ffmpeg";
static {
String envPath = System.getenv("FFMPEG_PATH");
if (envPath != null && !envPath.trim().isEmpty()) {
ffmpegPath = envPath;
}
}
/**
* 文本转语音
* @param text
* @return
* vcn 参数:
* x2_wanshu:讯飞万叔
* x4_xiuying:秀英
*/
public File tts(String text,String vcn){
UUID uuid = UUID.randomUUID();
String uuidString = uuid.toString().replace("-", "");
String tempDir = System.getProperty("java.io.tmpdir");
if (StringUtils.isNotBlank(tempDir) && !tempDir.endsWith(File.separator)) {
tempDir += File.separator;
}
File tempFile = null;
try {
tempFile = File.createTempFile(uuidString, ".mp3", new File(tempDir));
tempFile.deleteOnExit();//退出时删除
} catch (IOException e) {
throw new RuntimeException(e);
}
TtsClient ttsClient = null;
try {
ttsClient = new TtsClient.Builder()
.signature(appId, apiKey, apiSecret)
.vcn(vcn)
.build();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (SignatureException e) {
throw new RuntimeException(e);
}
CountDownLatch latch = new CountDownLatch(1);
try {
ttsClient.send(text, new AbstractTtsWebSocketListener(tempFile) {
@Override
public void onSuccess(byte[] bytes) {
latch.countDown(); // 任务完成,减少计数器
}
@Override
public void onFail(WebSocket webSocket, Throwable throwable, Response response) {
System.out.println(throwable.getMessage());
latch.countDown(); // 任务完成,减少计数器
}
@Override
public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
System.out.println(ttsResponse.toString());
latch.countDown(); // 任务完成,减少计数器
}
});
}catch (Exception e){
System.out.println(e.getMessage());
System.out.println("错误码查询链接:https://www.xfyun.cn/document/error-code");
}
try {
if (!latch.await(60, TimeUnit.SECONDS)) {
System.err.println("TTS request timed out.");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return tempFile;
}
//https://github.com/iFLYTEK-OP/websdk-java-demo/blob/main/src/main/java/cn/xfyun/demo/speech/IatClientApp.java
public String iat(File mp3) {
UUID uuid = UUID.randomUUID();
String uuidString = uuid.toString().replace("-", "");
//输出文件绝对路径
String tempDir = System.getProperty("java.io.tmpdir");
if (StringUtils.isNotBlank(tempDir) && !tempDir.endsWith(File.separator)) {
tempDir += File.separator;
}
String out=tempDir+uuidString+File.separator+mp3.getName()+".pcm";
File file1 = new File(tempDir + uuidString);
if(!file1.exists()){
file1.mkdir();
}
try {
Process process = Runtime.getRuntime().exec(ffmpegPath+" -i "+mp3.getAbsolutePath()+" -ar 16000 -ac 1 -acodec pcm_s16le -f s16le "+out);
// 等待命令执行完成
int exitCode = process.waitFor(); // 阻塞当前线程,直到命令执行完成
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
IatClient iatClient = new IatClient.Builder()
.signature(appId, apiKey, apiSecret)
.build();
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS");
Date dateBegin = new Date();
File file = new File(out);
StringBuffer finalResult = new StringBuffer();
CountDownLatch latch = new CountDownLatch(1);
try {
iatClient.send(file, new AbstractIatWebSocketListener() {
@Override
public void onSuccess(WebSocket webSocket, IatResponse iatResponse) {
if (iatResponse.getCode() != 0) {
System.out.println("code=>" + iatResponse.getCode() + " error=>" + iatResponse.getMessage() + " sid=" + iatResponse.getSid());
System.out.println("错误码查询链接:https://www.xfyun.cn/document/error-code");
latch.countDown(); // 任务完成,减少计数器
}
if (iatResponse.getData() != null) {
if (iatResponse.getData().getResult() != null) {
IatResult.Ws[] wss = iatResponse.getData().getResult().getWs();
String text = "";
for (IatResult.Ws ws : wss) {
IatResult.Cw[] cws = ws.getCw();
for (IatResult.Cw cw : cws) {
text += cw.getW();
}
}
try {
finalResult.append(text);
System.out.println("中间识别结果 ==》" + text);
} catch (Exception e) {
e.printStackTrace();
}
}
if (iatResponse.getData().getStatus() == 2) {
// resp.data.status ==2 说明数据全部返回完毕,可以关闭连接,释放资源
System.out.println("session end ");
Date dateEnd = new Date();
System.out.println(sdf.format(dateBegin) + "开始");
System.out.println(sdf.format(dateEnd) + "结束");
System.out.println("耗时:" + (dateEnd.getTime() - dateBegin.getTime()) + "ms");
System.out.println("最终识别结果 ==》" + finalResult.toString());
System.out.println("本次识别sid ==》" + iatResponse.getSid());
iatClient.closeWebsocket();
latch.countDown(); // 任务完成,减少计数器
} else {
// 根据返回的数据处理
//System.out.println(StringUtils.gson.toJson(iatResponse));
}
}
}
@Override
public void onFail(WebSocket webSocket, Throwable t, Response response) {
latch.countDown(); // 任务完成,减少计数器
}
});
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (SignatureException e) {
throw new RuntimeException(e);
}
try {
if (!latch.await(60, TimeUnit.SECONDS)) {
System.err.println("TTS request timed out.");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return finalResult.toString();
}
/**
* 根据url获取音频文件的文字
* @param mp3Url
* @return
*/
public String iat(String mp3Url) {
UUID uuid = UUID.randomUUID();
String uuidString = uuid.toString().replace("-", "");
//输出文件绝对路径
String tempDir = System.getProperty("java.io.tmpdir");
if (StringUtils.isNotBlank(tempDir) && !tempDir.endsWith(File.separator)) {
tempDir += File.separator;
}
String out=tempDir+uuidString+".pcm";
/* File file1 = new File(tempDir + uuidString);
if(!file1.exists()){
file1.mkdir();
}*/
try {
Process process = Runtime.getRuntime().exec(ffmpegPath+" -i "+mp3Url+" -ar 16000 -ac 1 -acodec pcm_s16le -f s16le "+out);
// 等待命令执行完成
int exitCode = process.waitFor(); // 阻塞当前线程,直到命令执行完成
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
IatClient iatClient = new IatClient.Builder()
.signature(appId, apiKey, apiSecret)
.build();
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS");
Date dateBegin = new Date();
File file = new File(out);
StringBuffer finalResult = new StringBuffer();
CountDownLatch latch = new CountDownLatch(1);
try {
iatClient.send(file, new AbstractIatWebSocketListener() {
@Override
public void onSuccess(WebSocket webSocket, IatResponse iatResponse) {
if (iatResponse.getCode() != 0) {
System.out.println("code=>" + iatResponse.getCode() + " error=>" + iatResponse.getMessage() + " sid=" + iatResponse.getSid());
System.out.println("错误码查询链接:https://www.xfyun.cn/document/error-code");
latch.countDown(); // 任务完成,减少计数器
}
if (iatResponse.getData() != null) {
if (iatResponse.getData().getResult() != null) {
IatResult.Ws[] wss = iatResponse.getData().getResult().getWs();
String text = "";
for (IatResult.Ws ws : wss) {
IatResult.Cw[] cws = ws.getCw();
for (IatResult.Cw cw : cws) {
text += cw.getW();
}
}
try {
finalResult.append(text);
System.out.println("中间识别结果 ==》" + text);
} catch (Exception e) {
e.printStackTrace();
}
}
if (iatResponse.getData().getStatus() == 2) {
// resp.data.status ==2 说明数据全部返回完毕,可以关闭连接,释放资源
System.out.println("session end ");
Date dateEnd = new Date();
System.out.println(sdf.format(dateBegin) + "开始");
System.out.println(sdf.format(dateEnd) + "结束");
System.out.println("耗时:" + (dateEnd.getTime() - dateBegin.getTime()) + "ms");
System.out.println("最终识别结果 ==》" + finalResult.toString());
System.out.println("本次识别sid ==》" + iatResponse.getSid());
iatClient.closeWebsocket();
latch.countDown(); // 任务完成,减少计数器
} else {
// 根据返回的数据处理
//System.out.println(StringUtils.gson.toJson(iatResponse));
}
}
}
@Override
public void onFail(WebSocket webSocket, Throwable t, Response response) {
latch.countDown(); // 任务完成,减少计数器
}
});
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (SignatureException e) {
throw new RuntimeException(e);
}
try {
if (!latch.await(60, TimeUnit.SECONDS)) {
System.err.println("TTS request timed out.");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return finalResult.toString();
}
/**
* 文件转base64
* @param file
* @return
*/
public String fileToBase64(File file) {
FileInputStream fileInputStream = null;
try {
// 打开文件输入流
fileInputStream = new FileInputStream(file);
// 读取文件内容到字节数组
byte[] fileContent = new byte[(int) file.length()];
fileInputStream.read(fileContent);
// 使用Base64编码器将字节数组转换为Base64字符串
return Base64.getEncoder().encodeToString(fileContent);
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
// 关闭文件输入流
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}