gpt4 book ai didi

python-3.x - 来自机器人的土 bean 质量音频

转载 作者:行者123 更新时间:2023-12-04 23:22:23 27 4
gpt4 key购买 nike

当我通过我的机器人播放音频时,它听起来真的很糟糕,我有一个快速的互联网连接,那么这可能是什么原因造成的?我在 Raspberry Pi 3 上运行我的机器人。我使用 FFMpeg。 RPI 是否会以某种方式使其成为瓶颈。是我的代码吗?
我的代码的简化版本:

@client.command()
async def play(ctx):
channel = client.get_channel(ctx.message.author.voice.channel.id)
voice = await channel.connect()
if not voice.is_playing():
voice.play(await discord.FFMpegOpusAudio(source='/path/to/file'))
while voice.is_playing():
await asyncio.sleep(1)
discord.AudioSource.cleanup(str(ctx.message.author.voice.channel.id))

最佳答案

pafy文档,您必须这样做:

def download_song(url)
video = pafy.new(url)
best = video.getbest()
best.download(quiet=False)
但是,我建议使用:
  • youtube-dl :
    pip install youtube-dl
    from youtube_dl import YoutubeDL

    ydl_opts = {
    'format': 'bestaudio/best',
    'noplaylist':'True',
    'outtmpl': 'song.%(ext)s'
    'postprocessors': [{
    'key': 'FFmpegExtractAudio',
    'preferredcodec': 'mp3',
    'preferredquality': '192',
    }],
    }

    def download_song(arg, url=True):
    with YoutubeDL(ydl_opts) as ydl:
    query = f"ytsearch:{arg}" if url else arg
    data = ydl.extract_info(query, download=True)
    return data if url else data['entries'][0]

    def display_info(data):
    video_info = {
    'title': data['title']
    'duration': data['duration']
    'uploader': data['uploader']
    'thumbnail': data['thumbnail']
    'url': data['webpage_url']
    'channel': data['channel_url']
    }
    for key, val in video_info.items():
    print(f"{key}: {val}")
    此功能将允许您从 url 和查询(例如“30 秒视频”)下载歌曲。 youtube_dl报价 a lot of options .
  • Wavelink (用于 discord.py 的 lavalink 包装器 → 更好的音频和音频控制):
    pip install lavalink
    #Wavelink github example
    import discord
    import wavelink
    from discord.ext import commands


    class Bot(commands.Bot):

    def __init__(self):
    super(Bot, self).__init__(command_prefix=['audio ', 'wave ','aw '])

    self.add_cog(Music(self))

    async def on_ready(self):
    print(f'Logged in as {self.user.name} | {self.user.id}')


    class Music(commands.Cog):

    def __init__(self, bot):
    self.bot = bot

    if not hasattr(bot, 'wavelink'):
    self.bot.wavelink = wavelink.Client(bot=self.bot)

    self.bot.loop.create_task(self.start_nodes())

    async def start_nodes(self):
    await self.bot.wait_until_ready()

    # Initiate our nodes. For this example we will use one server.
    # Region should be a discord.py guild.region e.g sydney or us_central (Though this is not technically required)
    await self.bot.wavelink.initiate_node(host='127.0.0.1',
    port=2333,
    rest_uri='http://127.0.0.1:2333',
    password='youshallnotpass',
    identifier='TEST',
    region='us_central')

    @commands.command(name='connect')
    async def connect_(self, ctx, *, channel: discord.VoiceChannel=None):
    if not channel:
    try:
    channel = ctx.author.voice.channel
    except AttributeError:
    raise discord.DiscordException('No channel to join. Please either specify a valid channel or join one.')

    player = self.bot.wavelink.get_player(ctx.guild.id)
    await ctx.send(f'Connecting to **`{channel.name}`**')
    await player.connect(channel.id)

    @commands.command()
    async def play(self, ctx, *, query: str):
    tracks = await self.bot.wavelink.get_tracks(f'ytsearch:{query}')

    if not tracks:
    return await ctx.send('Could not find any songs with that query.')

    player = self.bot.wavelink.get_player(ctx.guild.id)
    if not player.is_connected:
    await ctx.invoke(self.connect_)

    await ctx.send(f'Added {str(tracks[0])} to the queue.')
    await player.play(tracks[0])


    bot = Bot()
    bot.run('TOKEN')
    这个更复杂,但可以让你更好地控制你的音乐播放器。个人认为youtube-dl足以播放不和谐的音频,但这取决于您。
  • 关于python-3.x - 来自机器人的土 bean 质量音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63064978/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com