I’m working on a Discord music bot using discord.py and YT_DLP but I’m having audio quality problems. When my bot plays music in voice channels, the sound comes out really distorted and choppy. The weird thing is that the same audio files play perfectly fine when I test them locally on my computer.
Here’s the relevant code I’m using:
@client.command()
async def play_song(ctx, song_url):
if ctx.author.voice:
user_channel = ctx.author.voice.channel
bot_voice = await user_channel.connect()
download_options = {
'format': 'bestaudio',
'ffmpeg_location': os.path.realpath('D:/Projects/musicbot/ffmpeg/bin/'),
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'noplaylist': 'True',
}
audio_file = 'test_song.mp3'
bot_voice.play(discord.FFmpegPCMAudio(audio_file, executable='D:\\Projects\\musicbot\\ffmpeg\\bin\\ffmpeg.exe'))
else:
await ctx.send('You need to join a voice channel first')
I’ve tried adjusting the bitrate settings and tested different audio files but nothing seems to fix the distortion issue. My internet connection is stable so I don’t think that’s the problem. Has anyone else run into similar audio quality issues with Discord bots?
your ffmpeg options are missing key flags for discord streaming. add -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5
to your FFmpegPCMAudio call.
that postprocessor setup’s also wrong - don’t convert to mp3 when you’re using bestaudio format. discord handles opus natively, so use -f opus
instead. this’ll cut the processing overhead that’s likely causing your distortion.
Your main thread’s getting blocked - that’s what’s causing the choppy audio. Discord.py hates any delays in the event loop, and I’ve been there before. YT_DLP downloads were freezing my bot for split seconds, which made the audio stutter like crazy. Move your download stuff to a separate thread with asyncio.to_thread(). Also watch out for other heavy operations running while you’re streaming - even database calls can mess things up. What really helped me was adding proper error handling around FFmpeg since it fails silently sometimes and you get weird distorted playback. Your FFmpeg path looks fine, but make sure you’re using a newer build. Older versions had encoding problems with Discord’s format.
Had the exact same choppy audio problem - it’s a buffer issue. Discord wants consistent streaming but your setup can’t handle the buffering right. Switch to discord.FFmpegOpusAudio
instead of FFmpegPCMAudio
. Discord runs on Opus natively, so you’ll skip the transcoding that’s messing up your audio. Add before_options
with -reconnect 1 -reconnect_at_eof 1
to deal with connection drops. What really fixed it for me was switching to proper asyncio streaming instead of dumping whole files into memory. Skip the postprocessor completely and just grab the best audio format straight up. Try options='-vn'
in your FFmpeg call to make sure it’s only touching audio channels.