discordjs / voice

Implementation of the Discord Voice API for discord.js and other JS/TS libraries
Apache License 2.0
328 stars 112 forks source link

Somtimes ffmpeg process is not finished. #194

Open cjh980402 opened 2 years ago

cjh980402 commented 2 years ago
ubuntu     33024   33017  7  8월26 ?      04:34:36 /home/ubuntu/.nvm/versions/node/v16.6.2/bin/node /home/ubuntu/bot/index.js
ubuntu     36962   33024  0  8월26 ?      00:00:01 /home/ubuntu/bot/node_modules/ffmpeg-static/ffmpeg -i - -analyzeduration 0 -loglevel 0 -f s16le -ar 48000 -ac 2 pipe:1
ubuntu     37297   33024  0  8월27 ?      00:00:00 /home/ubuntu/bot/node_modules/ffmpeg-static/ffmpeg -i - -analyzeduration 0 -loglevel 0 -f s16le -ar 48000 -ac 2 pipe:1
ubuntu     44057   33024  0  8월27 ?      00:00:01 /home/ubuntu/bot/node_modules/ffmpeg-static/ffmpeg -i - -analyzeduration 0 -loglevel 0 -f s16le -ar 48000 -ac 2 pipe:1
ubuntu     56265   33024  0  8월28 ?      00:00:03 python /home/ubuntu/bot/node_modules/youtube-dl-exec/bin/youtube-dl https://www.youtube.com/watch?v=PUSkqc1amgI -o - -q -f bestaudio[
ubuntu     56268   33024  0  8월28 ?      00:00:20 /home/ubuntu/bot/node_modules/ffmpeg-static/ffmpeg -i - -analyzeduration 0 -loglevel 0 -f s16le -ar 48000 -ac 2 pipe:1

Even if 3 songs are finished, 3 ffmpeg processes are not ended. Is there any solution?

Further details:

amishshah commented 2 years ago

Please post code that can reproduce this situation

cjh980402 commented 2 years ago
// create AudioResource
async function songDownload(url) {
    const ytdlProcess = ytdl(
        url,
        {
            o: '-',
            q: '',
            f: 'bestaudio[ext=webm+acodec=opus+asr=48000]/bestaudio',
            r: '100K'
        },
        { stdio: ['ignore', 'pipe', 'ignore'] }
    );

    const stream = ytdlProcess.stdout;
    if (!stream) {
        throw new Error('no stdout');
    }

    const onError = () => {
        if (!ytdlProcess.killed) {
            ytdlProcess.kill();
        }
        stream.resume();
    };
    ytdlProcess.on('error', onError);

    try {
        const probe = await demuxProbe(stream);
        return createAudioResource(probe.stream, { inputType: probe.type, inlineVolume: true });
    } catch (err) {
        onError();
        throw err;
    }
};

// play the song
try {
    player.play(await songDownload(songs[0].url));
    player.state.resource.volume.setVolume(volume / 100);
} catch {
    sendMessage('failed to play song');
    songs.shift();
    return;
}
iim-ayush commented 2 years ago

Just a recommendation, you can switch to play-dl. Play-dl doesn't require ffmpeg in normal streams.

cjh980402 commented 2 years ago

Thanks for your recommendation. However, I think that ffmpeg used by discordjs/voice module (https://github.com/discordjs/voice/blob/main/src/audio/TransformerGraph.ts)

iim-ayush commented 2 years ago

Thanks for your recommendation. However, I think that ffmpeg used by discordjs/voice module (https://github.com/discordjs/voice/blob/main/src/audio/TransformerGraph.ts)

Can you test again with using this script running in a different ubuntu session after the 1st song has been completed ??

#!/bin/bash

# Check if ffmpeg is running
# -x flag only match processes whose name (or command line if -f is
# specified) exactly match the pattern. 

if pgrep -x "ffmpeg" > /dev/null
then
    echo "Running"
else
    echo "Stopped"
fi
cjh980402 commented 2 years ago

I will check if there is a bug that corresponds to this issue. Thanks!

cjh980402 commented 2 years ago

Even if all song is finished, result of that script is Running

iim-ayush commented 2 years ago

That only means ffmpeg instance is not deleted.

@amishshah

Maybe this is the reason of #164 [ Memory Leak ] .