-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
326 lines (295 loc) · 11.1 KB
/
utils.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
const { exec } = require('child_process');
const fs = require('fs');
const archiver = require('archiver');
// silence threashold in seconds
// slide duration threashold in seconds
const SLIDE_THREASHOLD = 10;
const async = require('async')
function downloadFile(url, targetPath) {
return new Promise((resolve, reject) => {
exec(`curl ${url} --output ${targetPath}`, (err) => {
if (err) {
return reject(err);
}
// ffmpeg emits warn messages on stderr, omit it and check if the file exists
if (!fs.existsSync(targetPath)) {
return reject(new Error('Failed to download file'));
}
return resolve(targetPath);
})
})
}
function getItemContent(item) {
if (item.type === 'pronunciation') {
return ` ${item.alternatives[0].content}`
} else {
return `${item.alternatives[0].content}`
}
}
function getNearestStarTime(items) {
return items.find((i) => i.startTime).startTime;
}
function divideSlidesIntoSubslides(slides) {
const speakersSlides = [];
// for each slide, we divide it into subslides based on the speaker label
for (let index = 0; index < slides.length; index++) {
const slide = slides[index];
if (!slide.slideItems) {
speakersSlides.push(slide);
continue;
}
const subSlides = [];
let itemsIndex = 0;
let subSlide = {
startTime: getNearestStarTime(slide.slideItems),
content: getItemContent(slide.slideItems[itemsIndex]),
speakerLabel: slide.slideItems[itemsIndex].speakerLabel,
}
// Go through all the items and divide collect them as subslides
// based on the speaker label
let nextItem = slide.slideItems[++itemsIndex];
while (nextItem) {
if (nextItem.speakerLabel === subSlide.speakerLabel) {
subSlide.content += getItemContent(nextItem);
if (!subSlide.startTime && nextItem.startTime) {
subSlide.startTime = nextItem.startTime;
}
if (nextItem.endTime) {
subSlide.endTime = nextItem.endTime;
}
} else {
subSlides.push({ ...subSlide, content: subSlide.content.trim(), duration: (subSlide.endTime - subSlide.startTime) * 1000 });
subSlide = {
startTime: nextItem.startTime || 0,
endTime: nextItem.endTime,
content: getItemContent(nextItem),
speakerLabel: nextItem.speakerLabel,
}
}
nextItem = slide.slideItems[++itemsIndex];
}
if (subSlide.startTime !== subSlide.endTime) {
subSlides.push({ ...subSlide, content: subSlide.content.trim(), duration: (subSlide.endTime - subSlide.startTime) * 1000 });
}
speakersSlides.push(subSlides)
}
return speakersSlides;
}
function formatTranscribedSlidesToCut(slides) {
let finalSlides = [];
const speakersSlides = [];
const items = slides
.reduce((acc, slide) => acc.concat(slide.items.map(item => ({ ...item, speakerLabel: slide.speakerLabel, startTime: parseFloat(item.start_time), endTime: parseFloat(item.end_time) }))), []);
for (let index = 0; index < items.length; index++) {
let item = items[index];
let startTime = item.startTime || 0;
let endTime = item.endTime || startTime;
let slideContent = getItemContent(item);
let nextItem = items[++index];
let slideItems = [item];
let prevItem = item;
while (nextItem && !((endTime - startTime >= SLIDE_THREASHOLD && getItemContent(prevItem).trim() === '.'))) {
prevItem = nextItem;
if (nextItem.endTime) {
endTime = nextItem.endTime;
}
slideContent += getItemContent(nextItem);
slideItems.push(nextItem);
nextItem = items[++index];
}
// speakersSlides.push({ slideItems, slideContent, startTime, endTime: slideItems.reduce((acc, item) => item.endTime && item.startTime ? acc + (item.endTime - item.startTime) : acc, startTime) })
speakersSlides.push({ slideItems, content: slideContent, startTime, endTime: slideItems.reduce((acc, item) => item.endTime && item.startTime ? item.endTime : acc, startTime) })
index--;
}
finalSlides = divideSlidesIntoSubslides(speakersSlides);
return finalSlides;
}
function getSpeakerNumberFromLabel(speakerLabel) {
return parseInt(speakerLabel.replace('spk_', ''))
}
function formatSlidesToSlideSpeakerSchema(slides) {
const formattedSlides = [];
slides.forEach((subslides, subslidesIndex) => {
subslides.forEach((slide) => {
slide.text = slide.content;
delete slide.content;
slide.audio = '';
slide.media = [];
if (slide.speakerLabel) {
slide.speakerProfile = {
speakerNumber: getSpeakerNumberFromLabel(slide.speakerLabel),
speakerGender: 'male',
}
}
})
formattedSlides.push({
content: subslides,
position: subslidesIndex,
convertStatus: 'done',
})
})
return formattedSlides;
}
function getSpeakersFromSlides(slides) {
const speakers = [];
slides.forEach((slide) => {
slide.content.forEach((subslide) => {
if (speakers.map(s => s.speakerNumber).indexOf(subslide.speakerProfile.speakerNumber) === -1) {
speakers.push(subslide.speakerProfile);
}
})
})
return speakers;
}
// function formatTranscribedSlidesToCut(slides, videoDuration) {
// let finalSlides = [];
// const speakersSlides = [];
// // Collect the same speaker's consicutive slides into one;
// for (let index = 0; index < slides.length; index++) {
// const slide = slides[index];
// let slideContent = slide.content;
// let subSlides = [slide];
// let slideStartTime = slide.startTime;
// let slideEndTime = slide.endTime;
// let items = [].concat(slide.items);
// let nextSlide = slides[++index];
// let totalTime = 0;
// while (nextSlide && nextSlide.speakerLabel === slide.speakerLabel) {
// slideContent += ` ${nextSlide.content}`;
// subSlides.push(nextSlide);
// slideEndTime = nextSlide.endTime;
// items = items.concat(nextSlide.items);
// nextSlide = slides[++index];
// }
// index--;
// const speakerSlideData = {
// speakerLabel: slide.speakerLabel,
// startTime: slideStartTime,
// endTime: slideEndTime,
// content: slideContent,
// // subSlides,
// items,
// };
// // If the slide time is greater than the threashold
// // divide it into sub slides splitted by dots
// if (slideEndTime - slideStartTime >= SLIDE_THREASHOLD) {
// divideSpeakerSlidesByDot(speakerSlideData).forEach((s) => speakersSlides.push(s));
// } else {
// speakersSlides.push(speakerSlideData);
// }
// }
// finalSlides = handleSlidesSilence(speakersSlides, videoDuration);
// return finalSlides;
// }
function getRemoteFileDuration(url) {
return new Promise((resolve, reject) => {
exec(`ffprobe -i ${url} -show_entries format=duration -v quiet -of csv="p=0"`, (err, stdout, stderr) => {
if (err) {
console.log(stderr);
return reject(err);
}
if (stderr) {
return reject(stderr);
}
const duration = parseFloat(stdout.replace('\\n', ''));
resolve(duration);
})
})
}
function formatCutTime(seconds) {
let hours = Math.floor(seconds / 3600);
let minutes = Math.floor((seconds - (hours * 3600)) / 60);
seconds = seconds - (hours * 3600) - (minutes * 60);
if (hours < 10) { hours = "0" + hours; }
if (minutes < 10) { minutes = "0" + minutes; }
if (seconds < 10) { seconds = "0" + seconds; }
let time = hours + ':' + minutes + ':' + seconds;
console.log(parseFloat(seconds) , seconds)
if ((parseFloat(seconds) === parseFloat(`0${seconds}`) || parseFloat(seconds) === 0) && String(seconds).indexOf('.') === -1) {
time += '.000';
}
if (time.length < 12) {
for (let i = 0; i < Array(12 - time.length).length; i++) {
time += '0';
}
}
return time.substr(0, 12);
}
function getFileExtension(url) {
return url.split('.').pop().toLowerCase();
}
function cleanupFiles(files) {
files.forEach((file) => {
fs.unlink(file, () => { });
})
}
function cleanupDir(dir){
exec(`rm -rf ${dir}`, (err) => {
console.log('deleted directory', dir, err);
})
}
/*
files: { path: 'path/to/file', name: 'File1.mp3' }
archiveType: zip|tar
archivePath: 'path/to/finalArchiveFolder.zip'
*/
function archiveFiles(files, archiveType, archivePath) {
return new Promise((resolve, reject) => {
const output = fs.createWriteStream(archivePath);
const archive = archiver(archiveType, {
zlib: { level: 9 } // Sets the compression level.
});
output.on('finish', function() {
console.log('Data has been drained');
resolve(archivePath);
});
archive.on('warning', function(err) {
console.log('Warning', err);
});
archive.on('error', function(err) {
reject(err);
});
// pipe archive data to the file
archive.pipe(output);
files.forEach((file) => {
archive.append(fs.createReadStream(file.path), { name: file.name });
})
archive.finalize();
})
}
function formatSlidesToSubslides(slides) {
return slides.reduce((acc, s) => s.content && s.content.length > 0 ? acc.concat(s.content.map((ss) => ({ ...ss, slidePosition: s.position, subslidePosition: ss.position }))) : acc, []).sort((a, b) => a.startTime - b.startTime)
}
function getFilesDuration(urls, callback) {
const getFilesDurationFuncArray = [];
urls.forEach(url => {
function getFileDuration(cb) {
getRemoteFileDuration(url)
.then((duration) => cb(null, duration))
.catch(err => callback(err))
}
getFilesDurationFuncArray.push(getFileDuration);
})
async.parallelLimit(getFilesDurationFuncArray, 3, (err, results) => {
if (err) {
return callback(err);
}
if (!results || results.length === 0) return callback(null, 0);
const duration = results.reduce((acc, d) => acc + parseFloat(d), 0);
return callback(null, duration);
})
}
module.exports = {
formatCutTime,
getRemoteFileDuration,
getFilesDuration,
formatTranscribedSlidesToCut,
downloadFile,
getFileExtension,
getSpeakersFromSlides,
formatSlidesToSlideSpeakerSchema,
cleanupFiles,
cleanupDir,
archiveFiles,
formatSlidesToSubslides,
}