首页 > 解决方案 > Discord.py ffmpeg 我已正确设置所有内容,即下载和播放文件,我需要帮助更改已安装文件的目录,代码如下:

问题描述

我已经正确设置了它可以完成我需要播放,暂停,恢复等的一切。即使在谷歌上我也找不到如何更改下载文件的目录。我只是想让它不会下载 .py 文件的主目录中的文件,而是将它们下载到一个单独的文件夹中,这样它就可以更有条理。谢谢你的帮助。

import youtube_dl
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
    'format': 'bestaudio/best',
    'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
    'restrictfilenames': True,
    'noplaylist': True,
    'nocheckcertificate': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'quiet': True,
    'no_warnings': True,
    'default_search': 'auto',
    'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}

ffmpeg_options = {
    'options': '-vn'
}

ytdl = youtube_dl.YoutubeDL(ytdl_format_options)

class YTDLSource(discord.PCMVolumeTransformer):
    def __init__(self, source, *, data, volume=0.5):
        super().__init__(source, volume)

        self.data = data

        self.title = data.get('title')
        self.url = data.get('url')

    @classmethod
    async def from_url(cls, url, *, loop=None, stream=False):
        loop = loop or asyncio.get_event_loop()
        data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))

        if 'entries' in data:
            # take first item from a playlist
            data = data['entries'][0]

        filename = data['url'] if stream else ytdl.prepare_filename(data)
        return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)

@client.command(aliases=['p'])
@commands.has_role('DJ')
async def play(ctx, url):
    channel = ctx.message.author.voice.channel
    server = ctx.message.guild
    author=ctx.message.author
    guild=ctx.message.guild
    voice=discord.utils.get(client.voice_clients,guild=ctx.guild)
    if voice==None:
        await channel.connect()
        embed=discord.Embed(title='Connected',description=f'{author.mention} I have Joined Channel {channel.mention}',colour=discord.Colour.green())
        await ctx.send(embed=embed)
        channel_2=893855384517873725
        audit_channel=client.get_channel(channel_2)
        embed=discord.Embed(title='zenabz Bot Log',description=f'{author.mention} Has Requested Bot to Join Channel {channel.mention}',colour=discord.Colour.random())
        await audit_channel.send(embed=embed)
        print(f'{author} Has Requested Bot to Join Channel {channel}')
        async with ctx.typing():
            voice_channel = server.voice_client
            player = await YTDLSource.from_url(url, loop=client.loop)
            voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
        await ctx.send(f'{author.mention} **Now playing:** {format(player.title)}')
        embed=discord.Embed(title='zenabz Bot Log',description=f'{author.mention} Has Requested Bot to play: {format(player.title)}',colour=discord.Colour.random())
        await audit_channel.send(embed=embed)
        print(f'{author} Has Requested Bot to to play: {format(player.title)}')
    
    async with ctx.typing():
        voice_channel = server.voice_client
        player = await YTDLSource.from_url(url, loop=client.loop)
        voice.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
    await ctx.send(f'{author.mention} **Now playing:** {format(player.title)}')
    channel_2=893855384517873725
    audit_channel=client.get_channel(channel_2)
    embed=discord.Embed(title='zenabz Bot Log',description=f'{author.mention} Has Requested Bot to play: {format(player.title)}',colour=discord.Colour.random())
    await audit_channel.send(embed=embed)
    print(f'{author} Has Requested Bot to to play: {format(player.title)}')

@play.error
async def play_error(ctx,error):
    print(error)
    if isinstance(error, commands.MissingRole):
        author=ctx.message.author
        embed=discord.Embed(title="Missing 'DJ' Role",description=f"{author.mention} you don't have the required roles to use this command", colour=discord.Colour.red())
        ctx.send(embed=embed)

标签: python-3.xffmpegdiscord.py

解决方案


Sorry I don't know how to change download directory but,

ytdl_format_options = {
    'format': 'bestaudio/best',
    'extractaudio': True,
    'audioformat': 'mp3',
    'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
    'restrictfilenames': True,
    'noplaylist': True,
    'nocheckcertificate': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'quiet': True,
    'no_warnings': True,
    'default_search': 'auto',
    'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}

This config will not download and save videos to your directory, instead extracts audio and plays it.


推荐阅读