You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ytdl-core返回无stream流数据,Discord音乐机器人播放无声音问题排查

故障解决方案

Discord语音模块对流的编码格式、状态校验要求高于普通流写入操作,因此会出现本地pipe测试正常但机器人无音频输出的情况,可按以下步骤排查修复:

1. 升级ytdl-core版本

YouTube会不定期更新接口规则,旧版本ytdl-core大概率会出现返回空PassThrough流的问题,执行命令更新到最新正式版:
npm install ytdl-core@latest
更新后未解决可尝试安装开发版:
npm install ytdl-core@nightly

2. 调整ytdl调用参数

移除原代码中的type: 'opus'参数,同时删除不必要的await关键字(ytdl直接返回可读流,不需要异步等待),修改后代码参考:

const stream = ytdl(song.url, {
    filter: 'audioonly',
    quality: 'highestaudio',
    highWaterMark: 1 << 25
});

3. 适配Discord语音模块流格式要求

若使用@discordjs/voice库(对应discord.js v13及以上版本),不能直接传入ytdl返回的流,需通过createAudioResource封装并指定流类型:

const { createAudioResource, StreamType, createAudioPlayer } = require('@discordjs/voice');
const player = createAudioPlayer();
const audioResource = createAudioResource(stream, {
    inputType: StreamType.Arbitrary
});

4. 等待流就绪后再播放

避免流未完成加载就触发播放导致的空输出,监听流的readable事件确认有数据后再执行播放操作:

stream.once('readable', () => {
    player.play(audioResource);
});

内容的提问来源于stack exchange,提问作者Shirayuki Yumi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 04:24:05