Python 如何使用关键字代替URL';youtube dl中有什么?
我正在为我的discord服务器编写一个bot,我已将Python 如何使用关键字代替URL';youtube dl中有什么?,python,python-3.x,discord.py,youtube-dl,Python,Python 3.x,Discord.py,Youtube Dl,我正在为我的discord服务器编写一个bot,我已将play命令放入我的bot。它可以很好地与URL的,但我希望机器人也与搜索工作。因此,与其复制和粘贴URL,我希望能够为其编写关键字。这是play命令: @client.command(pass_context=True, aliases=['p', 'pla']) async def play(ctx, url: str): def check_queue(): Queue_infile = os.path.isdi
play
命令放入我的bot。它可以很好地与URL的,但我希望机器人也与搜索工作。因此,与其复制和粘贴URL,我希望能够为其编写关键字。这是play
命令:
@client.command(pass_context=True, aliases=['p', 'pla'])
async def play(ctx, url: str):
def check_queue():
Queue_infile = os.path.isdir("./Queue")
if Queue_infile is True:
DIR = os.path.abspath(os.path.realpath("Queue"))
length = len(os.listdir(DIR))
still_q = length - 1
try:
first_file = os.listdir(DIR)[0]
except:
print("No more queued song(s)\n")
queues.clear()
return
main_location = os.path.dirname(os.path.realpath(__file__))
song_path = os.path.abspath(os.path.realpath("Queue") + "\\" + first_file)
if length != 0:
print("Song done, playing next queued\n")
print(f"Songs still in queue: {still_q}")
song_there = os.path.isfile("song.mp3")
if song_there:
os.remove("song.mp3")
shutil.move(song_path, main_location)
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, 'song.mp3')
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: check_queue())
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.07
else:
queues.clear()
return
else:
queues.clear()
print("No songs were queued before the ending of the last song\n")
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
queues.clear()
print("Removed old song file")
except PermissionError:
print("Trying to delete song file, but it's being played")
await ctx.send("ERROR: Music playing")
return
Queue_infile = os.path.isdir("./Queue")
try:
Queue_folder = "./Queue"
if Queue_infile is True:
print("Removed old Queue Folder")
shutil.rmtree(Queue_folder)
except:
print("No old Queue folder")
await ctx.send("Getting everything ready now")
voice = get(client.voice_clients, guild=ctx.guild)
ydl_opts = {
'format': 'bestaudio/best',
'quiet': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
print("Downloading audio now\n")
ydl.download([url])
except:
print("FALLBACK: youtube-dl does not support this URL, using Spotify (This is normal if Spotify URL)")
c_path = os.path.dirname(os.path.realpath(__file__))
system("spotdl -f " + '"' + c_path + '"' + " -s " + url)
for file in os.listdir("./"):
if file.endswith(".mp3"):
name = file
print(f"Renamed File: {file}\n")
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: check_queue())
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.14
nname = name.rsplit("-", 2)
await ctx.send(f"Playing: {nname[0]}")
print("playing\n")
我知道它看起来有点复杂,因为它有一个队列功能。我尝试了几种在网上看到的解决方案,但它们没有帮助,因为它们给出的代码与我拥有的代码之间存在差异。如果有任何空格错误,那是因为我可能忘记在stackoverflow中放置空格。在youtube dl选项中,设置
default\u search
。比如说,
ydl_opts = {
'default_search': 'ytsearch',
# ... all other options
如果输入不是URL,将使用YouTube搜索。这适用于任何一种,例如“默认搜索”:https://instagram.com/explore/tags/“
默认情况下将搜索instagram标签