Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
SashaXser authored Jan 25, 2024
1 parent 579e36e commit 205d8ad
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions src/subtitles.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ function formatYandexSubtitlesTokens(line) {
const lastToken = result[result.length - 1];
const alignRangeEnd = lastToken?.alignRange?.end ?? 0;
const newAlignRangeEnd = alignRangeEnd + token.text.length;
result.push(
Object.assign(Object.assign({}, token), {
alignRange: {
start: alignRangeEnd,
end: newAlignRangeEnd,
},
}),
);
result.push({
...token,
alignRange: {
start: alignRangeEnd,
end: newAlignRangeEnd,
},
});
if (nextToken) {
const endMs = token.startMs + token.durationMs;
const durationMs = nextToken.startMs
Expand All @@ -39,33 +38,32 @@ function formatYandexSubtitlesTokens(line) {
}

function createSubtitlesTokens(line, previousLineLastToken) {
const tokens = line.text
.split(new RegExp("([\n \t])"))
.reduce((result, tokenText) => {
if (tokenText.length) {
const lastToken = result[result.length - 1] ?? previousLineLastToken;
const alignRangeStart = lastToken?.alignRange?.end ?? 0;
const alignRangeEnd = alignRangeStart + tokenText.length;
result.push({
text: tokenText,
alignRange: {
start: alignRangeStart,
end: alignRangeEnd,
},
});
}
return result;
}, []);
const tokens = line.text.split(/([\n \t])/).reduce((result, tokenText) => {
if (tokenText.length) {
const lastToken = result[result.length - 1] ?? previousLineLastToken;
const alignRangeStart = lastToken?.alignRange?.end ?? 0;
const alignRangeEnd = alignRangeStart + tokenText.length;
result.push({
text: tokenText,
alignRange: {
start: alignRangeStart,
end: alignRangeEnd,
},
});
}
return result;
}, []);
const tokenDurationMs = Math.floor(line.durationMs / tokens.length);
const lineEndMs = line.startMs + line.durationMs;
return tokens.map((token, index) => {
const isLastToken = index === tokens.length - 1;
const startMs = line.startMs + tokenDurationMs * index;
const durationMs = isLastToken ? lineEndMs - startMs : tokenDurationMs;
return Object.assign(Object.assign({}, token), {
return {
...token,
startMs,
durationMs,
});
};
});
}

Expand All @@ -86,11 +84,10 @@ function getSubtitlesTokens(subtitles, source) {
tokens = createSubtitlesTokens(line, lastToken);
}
lastToken = tokens[tokens.length - 1];
result.push(
Object.assign(Object.assign({}, line), {
tokens,
}),
);
result.push({
...line,
tokens,
});
}
subtitles.containsTokens = true;
return result;
Expand Down

0 comments on commit 205d8ad

Please sign in to comment.