xonecas.io
311
Fn

ffmpeg-node / commit e838008

Changed params array into an object which extends the default params instead of concating

Taner Topal2012-07-26 e83800886abab644cc21cb25ff9cc5db0a3a3e16 parent c5b0260

4 files changed

.gitignore+1−0
@@ -0,0 +1 @@
1+node_modules
ffmpeg-node.js+65−59
@@ -5,10 +5,12 @@
55 */
66
77 var path = require('path'),
8+ _ = require('underscore'),
89 spawn = require('child_process').spawn,
10+ helpers = require('./helpers.js'),
911 that = this,
10- queue = [],
11- maxActive = 5, // Maximum of active FFMpeg jobs
12+ queue = exports.queue = [],
13+ maxActive = 4, // Maximum of active FFMpeg jobs
1214 active = 0;
1315
1416
@@ -24,29 +26,24 @@ function push (job) {
2426
2527 function next () {
2628 if(queue.length > 0 && active < maxActive) {
27- console.log(queue[0]);
2829 that.exec(queue[0].params, queue[0].callback);
2930 active++;
3031 queue.shift();
3132 }
3233 }
3334
34-
35-
3635 /**
3736 * Description:
3837 * calls ffmpeg with the specified flags and returns the output
3938 * to the callback function.
4039 *
4140 * Parameters:
42- * params - an array of ffmpeg options, ex: ['-i','./test.3gp']
41+ * params - an object of ffmpeg options, ex: {'-i': './test.3gp', '-vpre': ['slow', 'baseline'], '-vcodec': 'libx264'}
4342 * callback - a function to call when ffmpeg is done, ex:
4443 * function (stderr, stdout, exitCode) { ... }
4544 */
4645
4746 exports.exec = function (params, callback) {
48- console.log(params, callback);
49-
5047 if (params instanceof Array && params.length > 2) {
5148
5249 var stderr = '', stdout = '',
@@ -66,8 +63,13 @@ exports.exec = function (params, callback) {
6663 next();
6764 });
6865
66+ } else {
67+ if (params instanceof Array && params.length <= 2) {
68+ console.log('Params to short for ffmpeg');
69+ active--;
70+ next();
71+ }
6972 }
70-
7173 };
7274
7375
@@ -79,7 +81,7 @@ exports.exec = function (params, callback) {
7981 * Parameters:
8082 * type - one of 'mp4', 'ogg', 'webm', 'mp3', 'm4a' as a string.
8183 * file - path/to/the/inputFile.ext as a string.
82- * params - an array of ffmpeg options to be added to the predefined ones (optional).
84+ * params - an object of ffmpeg options to be added to the predefined ones (optional).
8385 * output - path/to/the/outputFile.ext as a string (optional).
8486 * callback - function to call when ffmpeg is done, ex:
8587 * function (stderr, stdout, exitCode) { ... }
@@ -88,11 +90,13 @@ exports.exec = function (params, callback) {
8890
8991 exports.convert = function (/* overloaded */) {
9092
91- var type = arguments[0], file = arguments[1],
92- params = [], output = '', callback = false;
93+ var type = arguments[0],
94+ file = arguments[1],
95+ params = {}, output = '',
96+ callback = false;
9397
9498 if (arguments.length === 3) {
95- params = [],
99+ params = {},
96100 output = path.dirname(file) +'/'+ path.basename(
97101 file, path.extname(file)) +'.'+ type,
98102 callback = arguments[2];
@@ -100,7 +104,7 @@ exports.convert = function (/* overloaded */) {
100104 else if (arguments.length > 3) {
101105 var err = false;
102106
103- if (arguments[2] instanceof Array &&
107+ if (arguments[2] instanceof Object &&
104108 typeof arguments[3] === 'string' &&
105109 arguments[4] instanceof Function) {
106110
@@ -108,7 +112,7 @@ exports.convert = function (/* overloaded */) {
108112 output = arguments[3];
109113 callback = arguments[4];
110114 }
111- else if (arguments[2] instanceof Array &&
115+ else if (arguments[2] instanceof Object &&
112116 arguments[3] instanceof Function) {
113117
114118 params = arguments[2];
@@ -129,64 +133,66 @@ exports.convert = function (/* overloaded */) {
129133 else
130134 throw new Error('Not enough arguments');
131135
136+
137+ // Here the params array should be modified into an object which can be extend
132138 switch(type) {
133139 case 'mp4':
134- params = [
135- '-i', file,
136- '-acodec', 'libfaac',
137- '-ab', '128k',
138- '-ar', '44100',
139- '-vcodec', 'libx264',
140- '-vpre', 'slow',
141- '-vpre', 'baseline',
142- '-r', '25',
143- '-y', output
144- ].concat(params);
140+ params = helpers.objectToArray(_({
141+ '-i': file,
142+ '-acodec': 'aac',
143+ '-ab': '128k',
144+ '-ar': '44100',
145+ '-vcodec': 'libx264',
146+ '-vpre': ['slow','baseline'],
147+ '-r': '25',
148+ '-y': output
149+ }).extend(params));
145150 break;
146151
147152 case 'ogg':
148- params = [
149- '-i', file,
150- '-acodec', 'libvorbis',
151- '-ab', '128k',
152- '-ar', '44100',
153- '-vcodec', 'libtheora',
154- '-r', '25',
155- '-y', output
156- ].concat(params);
153+ params = helpers.objectToArray(_({
154+ '-i': file,
155+ '-acodec': 'libvorbis',
156+ '-ab': '128k',
157+ '-ar': '44100',
158+ '-vcodec': 'libtheora',
159+ '-r': '25',
160+ '-y': output
161+ }).extend(params));
157162 break;
158163
159164 case 'webm':
160- params = [
161- '-i', file,
162- '-acodec', 'libvorbis',
163- '-ab', '128k',
164- '-ar', '44100',
165- '-vcodec', 'libvpx',
166- '-b', '614400',
167- '-aspect', '16:9',
168- '-y', output
169- ].concat(params);
165+ params = helpers.objectToArray(_({
166+ '-i': file,
167+ '-acodec': 'libvorbis',
168+ '-ab': '128k',
169+ '-ar': '44100',
170+ '-vcodec': 'libvpx',
171+ '-b': '614400',
172+ '-aspect': '16:9',
173+ '-y': output
174+ }).extend(params));
170175 break;
171176
172177 case 'mp3':
173- params = [
174- '-i', file,
175- '-acodec', 'libmp3lame',
176- '-ab', '128k',
177- '-ar', '44100',
178- '-y', output
179- ].concat(params);
178+ params = helpers.objectToArray(_({
179+ '-i': file,
180+ '-acodec': 'libmp3lame',
181+ '-ab': '128k',
182+ '-ar': '44100',
183+ '-y': output
184+ }).extend(params));
180185 break;
181186
182187 case 'm4a':
183- params = [
184- '-i', file,
185- '-acodec', 'aac',
186- '-ab', '64k',
187- '-ar', '44100',
188- '-y', output
189- ].concat(params);
188+ params = helpers.objectToArray(_({
189+ '-i': file,
190+ '-acodec': 'aac',
191+ '-ab': '64k',
192+ '-ar': '44100',
193+ '-strict': '-2',
194+ '-y': output
195+ }).extend(params));
190196 break;
191197 }
192198
helpers.js+25−0
@@ -0,0 +1,25 @@
1+/*
2+* @description Converts an object without nested object properties e.g. {'-key1': 'String1', '-key2': 'String2'} into an array like ['-key1','-String1','-key2','-String2']. If you need a key twice put the values into an array
3+* @example
4+* objectToArray({'-vpre': ['slow', 'baseline'], '-vcodec': 'libx264'})
5+* returns ['-vpre','slow','-vpre','baseline','-vcodec','libx264']
6+* @param {Object} obj
7+* @returns {Array} arr
8+*/
9+
10+var objectToArray = exports.objectToArray = function (obj) {
11+ var arr = [], key;
12+
13+ for (key in obj) {
14+ var objectValue = obj[key];
15+ if(objectValue instanceof Array) {
16+ objectValue.forEach(function (elm, i) {
17+ arr.push(key, elm);
18+ });
19+ } else {
20+ arr.push(key, objectValue);
21+ }
22+ }
23+
24+ return arr;
25+};
package.json+3−0
@@ -13,6 +13,9 @@
1313 "email" : "seancaetanomartin@gmail.com",
1414 "url" : "http://www.xonecas.com/"
1515 },
16+ "dependencies": {
17+ "underscore": ">= 1.3.3"
18+ },
1619 "engines": {
1720 "node": "*"
1821 }