【问题标题】:Improvring discord.js audio quailty for my bot提高我的机器人的 discord.js 音频质量
【发布时间】:2018-12-22 23:35:35
【问题描述】:

使用:Javascript、discord.js 和 Discord API。 如何使用 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;
  }
};

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    只需修改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 */});
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-05
        • 2021-11-29
        • 1970-01-01
        • 2020-12-01
        • 2021-12-08
        • 2021-07-04
        • 2020-07-17
        • 2020-10-24
        相关资源
        最近更新 更多