使用:具有Discord API的Javascript discord.js。 如何使用node-opus,ytdl-core改善机器人的音频质量? 我想做最好的音频被套。
Packages :
"discord.js": "^11.3.2",
"erlpack": "discordapp/erlpack",
"moment": "^2.22.1",
"moment-duration-format": "^2.2.2",
"node-opus": "^0.2.7",
"simple-youtube-api": "^5.0.2",
"sodium": "^2.0.3",
"sqlite": "^2.9.1",
"uws": "^9.147.0",
"walk": "^2.3.13",
"ytdl-core": "^0.20.2",
const {RichEmbed} = require('discord.js');
const moment = require('moment');
const ytdl = require('ytdl-core');
exports.run = async function (msg, args, search) {
const voiceChannel = msg.member.voiceChannel;
if (!voiceChannel) {
const embed = new RichEmbed()
.setTitle(' ').setColor('#031900')
.setDescription('**Join Voice First To Play Music**');
return msg.channel.send({embed});
}
if (msg.guild.me.voiceChannel && voiceChannel !== msg.guild.me.voiceChannel) {
const embed = new RichEmbed()
.setTitle(' ').setColor('#031900')
.setDescription('**Join My Voice Channel**');
return msg.channel.send({embed});
}
if (msg.member.deaf) return msg.reply(':no_entry_sign: **You must be listening...**');
const connection = msg.guild.voiceConnection;
if (connection && connection.dispatcher && connection.dispatcher.paused && !args) {
connection.dispatcher.resume();
return msg.channel.send(':arrow_forward: **Music Resumed **');
}
if (!args) {
msg.reply({embed: {description: 'Joined !',color:0x5DBCD2}});
return voiceChannel.join();
}
let response = search ? await msg.client.commands.get('search').search.call(this, msg, args) : await searchVideos.call(this, msg, args);
console.log('added to queue: ', (Date.now() - msg.createdTimestamp) / 1000)
if (!response) return;
if (msg.client.music.get(msg.guild.id).queue.length === 1) {
let connection = msg.guild.voiceConnection || await voiceChannel.join();
if (voiceChannel !== msg.guild.voiceConnection.channel) {
await msg.guild.voiceConnection.channel.leave();
connection = await voiceChannel.join();
}
exports.play.call(this, msg, connection);
}
};
exports.play = async function (msg, connection) {
const video = msg.client.music.get(msg.guild.id).queue[0];
if (!video) return;
if (!video.url) {
const embed = new RichEmbed().setTitle(``)
.setDescription(`\`\`\`md\n# Queued video had no URL, skipping song...\n\`\`\``)
.setColor('#031900');
await msg.channel.send({embed});
msg.client.music.get(msg.guild.id).queue.shift();
if (msg.client.music.get(msg.guild.id).queue.length > 0) {
setTimeout(() => exports.play.call(this, msg, connection), 1000);
}
return;
}
const dispatcher = connection.playStream(ytdl(video.url), {passes: 3, volume: msg.client.music.get(msg.guild.id).volume});
msg.client.music.get(msg.guild.id).current = video;
dispatcher.once('start', () => {
console.log('dispatcher start: ', (Date.now() - msg.createdTimestamp) / 1000)
const embed = new RichEmbed()
.setTitle(' Playing')
.setColor('RANDOM')
.setThumbnail(video.thumbnail)
.setDescription(`**Song:** ${video.title}\n**Duration:** ${video.length}\n**Listener:** <@${video.requester}>`);
msg.channel.send({embed});
});
dispatcher.on('error', err => {
const embed = new RichEmbed().setTitle(``)
.setDescription(`\`\`\`md\n# ${err}\n\`\`\``)
.setColor('#031900');
console.log(err);
msg.channel.send({embed});
});
dispatcher.once('end', reason => {
if (reason === 'stop') return;
msg.client.music.get(msg.guild.id).queue.shift();
if (msg.client.music.get(msg.guild.id).queue.length > 0) {
setTimeout(() => exports.play.call(this, msg, connection), 1000);
}
});
};
async function searchVideos(msg, args) {
try {
const items = await msg.client.youtube.searchVideos(args, 1);
if (!items.length) {
const embed = new RichEmbed()
.setTitle('').setColor('#031900')
.setDescription('**Nothing found..!**');
msg.channel.send({embed});
return false;
}
const item = items[0];
const video = await exports.queue.call(this, msg, item.id);
return video;
} catch (err) {
console.log(err);
const embed = new RichEmbed().setTitle(``)
.setDescription(`\`\`\`md\n# ${err}\n\`\`\``)
.setColor('#031900');
msg.channel.send({embed});
return false;
}
}
exports.queue = async function (msg, id) {
try {
const item = await msg.client.youtube.getVideoByID(id);
const video = {
url: item.id,
title: item.title,
requester: msg.author.id,
thumbnail: item.thumbnails.default.url,
length: moment.duration(item.durationSeconds, 'seconds').humanize()
};
if (msg.client.music.get(msg.guild.id).queue.length > 0) {
const embed = new RichEmbed()
.setTitle(' Added')
.setColor('#EA934E')
.setThumbnail(video.thumbnail)
.setDescription(`**Added to be playing **\n\n**Song:** ${video.title}\n**Duration:** ${video.length}\n**Queue:** ${msg.client.music.get(msg.guild.id).queue.length}\n**Listener:** <@${video.requester}>`);
msg.channel.send({embed});
}
msg.client.music.get(msg.guild.id).queue.push(video);
return video;
} catch (err) {
console.log(err);
const embed = new RichEmbed().setTitle(``)
.setDescription(`\`\`\`md\n# ${err}\n\`\`\``)
.setColor('#031900');
msg.channel.send({embed});
return false;
}
};
答案 0 :(得分:0)
只需修改playStream
函数!
/**
* Set the bitrate to (192kbps).
* To use the maximum bitrate that YouTube could provide.
*/
const dispatcher = connection.playStream(ytdl(video.url), {bitrate: 192000 /* 192kbps */});
答案 1 :(得分:0)
96kbps似乎是介于质量和文件大小之间的最佳选择: