Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #5 from Katoka/master

Changed how params are passed
  • Loading branch information...
commit 5cf0f911f64e47821f34be70e85e839cbb85a85a 2 parents c5b0260 + 04ac24d
@xonecas authored
Showing with 112 additions and 62 deletions.
  1. +1 −0  .gitignore
  2. +83 −62 ffmpeg-node.js
  3. +25 −0 helpers.js
  4. +3 −0  package.json
View
1  .gitignore
@@ -0,0 +1 @@
+node_modules
View
145 ffmpeg-node.js
@@ -5,15 +5,16 @@
*/
var path = require('path'),
- spawn = require('child_process').spawn,
+ _ = require('underscore'),
+ child_process = require('child_process'),
+ spawn = child_process.spawn,
+ helpers = require('./helpers.js'),
that = this,
- queue = [],
- maxActive = 5, // Maximum of active FFMpeg jobs
+ queue = exports.queue = [],
+ maxActive = 4, // 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);
@@ -24,29 +25,24 @@ function push (job) {
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 ffmpeg with the specified flags and returns the output
* to the callback function.
*
* Parameters:
- * params - an array of ffmpeg options, ex: ['-i','./test.3gp']
+ * params - an object of ffmpeg options, ex: {'-i': './test.3gp', '-vpre': ['slow', 'baseline'], '-vcodec': 'libx264'}
* callback - a function to call when ffmpeg 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 = '',
@@ -66,8 +62,13 @@ exports.exec = function (params, callback) {
next();
});
+ } else {
+ if (params instanceof Array && params.length <= 2) {
+ console.log('Params to short for ffmpeg');
+ active--;
+ next();
+ }
}
-
};
@@ -79,7 +80,7 @@ exports.exec = function (params, callback) {
* 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 ffmpeg options to be added to the predefined ones (optional).
+ * params - an object 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) { ... }
@@ -88,11 +89,13 @@ exports.exec = function (params, callback) {
exports.convert = function (/* overloaded */) {
- var type = arguments[0], file = arguments[1],
- params = [], output = '', callback = false;
+ var type = arguments[0],
+ file = arguments[1],
+ params = {}, output = '',
+ callback = false;
if (arguments.length === 3) {
- params = [],
+ params = {},
output = path.dirname(file) +'/'+ path.basename(
file, path.extname(file)) +'.'+ type,
callback = arguments[2];
@@ -100,7 +103,7 @@ exports.convert = function (/* overloaded */) {
else if (arguments.length > 3) {
var err = false;
- if (arguments[2] instanceof Array &&
+ if (arguments[2] instanceof Object &&
typeof arguments[3] === 'string' &&
arguments[4] instanceof Function) {
@@ -108,7 +111,7 @@ exports.convert = function (/* overloaded */) {
output = arguments[3];
callback = arguments[4];
}
- else if (arguments[2] instanceof Array &&
+ else if (arguments[2] instanceof Object &&
arguments[3] instanceof Function) {
params = arguments[2];
@@ -129,64 +132,66 @@ exports.convert = function (/* overloaded */) {
else
throw new Error('Not enough arguments');
+
+ // Here the params array should be modified into an object which can be extend
switch(type) {
case 'mp4':
- params = [
- '-i', file,
- '-acodec', 'libfaac',
- '-ab', '128k',
- '-ar', '44100',
- '-vcodec', 'libx264',
- '-vpre', 'slow',
- '-vpre', 'baseline',
- '-r', '25',
- '-y', output
- ].concat(params);
+ params = helpers.objectToArray(_({
+ '-i': file,
+ '-acodec': 'aac',
+ '-ab': '128k',
+ '-ar': '44100',
+ '-vcodec': 'libx264',
+ '-vpre': ['slow','baseline'],
+ '-r': '25',
+ '-y': output
+ }).extend(params));
break;
case 'ogg':
- params = [
- '-i', file,
- '-acodec', 'libvorbis',
- '-ab', '128k',
- '-ar', '44100',
- '-vcodec', 'libtheora',
- '-r', '25',
- '-y', output
- ].concat(params);
+ params = helpers.objectToArray(_({
+ '-i': file,
+ '-acodec': 'libvorbis',
+ '-ab': '128k',
+ '-ar': '44100',
+ '-vcodec': 'libtheora',
+ '-r': '25',
+ '-y': output
+ }).extend(params));
break;
case 'webm':
- params = [
- '-i', file,
- '-acodec', 'libvorbis',
- '-ab', '128k',
- '-ar', '44100',
- '-vcodec', 'libvpx',
- '-b', '614400',
- '-aspect', '16:9',
- '-y', output
- ].concat(params);
+ params = helpers.objectToArray(_({
+ '-i': file,
+ '-acodec': 'libvorbis',
+ '-ab': '128k',
+ '-ar': '44100',
+ '-vcodec': 'libvpx',
+ '-b': '614400',
+ '-aspect': '16:9',
+ '-y': output
+ }).extend(params));
break;
case 'mp3':
- params = [
- '-i', file,
- '-acodec', 'libmp3lame',
- '-ab', '128k',
- '-ar', '44100',
- '-y', output
- ].concat(params);
+ params = helpers.objectToArray(_({
+ '-i': file,
+ '-acodec': 'libmp3lame',
+ '-ab': '128k',
+ '-ar': '44100',
+ '-y': output
+ }).extend(params));
break;
case 'm4a':
- params = [
- '-i', file,
- '-acodec', 'aac',
- '-ab', '64k',
- '-ar', '44100',
- '-y', output
- ].concat(params);
+ params = helpers.objectToArray(_({
+ '-i': file,
+ '-acodec': 'aac',
+ '-ab': '64k',
+ '-ar': '44100',
+ '-strict': '-2',
+ '-y': output
+ }).extend(params));
break;
}
@@ -253,3 +258,19 @@ exports.m4a = function (/* overloaded */) {
};
+/*
+* The function takes a absolute file path and returns a meta data object
+* @params {String} input
+* @params {Callback} callback function(error, jsonMetaObject)
+*/
+
+exports.getMetadata = function (input, callback) {
+ child_process.exec("ffprobe -loglevel error -show_format -show_streams " + input + " -print_format json", function (error, stdout, stderr) {
+ try {
+ stdout = JSON.parse(stdout);
+ callback(error, stdout);
+ } catch (e) {
+ callback(error, stdout);
+ }
+ });
+};
View
25 helpers.js
@@ -0,0 +1,25 @@
+/*
+* @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
+* @example
+* objectToArray({'-vpre': ['slow', 'baseline'], '-vcodec': 'libx264'})
+* returns ['-vpre','slow','-vpre','baseline','-vcodec','libx264']
+* @param {Object} obj
+* @returns {Array} arr
+*/
+
+var objectToArray = exports.objectToArray = function (obj) {
+ var arr = [], key;
+
+ for (key in obj) {
+ var objectValue = obj[key];
+ if(objectValue instanceof Array) {
+ objectValue.forEach(function (elm, i) {
+ arr.push(key, elm);
+ });
+ } else {
+ arr.push(key, objectValue);
+ }
+ }
+
+ return arr;
+};
View
3  package.json
@@ -13,6 +13,9 @@
"email" : "seancaetanomartin@gmail.com",
"url" : "http://www.xonecas.com/"
},
+ "dependencies": {
+ "underscore": ">= 1.3.3"
+ },
"engines": {
"node": "*"
}
Please sign in to comment.
Something went wrong with that request. Please try again.