Comparing version 1.5.5 to 1.5.7
@@ -21,2 +21,3 @@ "use strict"; | ||
'...', | ||
'……', | ||
]; | ||
@@ -23,0 +24,0 @@ exports.PUNCTUATIONS = PUNCTUATIONS; |
@@ -24,3 +24,3 @@ "use strict"; | ||
let { videoScript = '', videoTerms, lastTime = 10, output = '', cacheDir = '', removeCache = true, subtitleMaxWidth = 9999, } = config; | ||
videoScript = (0, line_1.addPunctuationToParagraph)(videoScript); | ||
videoScript = (0, line_1.normalizeWhitespace)((0, line_1.addPunctuationToParagraph)(videoScript)); | ||
progress(5); | ||
@@ -27,0 +27,0 @@ fs_extra_1.default.ensureDir(path_1.default.dirname(output)); |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.generateSubtitle = void 0; | ||
const fs_extra_1 = __importDefault(require("fs-extra")); | ||
const lodash_1 = require("lodash"); | ||
@@ -12,74 +8,89 @@ const date_1 = require("./utils/date"); | ||
const line_1 = require("./utils/line"); | ||
const log_1 = require("./utils/log"); | ||
const file_1 = require("./utils/file"); | ||
const SIGN = '__x__'; | ||
const generateSubtitle = async (subMaker, text, subtitleFile, subtitleMaxWidth) => { | ||
const subItems = []; | ||
const formattedSubtitles = []; | ||
let scriptLines = (0, line_1.splitSubtitleByPunctuation)((0, char_1.normalizeWhitespace)(text)); | ||
let startTime = -1.0; | ||
let subIndex = 0; | ||
let endTime = -1.0; | ||
let scriptLinesIndex = 0; | ||
let subLine = ''; | ||
try { | ||
const scriptLinesc = segmentLinesBasedOnSubtitles(subMaker, scriptLines, subtitleMaxWidth); | ||
for (let i = 0; i < subMaker.offset.length; i++) { | ||
let [offset, sub] = [subMaker.offset[i], subMaker.subs[i]]; | ||
const [initialStartTime, endTime] = offset; | ||
if (startTime < 0) { | ||
startTime = initialStartTime; | ||
} | ||
subLine += (0, char_1.safeDecodeURIComponent)(sub); | ||
const subText = (0, line_1.extractMatchedLine)(scriptLinesc, subLine, subIndex); | ||
if (subText) { | ||
subIndex++; | ||
const line = (0, date_1.formatter)(subIndex, startTime, endTime, subText); | ||
subItems.push(line); | ||
startTime = -1.0; | ||
subLine = ''; | ||
} | ||
const scriptLinesc = resplitScriptLinesByMaxWidth(subMaker, scriptLines, subtitleMaxWidth); | ||
for (let i = 0; i < subMaker.offset.length; i++) { | ||
let [offset, sub] = [subMaker.offset[i], subMaker.subs[i]]; | ||
const [starTime, enTime] = offset; | ||
if (startTime < 0) | ||
startTime = starTime; | ||
endTime = enTime; | ||
subLine += (0, char_1.safeDecodeURIComponent)(sub); | ||
let lineText = ''; | ||
if (scriptLinesc.length > scriptLinesIndex) { | ||
const targetLine = scriptLinesc[scriptLinesIndex]; | ||
lineText = (0, line_1.getEqualedLine)(targetLine, subLine); | ||
} | ||
if (subItems.length > 2) { | ||
await fs_extra_1.default.writeFile(subtitleFile, subItems.join('\n') + '\n', { | ||
encoding: 'utf-8', | ||
}); | ||
log_1.Logger.log(`Subtitle synthesis successful. ${subItems.length}`); | ||
if (lineText) { | ||
scriptLinesIndex++; | ||
const subtitle = (0, date_1.formatter)(scriptLinesIndex, startTime, endTime, lineText); | ||
formattedSubtitles.push(subtitle); | ||
startTime = -1.0; | ||
endTime = -1.0; | ||
subLine = ''; | ||
} | ||
else { | ||
log_1.Logger.log(`Sorry, extractMatchedLine no vocabulary matched. ${subItems.length}`); | ||
await fs_extra_1.default.writeFile(subtitleFile, '', { encoding: 'utf-8' }); | ||
} | ||
if (subItems.length !== scriptLinesc.length) { | ||
log_1.Logger.warn(`subItems.length != scriptLines.length, subItems len: ${subItems.length}, scriptLines len: ${scriptLinesc.length}`); | ||
} | ||
} | ||
catch (e) { | ||
log_1.Logger.error(`failed, error: ${e}`); | ||
} | ||
await (0, file_1.writeSubtitles)(subtitleFile, formattedSubtitles, scriptLinesc.length); | ||
}; | ||
exports.generateSubtitle = generateSubtitle; | ||
const segmentLinesBasedOnSubtitles = (subMaker, scriptLines, subtitleMaxWidth) => { | ||
let subIndex = 0; | ||
let subLines = []; | ||
const resplitScriptLinesByMaxWidth = (subMaker, scriptLines, subtitleMaxWidth) => { | ||
let scriptLinesc = (0, lodash_1.clone)(scriptLines); | ||
let scriptLinesIndex = 0; | ||
let subLine = ''; | ||
let targetLine = ''; | ||
for (let i = 0; i < subMaker.offset.length; i++) { | ||
const sub = subMaker.subs[i]; | ||
subLines.push((0, char_1.safeDecodeURIComponent)(sub)); | ||
const matched = (0, line_1.isLineMatch)(scriptLinesc, subLines.join(''), subIndex); | ||
if (matched) { | ||
const lineStr = scriptLinesc[subIndex]; | ||
if (lineStr.length > subtitleMaxWidth) { | ||
const [index, len, subline] = (0, char_1.getSubarrayInfo)(subLines, subtitleMaxWidth); | ||
const line = lineStr.slice(0, len); | ||
if (index > 0 && (0, line_1.isLineEqual)(subline, line)) { | ||
scriptLinesc[subIndex] = (0, char_1.insertStringAt)(lineStr, len, SIGN); | ||
const newWord = (0, char_1.safeDecodeURIComponent)(sub); | ||
subLine += newWord; | ||
let lineText = ''; | ||
if (scriptLinesc.length > scriptLinesIndex) { | ||
targetLine = scriptLinesc[scriptLinesIndex]; | ||
lineText = (0, line_1.getEqualedLine)(targetLine, subLine); | ||
} | ||
if (targetLine.length > subtitleMaxWidth && | ||
subLine.length > subtitleMaxWidth) { | ||
scriptLinesc[scriptLinesIndex] = insertSIGNWord(targetLine, newWord); | ||
} | ||
if (lineText) { | ||
scriptLinesIndex++; | ||
targetLine = ''; | ||
subLine = ''; | ||
} | ||
} | ||
return splitArrayItemsBySign(scriptLinesc); | ||
}; | ||
function insertSIGNWord(targetLine, newWord) { | ||
if (targetLine.endsWith(newWord)) { | ||
const insertPosition = targetLine.length - newWord.length; | ||
return (0, char_1.insertStringAt)(targetLine, insertPosition, SIGN); | ||
} | ||
else { | ||
const cleanTargetLine = targetLine.replace(/[ _《》]/g, ''); | ||
const cleanNewWord = newWord.replace(/[ _《》]/g, ''); | ||
if (cleanTargetLine.endsWith(cleanNewWord)) { | ||
let startIndex = targetLine.length - newWord.length; | ||
while (startIndex > 0) { | ||
const subString = targetLine.slice(startIndex); | ||
if (subString.replace(/[ _《》]/g, '') === cleanNewWord) { | ||
break; | ||
} | ||
startIndex--; | ||
} | ||
subIndex++; | ||
subLines.length = 0; | ||
return (0, char_1.insertStringAt)(targetLine, startIndex, SIGN); | ||
} | ||
return targetLine; | ||
} | ||
return splitArrayItemsBySign(scriptLinesc); | ||
}; | ||
const splitArrayItemsBySign = (arr) => { | ||
return targetLine; | ||
} | ||
const splitArrayItemsBySign = (target) => { | ||
const result = []; | ||
for (let i = 0; i < arr.length; i++) { | ||
const item = arr[i]; | ||
for (let i = 0; i < target.length; i++) { | ||
const item = target[i]; | ||
if (typeof item === 'string' && item.includes(SIGN)) { | ||
@@ -86,0 +97,0 @@ const parts = item.split(SIGN); |
declare const removeSpecialCharacters: (str: string) => string; | ||
declare const normalizeWhitespace: (text: string) => string; | ||
declare const splitStringAtIndex: (arr: string[], index: number, x: number) => string[]; | ||
declare const getSubarrayInfo: (arr: string[], max: number) => [number, number, string]; | ||
declare const insertStringAt: (str: string, index: number, toInsert: string) => string; | ||
declare const safeDecodeURIComponent: (str: string) => string; | ||
export { insertStringAt, normalizeWhitespace, splitStringAtIndex, getSubarrayInfo, safeDecodeURIComponent, removeSpecialCharacters, }; | ||
declare const removeBlankLines: (text: string) => string; | ||
export { removeBlankLines, insertStringAt, normalizeWhitespace, splitStringAtIndex, safeDecodeURIComponent, removeSpecialCharacters, }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.removeSpecialCharacters = exports.safeDecodeURIComponent = exports.getSubarrayInfo = exports.splitStringAtIndex = exports.normalizeWhitespace = exports.insertStringAt = void 0; | ||
exports.removeSpecialCharacters = exports.safeDecodeURIComponent = exports.splitStringAtIndex = exports.normalizeWhitespace = exports.insertStringAt = exports.removeBlankLines = void 0; | ||
const removeSpecialCharacters = (str) => { | ||
@@ -34,19 +34,2 @@ const cnRegex = /[,。!?、;:“”‘’()《》【】〈〉「」『』【】〔〕〖〗〈〉《》「」『』【】〔〕【】﹝﹞()[]{}<>﹤﹥「」『』【】<>《》「」『』【】]/g; | ||
exports.splitStringAtIndex = splitStringAtIndex; | ||
const getSubarrayInfo = (arr, max) => { | ||
let len = 0; | ||
let index = 0; | ||
let str = ''; | ||
for (let i = 0; i < arr.length; i++) { | ||
if (len > max) { | ||
break; | ||
} | ||
else { | ||
len += arr[i].length; | ||
str += arr[i]; | ||
index = i; | ||
} | ||
} | ||
return [index, len, str]; | ||
}; | ||
exports.getSubarrayInfo = getSubarrayInfo; | ||
const insertStringAt = (str, index, toInsert) => { | ||
@@ -68,2 +51,9 @@ if (index > str.length || index < 0) { | ||
exports.safeDecodeURIComponent = safeDecodeURIComponent; | ||
const removeBlankLines = (text) => { | ||
return text | ||
.split('\n') | ||
.filter(line => line.trim() !== '') | ||
.join('\n'); | ||
}; | ||
exports.removeBlankLines = removeBlankLines; | ||
//# sourceMappingURL=char.js.map |
declare const mktimestamp: (time_unit: number) => string; | ||
declare const formatter: (idx: number, startTime: number, endTime: number, subText: string) => string; | ||
declare const formatter: (timeid: number, startTime: number, endTime: number, subText: string) => string; | ||
export { mktimestamp, formatter }; |
@@ -11,8 +11,8 @@ "use strict"; | ||
exports.mktimestamp = mktimestamp; | ||
const formatter = (idx, startTime, endTime, subText) => { | ||
const formatter = (timeid, startTime, endTime, subText) => { | ||
const startT = mktimestamp(startTime).replace('.', ','); | ||
const endT = mktimestamp(endTime).replace('.', ','); | ||
return `${idx}\n${startT} --> ${endT}\n${subText}\n`; | ||
return `${timeid}\n${startT} --> ${endT}\n${subText}\n`; | ||
}; | ||
exports.formatter = formatter; | ||
//# sourceMappingURL=date.js.map |
@@ -5,2 +5,3 @@ import { Stream } from 'stream'; | ||
declare const copyLocalFile: (targetPath: string, cacheDir: string) => Promise<string | null>; | ||
export { writeFileWithStream, copyLocalFile, isFilePath }; | ||
declare const writeSubtitles: (subtitleFile: string, formattedSubtitles: string[], scriptLinescLength: number) => Promise<void>; | ||
export { writeFileWithStream, writeSubtitles, copyLocalFile, isFilePath }; |
@@ -6,6 +6,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isFilePath = exports.copyLocalFile = exports.writeFileWithStream = void 0; | ||
exports.isFilePath = exports.copyLocalFile = exports.writeSubtitles = exports.writeFileWithStream = void 0; | ||
const md5_1 = __importDefault(require("md5")); | ||
const path_1 = __importDefault(require("path")); | ||
const fs_extra_1 = __importDefault(require("fs-extra")); | ||
const md5_1 = __importDefault(require("md5")); | ||
const log_1 = require("../utils/log"); | ||
@@ -43,2 +43,23 @@ const isFilePath = (url) => { | ||
exports.copyLocalFile = copyLocalFile; | ||
const writeSubtitles = async (subtitleFile, formattedSubtitles, scriptLinescLength) => { | ||
try { | ||
if (formattedSubtitles.length > 2) { | ||
await fs_extra_1.default.writeFile(subtitleFile, formattedSubtitles.join('\n') + '\n', { | ||
encoding: 'utf-8', | ||
}); | ||
log_1.Logger.log(`Subtitle synthesis successful. ${formattedSubtitles.length}`); | ||
} | ||
else { | ||
log_1.Logger.log(`Sorry, getEqualedLine no vocabulary equaled. ${formattedSubtitles.length}`); | ||
await fs_extra_1.default.writeFile(subtitleFile, '', { encoding: 'utf-8' }); | ||
} | ||
if (formattedSubtitles.length !== scriptLinescLength) { | ||
log_1.Logger.warn(`formattedSubtitles.length != scriptLines.length, formattedSubtitles len: ${formattedSubtitles.length}, scriptLines len: ${scriptLinescLength}`); | ||
} | ||
} | ||
catch (e) { | ||
log_1.Logger.error(`subtitle failed, error: ${e}`); | ||
} | ||
}; | ||
exports.writeSubtitles = writeSubtitles; | ||
//# sourceMappingURL=file.js.map |
@@ -1,7 +0,7 @@ | ||
declare const isLineMatch: (scriptLines: string[], subLine: string, subIndex: number) => boolean; | ||
declare const isLineEqual: (line: string, subLine: string) => boolean; | ||
declare const extractMatchedLine: (scriptLines: string[], subLine: string, subIndex: number) => string; | ||
declare const isLineEqual: (lineText: string, subLine: string) => boolean; | ||
declare const getEqualedLine: (targetLine: string, subLine: string) => string; | ||
declare const splitSubtitleByPunctuation: (s: string, maxWidth?: number) => string[]; | ||
declare const removeBlankLines: (text: string) => string; | ||
declare const resetScriptLinesContent: (scriptLines: string[], scriptLinesIndex: number, subLine: string) => void; | ||
declare const normalizeWhitespace: (text: string) => string; | ||
declare const addPunctuationToParagraph: (text: string) => string; | ||
export { isLineEqual, isLineMatch, extractMatchedLine, addPunctuationToParagraph, removeBlankLines, splitSubtitleByPunctuation, }; | ||
export { isLineEqual, getEqualedLine, normalizeWhitespace, resetScriptLinesContent, addPunctuationToParagraph, splitSubtitleByPunctuation, }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.splitSubtitleByPunctuation = exports.removeBlankLines = exports.addPunctuationToParagraph = exports.extractMatchedLine = exports.isLineMatch = exports.isLineEqual = void 0; | ||
exports.splitSubtitleByPunctuation = exports.addPunctuationToParagraph = exports.resetScriptLinesContent = exports.normalizeWhitespace = exports.getEqualedLine = exports.isLineEqual = void 0; | ||
const constant_1 = require("../config/constant"); | ||
const char_1 = require("./char"); | ||
const isLineMatch = (scriptLines, subLine, subIndex) => { | ||
if (scriptLines.length <= subIndex) { | ||
return false; | ||
} | ||
const line = scriptLines[subIndex]; | ||
return isLineEqual(line, subLine); | ||
}; | ||
exports.isLineMatch = isLineMatch; | ||
const isLineEqual = (line, subLine) => { | ||
if (subLine === line) { | ||
const isLineEqual = (lineText, subLine) => { | ||
if (subLine === lineText) { | ||
return true; | ||
} | ||
const cleanedSubLine = (0, char_1.removeSpecialCharacters)(subLine); | ||
const cleanedLine = (0, char_1.removeSpecialCharacters)(line); | ||
const cleanedLine = (0, char_1.removeSpecialCharacters)(lineText); | ||
if (cleanedSubLine === cleanedLine) { | ||
@@ -26,28 +18,14 @@ return true; | ||
exports.isLineEqual = isLineEqual; | ||
const extractMatchedLine = (scriptLines, subLine, subIndex) => { | ||
if (scriptLines.length <= subIndex) { | ||
return ''; | ||
const getEqualedLine = (targetLine, subLine) => { | ||
if (subLine === targetLine) { | ||
return targetLine.trim(); | ||
} | ||
const line = scriptLines[subIndex]; | ||
if (subLine === line) { | ||
return scriptLines[subIndex].trim(); | ||
} | ||
const cleanedSubLine = (0, char_1.removeSpecialCharacters)(subLine); | ||
const cleanedLine = (0, char_1.removeSpecialCharacters)(line); | ||
if (cleanedSubLine === cleanedLine) { | ||
return cleanedLine; | ||
const cleanedTargetLine = (0, char_1.removeSpecialCharacters)(targetLine); | ||
if (cleanedSubLine === cleanedTargetLine) { | ||
return cleanedTargetLine; | ||
} | ||
else if (cleanedSubLine.length > cleanedLine.length) { | ||
const excessStr = cleanedSubLine.slice(cleanedLine.length); | ||
if (scriptLines.length > subIndex + 1) { | ||
const nextLine = scriptLines[subIndex + 1]; | ||
if (nextLine.startsWith(excessStr)) { | ||
scriptLines[subIndex + 1] = nextLine.slice(excessStr.length); | ||
} | ||
} | ||
return cleanedSubLine; | ||
} | ||
return ''; | ||
}; | ||
exports.extractMatchedLine = extractMatchedLine; | ||
exports.getEqualedLine = getEqualedLine; | ||
const splitSubtitleByPunctuation = (s, maxWidth = 9999) => { | ||
@@ -61,3 +39,3 @@ const result = []; | ||
else { | ||
result.push(txt.trim()); | ||
result.push(txt.replace(/[\s\u3000]+/g, ' ').trim()); | ||
txt = ''; | ||
@@ -69,9 +47,21 @@ } | ||
exports.splitSubtitleByPunctuation = splitSubtitleByPunctuation; | ||
const removeBlankLines = (text) => { | ||
return text | ||
.split('\n') | ||
.filter(line => line.trim() !== '') | ||
.join('\n'); | ||
const resetScriptLinesContent = (scriptLines, scriptLinesIndex, subLine) => { | ||
const lineText = scriptLines[scriptLinesIndex]; | ||
const cleanedSubLine = (0, char_1.removeSpecialCharacters)(subLine); | ||
const cleanedLine = (0, char_1.removeSpecialCharacters)(lineText); | ||
if (cleanedSubLine.length > cleanedLine.length) { | ||
const excessStr = cleanedSubLine.slice(cleanedLine.length); | ||
if (scriptLines.length > scriptLinesIndex + 1) { | ||
const nextLine = scriptLines[scriptLinesIndex + 1]; | ||
if (nextLine.startsWith(excessStr)) { | ||
scriptLines[scriptLinesIndex + 1] = nextLine.slice(excessStr.length); | ||
} | ||
} | ||
} | ||
}; | ||
exports.removeBlankLines = removeBlankLines; | ||
exports.resetScriptLinesContent = resetScriptLinesContent; | ||
const normalizeWhitespace = (text) => { | ||
return text.replace(/[\s\u3000]+/g, ' ').trim(); | ||
}; | ||
exports.normalizeWhitespace = normalizeWhitespace; | ||
const addPunctuationToParagraph = (text) => { | ||
@@ -78,0 +68,0 @@ const chinesePunctuation = /[,。?!;]/; |
@@ -20,2 +20,3 @@ "use strict"; | ||
let edgeTTS = new edge_tts_node_1.MsEdgeTTS(undefined, false); | ||
console.log(voiceName, edge_tts_node_1.MsEdgeTTS.OUTPUT_FORMAT.AUDIO_24KHZ_96KBITRATE_MONO_MP3); | ||
await edgeTTS.setMetadata(voiceName, edge_tts_node_1.MsEdgeTTS.OUTPUT_FORMAT.AUDIO_24KHZ_96KBITRATE_MONO_MP3); | ||
@@ -50,3 +51,3 @@ const subMaker = new sub_maker_1.SubMaker(); | ||
catch (e) { | ||
log_1.Logger.log(`failed, error: ${e}`); | ||
log_1.Logger.error(`voice failed, error: ${e}`); | ||
} | ||
@@ -53,0 +54,0 @@ return null; |
{ | ||
"name": "ffaivideo", | ||
"version": "1.5.5", | ||
"version": "1.5.7", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
202448
2459
5