xonecas.io
311
Fn

ffmpeg-node / commit 8f5aba9

Added queueing functionalty.

Taner Topal2012-04-25 8f5aba9a3f44da08fecbeeac80710f3b87ed0a04 parent 0ac25e5

1 file changed

ffmpeg-node.js+84−15
@@ -1,11 +1,37 @@
11
22 /**
3- * Module to drive ffmpeg video enconding library with shortcuts
3+ * Module to drive ffmpeg video enconding library with shortcuts
44 * for web video. Requires a ffmpeg compiled with support for mp4/ogg/webm.
55 */
66
77 var path = require('path'),
8- spawn = require('child_process').spawn;
8+ spawn = require('child_process').spawn,
9+ that = this,
10+ queue = [],
11+ maxActive = 5, // Maximum of active FFMpeg jobs
12+ active = 0;
13+
14+
15+// The queue is limited to a max. of 5 active ffmpeg processes.
16+
17+
18+function push (job) {
19+ queue.push(job);
20+ if(active < maxActive) {
21+ next();
22+ }
23+}
24+
25+function next () {
26+ if(queue.length > 0 && active < maxActive) {
27+ console.log(queue[0]);
28+ that.exec(queue[0].params, queue[0].callback);
29+ active++;
30+ queue.shift();
31+ }
32+}
33+
34+
935
1036 /**
1137 * Description:
@@ -19,10 +45,11 @@ var path = require('path'),
1945 */
2046
2147 exports.exec = function (params, callback) {
22-
48+ console.log(params, callback);
49+
2350 if (params instanceof Array && params.length > 2) {
2451
25- var stderr = '', stdout = '',
52+ var stderr = '', stdout = '',
2653 ffmpeg = spawn('ffmpeg', params);
2754
2855 ffmpeg.stderr.on('data', function (err) {
@@ -35,11 +62,13 @@ exports.exec = function (params, callback) {
3562
3663 ffmpeg.on('exit', function (code) {
3764 callback(stderr, stdout, code);
65+ active--;
66+ next();
3867 });
3968
4069 }
4170
42-}
71+};
4372
4473
4574 /**
@@ -48,7 +77,7 @@ exports.exec = function (params, callback) {
4877 * avoid code repetition.
4978 *
5079 * Parameters:
51- * type - one of 'mp4', 'ogg', 'webm' as a string.
80+ * type - one of 'mp4', 'ogg', 'webm', 'mp3', 'm4a' as a string.
5281 * file - path/to/the/inputFile.ext as a string.
5382 * params - an array of ffmpeg options to be added to the predefined ones (optional).
5483 * output - path/to/the/outputFile.ext as a string (optional).
@@ -56,9 +85,10 @@ exports.exec = function (params, callback) {
5685 * function (stderr, stdout, exitCode) { ... }
5786 */
5887
88+
5989 exports.convert = function (/* overloaded */) {
6090
61- var type = arguments[0], file = arguments[1],
91+ var type = arguments[0], file = arguments[1],
6292 params = [], output = '', callback = false;
6393
6494 if (arguments.length === 3) {
@@ -105,7 +135,7 @@ exports.convert = function (/* overloaded */) {
105135 '-i', file,
106136 '-acodec', 'libfaac',
107137 '-ab', '128k',
108- '-ar', '41000',
138+ '-ar', '44100',
109139 '-vcodec', 'libx264',
110140 '-vpre', 'slow',
111141 '-vpre', 'baseline',
@@ -119,7 +149,7 @@ exports.convert = function (/* overloaded */) {
119149 '-i', file,
120150 '-acodec', 'libvorbis',
121151 '-ab', '128k',
122- '-ar', '41000',
152+ '-ar', '44100',
123153 '-vcodec', 'libtheora',
124154 '-r', '25',
125155 '-y', output
@@ -131,18 +161,39 @@ exports.convert = function (/* overloaded */) {
131161 '-i', file,
132162 '-acodec', 'libvorbis',
133163 '-ab', '128k',
134- '-ar', '41000',
164+ '-ar', '44100',
135165 '-vcodec', 'libvpx',
136166 '-b', '614400',
137167 '-aspect', '16:9',
138168 '-y', output
139169 ].concat(params);
140170 break;
171+
172+ case 'mp3':
173+ params = [
174+ '-i', file,
175+ '-acodec', 'libmp3lame',
176+ '-ab', '128k',
177+ '-ar', '44100',
178+ '-y', output
179+ ].concat(params);
180+ break;
181+
182+ case 'm4a':
183+ params = [
184+ '-i', file,
185+ '-acodec', 'aac',
186+ '-ab', '64k',
187+ '-ar', '44100',
188+ '-y', output
189+ ].concat(params);
190+ break;
141191 }
142192
143- this.exec(params, callback);
193+
194+ push({params: params, callback: callback});
144195
145-}
196+};
146197
147198 /**
148199 * Description:
@@ -164,7 +215,7 @@ exports.mp4 = function (/* overloaded */) {
164215
165216 this.convert.apply(this, arguments);
166217
167-}
218+};
168219
169220 exports.ogg = function (/* overloaded */) {
170221
@@ -173,7 +224,7 @@ exports.ogg = function (/* overloaded */) {
173224
174225 this.convert.apply(this, arguments);
175226
176-}
227+};
177228
178229 exports.webm = function (/* overloaded */) {
179230
@@ -182,5 +233,23 @@ exports.webm = function (/* overloaded */) {
182233
183234 this.convert.apply(this, arguments);
184235
185-}
236+};
237+
238+exports.mp3 = function (/* overloaded */) {
239+
240+ var unshift = Array.prototype.unshift;
241+ unshift.call(arguments, 'mp3');
242+
243+ this.convert.apply(this, arguments);
244+
245+};
246+
247+exports.m4a = function (/* overloaded */) {
248+
249+ var unshift = Array.prototype.unshift;
250+ unshift.call(arguments, 'm4a');
251+
252+ this.convert.apply(this, arguments);
253+
254+};