Permalink
Browse files

Added queueing functionalty.

  • Loading branch information...
1 parent 0ac25e5 commit 8f5aba9a3f44da08fecbeeac80710f3b87ed0a04 Taner Topal committed Apr 25, 2012
Showing with 84 additions and 15 deletions.
  1. +84 −15 ffmpeg-node.js
View
99 ffmpeg-node.js
@@ -1,11 +1,37 @@
/**
- * Module to drive ffmpeg video enconding library with shortcuts
+ * 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'),
- spawn = require('child_process').spawn;
+ spawn = require('child_process').spawn,
+ that = this,
+ queue = [],
+ maxActive = 5, // Maximum of active FFMpeg jobs
+ active = 0;
+
+
+// The queue is limited to a max. of 5 active ffmpeg 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:
@@ -19,10 +45,11 @@ var path = require('path'),
*/
exports.exec = function (params, callback) {
-
+ console.log(params, callback);
+
if (params instanceof Array && params.length > 2) {
- var stderr = '', stdout = '',
+ var stderr = '', stdout = '',
ffmpeg = spawn('ffmpeg', params);
ffmpeg.stderr.on('data', function (err) {
@@ -35,11 +62,13 @@ exports.exec = function (params, callback) {
ffmpeg.on('exit', function (code) {
callback(stderr, stdout, code);
+ active--;
+ next();
});
}
-}
+};
/**
@@ -48,17 +77,18 @@ exports.exec = function (params, callback) {
* avoid code repetition.
*
* Parameters:
- * type - one of 'mp4', 'ogg', 'webm' as a string.
+ * type - one of 'mp4', 'ogg', 'webm', 'mp3', 'm4a' as a string.
* 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.convert = function (/* overloaded */) {
- var type = arguments[0], file = arguments[1],
+ var type = arguments[0], file = arguments[1],
params = [], output = '', callback = false;
if (arguments.length === 3) {
@@ -105,7 +135,7 @@ exports.convert = function (/* overloaded */) {
'-i', file,
'-acodec', 'libfaac',
'-ab', '128k',
- '-ar', '41000',
+ '-ar', '44100',
'-vcodec', 'libx264',
'-vpre', 'slow',
'-vpre', 'baseline',
@@ -119,7 +149,7 @@ exports.convert = function (/* overloaded */) {
'-i', file,
'-acodec', 'libvorbis',
'-ab', '128k',
- '-ar', '41000',
+ '-ar', '44100',
'-vcodec', 'libtheora',
'-r', '25',
'-y', output
@@ -131,18 +161,39 @@ exports.convert = function (/* overloaded */) {
'-i', file,
'-acodec', 'libvorbis',
'-ab', '128k',
- '-ar', '41000',
+ '-ar', '44100',
'-vcodec', 'libvpx',
'-b', '614400',
'-aspect', '16:9',
'-y', output
].concat(params);
break;
+
+ case 'mp3':
+ params = [
+ '-i', file,
+ '-acodec', 'libmp3lame',
+ '-ab', '128k',
+ '-ar', '44100',
+ '-y', output
+ ].concat(params);
+ break;
+
+ case 'm4a':
+ params = [
+ '-i', file,
+ '-acodec', 'aac',
+ '-ab', '64k',
+ '-ar', '44100',
+ '-y', output
+ ].concat(params);
+ break;
}
- this.exec(params, callback);
+
+ push({params: params, callback: callback});
-}
+};
/**
* Description:
@@ -164,7 +215,7 @@ exports.mp4 = function (/* overloaded */) {
this.convert.apply(this, arguments);
-}
+};
exports.ogg = function (/* overloaded */) {
@@ -173,7 +224,7 @@ exports.ogg = function (/* overloaded */) {
this.convert.apply(this, arguments);
-}
+};
exports.webm = function (/* overloaded */) {
@@ -182,5 +233,23 @@ exports.webm = function (/* overloaded */) {
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);
+
+};

0 comments on commit 8f5aba9

Please sign in to comment.