【问题标题】:ytdl-core no stream dataytdl-core 没有流数据
【发布时间】:2022-02-23 22:57:50
【问题描述】:

我正在为discord制作一个NodeJS音乐机器人,突然遇到一个问题。机器人正确加入频道,亮起(表示它正在讲话),但随后没有音频。在尝试找到问题的根本原因后,我认为是ytdl-core模块中的ytdl()函数有问题。

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

查看流的结果,我发现了这个:

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

这意味着我没有得到任何缓冲区/流数据。它确实在播放,但因为什么都没有——只有寂静可以听到。

我尝试使用pipe(),效果很好,但我无法通过我的音乐机器人播放它。

【问题讨论】:

    标签: node.js youtube discord discord.js


    【解决方案1】:

    ytdl 函数类似于同步函数。

    "ytdl-core": "^4.10.1"

    const stream = await ytdl(song.url); 暂停代码执行,直到ytdl 完成下载流。

    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');
    });
    

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 2021-08-03
      • 1970-01-01
      • 2021-03-18
      • 2020-12-30
      • 2016-12-13
      • 2018-12-21
      • 2018-07-27
      • 2021-12-06
      相关资源
      最近更新 更多