改善了我的机器人的discord.js音频质量 [英] Improvring discord.js audio quailty for my bot

查看:5
本文介绍了改善了我的机器人的discord.js音频质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用: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
# Queued video had no URL, skipping song...
````)
      .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}
**Duration:** ${video.length}
**Listener:** <@${video.requester}>`);
    msg.channel.send({embed});
  });

  dispatcher.on('error', err => {
    const embed = new RichEmbed().setTitle(`🚫`)
      .setDescription(````md
# ${err}
````)
      .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
# ${err}
````)
      .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 **

**Song:** ${video.title}
**Duration:** ${video.length}
**Queue:** ${msg.client.music.get(msg.guild.id).queue.length}
**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
# ${err}
````)
      .setColor('#031900');

    msg.channel.send({embed});

    return false;
  }
};

推荐答案

96kbps似乎是质量和文件大小之间的最佳平衡点:

https://opus-bitrates.anthum.com/

这篇关于改善了我的机器人的discord.js音频质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆