简体   繁体   English

如何在 discord.js 语音 v13 中从语音通道录制音频?

[英]How to record audio from voice channel in discord.js voice v13?

I'm trying to record audio from a voice channel in discord using v13 of discord.js.我正在尝试使用 discord.js 的 v13 从 discord 中的语音通道录制音频。 Here is my code:这是我的代码:

const { joinVoiceChannel } = require("@discordjs/voice");
const fs = require("fs");
var { streams } = require("streams.js");

var voiceChannel = interaction.member.voice.channel;
streams[voiceChannel.id] = fs.createWriteStream("test.pcm");


const connection = await joinVoiceChannel({
                channelId: voiceChannel.id,
                guildId: interaction.guild.id,
                adapterCreator: interaction.guild.voiceAdapterCreator,
                selfDeaf: false
            });
const audio = connection.receiver.subscribe(`${interaction.user.id}`, {encoding: "utf-8"});
audio.pipe(streams[voiceChannel.id]);

then the stop command:然后停止命令:

…
streams[voiceChannel.id].close();
delete streams[voiceChannel.id];

And when I convert the pcm to wav using ffmpeg -f s16le -ar 44.1k -ac 2 -i test.pcm file.wav , it returns a short audio without my voice but the noise "ffffffffffff" Why?当我使用ffmpeg -f s16le -ar 44.1k -ac 2 -i test.pcm file.wav将 pcm 转换为 wav 时,它返回一个没有我的声音的短音频,但噪音“ffffffffffff”为什么? Thank you for your help.谢谢您的帮助。

Try this:尝试这个:

const receiver = connection.receiver.subscribe(userId, { end: { behavior: EndBehaviorType.AfterSilence, duration: 100 } });
const decoder = new opus.Decoder({ frameSize: 960, channels: 2, rate: 48000 });
const stream = receiver.pipe(decoder).pipe(createWriteStream("./test.pcm"))
stream.on("finish", () => {
    exec("ffmpeg -y -f s16le -ar 44.1k -ac 2 -i test.pcm test.mp3")
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM