Answer the question
In order to leave comments, you need to log in
Discord.py Why doesn't the bot play long music from youtube?
Here is the code to play music from youtube! The problem is that the bot launches short clips with music perfectly, but if you say to play a long clip that lasts more than 10 minutes, then the bot just downloads it but does not play it.
@client.command()
async def play(ctx, url : str):
global voice
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild = ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await ctx.send(f'Бот присоединился к каналу: {channel}')
song_there = os.path.isfile('song.mp3')
try:
if song_there:
os.remove('song.mp3')
print('[log] Старый файл удалён')
except PermissionError:
print('[log] Не удалось удалить страый файл')
await ctx.send('Пожалуйста ожидайте: 5 - 10 сек')
voice = get(client.voice_clients, guild = ctx.guild)
ydl_opts = {
'format' : 'bestaudio/best',
'postprocessors' : [{
'key' : 'FFmpegExtractAudio',
'preferredcodec' : 'mp3',
'preferredquality' : '192'
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
print('[log] Загружаю музыку')
ydl.download([url])
for file in os.listdir('./'):
if file.endswith('.mp3'):
name = file
print(f'[log] Переменовываю файл: {file}')
os.rename(file, 'song.mp3')
voice.play(discord.FFmpegPCMAudio('song.mp3'), after = lambda e: print(f'[log] {name}, музыка закончила своё проигрывание'))
voice.sourse.volume = 0.07
song_name = name.rsplit('-', 2)
await ctx.send(f'Сейчас проигрывает музыка: {song[0]}')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question