Discord bot fails to play audio files

I’m working with Discord.js version 12 on Ubuntu 20.04 with Node.js v14.0.0. My bot connects to voice channels but won’t play any audio at all.

Here’s my current setup:

const ytdlCore = require("ytdl-core-discord");

const audioStream = await ytdlCore(videoURL, { filter: "audioonly" });
const audioDispatcher = voiceChannel.play(audioStream);

I have these packages installed: [email protected], [email protected], @discordjs/[email protected] and [email protected].

After about 10 seconds, the dispatcher debug event shows this error:

Error: ffmpeg stream: write EPIPE
    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:92:16) {
  errno: -32,
  code: 'EPIPE',
  syscall: 'write'
}

I need help getting the bot to actually output sound whether it’s from YouTube or local files. What could be causing this issue?

had the exact same problem last month! the EPIPE error usually means ffmpeg process is crashing unexpectedly. try switching from ytdl-core-discord to regular ytdl-core and pipe it differently - sometimes the discord version has bugs. also check if your bot has proper permissions in the voice channel, lack of speak permissions can cause weird streaming errors like this.

I encountered a similar EPIPE error when working with Discord.js v12 audio streaming. The issue stems from version incompatibilities between your packages and the Discord.js version you’re using. The [email protected] package is quite outdated and causes problems with newer Node.js versions. I resolved this by completely removing the ffmpeg npm package since you already have [email protected] installed, which provides the necessary ffmpeg binary. Also, @discordjs/[email protected] and [email protected] shouldn’t be used together - pick one opus encoder. I’d recommend keeping only @discordjs/opus and removing opusscript. Try updating your ytdl-core-discord to the latest version as well, as older versions had compatibility issues with the streaming pipeline. After making these changes, restart your bot completely to ensure the new dependencies are properly loaded.

The EPIPE error typically occurs when there’s a broken pipe between your Node.js application and the ffmpeg process. In my experience with Discord.js v12, this often happens due to audio format mismatches or connection instability. Try adding error handling to your audio dispatcher and use the highWaterMark option when creating the stream. Something like voiceChannel.play(audioStream, { highWaterMark: 1 }) can help with buffering issues. Additionally, make sure your Ubuntu system has the latest audio codecs installed - I had to install libavcodec-extra on my Ubuntu setup to resolve similar streaming problems. The 10-second delay before the error suggests the connection starts fine but fails during playback, which points to encoding issues rather than network problems.