TTS 返回语音优化

This commit is contained in:
2025-10-19 15:44:04 +08:00
parent a2e7e78bee
commit 9e47d0787e
2 changed files with 26 additions and 3 deletions

View File

@@ -136,7 +136,7 @@ public class ChatWebSocketHandler {
log.info("2、开始进行AI回答时间:{}",System.currentTimeMillis()/1000);
//把结果文字转成语音文件
//生成文件
// 生成唯一文件名
//生成唯一文件名
String resultFileName = clientId + "_" + System.currentTimeMillis() + ".opus";
String resultPathUrl = RuoYiConfig.getProfile() + VOICE_STORAGE_RESULT_DIR + resultFileName;
ElevenLabsClient elevenLabsClient = SpringUtils.getBean(ElevenLabsClient.class);
@@ -144,8 +144,10 @@ public class ChatWebSocketHandler {
log.info("3、开始进行AI回答时间:{}",System.currentTimeMillis()/1000);
//持续返回数据流给客户端
try {
String resultOutPathUrl = RuoYiConfig.getProfile() + VOICE_STORAGE_RESULT_DIR + "123_"+resultFileName;
removeSilence(resultPathUrl, resultOutPathUrl);
//文件转换成文件流
ByteBuffer outByteBuffer = convertFileToByteBuffer(resultPathUrl);
ByteBuffer outByteBuffer = convertFileToByteBuffer(resultOutPathUrl);
//发送文件流数据
session.getBasicRemote().sendBinary(outByteBuffer);
// 发送响应确认
@@ -404,5 +406,26 @@ public class ChatWebSocketHandler {
}
}
public void removeSilence(String inputFile, String outputFile) {
try {
// FFmpeg 命令,去除音频后面的静音部分
String command = "ffmpeg -i " + inputFile +
" -af silenceremove=start_periods=1:start_duration=0.2:start_threshold=-40dB " +
"-af silenceremove=end_periods=1:end_duration=0.2:end_threshold=-40dB " +
outputFile;
// 执行命令
Process process = Runtime.getRuntime().exec(command);
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("静音部分已去除");
} else {
System.out.println("音频处理失败");
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}