This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
124 lines (104 loc) · 4.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//todo: implement multiline srt
// but only if lines match from <-> to (+-tolerance)
const moment = require('moment');
function createMaxCharacterPerLineRule(characterPerLine = 40) {
return (element, newElement) =>
element.content.length + 1 + newElement.content.length < characterPerLine;
}
function createMaxTimeRule(maxTimeInMs = 1000) {
return (element, newElement) => newElement.from - element.to < maxTimeInMs;
}
function createMaxLinesRule(maxLines = 2) {
return (element, newElement) => element.lines < maxLines;
}
function createTimeStamp(ms) {
let dur = moment.duration(ms);
return `${
dur.hours() > 9 ? dur.hours() : '0' + dur.hours()
}:${
dur.minutes() > 9 ? dur.minutes() : '0' + dur.minutes()
}:${
dur.seconds() > 9 ? dur.seconds() : '0' + dur.seconds()
}.${
dur.milliseconds() > 99 ? dur.milliseconds() : dur.milliseconds() > 9 ? '0' + dur.milliseconds() : '00' + dur.milliseconds()
}`;
}
exports.handler = async (event, {characterPerLine = 40, maxLines = 2, maxTimeInMs = 1000, debug = false} = {}) => {
if (debug) {
console.log(`Event: ${JSON.stringify(event)}`);
console.log(`CharacterPerLine: ${characterPerLine}`);
console.log(`Max Lines: ${maxLines}`);
console.log(`Max Time in Ms: ${maxTimeInMs}`);
}
let wordsToLineMergeRules = [
createMaxCharacterPerLineRule(characterPerLine),
createMaxTimeRule(maxTimeInMs)
];
let multilineMergeRules = [
createMaxLinesRule(maxLines),
createMaxTimeRule(maxTimeInMs)
];
return 'WEBVTT\n\n'
+ event.results.items
.map((e) => ({
content: e.alternatives[0].content,
from: Math.floor(parseFloat(e.start_time) * 1000),
to: Math.floor(parseFloat(e.end_time)) * 1000,
characterCount: e.alternatives[0].content.length,
//pronunciation|punctuation
type: e.type,
lines: 1
}))
.reduce((acc, cur) => {
if (acc.length === 0) {
return [cur]
}
let last = acc[acc.length - 1];
if (cur.type === 'pronunciation') {
let shouldMerge = wordsToLineMergeRules
.reduce((shouldMerge, rule) => shouldMerge ? rule(last, cur) : false, true);
if (shouldMerge) {
acc[acc.length - 1] = Object.assign({}, last, {
content: `${last.content} ${cur.content}`,
to: cur.to,
characterCount: last.characterCount + 1 + cur.characterCount
});
return acc;
}
return [...acc, cur];
}
//cur.type === 'punctuation'
else {
acc[acc.length - 1] = Object.assign({}, last, {
content: `${last.content}${cur.content}`,
characterCount: last.characterCount + cur.characterCount
});
return acc;
}
}, [])
.reduce((acc, cur) => {
if (acc.length === 0) {
return [cur];
}
let last = acc[acc.length - 1];
let shouldMerge = multilineMergeRules
.reduce((shouldMerge, rule) => shouldMerge ? rule(last, cur) : false, true);
if (shouldMerge) {
acc[acc.length - 1] = Object.assign({}, last, {
content: `${last.content}\n${cur.content}`,
to: cur.to,
characterCount: last.characterCount + cur.characterCount,
lines: ++last.lines
});
return acc;
}
return [...acc, cur];
}, [])
.reduce((acc, cur, idx) => {
return [...acc,
`${idx + 1} \n` +
`${createTimeStamp(cur.from)} --> ${createTimeStamp(cur.to)}\n` +
`${cur.content}\n\n`];
}, [])
.join('');
};