I have a little problem and cannot solve it. It might be just a little trick or something.
我有个小问题,解决不了。可能只是个小把戏什么的。
I want to code a music bot for discord. It should also play playlists.
It works but YT-DLP always downloads the whole Playlist first.
我想为不和谐编写一个音乐机器人。它还应该播放播放列表。它可以工作,但YT-DLP总是首先下载整个播放列表。
I cannot figure out, that YT-DLP downloads it one by one.
我想不通,YT-DLP是逐个下载的。
My Code:
我的代码:
import discord
from discord.ext import commands
import yt_dlp as youtube_dl
import asyncio
intents = discord.Intents.default()
intents.members = True
intents.message_content = True # needed for your bot
bot = commands.Bot(command_prefix='!', intents=intents)
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'downloads/%(extractor)s-%(id)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': False,
'nocheckcertificate': True,
'ignoreerrors': True,
'logtostderr': False,
'quiet': False,
'no_warnings': False,
'default_search': 'auto',
'source_address': '0.0.0.0',
'force-ipv4': True,
'preferredcodec': 'mp3',
'cachedir': False
}
playlist_queue = []
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.command()
async def play(ctx, url):
try:
voice_channel = ctx.author.voice.channel # checking if user is in a voice channel
except AttributeError:
return await ctx.send(
"Du bist in keinem Sprachkanal. Bitte geh in einen Sprachkanal!") # member is not in a voice channel
permissions = voice_channel.permissions_for(ctx.me)
if not permissions.connect or not permissions.speak:
await ctx.send("Ich hab keine Berechtigungen zu sprechen oder dem Sprachkanal beizutreten.")
return
voice_client = ctx.guild.voice_client
if not voice_client:
await voice_channel.connect()
voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice_client.is_playing():
voice_client.stop()
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
if 'entries' in info: # Prüfen, ob es sich um eine Playlist handelt
for entry in info['entries']:
url2 = f"downloads/youtube-{entry['id']}.webm" # Pfad zur heruntergeladenen Datei
playlist_queue.append(url2) # Füge das Lied zur Warteschlange hinzu
else:
url2 = f"downloads/youtube-{info['id']}.webm"
voice_client.stop()
voice_client.play(discord.FFmpegPCMAudio(url2))
@bot.command()
async def skip(ctx):
voice_client = ctx.voice_client
if voice_client.is_playing():
voice_client.stop()
# Überspringe das aktuelle Lied in der Warteschlange
if playlist_queue:
next_song = playlist_queue.pop(0)
voice_client.play(discord.FFmpegPCMAudio(next_song))
@bot.command()
async def repeat(ctx, url):
try:
voice_channel = ctx.author.voice.channel # checking if user is in a voice channel
except AttributeError:
return await ctx.send(
"Du bist in keinem Sprachkanal. Bitte geh in einen Sprachkanal!") # member is not in a voice channel
permissions = voice_channel.permissions_for(ctx.me)
if not permissions.connect or not permissions.speak:
await ctx.send("Ich hab keine Berechtigungen zu sprechen oder dem Sprachkanal beizutreten.")
return
voice_client = ctx.guild.voice_client
if not voice_client:
await voice_channel.connect()
voice_client = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice_client.is_playing():
voice_client.stop()
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
if 'entries' in info: # Prüfen, ob es sich um eine Playlist handelt
url2 = f"downloads/youtube-{info['entries'][0]['id']}.webm"
else:
url2 = f"downloads/youtube-{info['id']}.webm"
while True:
voice_client.stop()
voice_client.play(discord.FFmpegPCMAudio(url2))
while voice_client.is_playing():
await asyncio.sleep(1)
@bot.command()
async def pause(ctx):
voice_client = ctx.voice_client
if voice_client.is_playing():
voice_client.pause()
@bot.command()
async def resume(ctx):
voice_client = ctx.voice_client
if voice_client.is_paused():
voice_client.resume()
@bot.command()
async def stop(ctx):
voice_client = ctx.voice_client
if voice_client.is_connected():
await voice_client.disconnect()
bot.run('*not for public eyes*')
Can you guys help me?
你们能帮帮我吗?
Thank you!
谢谢!
Best regards,
诚挚的问候,
Leon
莱昂
更多回答
Please note that you should always provide a minimal reproducible example - emphasis on minimal, as most of the code here seems irrelevant to the question.
请注意,您应该始终提供一个最小的可重现的示例--重点放在最小,因为这里的大部分代码似乎与问题无关。
yt-dlp --flat-playlist --get-id <url of the list>
will give you the ids of all videos in the playlist. yt-dlp --flat-playlist --get-id --get-title <...>
will also give you the titles
YT-DLP--Flat-PlayList--Get-id将为您提供播放列表中所有视频的ID。YT-DLP--平面播放列表--GET-ID--GET-TITLE也将为您提供字幕.
我是一名优秀的程序员,十分优秀!