TooTallNate / node-lame

Node.js native bindings to libmp3lame & libmpg123
MIT License
567 stars 113 forks source link

Error: output buffer too small #79

Open hxfdarling opened 7 years ago

hxfdarling commented 7 years ago

I want to transfrom wav to mp3 ,but i got an error Error: output buffer too small

let     lame = require('lame')
const wav = require('wav')
const fs = require('fs')

function wav2mp3(input, output, next = function() {}) {
    const rs = fs.createReadStream(input)
    const ws = fs.createWriteStream(output)
    const reader = new wav.Reader()

    // the "format" event gets emitted at the end of the WAVE header
    reader.on('format', function(format) {
        const encoder = new lame.Encoder({
            // input
            channels: format.channels, // 2 channels (left and right)
            bitDepth: format.bitDepth, // 16-bit samples
            sampleRate: format.sampleRate, // 44,100 Hz sample rate

            // output
            bitRate: 128,
            outSampleRate: 22050,
            mode: lame.STEREO // STEREO (default), JOINTSTEREO, DUALCHANNEL or MONO
        })
        // the WAVE header is stripped from the output of the reader
        reader.pipe(encoder)
        encoder.pipe(ws)
    })
    // pipe the WAVE file to the Reader instance
    reader.on('finish', () => {
        rs.close()
        ws.close()
        next(null);
    })
    rs.pipe(reader)
}
wav2mp3('./xxx.wav', './xxx.mp3', (err) => {
    console.log('success')
})

files

xxx.zip