Node.js - I keep getting the following error: Error: ffmpeg stream: write EPIPE

Viewed 5474

I'm currently programming a Discord bot using discord.js, and I'm using ytdl-core to play music. Here is the program I'm using to play music:

const {google} = require('googleapis');
const ytdl = require('ytdl-core');
// Initialise Google API
const youtube = google.youtube({
    version: 'v3',
    auth: MyAuth
});
musicQueue = [] // Queue for playing music
dispatcher = null; // Transmits voice packets from stream

module.exports = {
    name: "play",

    async execute(msg, args) { // msg is a Discord Message
        // Play music and music queued after
        async function playAndQueue(stream) {

            // Join voice channel
            voiceChannel = client.channels.cache.find(channel => channel.type === "voice" && channel.name === "music-channel");
            voiceConnection = voiceChannel.join();

            dispatcher = await voiceConnection.play(stream, {volume: 0.3}); // Decrease volume to prevent clipping

            // When music stops
            dispatcher.on("finish", async reason => {
                if (musicQueue[0]) { // Still have music queued
                    const nextVideoLink = musicQueue[0]; // Next video to play
                    const stream = ytdl(nextVideoLink, {filter: 'audioonly'});

                    playAndQueue(stream);
                    dispatcherInfo = await ytdl.getInfo(nextVideoLink);
                    musicQueue.shift();
                } else { // No music to play
                    dispatcher = null;
                    dispatcherInfo = null;
                }
            });

            dispatcher.on("error", console.log);
            dispatcher.on("debug", console.log);

        }

        // Search Youtube using args
        const youtubeSearchResult = await youtube.search.list({
            part: 'snippet',
            type: 'video', // We do not want channels or playlists
            q: args.join(' '),
            maxResults: 1 // We only need first search result
        });
        const youtubeVideo = youtubeSearchResult.data.items[0];
        if (! youtubeVideo) {
            msg.channel.send("Error: Could not find any music matching search.");
            return;
        }

        const videoLink = `https://www.youtube.com/watch?v=${youtubeVideo.id.videoId}`; // Link to video

        const stream = ytdl(videoLink, {filter: 'audioonly'});
        const videoInfo = await ytdl.getInfo(videoLink);


        if (dispatcher) { // Currently playing music
            musicQueue.push(videoLink);
            msg.channel.send(`**${videoInfo.title}** has been added into the queue!`);
        } else {
            playAndQueue(stream);
            dispatcherInfo = videoInfo;
            msg.channel.send(`Currently playing **${videoInfo.title}**!`);
        }
    }
}

However, when I try to run the program on Heroku, I get this error:

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

What can I do?

2 Answers

After digging a bit, I eventually figured out that the error occurred when I called dispatcher.destroy() in another function. I replaced the code with dispatcher.end() which fixed the issue.

Just for future documentation that may help somebody, I got a similar EPIPE error:

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

I am using FFmpeg on ubuntu/docker nodeJS app with ffmpeg/fluent-ffmpeg/editly. Basically this error happened because my audio stream was not as long as the video file.

Related