From 36e40d1b35ef6303b31dd6787b0f60bcb88460a9 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sun, 8 Nov 2015 16:31:33 +0000 Subject: [PATCH] Fixed order --- lib/Voice/AudioEncoder.js | 33 ++++++++++++++++++++++++++++++++- src/Voice/AudioEncoder.js | 21 ++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/Voice/AudioEncoder.js b/lib/Voice/AudioEncoder.js index 01e0fb638..9ca95a128 100644 --- a/lib/Voice/AudioEncoder.js +++ b/lib/Voice/AudioEncoder.js @@ -19,6 +19,7 @@ var AudioEncoder = (function () { if (opus) { this.opus = new opus.OpusEncoder(48000, 1); } + this.choice = false; } AudioEncoder.prototype.opusBuffer = function opusBuffer(buffer) { @@ -26,12 +27,42 @@ var AudioEncoder = (function () { return this.opus.encode(buffer, 1920); }; + AudioEncoder.prototype.getCommand = function getCommand(force) { + + if (this.choice && force) return choice; + + var choices = ["avconv", "ffmpeg"]; + + for (var _iterator = choices, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { + var _ref; + + if (_isArray) { + if (_i >= _iterator.length) break; + _ref = _iterator[_i++]; + } else { + _i = _iterator.next(); + if (_i.done) break; + _ref = _i.value; + } + + var choice = _ref; + + var p = cpoc.spawnSync(choice); + if (!p.error) { + this.choice = choice; + return choice; + } + } + + return "help"; + }; + AudioEncoder.prototype.encodeStream = function encodeStream(stream) { var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1]; var self = this; return new Promise(function (resolve, reject) { - var enc = cpoc.spawn("ffmpeg", ["-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. + var enc = cpoc.spawn(self.getCommand(), ["-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth. "-af", "volume=1", "pipe:1", "-i", "-"]); stream.pipe(enc.stdin); diff --git a/src/Voice/AudioEncoder.js b/src/Voice/AudioEncoder.js index 7293d5ef1..33aa5b0d4 100644 --- a/src/Voice/AudioEncoder.js +++ b/src/Voice/AudioEncoder.js @@ -15,6 +15,7 @@ class AudioEncoder{ if(opus){ this.opus = new opus.OpusEncoder(48000, 1); } + this.choice = false; } opusBuffer(buffer){ @@ -23,10 +24,28 @@ class AudioEncoder{ } + getCommand(force){ + + if(this.choice && force) + return choice; + + var choices = ["avconv", "ffmpeg"]; + + for(var choice of choices){ + var p = cpoc.spawnSync(choice); + if(!p.error){ + this.choice = choice; + return choice; + } + } + + return "help"; + } + encodeStream(stream, callback=function(err, buffer){}){ var self = this; return new Promise((resolve, reject) => { - var enc = cpoc.spawn("ffmpeg" , [ + var enc = cpoc.spawn(self.getCommand() , [ "-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth.