var spawn = require('child_process').spawn;
//var fs = require('');

var ffmpegio = module.exports;

function exec(command, params, callback){
    var e = spawn(command, params);
    var stderr = '', stdout = '';

    e.stdout.on('data', function (data) {
        stdout += data;
    });

    e.stderr.on('data', function (err) {
        stderr += err;
    });

    e.on('close', function (code) {
        callback(stdout, stderr, code);
    });
};

function objectToArray (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;
};

//{input:'miVideo.mp4', format:'640x360', output:'miVideo.mp4'}
//ffmpeg -i miVideo.mp4 -vcodec libx264 -crf 30 -threads 0 -s 640x360 -acodec copy -y modificado.mp4
ffmpegio.changerQuality = function(json, callback){
    if(Object.keys(json).length === 3){
        var params = {};
        params = objectToArray({
            '-i': json.input,            
            '-vcodec': 'libx264',
            '-crf': json.quality || '30',
            '-threads': json.threads || '0',
            '-s': json.format,
            '-acodec': 'copy',
            '-y': json.output
        });
        exec('ffmpeg', params, function(stdout, stderr, code){
            console.log(stdout + stderr + code);
            if(code === 0){
                callback(true);
            }else{
                callback(false);
            }
        });
    }else{
         return callback('Cantidad de parametros no valido');
    }
};

//{input:'mivideo.mp4', output:'mihls.m3u8'}
//// ffmpeg -i mivideo.mp4 -y mihls.m3u8
ffmpegio.generateHls = function(json, callback){
    if(Object.keys(json).length === 2){
        var params = {};
        params = objectToArray({
            '-i': json.input,
            '-y': json.output
        });
        exec('ffmpeg', params, function(stdout, stderr, code){
            console.log(stdout + stderr + code);
            if(code === 0){
                callback(true);
            }else{
                callback(false);
            }
        });
    }else{
        return callback('Cantidad de parametros no valido');
    }
};

//EJEMPLO DE USO
//command
//params
//value -> resultado de la ejecución del comando
//exec('node', ['-v', '-v'], function(stdout, stderr, code){
   //console.log(stdout + stderr + code);    
//});

@@ -1666,35 +1666,33 @@
 llback('
-Cantidad de
+number of
  paramet
 ros no v
@@ -1679,37 +1679,35 @@
 r of paramet
-ros no 
+ers un
 valid
-o
 ');%0A    %7D%0A%7D;
@@ -2303,27 +2303,25 @@
 ck('
-Cantidad de
+number of
  paramet
 ros 
@@ -2320,21 +2320,19 @@
 amet
-ros no 
+ers un
 valid
-o
 ');%0A
@@ -2348,21 +2348,21 @@
 %0A//E
-JEMPLO DE USO
+XAMPLE OF USE
 %0A//c
@@ -2398,39 +2398,29 @@
 sult
-ado de la ejecuci%C3%B3n del comando
+ of command execution
 %0A//e

Translate strings to english