Commit a9dba503 by 苗权威

8.13

parent 7542ddaf
......@@ -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); //错误输出流合并到标准输出流中
......
......@@ -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<String> filePathList, String targetFilepath)
public static void combineMp3File(Collection<String> filePathList, String targetFilepath, Boolean xiaoai)
throws IOException {
// try {
//// List<String> 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<String> 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<String> 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<String> filePathList, String targetFilepath) {
long startMark = System.currentTimeMillis();
String strMark = RandomStringUtils.random(4, true, true);
int i = 0, ln = filePathList.size()-1;
String tempPath;
List<Future> resultList = new ArrayList<>(ln+1);
List<String> 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<String> 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 <T>
......
......@@ -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";
}
......
......@@ -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;
......
......@@ -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文件失败");
}
......
......@@ -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文件失败");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment