'ytdl-core no stream data

I am making a NodeJS Music bot for discord, and I suddenly encountered a problem. The bot properly joins the channel, lights up (indicating it is speaking), but then there's no audio. After trying to find the root cause of the problem, I believe it to be a problem with the ytdl() function from the ytdl-core module.

const stream = await ytdl(song.url, {
    filter: 'audioonly',
    type: 'opus',
    highWaterMark: waterMark
  });

Looking at the result of stream, I found this:

PassThrough {
  _readableState: ReadableState {
    objectMode: false,
    highWaterMark: 524288,
    buffer: BufferList { head: null, tail: null, length: 0 },
    length: 0,
    ...

Which meant that I am not getting any buffer/stream data. It is indeed playing, but because there's nothing-- there's only silence to be heard.

I tried using pipe() and it worked just fine, but I can't play it as it is through my Music Bot.



Solution 1:[1]

ytdl function is like synchronous function.

"ytdl-core": "^4.10.1"

const stream = await ytdl(song.url); pause code execution until ytdl finished downloading stream.

console.log("YTDL Start");
const stream = await ytdl(song.url, {
    filter: 'audioonly',
    type: 'opus',
    highWaterMark: waterMark
});
console.log("Is it done?");

stream.on('close', () => {
  console.log('Read stream closed');
});

stream.on('finish', () => {
  console.log('Read stream Finished');
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Qubiet