-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
121 lines (112 loc) · 3.66 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
function convertVttToJson(str) {
return new Promise((resolve, reject) => {
var current = {}
var sections = []
var start = false;
var vttString = str.trimEnd();
var vttArray = vttString.split('\n');
vttArray.forEach((line, index) => {
if (line.replace(/<\/?[^>]+(>|$)/g, "") === " "){
} else if (line.replace(/<\/?[^>]+(>|$)/g, "") == "") {
} else if (line.indexOf('-->') !== -1 ) {
start = true;
if (current.start === 0 || current.start) {
const c = clone(current)
const prevSection = sections[sections.length - 1]
if (prevSection.start === c.start && prevSection.end === c.end) {
prevSection.part = `${prevSection.part}\n${c.part}`
} else {
sections.push(clone(current))
}
}
current = {
start: timeString2ms(line.split("-->")[0].trimRight().split(" ").pop()),
end: timeString2ms(line.split("-->")[1].trimLeft().split(" ").shift()),
part: ''
}
} else if (line.replace(/<\/?[^>]+(>|$)/g, "") === ""){
} else if (line.replace(/<\/?[^>]+(>|$)/g, "") === " "){
} else {
if (start){
if (sections.length !== 0) {
if (sections[sections.length - 1].part.replace(/<\/?[^>]+(>|$)/g, "") === line.replace(/<\/?[^>]+(>|$)/g, "")) {
} else {
if (current.part.length === 0) {
current.part = line
} else {
current.part = `${current.part}\n${line}`
}
// If it's the last line of the subtitles
if (index + 1 === vttArray.length) {
sections.push(clone(current))
}
}
} else {
current.part = line
sections.push(clone(current))
current.part = ''
}
}
}
})
current = []
var regex = /(<([0-9:.>]+)>)/ig
sections.forEach(section => {
strs = section.part.split()
var results = strs.map(function(s){
return s.replace(regex, function(n){
return n.split('').reduce(function(s,i){ return `==${n.replace("<", "").replace(">", "")}` }, 0)
})
});
cleanText = results[0].replace(/<\/?[^>]+(>|$)/g, "");
cleanArray = cleanText.split(" ")
resultsArray = [];
cleanArray.forEach(function(item){
if (item.indexOf('==') > -1) {
var pair = item.split("==")
var key = pair[0]
var value = pair[1]
if(key == "" || key == "##") {
return;
}
resultsArray.push({
word: cleanWord(item.split("==")[0]),
time: timeString2ms(item.split("==")[1]),
})
} else {
resultsArray.push({
word: cleanWord(item),
time: undefined,
})
}
})
section.words = resultsArray;
section.part = section.part.replace(/<\/?[^>]+(>|$)/g, "")
})
sections.forEach((section, index) => {
section.part = section.part.trimEnd()
})
resolve(sections);
})
}
// helpers
// http://codereview.stackexchange.com/questions/45335/milliseconds-to-time-string-time-string-to-milliseconds
function timeString2ms(a,b){// time(HH:MM:SS.mss) // optimized
return a=a.split('.'), // optimized
b=a[1]*1||0, // optimized
a=a[0].split(':'),
b+(a[2]?a[0]*3600+a[1]*60+a[2]*1:a[1]?a[0]*60+a[1]*1:a[0]*1)*1e3 // optimized
}
// removes everything but characters and apostrophe and dash
function cleanWord(word) {
return word.replace(/[^0-9a-z'-]/gi, '').toLowerCase()
}
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
}
module.exports = convertVttToJson;