mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
124 lines
2.1 KiB
JavaScript
124 lines
2.1 KiB
JavaScript
"use strict";
|
|
|
|
import cpoc from "child_process";
|
|
|
|
var opus;
|
|
try {
|
|
opus = require("node-opus");
|
|
} catch (e) {
|
|
// no opus!
|
|
}
|
|
|
|
export default class AudioEncoder {
|
|
constructor() {
|
|
if (opus) {
|
|
this.opus = new opus.OpusEncoder(48000, 2);
|
|
}
|
|
this.choice = false;
|
|
}
|
|
|
|
opusBuffer(buffer) {
|
|
|
|
return this.opus.encode(buffer, 1920);
|
|
|
|
}
|
|
|
|
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(self.getCommand(), [
|
|
'-loglevel', '0',
|
|
'-i', '-',
|
|
'-f', 's16le',
|
|
'-ar', '48000',
|
|
'-ac', 2,
|
|
'pipe:1'
|
|
], {stdio: ['pipe', 'pipe', 'ignore']});
|
|
|
|
stream.pipe(enc.stdin);
|
|
|
|
enc.stdout.once("readable", function () {
|
|
callback(null, {
|
|
proc: enc,
|
|
stream: enc.stdout,
|
|
instream: stream,
|
|
channels : 2
|
|
});
|
|
resolve({
|
|
proc: enc,
|
|
stream: enc.stdout,
|
|
instream: stream,
|
|
channels : 2
|
|
});
|
|
});
|
|
|
|
enc.stdout.on("end", function () {
|
|
callback("end");
|
|
reject("end");
|
|
});
|
|
|
|
enc.stdout.on("close", function () {
|
|
callback("close");
|
|
reject("close");
|
|
});
|
|
});
|
|
}
|
|
|
|
encodeFile(file, callback = function (err, buffer) { }) {
|
|
var self = this;
|
|
return new Promise((resolve, reject) => {
|
|
var enc = cpoc.spawn(self.getCommand(), [
|
|
'-loglevel', '0',
|
|
'-i', file,
|
|
'-f', 's16le',
|
|
'-ar', '48000',
|
|
'-ac', 2,
|
|
'pipe:1'
|
|
], {stdio: ['pipe', 'pipe', 'ignore']});
|
|
|
|
enc.stdout.once("readable", function () {
|
|
callback(null, {
|
|
proc: enc,
|
|
stream: enc.stdout,
|
|
channels : 2
|
|
});
|
|
resolve({
|
|
proc: enc,
|
|
stream: enc.stdout,
|
|
channels : 2
|
|
});
|
|
});
|
|
|
|
enc.stdout.on("end", function () {
|
|
console.log("end");
|
|
callback("end");
|
|
reject("end");
|
|
});
|
|
|
|
enc.stdout.on("close", function () {
|
|
console.log("close");
|
|
callback("close");
|
|
reject("close");
|
|
});
|
|
});
|
|
}
|
|
}
|