I'm trying to make a record command in my discord.js bot. My code so far is:
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...');
});
That saves recording.pcm
to my PC. If I try to open the file in windows media player or anything, it doesn't recognise the file type. I used Audacity import raw audio function, and I could hear my recording, so I know it works. However, giving a user that type of file is very inconvenient. How can I turn this .pcm
file into a .wav
or .mp3
in node.js? Thanks!
You could use 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);
}
This should save the new mp3 file to the specified location.