Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

discord.py ffmpeg song plays too quickly at the beginning

I built a discord bot using discord.py and ffmpeg, it works but the song in the first 0-5 seconds plays too fast, I think it's buffering so I tried to use asyncio.sleep but didn't work, suggestions to solve the problem?

play func:

with youtube_dl.YoutubeDL(self.opts) as ydl:
    await asyncio.sleep(0.1)
    ydl.cache.remove()
    await asyncio.sleep(0.1)
    info = ydl.extract_info(url, download=False)
    raw_url_song = info['formats'][0]['url']
    await self.real_play(ctx, raw_url_song)

real_play func:

source = await discord.FFmpegOpusAudio.from_probe(processed_url_song, **self.FFMPEG_OPTIONS)
await asyncio.sleep(0.5)
self.vc.play(source, after=lambda e: asyncio.run_coroutine_threadsafe(self.real_play(ctx, processed_url_song), self.client.loop))

these are the options:

self.FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
self.OPTIONS = {'format': 'bestaudio'}
self.opts = {'extract_flat': True, 'skip_download': True}

the functions are separate because I do something else (such as playlists), I have reported only the essential code to understand the problem, and the cache remove is essential to reduce the occurence of HTTP 404 forbidden error, btw also without cache remove the problem persists

like image 311
C-Gian Avatar asked Oct 27 '25 10:10

C-Gian


1 Answers

I've added a pause() and resume() function with a sleep function inside it. It works but it takes a second.

import asyncio
    
voice = bot.voice_clients[0]
voice.play(discord.FFmpegPCMAudio("song.mp3"))
voice.pause()
await asyncio.sleep(1)
voice.resume()
like image 115
SegergrenDev Avatar answered Oct 28 '25 23:10

SegergrenDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!