From a9dba503f22dd3cdb1ac9bc6e74128d8bfb52ace Mon Sep 17 00:00:00 2001
From: 苗权威 <1160292058@qq.com>
Date: Thu, 13 Aug 2020 21:22:52 +0800
Subject: [PATCH] 8.13
---
.idea/workspace.xml | 391 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
tts_service/src/main/java/ai/qiwu/com/tts_service/common/CallPython.java | 36 ++++++++++++++++++++++++++++++++++++
tts_service/src/main/java/ai/qiwu/com/tts_service/common/TTSUtils.java | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
tts_service/src/main/java/ai/qiwu/com/tts_service/controller/DeployController.java | 3 ++-
tts_service/src/main/java/ai/qiwu/com/tts_service/pojo/deploy/MoveBotAudio.java | 2 ++
tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTService.java | 10 +++++-----
tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTServiceEdition2.java | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------------------
tts_service/src/main/java/ai/qiwu/com/tts_service/service/TtsLsService.java | 2 +-
8 files changed, 412 insertions(+), 259 deletions(-)
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index cd0e773..34ab04b 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,14 +5,14 @@
-
-
+
-
+
+
@@ -50,103 +50,76 @@
-
+
-
-
-
-
-
+
+
-
+
-
-
+
+
-
+
-
-
-
-
-
+
+
-
+
-
-
+
+
-
+
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -166,11 +139,6 @@
- notVariableTextPath
- THIRD_PARTY_TTS
- usDir
- tone
- combineMp3Text2FilePath
variablePathInUsDir
text
tts_ls
@@ -194,8 +162,13 @@
asyncDelete
checkZipFile
referenceName
- genRecordWithChange
zipDir
+ add
+ asynResultList
+ stringRedisTemplate
+ genRecordWithChange
+ stringRedisTemplate.opsForValue()
+ tts
!CollectionUtils.isEmpty
@@ -211,7 +184,6 @@
-
@@ -248,16 +220,17 @@
-
-
-
-
+
+
+
+
+
@@ -290,8 +263,8 @@
-
-
+
+
@@ -303,6 +276,7 @@
+
@@ -353,21 +327,31 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
@@ -503,13 +487,13 @@
+
-
@@ -700,28 +684,31 @@
-
+
+
+
-
+
-
+
+
-
+
-
-
+
+
@@ -744,7 +731,7 @@
-
+
@@ -796,80 +783,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -910,13 +823,6 @@
-
-
-
-
-
-
-
@@ -981,13 +887,6 @@
-
-
-
-
-
-
-
@@ -1049,16 +948,6 @@
-
-
-
-
-
-
-
-
-
-
@@ -1101,68 +990,103 @@
-
+
-
-
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
-
-
-
+
+
-
+
-
-
+
+
+
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
@@ -1171,20 +1095,79 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/common/CallPython.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/common/CallPython.java
index 1e72fc5..aef76fd 100755
--- a/tts_service/src/main/java/ai/qiwu/com/tts_service/common/CallPython.java
+++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/common/CallPython.java
@@ -131,6 +131,42 @@ public class CallPython {
return "1";
}
+ static String soxCut(String arg) {
+ ProcessBuilder processBuilder = new ProcessBuilder(arg.split(" "));
+ processBuilder.redirectErrorStream(true); //错误输出流合并到标准输出流中
+ StringBuilder builder = new StringBuilder();
+ Process process = null;
+ try {
+ process = processBuilder.start();
+ process.waitFor();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+ String line;
+ if (process.exitValue() != 0) {
+ while ((line = reader.readLine()) != null) {
+ builder.append(line);
+ }
+ if (builder.length() > 0) {
+ log.error("ffmpeg执行出错:{}",builder.toString());
+ return "0";
+ }
+ } else {
+ while ((line = reader.readLine()) != null) {
+// builder.append(line);
+ }
+// if (builder.length() != 0){
+// log.info("ffmpeg返回信息:{}", builder.toString());
+// }
+ }
+ //log.info(Thread.currentThread().getName()+"耗时:"+(System.currentTimeMillis()-startTime));
+ } catch (IOException | InterruptedException e) {
+ log.info("Error while running sox cut. {}",e.getMessage());
+ return "0";
+ } finally {
+ if (process != null) process.destroy();
+ }
+ return "1";
+ }
+
static String ffmpegExecute(String arg) {
ProcessBuilder processBuilder = new ProcessBuilder(arg.split(" "));
processBuilder.redirectErrorStream(true); //错误输出流合并到标准输出流中
diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/common/TTSUtils.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/common/TTSUtils.java
index b726f24..98d0c61 100755
--- a/tts_service/src/main/java/ai/qiwu/com/tts_service/common/TTSUtils.java
+++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/common/TTSUtils.java
@@ -6,8 +6,10 @@ import ai.qiwu.com.tts_service.service.Initialize;
import ai.qiwu.com.tts_service.service.TTService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
@@ -22,12 +24,18 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.text.Normalizer;
import java.util.*;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
+import static ai.qiwu.com.tts_service.service.TTService.POOL;
+
/**
* 工具类
* @author 苗权威
@@ -552,7 +560,7 @@ public class TTSUtils {
* @param targetFilepath 合并文件地址
* @throws IOException
*/
- public static void combineMp3File(Collection filePathList, String targetFilepath)
+ public static void combineMp3File(Collection filePathList, String targetFilepath, Boolean xiaoai)
throws IOException {
// try {
//// List filePaths = SampleRateConverter.equalSampleRate(filePathList);
@@ -572,13 +580,17 @@ public class TTSUtils {
String[] arr = filePathList.toArray(new String[]{});
Files.copy(Paths.get(arr[0]), Paths.get(targetFilepath), StandardCopyOption.REPLACE_EXISTING);
} else {
- List str = filePathList.stream().map(s -> "-i "+s).collect(Collectors.toList());
- StringBuilder builder = new StringBuilder("ffmpeg ").append(StringUtils.join(str, " ")).append(" -filter_complex ");
- for (int i = 0; i < filePathList.size(); i++) {
- builder.append("[").append(i).append(":a:0]");
+ if (xiaoai) {
+ List str = filePathList.stream().map(s -> "-i "+s).collect(Collectors.toList());
+ StringBuilder builder = new StringBuilder("ffmpeg ").append(StringUtils.join(str, " ")).append(" -filter_complex ");
+ for (int i = 0; i < filePathList.size(); i++) {
+ builder.append("[").append(i).append(":a:0]");
+ }
+ builder.append("concat=n=").append(filePathList.size()).append(":v=0:a=1[a] -map [a] ").append(targetFilepath);
+ CallPython.ffmpegExecute(builder.toString());
+ } else {
+ buildSoxCutAndCombine(filePathList, targetFilepath);
}
- builder.append("concat=n=").append(filePathList.size()).append(":v=0:a=1[a] -map [a] ").append(targetFilepath);
- CallPython.ffmpegExecute(builder.toString());
}
// filePathList.add(targetFilepath);
// CallPython.execute("sox "+ StringUtils.join(filePathList, " "));
@@ -613,6 +625,61 @@ public class TTSUtils {
// });
}
+ private static void buildSoxCutAndCombine(Collection filePathList, String targetFilepath) {
+ long startMark = System.currentTimeMillis();
+ String strMark = RandomStringUtils.random(4, true, true);
+ int i = 0, ln = filePathList.size()-1;
+ String tempPath;
+ List resultList = new ArrayList<>(ln+1);
+ List cutedPaths = new ArrayList<>(resultList.size());
+ for (String sourcePath: filePathList) {
+ StringBuilder soxCutCommand = new StringBuilder("sox ");
+ if (i == 0) {
+ tempPath = sourcePath.substring(0, sourcePath.lastIndexOf("."))+startMark+strMark+"l.mp3";
+ soxCutCommand.append(sourcePath).append(" ").append(tempPath).append(" reverse silence 1 0.1 1% reverse");
+ }else if (i == ln) {
+ tempPath = sourcePath.substring(0, sourcePath.lastIndexOf("."))+startMark+strMark+"f.mp3";
+ soxCutCommand.append(sourcePath).append(" ").append(tempPath).append(" silence 1 0.1 1%");
+ } else {
+ tempPath = sourcePath.substring(0, sourcePath.lastIndexOf("."))+startMark+strMark+"a.mp3";
+ soxCutCommand.append(sourcePath).append(" ").append(tempPath).append(" silence 1 0.1 1% reverse silence 1 0.1 1% reverse");
+ }
+ i++;
+ if (!cutedPaths.contains(tempPath)) {
+ if (!doesFileExist(tempPath)) {
+ resultList.add(POOL.submit(() -> {
+ CallPython.soxCut(soxCutCommand.toString());
+ }));
+ }
+ }
+ cutedPaths.add(tempPath);
+ }
+ for (Future future: resultList) {
+ try {
+ future.get(10, TimeUnit.SECONDS);
+ } catch (InterruptedException | ExecutionException e) {
+ log.error("buildSoxCutAndCombine ,分段任务执行出错:"+e);
+ return;
+ } catch (TimeoutException e2) {
+ log.error("buildSoxCutAndCombine ,等待10s分段任务仍未执行结束");
+ return;
+ }
+ }
+ long temp;
+ log.warn("sox 切除静音耗时:"+((temp=System.currentTimeMillis())-startMark));
+ List str = cutedPaths.stream().map(s -> "-i "+s).collect(Collectors.toList());
+ StringBuilder builder = new StringBuilder("ffmpeg ").append(StringUtils.join(str, " ")).append(" -filter_complex ");
+ for (int k = 0; k < filePathList.size(); k++) {
+ builder.append("[").append(k).append(":a:0]");
+ }
+ builder.append("concat=n=").append(filePathList.size()).append(":v=0:a=1[a] -map [a] ").append(targetFilepath);
+ CallPython.ffmpegExecute(builder.toString());
+ log.warn("ffmpeg 拼接耗时:"+(System.currentTimeMillis()-temp));
+ POOL.submit(() -> {
+ for (String delPath: cutedPaths) new File(delPath).delete();
+ });
+ }
+
/**
* 节点
* @param
diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/controller/DeployController.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/controller/DeployController.java
index 05de4de..282189b 100644
--- a/tts_service/src/main/java/ai/qiwu/com/tts_service/controller/DeployController.java
+++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/controller/DeployController.java
@@ -367,9 +367,10 @@ public class DeployController {
try {
FileUtil.deleteFile(new File(Initialize.AUDIO_DIR + "/" + moveBotAudio.getDestBotAccount()));
moveAudio(moveBotAudio.getSrcBotAccount(), moveBotAudio.getDestBotAccount());
+ log.warn("导入移动音频文件成功:"+moveBotAudio);
return "success";
} catch (Exception e) {
- log.error("导入人工语音项目移动音频出错:" + e);
+ log.error("导入人工语音项目移动音频出错:{}->{}",e, moveBotAudio);
}
return "fail";
}
diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/pojo/deploy/MoveBotAudio.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/pojo/deploy/MoveBotAudio.java
index 521fde1..318ff8c 100644
--- a/tts_service/src/main/java/ai/qiwu/com/tts_service/pojo/deploy/MoveBotAudio.java
+++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/pojo/deploy/MoveBotAudio.java
@@ -2,6 +2,7 @@ package ai.qiwu.com.tts_service.pojo.deploy;
import lombok.Getter;
import lombok.Setter;
+import lombok.ToString;
/**
* painter
@@ -9,6 +10,7 @@ import lombok.Setter;
*/
@Getter
@Setter
+@ToString
public class MoveBotAudio {
private String srcBotAccount;
private String destBotAccount;
diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTService.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTService.java
index 0f466a7..bd17337 100755
--- a/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTService.java
+++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTService.java
@@ -51,7 +51,7 @@ public class TTService {
* 线程池,核心线程数cpu数目的两倍,非核心线程存活时间为10s
*/
public static final ThreadPoolExecutor POOL = new ThreadPoolExecutor(CORE_THREAD_NUM*2,
- 1500, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<>(CORE_THREAD_NUM),
+ 3000, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<>(CORE_THREAD_NUM),
new ThreadPoolExecutor.CallerRunsPolicy());
// private static final ThreadPoolExecutor CACHE_POOL = new ThreadPoolExecutor(10, 20,
@@ -485,7 +485,7 @@ public class TTService {
if (!TTSUtils.doesFileExist(tmp_audio_name)) {
TTSUtils.makedir(folder);
// long startTime = System.currentTimeMillis();
- TTSUtils.combineMp3File(file_ls, tmp_audio_name);
+ TTSUtils.combineMp3File(file_ls, tmp_audio_name, true);
// log.info("ffmpeg合并耗时:"+(System.currentTimeMillis()-startTime));
//CallPython.combine(file_ls, tmp_audio_name);
}
@@ -1138,7 +1138,7 @@ public class TTService {
// int lastSep = personToneFilePath.lastIndexOf(File.separator);
// StringBuilder combineRecordPath = new StringBuilder(
// personToneFilePath.substring(0, personToneFilePath.lastIndexOf(File.separator)));
- TTSUtils.combineMp3File(combineMp3Text2FilePath.values(), personToneFilePath);
+ TTSUtils.combineMp3File(combineMp3Text2FilePath.values(), personToneFilePath, false);
if (!new File(personToneFilePath).exists()) {
log.error("分段合并音频,未找到合并后的文件");
return false;
@@ -1243,7 +1243,7 @@ public class TTService {
}
// 合并所有分段mp3文件
try {
- TTSUtils.combineMp3File(combineMp3FilePathList, personToneFilePath);
+ TTSUtils.combineMp3File(combineMp3FilePathList, personToneFilePath, false);
} catch (Exception e) {
log.error("genRecordWithChange:合成音频失败:{}",e.toString());
return false;
@@ -2540,7 +2540,7 @@ public class TTService {
if ((addSuccess=Boolean.TRUE.equals(stringRedisTemplate.opsForValue()
.setIfAbsent(tmp_audio_name, "1", 20, TimeUnit.SECONDS)))) {
Files.createDirectories(Paths.get(tmp_audio_name.substring(0, tmp_audio_name.lastIndexOf(File.separator))));
- TTSUtils.combineMp3File(file_ls, tmp_audio_name);
+ TTSUtils.combineMp3File(file_ls, tmp_audio_name, true);
if (!TTSUtils.doesFileExist(tmp_audio_name)) {
log.error("小爱平台的请求,聚合mp3文件失败");
}
diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTServiceEdition2.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTServiceEdition2.java
index da7ee63..a0cd3c3 100644
--- a/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTServiceEdition2.java
+++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TTServiceEdition2.java
@@ -115,7 +115,7 @@ public class TTServiceEdition2 {
}
TTSResponse response = null;
// _type为json,则构建请求get_audio接口的信息
- boolean shouldGenerateMp3 = !TTSUtils.doesFileExist(filePath) || requestMethod.equals("PUT");
+ boolean shouldGenerateMp3 = !TTSUtils.doesFileExist(filePath) || "PUT".equals(requestMethod);
if ("json".equals(ttsRequest.get_type())) {
String audioGetUrl;
if (tmpRequest) {
@@ -266,7 +266,7 @@ public class TTServiceEdition2 {
// botname不能为空
String botName = request.getBotname();
if (botName == null) botName = "中控";
- else if (botName.trim().equals("")) {
+ else if ("".equals(botName.trim())) {
TTServiceUtil.buildTTS_lsResponse(response, new TTSResponse(HttpServletResponse.SC_BAD_REQUEST, "no botname"), null, HttpServletResponse.SC_BAD_REQUEST);
return;
}
@@ -437,7 +437,7 @@ public class TTServiceEdition2 {
// 对于put请求则直接讲wav转为mp3,其他则先判断mp3文件是否存在,
// 不存在对于线下请求则去url地址下载该mp3文件,线上的则直接转wav
boolean fileExist = TTSUtils.doesFileExist(filePath);
- if (requestMethod.equals("PUT")) {
+ if ("PUT".equals(requestMethod)) {
if (fileExist) {
new File(filePath).delete();
}
@@ -534,6 +534,7 @@ public class TTServiceEdition2 {
// 遍历所有非变量,若其mp3文件在人工目录下不存在则返回false回到generalMp3方法,整句合成
int i = 0;
List> asynResultList = null;
+ List conditionPathList = null; // 竞争音频路径列表
for (String notVariableText: notVariableTextArray) {
if (StringUtils.isBlank(notVariableText)) {
++i;
@@ -558,21 +559,38 @@ public class TTServiceEdition2 {
if ("Liehu1".equals(data.getPerson())) return false;
String thirdPath = new StringBuilder(usDir).append(File.separator).append(filename).toString();
if (!TTSUtils.doesFileExist(thirdPath)) {
- if (notVariableTextArray.length == 1) {
- GeneralMP3Data variableData = new GeneralMP3Data(data.getServer(), notVariableText,data.getPerson(),
- data.getTone(), data.getBotname(), data.getCache(),data.getVolume(),data.getSpeed(),data.getPitch());
- ResponseCodeWithData response = generalMP3(url, variableData, thirdPath, requestMethod, getOnline);
- if (response == null || response.getCode() != 200) return false;
+ if (Boolean.TRUE.equals(stringRedisTemplate.opsForValue().setIfAbsent(thirdPath, "1", 30, TimeUnit.SECONDS))) {
+ if (!TTSUtils.doesFileExist(thirdPath)) {
+ if (notVariableTextArray.length == 1) {
+ try {
+ GeneralMP3Data variableData = new GeneralMP3Data(data.getServer(), notVariableText,data.getPerson(),
+ data.getTone(), data.getBotname(), data.getCache(),data.getVolume(),data.getSpeed(),data.getPitch());
+ ResponseCodeWithData response = generalMP3(url, variableData, thirdPath, requestMethod, getOnline);
+ if (response == null || response.getCode() != 200) return false;
+ } finally {
+ stringRedisTemplate.delete(thirdPath);
+ }
+ } else {
+ if (asynResultList == null) asynResultList = new ArrayList<>(notVariableTextArray.length);
+ Future result = POOL.submit(() -> {
+ try {
+ GeneralMP3Data variableData = new GeneralMP3Data(data.getServer(), notVariableText,data.getPerson(),
+ data.getTone(), data.getBotname(), data.getCache(),data.getVolume(),data.getSpeed(),data.getPitch());
+ ResponseCodeWithData response = generalMP3(url, variableData, thirdPath, requestMethod, getOnline);
+ if (response == null || response.getCode() != 200) return "0";
+ return "1";
+ } finally {
+ stringRedisTemplate.delete(thirdPath);
+ }
+ });
+ asynResultList.add(result);
+ }
+ } else {
+ stringRedisTemplate.delete(thirdPath);
+ }
} else {
- if (asynResultList == null) asynResultList = new ArrayList<>(notVariableTextArray.length);
- Future result = POOL.submit(() -> {
- GeneralMP3Data variableData = new GeneralMP3Data(data.getServer(), notVariableText,data.getPerson(),
- data.getTone(), data.getBotname(), data.getCache(),data.getVolume(),data.getSpeed(),data.getPitch());
- ResponseCodeWithData response = generalMP3(url, variableData, thirdPath, requestMethod, getOnline);
- if (response == null || response.getCode() != 200) return "0";
- return "1";
- });
- asynResultList.add(result);
+ if (conditionPathList == null) conditionPathList = new ArrayList<>(notVariableTextArray.length);
+ conditionPathList.add(thirdPath);
}
}
notVariableTextPath = thirdPath;
@@ -591,22 +609,39 @@ public class TTServiceEdition2 {
if (!TTSUtils.doesFileExist(variableTextPath)) {
String variablePathInUsDir = new StringBuilder(usDir).append(File.separator).append(variableTextFilename).toString();
if (!TTSUtils.doesFileExist(variablePathInUsDir)) {
- if (variableTextList.size() == 1) {
- GeneralMP3Data variableData = new GeneralMP3Data(data.getServer(), variableText,data.getPerson(),
- data.getTone(), data.getBotname(), data.getCache(),data.getVolume(),data.getSpeed(),data.getPitch());
- ResponseCodeWithData response = generalMP3(url, variableData, variablePathInUsDir, requestMethod, getOnline);
- if (response == null || response.getCode() != 200) return false;
- } else {
- if (asynResultList == null) asynResultList = new ArrayList<>(variableTextList.size());
- Future result = POOL.submit(() -> {
- GeneralMP3Data variableData = new GeneralMP3Data(data.getServer(), variableText,data.getPerson(),
- data.getTone(), data.getBotname(), data.getCache(),data.getVolume(),data.getSpeed(),data.getPitch());
- ResponseCodeWithData response = generalMP3(url, variableData, variablePathInUsDir, requestMethod, getOnline);
- if (response == null || response.getCode() != 200) return "0";
- return "1";
- });
- asynResultList.add(result);
+ if (Boolean.TRUE.equals(stringRedisTemplate.opsForValue().setIfAbsent(variablePathInUsDir, "1", 30, TimeUnit.SECONDS))) {
+ if (!TTSUtils.doesFileExist(variablePathInUsDir)) {
+ if (variableTextList.size() == 1) {
+ try {
+ GeneralMP3Data variableData = new GeneralMP3Data(data.getServer(), variableText,data.getPerson(),
+ data.getTone(), data.getBotname(), data.getCache(),data.getVolume(),data.getSpeed(),data.getPitch());
+ ResponseCodeWithData response = generalMP3(url, variableData, variablePathInUsDir, requestMethod, getOnline);
+ if (response == null || response.getCode() != 200) return false;
+ } finally {
+ stringRedisTemplate.delete(variablePathInUsDir);
+ }
+ } else {
+ if (asynResultList == null) asynResultList = new ArrayList<>(variableTextList.size());
+ Future result = POOL.submit(() -> {
+ try {
+ GeneralMP3Data variableData = new GeneralMP3Data(data.getServer(), variableText,data.getPerson(),
+ data.getTone(), data.getBotname(), data.getCache(),data.getVolume(),data.getSpeed(),data.getPitch());
+ ResponseCodeWithData response = generalMP3(url, variableData, variablePathInUsDir, requestMethod, getOnline);
+ if (response == null || response.getCode() != 200) return "0";
+ return "1";
+ } finally {
+ stringRedisTemplate.delete(variablePathInUsDir);
+ }
+ });
+ asynResultList.add(result);
+ }
+ } else {
+ stringRedisTemplate.delete(variablePathInUsDir);
+ }
}
+ } else {
+ if (conditionPathList == null) conditionPathList = new ArrayList<>(variableTextList.size());
+ conditionPathList.add(variablePathInUsDir);
}
variableTextPath = variablePathInUsDir;
}
@@ -626,8 +661,24 @@ public class TTServiceEdition2 {
return false;
}
}
+ if (conditionPathList != null) {
+ for (String conditionPath: conditionPathList) {
+ int co = 0;
+ while (Boolean.TRUE.equals(stringRedisTemplate.hasKey(conditionPath)) && co++ < 10) {
+ try {
+ Thread.sleep(500);
+ } catch (Exception e) {
+ // do nothing
+ }
+ }
+ if (co == 11 && Boolean.TRUE.equals(stringRedisTemplate.hasKey(conditionPath))) {
+ log.error("genRecordWithChange 分段遇到竞争等待5s对方仍未结束,"+conditionPath);
+ return false;
+ }
+ }
+ }
// long start = System.currentTimeMillis();
- TTSUtils.combineMp3File(combineMp3Text2FilePath.values(), personToneFilePath);
+ TTSUtils.combineMp3File(combineMp3Text2FilePath.values(), personToneFilePath, false);
// log.warn("拼接耗时:{}->{}",personToneFilePath,(System.currentTimeMillis()-start));
if (!new File(personToneFilePath).exists()) {
log.error("分段合并音频,未找到合并后的文件");
@@ -736,19 +787,32 @@ public class TTServiceEdition2 {
if (!TTSUtils.doesFileExist(tmp_audio_name)) {
if (Boolean.TRUE.equals(stringRedisTemplate.opsForValue()
.setIfAbsent(tmp_audio_name, "1", 20, TimeUnit.SECONDS))) {
- if (!TTSUtils.doesFileExist(tmp_audio_name)) {
- try {
+ try {
+ if (!TTSUtils.doesFileExist(tmp_audio_name)) {
Files.createDirectories(Paths.get(tmp_audio_name).getParent());
- TTSUtils.combineMp3File(file_ls, tmp_audio_name);
+ TTSUtils.combineMp3File(file_ls, tmp_audio_name, true);
if (!TTSUtils.doesFileExist(tmp_audio_name)) {
log.error("小爱平台的请求,合并mp3文件失败:"+file_ls);
}
- } catch (Exception e) {
- log.error("ffmpeg 合并音频出错:{},file_ls:{}",e,file_ls);
- } finally {
- stringRedisTemplate.delete(tmp_audio_name);
}
+ } catch (Exception e) {
+ log.error("ffmpeg 合并音频出错:{},file_ls:{}",e,file_ls);
+ } finally {
+ stringRedisTemplate.delete(tmp_audio_name);
}
+// if (!TTSUtils.doesFileExist(tmp_audio_name)) {
+// try {
+// Files.createDirectories(Paths.get(tmp_audio_name).getParent());
+// TTSUtils.combineMp3File(file_ls, tmp_audio_name);
+// if (!TTSUtils.doesFileExist(tmp_audio_name)) {
+// log.error("小爱平台的请求,合并mp3文件失败:"+file_ls);
+// }
+// } catch (Exception e) {
+// log.error("ffmpeg 合并音频出错:{},file_ls:{}",e,file_ls);
+// } finally {
+// stringRedisTemplate.delete(tmp_audio_name);
+// }
+// }
} else {
log.warn("tts_ls xiaoai 合成竞争->"+tmp_audio_name+",时间:"+new Date(System.currentTimeMillis()));
}
@@ -1143,7 +1207,7 @@ public class TTServiceEdition2 {
.setIfAbsent(tmp_audio_name, "1", 20, TimeUnit.SECONDS)))) {
if (!TTSUtils.doesFileExist(tmp_audio_name)) {
Files.createDirectories(Paths.get(tmp_audio_name).getParent());
- TTSUtils.combineMp3File(file_ls, tmp_audio_name);
+ TTSUtils.combineMp3File(file_ls, tmp_audio_name, true);
if (!TTSUtils.doesFileExist(tmp_audio_name)) {
log.error("音箱小爱平台的请求,聚合mp3文件失败");
}
diff --git a/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TtsLsService.java b/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TtsLsService.java
index 2e07107..179ac4e 100644
--- a/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TtsLsService.java
+++ b/tts_service/src/main/java/ai/qiwu/com/tts_service/service/TtsLsService.java
@@ -225,7 +225,7 @@ public class TtsLsService {
.setIfAbsent(tmp_audio_name, "1", 20, TimeUnit.SECONDS))) {
try {
Files.createDirectories(Paths.get(tmp_audio_name).getParent());
- TTSUtils.combineMp3File(file_ls, tmp_audio_name);
+ TTSUtils.combineMp3File(file_ls, tmp_audio_name, true);
if (!TTSUtils.doesFileExist(tmp_audio_name)) {
log.error("小爱平台的请求,合并mp3文件失败");
}
--
libgit2 0.27.1