Creating a .wav file in Node.js with fs.writeFile&

2019-05-18 10:16发布

问题:

I'm attempting to create a .wav file from a blob of data in Node JS, using fs.writeFile(). Firstly, is this even possible?

I'm currently trying with this...

fs.writeFile(filename + '.wav', blob.recording, function (err) {

    // On completing writeFile, transfer file via scp
    var options = {...};
    var target_path = "the/path"

    if (err)
        return logger.error(err);

    else{

        scp2.scp(options, target_path, function (err) {

            // scp2 callback: always gets here
        });
    }
});

The scp of the written file "completes" OK, as I can see the file in the target location, however, it just shows a file size of 0, see below.

-rw-r--r-- 1 root root      0 Jan 21 01:11 1437629575-00220000.wav

Console output for blob.recording is...

<Buffer 52 49 46 46 4d b9 00 00 57 41 56 45 66 6d 74 20 14 00 00 00 31 00 01 00 40 1f 00 00 59 06 00 00 41 00 00 00 02 00 40 01 66 61 63 74 04 00 00 00 40 8f 03 ...>

scp2: https://github.com/spmjs/node-scp2

I'm thinking I need to encode blob.recording or something. Can anyone point me in the right direction? :-)

Thanks