-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
198 lines (170 loc) · 5.5 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
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
/**
MeDroid - Multimedia Transcoder Service.
The following input formats are supported:
images (all image formats supported by imagegraphics)
videos (all video formats supported by ffmpeg/avconv)
pdf
(c) 2013 OptimalBits - MIT Licensed.
*/
"use strict";
var Redis = require("ioredis"),
Queue = require("bull"),
util = require("util");
var TranscoderConfig = require("./presets.js");
//
// Returns the channel for informing on job progress.
//
function progressChannel(clientId) {
return "medroid:" + clientId + ":progress:";
}
//------------------------------------------------------------------------------
//
// Medroid Client
// opts: {
// redis: {
// port: 6379,
// host: localhost
// opts: {}
// }
// }
//------------------------------------------------------------------------------
var Medroid = function Medroid(opts, queueName) {
if (!this) {
return new Medroid(opts, queueName);
}
queueName = queueName || "Medroid";
this.clientId = opts.clientId || "default";
this.jobQueue = Queue(queueName, opts);
this.jobQueue.on("error", (err) => {
console.error("Job Queue Error", err);
});
this.resultsQueueName = "Medroid-client-" + this.clientId;
this.resultsQueue = Queue(this.resultsQueueName, opts);
this.resultsQueue.on("error", (err) => {
console.error("Results Queue Error", err);
});
// Open a sub channel with redis for receiving progress events.
this.subClient = new Redis(opts.redis);
var channel = progressChannel(this.clientId);
this.subClient.subscribe(channel);
var _this = this;
this.subClient.on("message", function (channel, msg) {
var data = JSON.parse(msg);
_this.emit("progress", data.args, data.progress);
});
this.receiveResult(function (job, done) {
try {
if (job.data.result.err) {
var err = Error(job.data.result.err.message);
err.stack = job.data.result.err.stack;
_this.emit("failed", job.data.jobData, err);
} else {
_this.emit("completed", job.data.jobData, job.data.result);
}
done();
} catch (err) {
_this.emit("failed", job.data.jobData, err);
done(err);
}
});
};
util.inherits(Medroid, require("events").EventEmitter);
Medroid.prototype.transcode = function (mimeType, args) {
// TODO: Check if supported mimeType
var mime = mimeType.split("/");
switch (mime[0]) {
case "video":
args.outputPath = args.outputPath + ".mp4"; // Hack
if (args.previewPath) {
args.previewPath += ".mp4";
}
return this.transcodeVideo(args);
case "image":
args.outputPath = args.outputPath + ".jpg"; // Hack
if (args.previewPath) {
args.previewPath += ".jpg";
}
return this.transcodeImage(args);
case "audio":
args.outputPath = args.outputPath + ".mp3"; // Hack
return this.transcodeAudio(args);
case "application":
switch (mime[1]) {
case "vnd.openxmlformats-officedocument.presentationml.presentation":
case "vnd.ms-powerpoint":
args.outputPath = args.outputPath + ".mp4"; // Hack
if (args.previewPath) {
args.previewPath += ".mp4";
}
return this.transcodePPT(args);
case "pdf":
args.outputPath = args.outputPath + ".mp4"; // Hack
if (args.previewPath) {
args.previewPath += ".mp4";
}
return this.transcodePDF(args);
case "zip":
return this.transcodeZip(args);
}
}
};
Medroid.prototype.transcodeZip = function (opts) {
return this._addJob("Zip", undefined, opts);
};
Medroid.prototype.transcodeVideo = function (opts) {
var preset = opts.presets
? opts.presets.video
: opts.account.plan.quota.videoPreset;
var config = TranscoderConfig.video[preset];
config.preview = TranscoderConfig.video["320p"];
return this._addJob("Video", config, opts);
};
Medroid.prototype.transcodeImage = function (opts) {
var preset = opts.presets
? opts.presets.image
: opts.account.plan.quota.imagePreset;
var config = TranscoderConfig.image[preset];
config.preview = TranscoderConfig.image["320p"];
return this._addJob("Image", config, opts);
};
Medroid.prototype.transcodeAudio = function (opts) {
var preset = opts.presets
? opts.presets.audio
: opts.account.plan.quota.audioPreset || "high";
return this._addJob("Audio", TranscoderConfig.audio[preset], opts);
};
Medroid.prototype.transcodePPT = function (opts) {
var preset = opts.presets
? opts.presets.ppt
: opts.account.plan.quota.pptPreset;
var config = TranscoderConfig.video[preset];
config.preview = TranscoderConfig.video["320p"];
return this._addJob("Powerpoint", config, opts);
};
Medroid.prototype.transcodePDF = function (opts) {
var preset = opts.presets
? opts.presets.pdf
: opts.account.plan.quota.pdfPreset;
var config = TranscoderConfig.video[preset];
config.preview = TranscoderConfig.video["320p"];
return this._addJob("PDF", config, opts);
};
Medroid.prototype._addJob = function (mediaType, config, args) {
var data = {
args: args,
config: config,
mediaType: mediaType,
clientId: this.clientId,
resultsQueueName: this.resultsQueueName,
};
return this.jobQueue.add(data);
};
Medroid.prototype.receiveResult = function (cb) {
//
// We may need to ignore certain responses here some massage the results
// into something suitable for clients.
//
this.resultsQueue.process(cb);
};
//------------------------------------------------------------------------------
module.exports = Medroid;