-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvideotranscoder.js
109 lines (104 loc) · 3.04 KB
/
videotranscoder.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
var queue = require('./app/server/modules/queue'),
config = require('getconfig'),
Footage = require('./app/server/models/footage'),
Filehandler = require('./app/server/modules/filehandler'),
_ = require('lodash'),
mongoose = require('mongoose');
mongoose.connect(config.mongo);
mongoose.connection.on('error', function(error) {
console.log('MONGOOSE ERROR', error);
});
mongoose.connection.once('open', function() {
mongoose.set('debug', true);
console.log(config.mongo);
});
queue.connect(function(err) {
if (err) throw err;
processQueue();
});
var processQueue = function() {
queue.get(function(err, job) {
if (err) throw err;
if (job) {
handleJob(job, processQueue);
} else {
setTimeout(processQueue, 1000);
}
});
};
var handleJob = function(job, next) {
switch(job.payload.type) {
case 'thumbnail':
thumbnail(job, function(err, job) {
queue.remove(job.ack, function(err) {
if (err) throw err;
console.log('thumbnail job finished', job.id);
next();
});
});
break;
case 'transcode':
handleFile(job, function(err, job) {
if (err) throw err;
if (job) {
queue.remove(job.ack, function(err) {
if (err) throw err;
console.log('transcode job finished', job.id);
next();
});
}
});
break;
default:
console.log('Unknown type:', job.payload.type);
setTimeout(next, 1000);
}
};
var handleFile = function(job, next) {
var interval = setInterval(function() {
queue.ping(job.ack, function(err) {
if (err) console.log(err);
});
}, 5000);
Filehandler.info(job.payload.file, function(err, metadata) {
Footage.findByIdAndUpdate(job.payload.footage, {$set: {
'file.metadata': metadata,
'file.duration': metadata.format.duration
}}, function (err) {
if (err) throw err;
Filehandler.transcode(job.payload.file, function(err) {
if (err) throw err;
Footage.findByIdAndUpdate(job.payload.footage, {$set: {'file.status.transcoded': true}}, function (err) {
if (err) throw err;
Filehandler.mobileVersion(job.payload.file, function(err) {
if (err) throw err;
Footage.findByIdAndUpdate(job.payload.footage, {$set: {'file.status.mobile': true}}, function (err) {
if (err) throw err;
clearInterval(interval);
next(null, job);
});
});
});
});
});
});
};
var thumbnail = function(job, next) {
var interval = setInterval(function() {
queue.ping(job.ack, function(err) {
if (err) console.log(err);
});
}, 5000);
Filehandler.createThumbnails(job.payload.file, function(err, result) {
if (err) throw err;
var previews = result;
if (!_.isArray(result)) {
previews = [result];
}
Footage.findByIdAndUpdate(job.payload.footage, {$set: {'file.previews': previews, 'file.status.preview': true}}, {}, function (err) {
if (err) throw err;
clearInterval(interval);
next(null, job);
});
});
};