Discord.js录制MP3的语音通道?



我试图在我的discord.js bot中创建一个记录命令。到目前为止我的代码是:

const channel = message.member.voice.channel;
if(!channel) return message.channel.send('Join a VC first!');
const connection = await channel.join();
const receiver = connection.receiver.createStream(message.member, {
mode: "pcm",
end: "silence"
});
const writer = receiver.pipe(fs.createWriteStream('./recording.pcm'));
writer.on('finish', () => {
channel.leave();
message.channel.send('It went quiet, so I left...');
});

recording.pcm保存到我的PC。如果我尝试在windows媒体播放器或任何东西中打开文件,它不识别文件类型。我使用了Audacity导入原始音频功能,我可以听到我的录音,所以我知道它是有效的。然而,给用户这种类型的文件是非常不方便的。我怎么能把这个.pcm文件变成.wav.mp3在node.js?谢谢!

您可以使用ffmpeg- npm i ffmpeg

const ffmpeg = require('ffmpeg');
try {
var process = new ffmpeg('path/to/pcm/file');
process.then(function (audio) {
audio.fnExtractSoundToMP3('path/to/new/file.mp3', function (error, file) {
if (!error) console.log('Audio File: ' + file);
});
}, function (err) {
console.log('Error: ' + err);      
});
} catch (e) {
console.log(e);
}

这将保存新的mp3文件到指定的位置。

最新更新