xonecas.io
311
Fn

ffmpeg-node / commit 5cf0f91

Merge pull request #5 from Katoka/master

Changed how params are passed

Sean Caetano Martin2012-08-23 5cf0f911f64e47821f34be70e85e839cbb85a85a parent c5b0260parent 04ac24d

4 files changed

.gitignore+1−0
@@ -0,0 +1 @@
1+node_modules
ffmpeg-node.js+83−62
@@ -5,15 +5,16 @@
55 */
66
77 var path = require('path'),
8- spawn = require('child_process').spawn,
8+ _ = require('underscore'),
9+ child_process = require('child_process'),
10+ spawn = child_process.spawn,
11+ helpers = require('./helpers.js'),
912 that = this,
10- queue = [],
11- maxActive = 5, // Maximum of active FFMpeg jobs
13+ queue = exports.queue = [],
14+ maxActive = 4, // Maximum of active FFMpeg jobs
1215 active = 0;
1316
1417
15-// The queue is limited to a max. of 5 active ffmpeg processes.
16-
1718
1819 function push (job) {
1920 queue.push(job);
@@ -24,29 +25,24 @@ function push (job) {
2425
2526 function next () {
2627 if(queue.length > 0 && active < maxActive) {
27- console.log(queue[0]);
2828 that.exec(queue[0].params, queue[0].callback);
2929 active++;
3030 queue.shift();
3131 }
3232 }
3333
34-
35-
3634 /**
3735 * Description:
3836 * calls ffmpeg with the specified flags and returns the output
3937 * to the callback function.
4038 *
4139 * Parameters:
42- * params - an array of ffmpeg options, ex: ['-i','./test.3gp']
40+ * params - an object of ffmpeg options, ex: {'-i': './test.3gp', '-vpre': ['slow', 'baseline'], '-vcodec': 'libx264'}
4341 * callback - a function to call when ffmpeg is done, ex:
4442 * function (stderr, stdout, exitCode) { ... }
4543 */
4644
4745 exports.exec = function (params, callback) {
48- console.log(params, callback);
49-
5046 if (params instanceof Array && params.length > 2) {
5147
5248 var stderr = '', stdout = '',
@@ -66,8 +62,13 @@ exports.exec = function (params, callback) {
6662 next();
6763 });
6864
65+ } else {
66+ if (params instanceof Array && params.length <= 2) {
67+ console.log('Params to short for ffmpeg');
68+ active--;
69+ next();
70+ }
6971 }
70-
7172 };
7273
7374
@@ -79,7 +80,7 @@ exports.exec = function (params, callback) {
7980 * Parameters:
8081 * type - one of 'mp4', 'ogg', 'webm', 'mp3', 'm4a' as a string.
8182 * file - path/to/the/inputFile.ext as a string.
82- * params - an array of ffmpeg options to be added to the predefined ones (optional).
83+ * params - an object of ffmpeg options to be added to the predefined ones (optional).
8384 * output - path/to/the/outputFile.ext as a string (optional).
8485 * callback - function to call when ffmpeg is done, ex:
8586 * function (stderr, stdout, exitCode) { ... }
@@ -88,11 +89,13 @@ exports.exec = function (params, callback) {
8889
8990 exports.convert = function (/* overloaded */) {
9091
91- var type = arguments[0], file = arguments[1],
92- params = [], output = '', callback = false;
92+ var type = arguments[0],
93+ file = arguments[1],
94+ params = {}, output = '',
95+ callback = false;
9396
9497 if (arguments.length === 3) {
95- params = [],
98+ params = {},
9699 output = path.dirname(file) +'/'+ path.basename(
97100 file, path.extname(file)) +'.'+ type,
98101 callback = arguments[2];
@@ -100,7 +103,7 @@ exports.convert = function (/* overloaded */) {
100103 else if (arguments.length > 3) {
101104 var err = false;
102105
103- if (arguments[2] instanceof Array &&
106+ if (arguments[2] instanceof Object &&
104107 typeof arguments[3] === 'string' &&
105108 arguments[4] instanceof Function) {
106109
@@ -108,7 +111,7 @@ exports.convert = function (/* overloaded */) {
108111 output = arguments[3];
109112 callback = arguments[4];
110113 }
111- else if (arguments[2] instanceof Array &&
114+ else if (arguments[2] instanceof Object &&
112115 arguments[3] instanceof Function) {
113116
114117 params = arguments[2];
@@ -129,64 +132,66 @@ exports.convert = function (/* overloaded */) {
129132 else
130133 throw new Error('Not enough arguments');
131134
135+
136+ // Here the params array should be modified into an object which can be extend
132137 switch(type) {
133138 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);
139+ params = helpers.objectToArray(_({
140+ '-i': file,
141+ '-acodec': 'aac',
142+ '-ab': '128k',
143+ '-ar': '44100',
144+ '-vcodec': 'libx264',
145+ '-vpre': ['slow','baseline'],
146+ '-r': '25',
147+ '-y': output
148+ }).extend(params));
145149 break;
146150
147151 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);
152+ params = helpers.objectToArray(_({
153+ '-i': file,
154+ '-acodec': 'libvorbis',
155+ '-ab': '128k',
156+ '-ar': '44100',
157+ '-vcodec': 'libtheora',
158+ '-r': '25',
159+ '-y': output
160+ }).extend(params));
157161 break;
158162
159163 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);
164+ params = helpers.objectToArray(_({
165+ '-i': file,
166+ '-acodec': 'libvorbis',
167+ '-ab': '128k',
168+ '-ar': '44100',
169+ '-vcodec': 'libvpx',
170+ '-b': '614400',
171+ '-aspect': '16:9',
172+ '-y': output
173+ }).extend(params));
170174 break;
171175
172176 case 'mp3':
173- params = [
174- '-i', file,
175- '-acodec', 'libmp3lame',
176- '-ab', '128k',
177- '-ar', '44100',
178- '-y', output
179- ].concat(params);
177+ params = helpers.objectToArray(_({
178+ '-i': file,
179+ '-acodec': 'libmp3lame',
180+ '-ab': '128k',
181+ '-ar': '44100',
182+ '-y': output
183+ }).extend(params));
180184 break;
181185
182186 case 'm4a':
183- params = [
184- '-i', file,
185- '-acodec', 'aac',
186- '-ab', '64k',
187- '-ar', '44100',
188- '-y', output
189- ].concat(params);
187+ params = helpers.objectToArray(_({
188+ '-i': file,
189+ '-acodec': 'aac',
190+ '-ab': '64k',
191+ '-ar': '44100',
192+ '-strict': '-2',
193+ '-y': output
194+ }).extend(params));
190195 break;
191196 }
192197
@@ -253,3 +258,19 @@ exports.m4a = function (/* overloaded */) {
253258
254259 };
255260
261+/*
262+* The function takes a absolute file path and returns a meta data object
263+* @params {String} input
264+* @params {Callback} callback function(error, jsonMetaObject)
265+*/
266+
267+exports.getMetadata = function (input, callback) {
268+ child_process.exec("ffprobe -loglevel error -show_format -show_streams " + input + " -print_format json", function (error, stdout, stderr) {
269+ try {
270+ stdout = JSON.parse(stdout);
271+ callback(error, stdout);
272+ } catch (e) {
273+ callback(error, stdout);
274+ }
275+ });
276+};
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 }