forked from xonecas/ffmpeg-node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
avconv-node.js
250 lines (191 loc) · 5.7 KB
/
avconv-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
/**
* Module to drive avconv video enconding library with shortcuts
* for web video. Requires an avconv compiled with support for mp4/ogg/webm.
*/
var path = require('path'),
spawn = require('child_process').spawn,
that = this,
queue = [],
maxActive = 5, // Maximum of active avconv jobs
active = 0;
// The queue is limited to a max. of 5 active avconv processes.
function push (job) {
queue.push(job);
if(active < maxActive) {
next();
}
}
function next () {
if(queue.length > 0 && active < maxActive) {
console.log(queue[0]);
that.exec(queue[0].params, queue[0].callback);
active++;
queue.shift();
}
}
/**
* Description:
* calls avconv with the specified flags and returns the output
* to the callback function.
*
* Parameters:
* params - an array of avconv options, ex: ['-i','./test.3gp']
* callback - a function to call when avconv is done, ex:
* function (stderr, stdout, exitCode) { ... }
*/
exports.exec = function (params, callback) {
console.log(params, callback);
if (params instanceof Array && params.length > 2) {
var stderr = '', stdout = '',
avconv = spawn('avconv', params);
avconv.stderr.on('data', function (err) {
stderr += err;
});
avconv.stdout.on('data', function (output) {
stdout += output;
});
avconv.on('exit', function (code) {
callback(stderr, stdout, code);
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 array of avconv options to be added to the predefined ones (optional).
* output - path/to/the/outputFile.ext as a string (optional).
* callback - function to call when avconv 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 Array &&
typeof arguments[3] === 'string' &&
arguments[4] instanceof Function) {
params = arguments[2];
output = arguments[3];
callback = arguments[4];
}
else if (arguments[2] instanceof Array &&
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');
switch(type) {
case 'mp4':
params = [
'-i', file,
'-c:a', 'libvo_aacenc',
'-ab', '128k',
'-ar', '48k',
'-c:v', 'libx264',
'-tune', 'film',
'-preset', 'slow',
'-y', output
].concat(params);
break;
case 'ogg':
params = [
'-i', file,
'-c:a', 'libvorbis',
'-ab', '128k',
'-c:v', 'libtheora',
'-y', output
].concat(params);
break;
case 'webm':
params = [
'-i', file,
'-c:a', 'libvorbis',
'-ab', '128k',
'-c:v', 'libvpx',
'-deadline', 'best',
'-y', output
].concat(params);
break;
case 'mp3':
params = [
'-i', file,
'-c:a', 'libmp3lame',
'-ab', '128k',
'-ar', '48k',
'-y', output
].concat(params);
break;
case 'm4a':
params = [
'-i', file,
'-c:a', 'libvo_aacenc',
'-ab', '64k',
'-ar', '48k',
'-y', output
].concat(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 avconv options to be added to the predefined ones (optional).
* output - path/to/the/outputFile.ext as a string (optional).
* callback - function to call when avconv 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);
};