最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to write .wav file from blob in JavascriptNode - Stack Overflow

programmeradmin2浏览0评论

I'm trying to write a .wav file with fs.writeFile. The file is created successfully, however it's only 8-13bytes long, so obviously I'm not doing something right.

If the blob is already audio/wav can I write to disk or do I need to convert it to Base 64?

I'm pretty much at a loss here, I found another similar thread with no answer - Here

Any input would be appreciated.

routerApp.controller('audiotest', function($scope) {
 $scope.saveToDisk = function(){
  var nw = require('nw.gui');
  var fs = require('fs');
  var path = require('path');
  fs.writeFileSync('test.wav', $scope.recordedInput)
 };
}

console.log($scope.recordedInput) returns Blob {size: 294956, type: "audio/wav"}

It's not really relevant, but here's my HTML

<div class="row" ng-controller="audiotest">
<div class="row">
    <button type="button" ng-click="saveToDisk()"> Write this sucker to disk </button>
    </div>

<ng-audio-recorder id='audioInput' audio-model='recordedInput'>
  <!-- Start controls, exposed via recorder-->
  <div ng-if="recorder.isAvailable">
    <button ng-click="recorder.startRecord()" type="button" ng-disabled="recorder.status.isRecording">
        Start Record
    </button>
    <button ng-click="recorder.stopRecord()" type="button" ng-disabled="recorder.status.isRecording === false">
        Stop Record
    </button>

</ng-audio-recorder>
</div>

I'm trying to write a .wav file with fs.writeFile. The file is created successfully, however it's only 8-13bytes long, so obviously I'm not doing something right.

If the blob is already audio/wav can I write to disk or do I need to convert it to Base 64?

I'm pretty much at a loss here, I found another similar thread with no answer - Here

Any input would be appreciated.

routerApp.controller('audiotest', function($scope) {
 $scope.saveToDisk = function(){
  var nw = require('nw.gui');
  var fs = require('fs');
  var path = require('path');
  fs.writeFileSync('test.wav', $scope.recordedInput)
 };
}

console.log($scope.recordedInput) returns Blob {size: 294956, type: "audio/wav"}

It's not really relevant, but here's my HTML

<div class="row" ng-controller="audiotest">
<div class="row">
    <button type="button" ng-click="saveToDisk()"> Write this sucker to disk </button>
    </div>

<ng-audio-recorder id='audioInput' audio-model='recordedInput'>
  <!-- Start controls, exposed via recorder-->
  <div ng-if="recorder.isAvailable">
    <button ng-click="recorder.startRecord()" type="button" ng-disabled="recorder.status.isRecording">
        Start Record
    </button>
    <button ng-click="recorder.stopRecord()" type="button" ng-disabled="recorder.status.isRecording === false">
        Stop Record
    </button>

</ng-audio-recorder>
</div>
Share Improve this question edited May 23, 2017 at 11:47 CommunityBot 11 silver badge asked Sep 8, 2016 at 15:42 angularchoboangularchobo 3131 gold badge5 silver badges17 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

You can convert the Blob to a Typed Array and then to a Buffer for passing directly to fs.writeFileSync():

var fileReader = new FileReader();
fileReader.onload = function() {
  fs.writeFileSync('test.wav', Buffer.from(new Uint8Array(this.result)));
};
fileReader.readAsArrayBuffer($scope.recordedInput);

I had issues getting either of these to work for me. I found success, and much easier implementation with this node module express-fileupload. Here's some code:

var express = require('express');
var fileUpload = require('express-fileupload');

app.use(fileUpload());

app.post('/', function(req, res) {


  console.log(req.files.data);
  req.files.data.mv('test.wav', function(err) {
    if (err) {
      console.log(err);
    }
  });
});

Using mscdex answer.

This is what worked for me when using NodeWebkit as browser.

var fileReader = new FileReader();
fileReader.onload = function () {
       fs.writeFileSync('test.wav', Buffer(new Uint8Array(this.result)));
};
fileReader.readAsArrayBuffer(blob);

Notice the "from" method of Buffer has disappear. And "blob" that is passed in my last line is audio data encoded in wav.
For the rest, its only magic. Don't ask me...

发布评论

评论列表(0)

  1. 暂无评论