all files / lib/convert/ convert.js

40% Statements 28/70
22.5% Branches 9/40
22.73% Functions 5/22
25.49% Lines 13/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209          12×                                                                                                                                                                                                                                                                                                                                                                                              
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Eif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _fileExists = require('file-exists');
 
var _fileExists2 = _interopRequireDefault(_fileExists);
 
var _fluentFfmpeg = require('fluent-ffmpeg');
 
var _fluentFfmpeg2 = _interopRequireDefault(_fluentFfmpeg);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/* eslint class-methods-use-this:
["error", {"exceptMethods": ["checkTypes", "isString", "getNameOfPath"] }] */
 
var Convert = function () {
  function Convert() {
    var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.throwIfMissing();
    var ext = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.throwIfMissing();
    var image = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.throwIfMissing();
 
    _classCallCheck(this, Convert);
 
    this.checkTypes(path, ext, image);
    this.path = path;
    this.ext = ext;
    this.image = image;
    this.output = this.getNameOfPath(this.path) + '.' + this.ext;
  }
 
  /**
    * It is a getter, should show all params
  */
 
 
  _createClass(Convert, [{
    key: 'checkTypes',
 
 
    /**
      * For each params and calls _isString function
      * @param {object} Should be a string
    */
    value: function checkTypes() {
      var _this = this;
 
      for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }
 
      args.forEach(function (value) {
        return _this.isString(value);
      });
    }
 
    /**
      * Check if params is a string
      * @param {any} Should be a string
    */
 
  }, {
    key: 'isString',
    value: function isString(value) {
      if (typeof value !== 'string' && !(value instanceof String)) {
        throw new Error('Wrong type parameter.');
      }
    }
 
    /**
      * It return a basename of path
      * @param {string} Path of mp3
    */
 
  }, {
    key: 'getNameOfPath',
    value: function getNameOfPath(path) {
      var baseName = /^.+\.([^.]+)$/.exec(path);
      return baseName == null ? '' : baseName[0];
    }
 
    /**
      * Takes an image and mp3 and converts it to an mp4
      * @param {string} image path to image file (eg. /path/to/image.jpg)
      * @param {string} audio path to audi file (eg. /path/to/audio.mp3)
      * @param {string} output path to output file (eg. /path/to/output.mp4)
      * @param {convert~requestCallback} callback
    */
 
  }, {
    key: 'init',
 
    /**
      * It is a module initialize
      * @param {init~requestCallback} callback
    */
    value: function init(callback) {
      console.log(this.path);
      console.log((0, _fileExists2.default)(this.path));
      if (!(0, _fileExists2.default)(this.path)) throw new Error('MP3 path is wrong.');
      if (!(0, _fileExists2.default)(this.image)) throw new Error('Image path is wrong.');
      var flagOK = this.convert(this.path, this.image, this.output, this.hasFinished);
      if (flagOK) {
        callback({
          status: 'OK',
          message: 'Everything OK',
          videoPath: this.output
        });
      }
    }
  }, {
    key: 'getAttributes',
    get: function get() {
      return this.path + ' ' + this.ext + ' ' + this.image + ' ' + this.output + '!';
    }
 
    /**
      * It is a setter for path
      * @param {string} path of mp3
    */
 
  }, {
    key: 'changePath',
    set: function set(path) {
      this.path = path;
    }
 
    /**
      * It is a setter for output path
      * @param {string} output path of video
    */
 
  }, {
    key: 'changeOutputPath',
    set: function set(output) {
      this.output = output;
    }
 
    /**
      * It is a setter for extension
      * @param {string} extension of video
    */
 
  }, {
    key: 'changeExtension',
    set: function set(ext) {
      this.ext = ext;
    }
 
    /**
      * It is a setter for image
      * @param {string} path of image
    */
 
  }, {
    key: 'changeImage',
    set: function set(image) {
      this.image = image;
    }
  }], [{
    key: 'convert',
    value: function convert(audio, image, output, callback) {
      var Command = new _fluentFfmpeg2.default();
      Command.input(image).inputOptions(['-loop 1']).input(audio).output(output).outputOptions(['-c:v libx264', '-c:v libx264', '-c:a aac', '-strict experimental', '-b:a 192k', '-pix_fmt yuv420p', '-shortest']).on('error', function (err) {
        callback(err);
      }).on('end', function () {
        callback(null, true);
      }).run();
    }
 
    /**
      * Set default error if parameter is missing
    */
 
  }, {
    key: 'throwIfMissing',
    value: function throwIfMissing() {
      throw new Error('Missing parameter.');
    }
 
    /**
      * It is a callback from convert function
      * @param {object} Can be null or error object
      * @param {boolean} Can be null or true
      * @param {hasFinished~requestCallback} callback
    */
 
  }, {
    key: 'hasFinished',
    value: function hasFinished(err, success) {
      if (err) {
        throw new Error('Something has wrong: ' + err);
      } else {
        return success;
      }
    }
  }]);
 
  return Convert;
}();
 
exports.default = Convert;