Zwoosks
Zwoosks

Reputation: 51

Converting a PCM file to .mp3 (Nodejs 12)

I'm making a Discord bot and at one point I have to save an audio with the PCM format

voiceChannel.join().then(connection =>
  {
    const fs = require('fs');
    // Create a ReadableStream of s16le PCM audio
    var usee = message.member;
    const audio = connection.receiver.createStream(usee, { mode: 'pcm' });
    var idAudio = makeid(10);
    audio.pipe(fs.createWriteStream(idAudio + '.pcm'));

(idAudio is a random generated string). The thing is that I need to convert the PCM file to MP3. I already tried to use node-lame but didn't work. I'm sure I'm missing something. The PCm is a signed 16-bit little-endian (s16le) PCM

const Lame = require("node-lame").Lame;

    const encoder = new Lame({
        output: "./audio-files/demox122.mp3",
        bitrate: 192
    }).setFile("./user_audio.pcm");

    encoder
        .encode()
        .then(() => {
            // Encoding finished
            console.error("all good");
        })
        .catch(error => {
            // Something went wrong
            console.error("error");
        });

Upvotes: 5

Views: 1810

Answers (0)

Related Questions