LaiTool V3.0.1-preview.6

This commit is contained in:
lq1405 2024-08-13 13:38:13 +08:00
parent e85b0a986d
commit 07f99708da
6 changed files with 57 additions and 24 deletions

1
.gitignore vendored
View File

@ -18,6 +18,7 @@ resources/scripts/virtual py
resources/scripts/_internal
resources/logger
resources/scripts/Temp
resources/scripts/db
*scripts/db*
resources/image/Temp*
resources/package/ffmpeg/w*

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "laitool",
"version": "3.0.1-preview.5",
"version": "3.0.1-preview.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "laitool",
"version": "3.0.1-preview.5",
"version": "3.0.1-preview.6",
"hasInstallScript": true,
"dependencies": {
"@alicloud/alimt20181012": "^1.2.0",

View File

@ -1,6 +1,6 @@
{
"name": "laitool",
"version": "3.0.1-preview.5",
"version": "3.0.1-preview.6",
"description": "An AI tool for image processing, video processing, and other functions.",
"main": "./out/main/index.js",
"author": "laitool.cn",

Binary file not shown.

View File

@ -232,40 +232,72 @@ export class TTS {
const parts = text.match(
/[^,。!?;:“”()《》,.!?;:"()<>$$$$\n]+[,。!?;:“”()《》,.!?;:"()<>$$$$\n]*/g
);
// 初始化 SRT 内容
let srtContent = "";
let index = 1;
// 函数用于去掉文本末尾的标点符号
const removeTrailingPunctuation = (text: string) => {
return text.replace(/[\s“”《》,.!?;:"()<>$$$$]+$/, "");
const removeTrailingPunctuation = (text) => {
return text.replace(/[\s“”《》,.!?;:"()\n]+$/, "");
};
// 配对时间轴和文案分段
// 函数用于将毫秒格式化为 SRT 时间格式
const formatTime = (ms) => {
const date = new Date(ms);
const hours = String(date.getUTCHours()).padStart(2, "0");
const minutes = String(date.getUTCMinutes()).padStart(2, "0");
const seconds = String(date.getUTCSeconds()).padStart(2, "0");
const milliseconds = String(date.getUTCMilliseconds()).padStart(3, "0");
return `${hours}:${minutes}:${seconds},${milliseconds}`;
};
// 合并文本和时间轴
let combinedPart = "";
let startTime = null;
let endTime = null;
let j = 0;
let count = 0;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
let startTime =
srtJson[i * 2]?.start || srtJson[srtJson.length - 1].start;
let endTime =
srtJson[i * 2 + 1]?.end || srtJson[srtJson.length - 1].end;
let partText = removeTrailingPunctuation(parts[i].trim());
// partText = partText.trim()
let tempText = partText.replaceAll(' ', '');
for (; j < srtJson.length; j++) {
let timePart = srtJson[j].part;
timePart = removeTrailingPunctuation(timePart.trim());
// 去掉文案末尾的标点符号
const cleanedPart = removeTrailingPunctuation(part.trim());
if (tempText.includes(timePart)) {
// 如果匹配到,合并文本片段并更新结束时间
combinedPart += timePart;
if (startTime === null) {
startTime = srtJson[j].start; // 设置开始时间
}
endTime = srtJson[j].end; // 更新结束时间
// 将时间格式化为 SRT 格式
const formatTime = (ms) => {
const date = new Date(ms);
const hours = String(date.getUTCHours()).padStart(2, "0");
const minutes = String(date.getUTCMinutes()).padStart(2, "0");
const seconds = String(date.getUTCSeconds()).padStart(2, "0");
const milliseconds = String(date.getUTCMilliseconds()).padStart(3, "0");
return `${hours}:${minutes}:${seconds},${milliseconds}`;
};
// 如果下一个文本片段开始匹配下一个标点,则跳出内层循环
if (combinedPart == tempText) {
j++; // 准备下一次对比从下一个时间片段开始
break;
}
} else {
j++; // 准备下一次对比从下一个时间片段开始
break;
}
count++;
console.log(count);
count = 0;
}
// 生成 SRT 片段
// 生成SRT段
srtContent += `${index}\n${formatTime(startTime)} --> ${formatTime(
endTime
)}\n${cleanedPart}\n\n`;
)}\n${partText}\n\n`;
// 重置变量
combinedPart = "";
startTime = null;
endTime = null;
index++;
}