-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathffmpeg-node.js
276 lines (220 loc) · 6.8 KB
/
ffmpeg-node.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
/**
* Module to drive ffmpeg video enconding library with shortcuts
* for web video. Requires a ffmpeg compiled with support for mp4/ogg/webm.
*/
var path = require('path'),
_ = require('underscore'),
child_process = require('child_process'),
spawn = child_process.spawn,
helpers = require('./helpers.js'),
that = this,
queue = exports.queue = [],
maxActive = 4, // Maximum of active FFMpeg jobs
active = 0;
function push (job) {
queue.push(job);
if(active < maxActive) {
next();
}
}
function next () {
if(queue.length > 0 && active < maxActive) {
that.exec(queue[0].params, queue[0].callback);
active++;
queue.shift();
}
}
/**
* Description:
* calls ffmpeg with the specified flags and returns the output
* to the callback function.
*
* Parameters:
* params - an array of ffmpeg options, ex, ['-i', './test.3gp', '-vpre', 'slow', '-vpre', 'baseline', '-vcodec', 'libx264']
* callback - a function to call when ffmpeg is done, ex:
* function (stderr, stdout, exitCode) { ... }
*/
exports.exec = function (params, callback) {
if (params instanceof Array && params.length > 2) {
var stderr = '', stdout = '',
ffmpeg = spawn('ffmpeg', params);
ffmpeg.stderr.on('data', function (err) {
stderr += err;
});
ffmpeg.stdout.on('data', function (output) {
stdout += output;
});
ffmpeg.on('exit', function (code) {
callback(stderr, stdout, code);
active--;
next();
});
} else {
if (params instanceof Array && params.length <= 2) {
console.log('Params to short for ffmpeg');
active--;
next();
}
}
};
/**
* Description:
* serves as middle man for convenience method, required to
* avoid code repetition.
*
* Parameters:
* type - one of 'mp4', 'ogg', 'webm', 'mp3', 'm4a' as a string.
* file - path/to/the/inputFile.ext as a string.
* params - an object of ffmpeg options to be added to the predefined ones (optional).
* output - path/to/the/outputFile.ext as a string (optional).
* callback - function to call when ffmpeg is done, ex:
* function (stderr, stdout, exitCode) { ... }
*/
exports.convert = function (/* overloaded */) {
var type = arguments[0],
file = arguments[1],
params = {}, output = '',
callback = false;
if (arguments.length === 3) {
params = {},
output = path.dirname(file) +'/'+ path.basename(
file, path.extname(file)) +'.'+ type,
callback = arguments[2];
}
else if (arguments.length > 3) {
var err = false;
if (arguments[2] instanceof Object &&
typeof arguments[3] === 'string' &&
arguments[4] instanceof Function) {
params = arguments[2];
output = arguments[3];
callback = arguments[4];
}
else if (arguments[2] instanceof Object &&
arguments[3] instanceof Function) {
params = arguments[2];
callback = arguments[3];
}
else if (typeof arguments[2] === string &&
arguments[3] instanceof Function) {
output = arguments[2];
callback = arguments[3];
}
else if (arguments[2] instanceof Function)
callback = arguments[2];
else
throw new Error("Couldn't parse arguments");
}
else
throw new Error('Not enough arguments');
// Here the params array should be modified into an object which can be extend
switch(type) {
case 'mp4':
params = helpers.objectToArray(_({
'-i': file,
'-acodec': 'aac',
'-ab': '128k',
'-ar': '44100',
'-vcodec': 'libx264',
'-vpre': ['slow','baseline'],
'-r': '25',
'-y': output
}).extend(params));
break;
case 'ogg':
params = helpers.objectToArray(_({
'-i': file,
'-acodec': 'libvorbis',
'-ab': '128k',
'-ar': '44100',
'-vcodec': 'libtheora',
'-r': '25',
'-y': output
}).extend(params));
break;
case 'webm':
params = helpers.objectToArray(_({
'-i': file,
'-acodec': 'libvorbis',
'-ab': '128k',
'-ar': '44100',
'-vcodec': 'libvpx',
'-b': '614400',
'-aspect': '16:9',
'-y': output
}).extend(params));
break;
case 'mp3':
params = helpers.objectToArray(_({
'-i': file,
'-acodec': 'libmp3lame',
'-ab': '128k',
'-ar': '44100',
'-y': output
}).extend(params));
break;
case 'm4a':
params = helpers.objectToArray(_({
'-i': file,
'-acodec': 'aac',
'-ab': '64k',
'-ar': '44100',
'-strict': '-2',
'-y': output
}).extend(params));
break;
}
push({params: params, callback: callback});
};
/**
* Description:
* Convenience methods to convert to popular web formats (flash/html5)
* If you know how to improve these default options, let me know.
*
* Parameters:
* file - path/to/the/inputFile.ext as a string.
* params - an array of ffmpeg options to be added to the predefined ones (optional).
* output - path/to/the/outputFile.ext as a string (optional).
* callback - function to call when ffmpeg is done, ex:
* function (stderr, stdout, exitCode) { ... }
*/
exports.mp4 = function (/* overloaded */) {
var unshift = Array.prototype.unshift;
unshift.call(arguments, 'mp4');
this.convert.apply(this, arguments);
};
exports.ogg = function (/* overloaded */) {
var unshift = Array.prototype.unshift;
unshift.call(arguments, 'ogg');
this.convert.apply(this, arguments);
};
exports.webm = function (/* overloaded */) {
var unshift = Array.prototype.unshift;
unshift.call(arguments, 'webm');
this.convert.apply(this, arguments);
};
exports.mp3 = function (/* overloaded */) {
var unshift = Array.prototype.unshift;
unshift.call(arguments, 'mp3');
this.convert.apply(this, arguments);
};
exports.m4a = function (/* overloaded */) {
var unshift = Array.prototype.unshift;
unshift.call(arguments, 'm4a');
this.convert.apply(this, arguments);
};
/*
* The function takes a absolute file path and returns a meta data object
* @params {String} input
* @params {Callback} callback function(error, jsonMetaObject)
*/
exports.getMetadata = function (input, callback) {
child_process.exec("ffprobe -loglevel error -show_format -show_streams " + input + " -print_format json", function (error, stdout, stderr) {
try {
stdout = JSON.parse(stdout);
callback(error, stdout);
} catch (e) {
callback(error, stdout);
}
});
};