all files / mp3-to-video/lib/convert/ convert.js

81.82% Statements 54/66
69.44% Branches 25/36
68.18% Functions 15/22
82.61% Lines 38/46
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          11×                                                                                                                                                                                                                                                                                                                
'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); Iif (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) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/* eslint class-methods-use-this:
[ "error",
  {"exceptMethods": ["checkTypes", "isString", "fillWithNewExt", "convert", "throwIfMissing"] }
] */
 
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.fillWithNewExt(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) {
      Iif (typeof value !== 'string' && !(value instanceof String)) {
        throw new Error('Wrong type parameter.');
      }
    }
 
    /**
      * It return a path with output extension
      * @param {string} Path of mp3
      * @param {string} extension
    */
 
  }, {
    key: 'fillWithNewExt',
    value: function fillWithNewExt(path, ext) {
      return path.substr(0, path.lastIndexOf('.')) + '.' + ext;
    }
 
    /**
      * 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: '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', '-crf 18', '-c:a aac', '-strict experimental', '-b:a 192k', '-shortest']).on('error', function (err) {
        callback(err);
      }).on('end', function () {
        callback(null, 'OK');
      }).run();
    }
 
    /**
      * Set default error if parameter is missing
    */
 
  }, {
    key: 'throwIfMissing',
    value: function throwIfMissing() {
      throw new Error('Missing parameter.');
    }
 
    /**
      * It is a module initialize
      * @param {init~requestCallback} callback
    */
 
  }, {
    key: 'init',
    value: function init(callback) {
      var _this2 = this;
 
      if (!(0, _fileExists2.default)(this.path)) callback(new Error('MP3 path is wrong.'));
      Iif (!(0, _fileExists2.default)(this.image)) callback(new Error('Image path is wrong.'));
      this.convert(this.path, this.image, this.output, function (err, success) {
        Iif (err) callback(err);
        callback(null, {
          status: success,
          message: 'Everything OK',
          videoPath: _this2.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 = this.fillWithNewExt(output, this.ext);
    }
 
    /**
      * 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;
    }
  }]);
 
  return Convert;
}();
 
exports.default = Convert;