【问题标题】:How to get best audio quality on music bot using discord.py?如何使用 discord.py 在音乐机器人上获得最佳音频质量?
【发布时间】:2021-05-20 06:39:18
【问题描述】:

我在 discord.py 中构建了一个不和谐音乐机器人,但由于某种原因,它播放的音乐质量不如 Fredboat 或 Rythm(所以我认为语音聊天的比特率不是问题)。我在网上尝试了几件事。

唯一能稍微提高质量的就是在播放之前下载歌曲。但质量仍远不如 Fredboat 的。这也是非常不切实际的,因为下载一首 1 小时的歌曲需要一段时间,而且很耗空间。

我对如何解决这个问题以及为什么会发生这种情况的解释很感兴趣。

这是我们目前用于音乐机器人的代码:

from discord import FFmpegPCMAudio
import discord
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from discord.ext import commands, tasks
from youtubesearchpython import VideosSearch

class cmd_music(commands.Cog, name="music_commands"):

    def __init__(self, bot):
        self.bot = bot
        self.music_queue = []
        self.scheduler = AsyncIOScheduler()
        self.scheduler.add_job(self.check_queue, CronTrigger(second="0,5,10,15,20,25,30,35,40,45,50,55"))
        self.scheduler.start()
    
    async def play_raw(self, voice_client):
        if not self.music_queue:
            return

        YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist':'True'}
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
        if not voice_client.is_playing():
            with YoutubeDL(YDL_OPTIONS) as ydl:
                info = ydl.extract_info(self.music_queue.pop(0), download=False)
            URL = info['formats'][0]['url']
            voice_client.play(FFmpegPCMAudio(URL, **FFMPEG_OPTIONS))
            voice_client.is_playing()

    async def check_queue(self):
        if not self.bot.voice_clients: return
        
        client = self.bot.voice_clients[0]
        if not client.is_playing():
            if self.music_queue:
                await self.play_raw(client)
           
        
    @commands.command(brief="join")
    async def join(self, ctx):
        await ctx.author.voice.channel.connect()

    @commands.command(brief="leave")
    async def leave(self, ctx):
        await ctx.voice_client.disconnect()
        self.music_queue = []

    @commands.command(brief="play")
    async def play(self, ctx, *name):
        url = VideosSearch(" ".join(name[:]), 1).result().get("result")[0].get("link")
        self.music_queue.append(url)
        await ctx.send("Now playing: " + url)

    @commands.command(brief="skip")
    async def skip(self, ctx):
        await ctx.send("Skipped current song")
        ctx.voice_client.stop()
        if self.music_queue:
            await self.play_raw(ctx.voice_client)``` 

【问题讨论】:

    标签: python ffmpeg discord discord.py


    【解决方案1】:

    我也陷入了这个质量问题。但我找到了解决这个问题的方法。

    你也可以测试一下,我得到了和流媒体一样的质量,没有问题。

    FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
    
    YDL_OPTIONS = {'format': 'bestaudio/best', 'noplaylist':'True'}
    
    voice = get(client.voice_clients, guild=ctx.guild)
    with YoutubeDL(YDL_OPTIONS) as ydl:
        info = ydl.extract_info(url, download=False)
        I_URL = info['formats'][0]['url']
        source = await discord.FFmpegOpusAudio.from_probe(I_URL, **FFMPEG_OPTIONS)
        voice.play(source)
        voice.is_playing()
    

    【讨论】:

      【解决方案2】:

      以防万一有人遇到这种情况。我的问题似乎是 ffmpeg 的音频流质量低。 Lavalink 似乎提供了更好的质量。

      我建议查看这个 youtube 视频播放列表,您可以在其中了解使用 discord.py 和 Lavalink 创建高质量音乐机器人的每一步: https://www.youtube.com/watch?v=tZPrkKT9QHc&list=PLYeOw6sTSy6ZIfraPiUsJWuxjqoL47U3u&index=1&ab_channel=CarberraTutorials

      【讨论】:

        猜你喜欢
        • 2021-05-05
        • 2021-05-24
        • 2020-08-31
        • 2020-09-27
        • 2022-01-04
        • 2021-05-27
        • 2021-04-29
        • 1970-01-01
        • 2011-02-26
        相关资源
        最近更新 更多