-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.js
735 lines (679 loc) · 28.1 KB
/
worker.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
require('dotenv').config({path: '.env'});
const args = process.argv.slice(2);
const lang = args[0];
console.log('lang is', lang)
const amqp = require('amqplib/callback_api');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const async = require('async');
const mongoose = require('mongoose');
const cheerio = require('cheerio');
const {
imageToSilentVideo,
videoToSilentVideo,
gifToSilentVideo,
combineVideos,
slowVideoRate,
wavToWebm,
addFadeEffects,
addFadeInEffect,
addFadeOutEffect,
addAudioToVideo,
extractAudioFromVideo,
generateSilentAudio,
combineAudios,
} = require('./converter');
const utils = require('./utils');
const subtitles = require('./subtitles');
const { DEFAUL_IMAGE_URL, SLIDE_CONVERT_PER_TIME, FADE_EFFECT_DURATION, VIDEO_WIDTH, VIDEO_HEIGHT } = require('./constants');
const UserModel = require('./models/User');
const ArticleModel = require('./models/Article');
const VideoModel = require('./models/Video');
const HumanVoiceModel = require('./models/HumanVoice');
const DELETE_AWS_VIDEO = 'DELETE_AWS_VIDEO';
const CONVERT_QUEUE = `CONVERT_ARTICLE_QUEUE_${lang}`;
const UPDLOAD_CONVERTED_TO_COMMONS_QUEUE = `UPDLOAD_CONVERTED_TO_COMMONS_QUEUE_${lang}`;
const DB_CONNECTION = `${process.env.DB_HOST_URL}-${lang}`;
// const DB_CONNECTION = 'mongodb://localhost/videowiki-en'
console.log('connecting to database ', DB_CONNECTION);
mongoose.connect(DB_CONNECTION)
let convertChannel;
amqp.connect(process.env.RABBITMQ_HOST_URL, (err, conn) => {
console.log('error is', err);
conn.createChannel((err, ch) => {
convertChannel = ch;
convertChannel.prefetch(1);
console.log('connection created')
convertChannel.assertQueue(CONVERT_QUEUE, {durable: true}, (err, ok) => {
console.log('queue assert', err, ok)
})
convertChannel.assertQueue(UPDLOAD_CONVERTED_TO_COMMONS_QUEUE, { durable: true });
convertChannel.assertQueue(DELETE_AWS_VIDEO, { durable: true });
convertChannel.consume(DELETE_AWS_VIDEO, deleteAWSVideoCallback);
convertChannel.consume(CONVERT_QUEUE, convertQueueCallback, { noAck: false });
})
})
function convertQueueCallback(msg) {
const { videoId } = JSON.parse(msg.content.toString());
VideoModel
.findById(videoId)
.populate('humanvoice')
.populate('user')
.exec((err, video) => {
if (err) {
updateStatus(videoId, 'failed');
console.log('error retrieving video', err);
return convertChannel.ack(msg);
}
if (!video) {
console.log('invalid video id');
updateStatus(videoId, 'failed');
return convertChannel.ack(msg);
}
console.log('video is ', video);
ArticleModel.findOne({title: video.title, wikiSource: video.wikiSource, published: true}, (err, article) => {
if (err) {
updateStatus(videoId, 'failed');
console.log('error fetching article ', err);
return convertChannel.ack(msg);
}
console.log('converting article ', article.title)
// Update status
updateStatus(videoId, 'progress');
convertArticle({ article, video, videoId, withSubtitles: video.withSubtitles }, (err, convertResult) => {
console.log('convert rsult is ', convertResult)
if (err) {
updateStatus(videoId, 'failed');
console.log(err);
return convertChannel.ack(msg);
}
utils.uploadVideoToS3(convertResult.videoPath, (err, uploadVideoResult) => {
if (err) {
console.log('error uploading file', err);
updateStatus(videoId, 'failed');
return convertChannel.ack(msg);
}
const { url, ETag } = uploadVideoResult;
let videoUpdate = {
url,
ETag,
status: 'converted',
wrapupVideoProgress: 100,
}
// console.log('converted at ', url)
if (convertResult.subtitles) {
// Upload generated subtitles to s3
utils.uploadSubtitlesToS3(convertResult.subtitles, (err, uploadSubtitlesResult) => {
if (err) {
console.log('error uploading subtitles to s3', err);
} else if (uploadSubtitlesResult && Object.keys(uploadSubtitlesResult).length > 0) {
videoUpdate.commonsSubtitles = uploadSubtitlesResult.url;
videoUpdate = {
...videoUpdate,
...uploadSubtitlesResult
}
}
VideoModel.findByIdAndUpdate(videoId, { $set: videoUpdate }, { new: true }, (err, result) => {
if (err) {
updateStatus(videoId, 'failed');
console.log(err);
}
console.log('Done!', result)
convertChannel.ack(msg);
updateProgress(videoId, 100);
convertChannel.sendToQueue(UPDLOAD_CONVERTED_TO_COMMONS_QUEUE, new Buffer(JSON.stringify({ videoId })), { persistent: true })
// Cleanup
fs.unlink(convertResult.videoPath, () => {});
Object.keys(convertResult.subtitles).forEach(key => {
fs.unlink(convertResult.subtitles[key], () => {});
})
})
})
} else {
VideoModel.findByIdAndUpdate(videoId, { $set: videoUpdate }, { new: true }, (err, result) => {
if (err) {
updateStatus(videoId, 'failed');
console.log(err);
}
console.log('Done!', result);
convertChannel.ack(msg);
updateProgress(videoId, 100);
convertChannel.sendToQueue(UPDLOAD_CONVERTED_TO_COMMONS_QUEUE, new Buffer(JSON.stringify({ videoId })), { persistent: true })
// Cleanup
fs.unlink(convertResult.videoPath, () => {});
})
}
})
})
})
})
}
function deleteAWSVideoCallback(msg) {
const { videoId } = JSON.parse(msg.content.toString());
VideoModel.findById(videoId, (err, video) => {
if (err) {
console.log('error fetching video ', err, videoId);
return;
}
if (!video) {
console.log('invalid video id ', videoId);
return;
}
if (video && video.url) {
const fileName = video.url.split('/').pop();
console.log('file name is ', fileName)
utils.deleteVideoFromS3(fileName, (err, result) => {
if (err) {
console.log('Error deleting video from s3', err);
return;
}
console.log('successfully delete video from s3', result);
VideoModel.findByIdAndUpdate(videoId, { $unset: { url: true }}, (err, result) => {
console.log(err, result);
})
})
}
})
}
const verifyMedia = (slide, mitem) => (cb) => {
if (!mitem.url && !mitem.origianlUrl) {
mitem.url = DEFAUL_IMAGE_URL;
mitem.type = 'image';
mitem.time = slide.duration;
return cb();
}
let slideMediaUrl = mitem.origianlUrl || mitem.url;
const tmpMediaName = path.join(__dirname, 'tmp', `downTmpMedia-${Date.now()}-${parseInt(Math.random() * 10000)}.${slideMediaUrl.split('.').pop()}`);
console.log('veirying', slideMediaUrl)
if (slideMediaUrl.indexOf('400px-') !== -1) {
slideMediaUrl = slideMediaUrl.replace('400px-', '800px-');
}
// Svg files are rendered as pngs
if (mitem.origianlUrl && mitem.origianlUrl.split('.').pop().toLowerCase() === 'svg') {
slideMediaUrl = mitem.thumburl || mitem.url;
}
utils.downloadMediaFile(slideMediaUrl, tmpMediaName, (err) => {
if (err) {
console.log(err);
mitem.url = DEFAUL_IMAGE_URL;
mitem.type = 'image';
mitem.time = slide.duration;
return cb();
}
mitem.tmpUrl = tmpMediaName;
return cb();
})
}
const downloadSlideAudio = slide => (cb) => {
const tempAudioFile = path.join(__dirname, 'tmp', `downTmpAudio-${Date.now()}-${slide.audio.split('/').pop()}`);
const audioUrl = slide.audio.indexOf('http') === -1 ? `https:${slide.audio}` : slide.audio;
console.log('downloading', slide.audio)
utils.downloadMediaFile(audioUrl, tempAudioFile, (err) => {
if (!err) {
slide.tmpAudio = tempAudioFile;
const audioExt = tempAudioFile.split('.').pop();
// If the file extension is wav, convert it to webm for consistent encoding
if (audioExt !== 'wav') return cb();
wavToWebm(slide.tmpAudio, `${slide.tmpAudio}.webm`, (err, newTmpPath) => {
if (newTmpPath) {
slide.tmpAudio = newTmpPath;
}
return cb();
})
} else {
console.log('error downloading tmp audio', err);
return cb();
}
})
}
const generateSlideAudioFromMedia = slide => cb => {
console.log('=========================== generateSlideAudioFromMedia ==============================')
// loop over slide's media
// if the media item is video, extract it's audio
// if the media item is gif or image, generate silence audio with it's duration
// concat all audios
// cleanup tmp media audios
// assign to slide.tmpAudio and cb()
const generateMediaFuncArray = [];
const mediaAudiosPaths = [];
slide.media.forEach(mitem => {
generateMediaFuncArray.push((cb) => {
if (utils.getFileType(mitem.url) === 'video') {
extractAudioFromVideo(mitem.url, (err, audioPath) => {
console.log(err)
if (err) return cb(err);
mediaAudiosPaths.push(audioPath);
utils.getRemoteFileDuration(mitem.url, (err, duration) => {
console.log(err)
if (err) return cb(err);
mitem.time = duration * 1000;
cb()
})
})
} else if (['image', 'gif'].indexOf(utils.getFileType(mitem.url))) {
generateSilentAudio(5000, (err, audioPath) => {
console.log(err)
if (err) return cb(err);
mediaAudiosPaths.push(audioPath);
mitem.time = 5000;
cb();
})
} else {
console.log('Invalid media type')
return cb(new Error('Invalid media type' + mitem.url))
}
})
})
async.parallelLimit(generateMediaFuncArray, 1, (err) => {
console.log(err)
if (err) return cb(err);
console.log('================= combining audios ============================')
combineAudios(mediaAudiosPaths, (err, audioPath) => {
console.log(err)
if (err) return cb(err);
mediaAudiosPaths.forEach(p => {
fs.unlink(p, (err) => {
if (err) {
console.log(err);
}
})
})
slide.tmpAudio = audioPath;
console.log('COMBINED AUDIO', audioPath, slide)
return cb()
})
})
}
function convertArticle({ article, video, videoId, withSubtitles }, callback) {
const convertFuncArray = [];
let progress = 0;
// const slidesHtml = article.slidesHtml.slice().filter(slide => slide.text && slide.audio);
const slidesHtml = article.slidesHtml.slice();
const verifySlidesMediaFuncArray = [];
const humanvoiceFuncArray = [];
if (video.humanvoice && video.humanvoice.audios && video.humanvoice.audios.length > 0) {
video.humanvoice.audios.forEach((audio) => {
if (audio.position < slidesHtml.length) {
humanvoiceFuncArray.push((cb) => {
utils.getRemoteFileDuration(`https:${audio.audioURL}`, (err, duration) => {
console.log('audio duration', audio)
if (err) {
console.log('error egtting duration', err);
} else {
audio.duration = duration * 1000;
}
// Set human voice audio and duration on normal slides
const matchingSlide = slidesHtml.find(s => s.position === audio.position);
matchingSlide.audio = audio.audioURL;
matchingSlide.duration = audio.duration;
// Set media timing
if (!matchingSlide.media || matchingSlide.media.length === 0) {
matchingSlide.media = [{
url: DEFAUL_IMAGE_URL,
type: 'image',
time: audio.duration,
}]
} else if (matchingSlide.media.length === 1) {
matchingSlide.media[0].time = audio.duration;
} else {
/* we have two cases here
1- the medias are smaller than human voice audios
- in this case, we add the extra time to the last media item
2- the medias are longer than human voice audios
- in this case, we see the difference and remove it from
the last media item if possible. if not, we set the timings
equally between all media items
*/
const totalMediaDuration = matchingSlide.media.reduce((acc, m) => m.time + acc, 0);
const durationDifference = Math.abs(matchingSlide.duration - totalMediaDuration);
if (matchingSlide.duration >= totalMediaDuration) {
const durationDifference = matchingSlide.duration - totalMediaDuration;
matchingSlide.media[matchingSlide.media.length - 1].time = matchingSlide.media[matchingSlide.media.length - 1].time + durationDifference;
} else if (totalMediaDuration > matchingSlide.duration) {
// check the last media item, if its duration - duration difference is more than 2 seconds,
// just remove trim the duration to match the audio duration
// otherwise, reset duration on all media items
const lastMediaItem = matchingSlide.media[matchingSlide.media.length - 1];
if ((lastMediaItem.time - durationDifference) >= 2000) {
lastMediaItem.time = lastMediaItem.time - durationDifference;
} else {
matchingSlide.media.forEach((mitem) => {
mitem.time = matchingSlide.duration / matchingSlide.media.length;
})
}
}
}
return cb();
})
})
}
})
}
async.parallelLimit(humanvoiceFuncArray, 2, () => {
slidesHtml.forEach(slide => {
if (!slide.media || slide.media.length === 0) {
slide.media = [{
url: DEFAUL_IMAGE_URL,
type: 'image',
time: slide.duration,
}];
} else {
slide.media.forEach((mitem) => {
if (process.env.NODE_ENV !== 'production') {
verifySlidesMediaFuncArray.push(verifyMedia(slide, mitem));
}
})
}
})
console.log('verifying media');
async.parallelLimit(async.reflectAll(verifySlidesMediaFuncArray), 1, (err, result) => {
if (err) {
console.log('error verifying slides media');
}
// Download media and audio for local use
const downAudioFuncArray = [];
slidesHtml.forEach((slide) => {
if (slide.audio) {
downAudioFuncArray.push(downloadSlideAudio(slide));
} else {
console.log('doesnt have audio, generating audio from media instead')
downAudioFuncArray.push(generateSlideAudioFromMedia(slide))
}
})
console.log('downloading audios', slidesHtml.length, slidesHtml);
async.parallelLimit(async.reflectAll(downAudioFuncArray), 2, (err, value) => {
console.log('start time', new Date())
if (err) {
console.log('error fetching tmp medias', err);
}
let videoDerivatives = [];
slidesHtml.sort((a,b) => a.position - b.position).forEach((slide, index) => {
function convert(cb) {
console.log('converting', slide.position)
const audioUrl = slide.tmpAudio ? slide.tmpAudio : (slide.audio ? `https:${slide.audio}` : '');
const convertCallback = (err, result) => {
if (err) {
console.log('error in async ', err);
return cb(err);
}
// Clear tmp media and audio if exists
if (slide.tmpAudio) {
fs.unlink(slide.tmpAudio, () => {});
}
let { videoPath, videoDerivative } = result;
if (videoDerivative) {
videoDerivatives = videoDerivatives.concat(videoDerivative)
}
const finalizeSlideFunc = [];
slide.video = videoPath;
utils.getRemoteFileDuration(videoPath, (err, duration) => {
// Add fade effect only to slides having at least 2 seconds of content
if (!err && Math.floor(duration) > 2) {
finalizeSlideFunc.push((finalizeSlideCB) => {
addFadeEffects(videoPath, FADE_EFFECT_DURATION, (err, fadedVideo) => {
if (err) {
console.log('error adding fade effects', err);
slide.video = videoPath;
} else if (fadedVideo && fs.existsSync(fadedVideo)) {
fs.unlinkSync(videoPath);
slide.video = fadedVideo;
}
finalizeSlideCB();
})
})
}
finalizeSlideFunc.push((finalizeSlideCB) => {
progress += (1 / article.slides.length) * 100;
updateProgress(videoId, progress);
console.log(`Progress ####### ${progress} ######`);
finalizeSlideCB();
return cb(null, {
fileName: slide.video,
index
});
})
async.series(finalizeSlideFunc, () => {});
})
}
// End convert callback
if (!slide.media) {
slide.media = [{
url: DEFAUL_IMAGE_URL,
type: 'image',
}];
}
convertMedias(slide.media, audioUrl, slide.position, convertCallback);
}
convertFuncArray.push(convert);
})
async.parallelLimit(convertFuncArray, SLIDE_CONVERT_PER_TIME, (err, results) => {
if (err) {
VideoModel.findByIdAndUpdate(videoId, {$set: { status: 'failed' }}, (err, result) => {
})
return callback(err);
}
updateProgress(videoId, 100);
// Set video derivatives to be put in the licence info
VideoModel.findByIdAndUpdate(videoId, { $set: { derivatives: videoDerivatives } }, (err) => {
if (err) {
console.log('error saving video derivatives');
}
})
results = results.sort((a, b) => a.index - b.index);
// Generate the user credits slides
utils.generateCreditsVideos(article, video, (err, creditsVideos) => {
if (err) {
console.log('error creating credits videos', err);
}
// Generate the article references slides
utils.generateReferencesVideos(article.title, article.wikiSource, article.referencesList,{
onProgress: (progress) => {
if (progress && progress !== 'null') {
VideoModel.findByIdAndUpdate(videoId, {$set: { textReferencesProgress: progress }}, (err, result) => {
})
}
},
onEnd: (err, referencesVideos) => {
// Considere progress done
VideoModel.findByIdAndUpdate(videoId, {$set: { textReferencesProgress: 100 }}, (err, result) => {
})
if (err) {
console.log('error creating references videos', err);
}
let finalVideos = [];
if (results) {
finalVideos = finalVideos.concat(results);
}
// Add Share video
finalVideos.push({ fileName: 'cc_share.webm', });
if (creditsVideos && creditsVideos.length > 0) {
finalVideos = finalVideos.concat(creditsVideos);
}
if (referencesVideos && referencesVideos.length > 0) {
finalVideos = finalVideos.concat(referencesVideos);
}
combineVideos(finalVideos, false, {
onProgress: (progress) => {
if (progress && progress !== 'null') {
VideoModel.findByIdAndUpdate(videoId, {$set: { combiningVideosProgress: progress }}, (err, result) => {
})
}
},
onEnd: (err, videoPath) => {
if (err) {
console.log(err);
VideoModel.findByIdAndUpdate(videoId, {$set: { status: 'failed' }}, (err, result) => {
})
return callback(err);
}
VideoModel.findByIdAndUpdate(videoId, {$set: { combiningVideosProgress: 100, wrapupVideoProgress: 20 }}, (err, result) => {
})
const subtitledSlides = JSON.parse(JSON.stringify(slidesHtml));
// If we have human voice, use the user's translation as the subtitles
if (video.humanvoice && video.humanvoice.translatedSlides && video.lang !== article.lang) {
video.humanvoice.translatedSlides.forEach((slide) => {
if (subtitledSlides[slide.position]) {
subtitledSlides[slide.position].text = slide.text;
}
});
}
subtitles.generateSrtSubtitles(subtitledSlides, 1, (err, subs) => {
const cbResult = { videoPath };
if (err) {
console.log('error generating subtitles file', err);
}
if (subs) {
cbResult.subtitles = subs;
}
VideoModel.findByIdAndUpdate(videoId, {$set: { wrapupVideoProgress: 70 }}, (err, result) => {
})
// Cleanup
slidesHtml.forEach(slide => {
if (slide.video && fs.existsSync(slide.video)) {
fs.unlink(slide.video, () => {});
}
})
if (referencesVideos) {
referencesVideos.forEach(video => fs.existsSync(video.fileName) && fs.unlink(video.fileName, () => {}));
}
if (creditsVideos) {
creditsVideos.forEach(video => fs.existsSync(video.fileName) && fs.unlink(video.fileName, () => {}));
}
console.log('end time', new Date())
return callback(null, cbResult);
})
}
})
}
})
})
})
})
})
})
}
function convertMedias(medias, audio, slidePosition, callback = () => {}) {
const convertMediaFuncArray = [];
let videoDerivative = [];
medias.forEach((mitem, index) => {
convertMediaFuncArray.push((singleCB) => {
const fileName = `videos/video-${parseInt(Date.now() + Math.random() * 100000)}.webm`;
utils.getMediaInfo(mitem.url, (err, info) => {
let subtext = '';
if (err) {
console.log('error fetching media author and licence', err, medias);
} else if (info){
if (info.author) {
subtext = `Visual Content by ${info.author}${info.licence ? ', ' : '.'}`
}
if (info.licence) {
subtext += info.licence
}
}
// Collect derivatives info
if (info && info.author && info.licenseCode && info.fileName) {
videoDerivative.push({
fileName: info.fileName,
author: info.author,
licence: info.licenseCode,
position: slidePosition,
})
}
let slideMediaUrl = mitem.tmpUrl || mitem.origianlUrl || mitem.url;
if (mitem.origianlUrl && mitem.origianlUrl.split('.').pop().toLowerCase() === 'svg') {
slideMediaUrl = mitem.thumburl || mitem.url;
}
const convertSingleCallback = function convertSingleCallback(err, fileName) {
if (err) return singleCB(err);
// Dont add extra fade effect for a single media item
if (medias.length === 1) {
return singleCB(null, { fileName, index });
}
let fadeFunc
// Add fade in effect only to last media item
if (index === medias.length - 1) {
fadeFunc = addFadeInEffect
} else if (index === 0) {
// Add fade out effect only to first media item
fadeFunc = addFadeOutEffect
} else {
// In middle media's, add both fades
fadeFunc = addFadeEffects
}
fadeFunc(fileName, FADE_EFFECT_DURATION, (err, fadedVideo) => {
if (err) {
return singleCB(null, { fileName, index })
}
return singleCB(null, { fileName: fadedVideo, index });
})
}
console.log('converting submedia', slideMediaUrl, subtext)
if (utils.getFileType(mitem.url) === 'image') {
utils.getFileDimentions(slideMediaUrl, (err, dimentions) => {
if (err && !dimentions) {
console.log('error getting dimentions', err);
dimentions = `${VIDEO_WIDTH}x${VIDEO_HEIGHT}`;
}
// If the width is larger than the default video width
// get a thumbnail image instead
const imageWidth = parseInt(dimentions.split('x')[0]);
if (imageWidth > VIDEO_WIDTH) {
slideMediaUrl = mitem.thumburl;
}
imageToSilentVideo({ image: slideMediaUrl, subtext, duration: mitem.time / 1000, outputPath: fileName }, convertSingleCallback);
})
} else if (utils.getFileType(mitem.url) === 'video') {
videoToSilentVideo({ video: slideMediaUrl, subtext, duration: mitem.time / 1000, outputPath: fileName }, convertSingleCallback);
} else if (utils.getFileType(mitem.url) === 'gif') {
gifToSilentVideo({ gif: slideMediaUrl, subtext, duration: mitem.time / 1000, outputPath: fileName}, convertSingleCallback);
} else {
return singleCB(new Error('Invalid file type'));
}
})
})
})
async.parallelLimit(convertMediaFuncArray, SLIDE_CONVERT_PER_TIME, (err, outputInfo) => {
if (err) return callback(err);
const slideVideos = outputInfo.sort((a, b) => a.index - b.index);
console.log('combining videos of submedia');
const finalSlideVidPath = path.join(__dirname, 'videos', `slide_with_audio-${Date.now()}-${parseInt(Math.random() * 100000)}.webm`)
if (medias.length > 1) {
combineVideos(slideVideos, true, {
onEnd: (err, videoPath) => {
if (err) return callback(err);
return addAudioToVideo(videoPath, audio, finalSlideVidPath, (err, videoPath) => {
if (err) return callback(err);
return callback(null, { videoPath: finalSlideVidPath, videoDerivative });
});
},
})
} else {
addAudioToVideo(slideVideos[0].fileName, audio, finalSlideVidPath, (err, videoPath) => {
if (err) return callback(err);
return callback(null, { videoPath: finalSlideVidPath, videoDerivative });
});
}
})
}
function updateProgress(videoId, conversionProgress) {
VideoModel.findByIdAndUpdate(videoId, {$set: { conversionProgress }}, (err, result) => {
if (err) {
console.log('error updating progress', err);
}
})
}
function updateStatus(videoId, status) {
VideoModel.findByIdAndUpdate(videoId, {$set: { status }}, (err, result) => {
if (err) {
console.log('error updating progress', err);
}
})
}
ArticleModel.count({ published: true }, (err, count) => {
if (err) {
console.log(err);
} else {
console.log(`Ready to handle a total of ${count} published articles in the database!`)
}
})