-
-
Notifications
You must be signed in to change notification settings - Fork 468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Playing audio by passingio.BytesIO
doesn't work on Linux
#2205
Comments
io.BytesIO
doesn't work on Linuxio.BytesIO
doesn't work on Linux
Hi! ffmpeg_options_linux = {'executable': './requirements/ffmpeg'}
sound_path = "./sounds/file.wav" # mp3, wav, etc
@client.event
async def on_ready():
# Preparing the sound with the volume
source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(sound_path , **FFMPEG_OPTIONS), volume=0.5)
channel: discord.VoiceChannel = client.get_channel(1132373179210399844)
vc = await channel.connect()
vc.play(source)
while vc.is_playing():
await asyncio.sleep(1)
await vc.disconnect() |
It works directly with a file, just not with |
Could you try explicitly seeking to the start of the BytesIO object and then playing it? |
import discord
import secret
import io
import asyncio
client = discord.Client()
@client.event
async def on_ready():
with open("test.mp3", "rb") as file:
audio_data = file.read()
channel: discord.VoiceChannel = client.get_channel(1132373179210399844)
vc = await channel.connect()
stream = io.BytesIO(audio_data)
stream.seek(0)
vc.play(
discord.FFmpegPCMAudio(stream, pipe=True)
)
while vc.is_playing():
await asyncio.sleep(1)
await vc.disconnect()
client.run(secret.TOKEN) Same result |
Just thought I'd check, I've seen my fair share of issues with buffers not being read from the start |
I was about to submit a bug with a similar issue i've managed to temporarily fix. How long is the audio clip you're testing on? It seems FFmpeg is terminated prematurely when playing a stream as opposed to supplying a filename, which is especially noticable when the audio clip is short. The fix I'm using is a small change in the _pipe_writer function of the FFmpegAudio class in player.py. def _pipe_writer(self, source: io.BufferedIOBase) -> None:
while self._process:
# arbitrarily large read size
data = source.read(8192)
if not data:
##HOTFIX
self._stdin.close()
##
#self._process.terminate()
return
try:
self._stdin.write(data)
except Exception:
_log.debug(
"Write error for %s, this is probably not a problem",
self,
exc_info=True,
)
# at this point the source data is either exhausted or the process is fubar
self._process.terminate()
return Try and see if this fixes the problem |
The audio is about 10 seconds. |
It does when the pipe is closed and, vitally, input fully processed and written back out. I can only assume the intent was to free up resources as fast as possible but I couldn't say for sure |
Appears to be fixed in #2240. |
Summary
When using
io.BytesIO
andpipe=True
insidediscord.FFmpegPCMAudio(io.BytesIO(audio_data), pipe=True)
, the voice won't work on Linux, however it works on Windows.Reproduction Steps
Passing
io.ByteIO
todiscord.FFmpegPCMAudio(io.BytesIO(audio_data), pipe=True)
on Linux systems and thentrying to play audio with
VoiceClient.play
.Minimal Reproducible Code
Expected Results
For the audio to be played.
Actual Results
The bot connects and disconnects instantly without throwing any errors.
Intents
discord.Intents.default()
System Information
master version of pycord,
pip install --editable .
Checklist
Additional Context
When using debug logging, this is displayed:
The text was updated successfully, but these errors were encountered: