python-3.x - 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)
解决方案
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.
推荐阅读
- python - 使用python从几个列表中嵌套字典
- c# - 会话作为参考 .net 核心
- asp.net - 如何将列的最大值存储为数据库表中的整数?
- javascript - 以编程方式选择一个选项 - Materialize css
- excel - Apache POI - 在列上添加日期格式、开始日期、结束日期验证
- c++ - 在没有窗口获得焦点的情况下在mac终端中运行C++应用程序?
- python - 如何将用户多个输入添加到列表中?
- python - 如何将新列添加到 4d numpy 数组
- sql-server - SQL Server如何为特定类别求和最大值?
- material-ui - 如何使用 material-ui(-next) Select with async options