-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
201 lines (158 loc) · 5.08 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const readFile = (file) => {
return new Promise((resolve, reject) => {
let fr = new FileReader();
fr.onload = x=> resolve(fr.result);
fr.readAsText(file);
});
}
const read = async (input) => {
try {
dispayResult({
content: 'Parsing and Converting the file contents. Please wait...'
});
const textContent = await readFile(input.files.length && input.files[0]);
const parsedSegments = await parseTimedText(textContent);
const filename = createSrtFileName(input.files.length && input.files[0].name);
await handleProcessAndDownload(parsedSegments, filename);
setTimeout(() => {
dispayResult({
content: 'Your converted file has been downloaded...'
});
}, 1000);
} catch (err) {
console.log('error occured');
console.log(err);
dispayResult({
isInvalid: true,
content: 'Error while reading the file! Please try again'
});
}
}
/**
* Parses stringified JSON content for getting text & time details
* @param {string} txt
* @returns {Array<Segment>} segments
*/
const parseTimedText = async (txt) => {
const timedTextJson = JSON.parse(txt);
const events = timedTextJson.events || [];
const x = await getSegments(events);
console.log(x);
return x;
}
/**
* Parses events array and returns Array of Segments containing:
* (startTime, endTime, and caption text)
* @param {Array<EventObject>} events
* EventObject : {
"tStartMs": Number,
"dDurationMs": Number,
"id"?: Number,
"wpWinPosId"?: Number,
"wsWinStyleId"?: Number,
"wWinId"?: Number,
"segs"?: Array<{utf8: String}>,
"aAppend"?: Number
}
* @returns {Array<Segment>} segments
* Segment: {
"startTimeMs": Number,
"endTimeMs": Number,
"text": String
}
*
*/
const getSegments = async (events) => {
const segments = [];
let segmentText = '';
let segment = {
startTimeMs: 0,
};
// events = events.slice(0, 10);
// console.log(events);
events.map(evt => {
// check whether it is manually uploaded CC or not
if (!evt.wWinId && evt.segs) {
segment.startTimeMs = evt.tStartMs;
segment.endTimeMs = evt.tStartMs + evt.dDurationMs;
(evt.segs || []).map(seg => {
const text = seg.utf8; //.trim();
if (text && text !== '\n') {
segmentText += (text || '');
}
});
segment.text = segmentText;
segments.push(segment);
segment = {};
segmentText = '';
return;
}
// Below code is a fall back for Auto Generated CC
if (!evt.aAppend) {
segment.startTimeMs = evt.tStartMs;
(evt.segs || []).map(seg => {
const text = seg.utf8; //.trim();
if (text && text !== '\n') {
segmentText += (text || '');
}
});
} else {
segment.endTimeMs = evt.tStartMs;
segment.text = segmentText;
segments.push(segment);
segment = {};
segmentText = '';
}
});
return segments;
}
/**
*
* @param {Array<Segment>} segments
*/
const convertSegmentsToSrt = async (segments) => {
let srtString = '';
segments.map((segment, index) => {
const startTime = convertToStrTime(segment.startTimeMs);
const endTime = convertToStrTime(segment.endTimeMs);
const text = "" + (index + 1) + "\r\n" +
startTime + " --> " + endTime + "\r\n" +
segment.text + "\r\n\r\n";
srtString += text;
});
return srtString;
}
const convertToStrTime = (ms) => {
let seconds = Math.floor(ms / 1000);
ms = ms % 1000;
const milliseconds = ms;
let minutes = Math.floor(seconds / 60);
seconds = seconds % 60;
const hours = Math.floor(minutes / 60);
minutes = minutes % 60;
// return format: 00:00:06,319
return `${paddZero([hours, minutes, seconds]).join(':')},${paddZero([milliseconds])[0]}`;
}
const paddZero = (numList) => {
if(!Array.isArray(numList)) return [];
return numList.map(num => {
if (+num >= 10) return `${num}`;
return `0${num}`;
});
}
const createSrtFileName = (fileName) => {
if (!fileName) return 'captions';
let splitName = fileName.split('.');
if (splitName.length > 1) {
splitName = splitName.slice(0, splitName.length - 1);
}
return splitName.join('.');
}
const handleProcessAndDownload = async (parsedSegments, filename) => {
const srtData = await convertSegmentsToSrt(parsedSegments);
var blob = new Blob([srtData], {type: "text/plain;charset=utf-8"});
saveAs(blob, `${filename}.srt`);
}
const dispayResult = (result) => {
msg.innerText = result.content;
}