python,使用recording.js将blob转换为wav文件

时间:2019-04-09 04:59:12

标签: javascript python audio-recording

我正在尝试使用recording.js录制来自网络的音频,然后将blob数据发送到python,以将其另存为wav文件到我的服务器目录中。现在它正在我的服务器中创建wav文件,但是我录制的声音不正确。我不知道哪一部分错了。

JavaScript前端

rec.exportWAV(upload);

function upload(blob) {
  var xhr=new XMLHttpRequest();
  xhr.onload=function(e) {
      if(this.readyState === 4) {
          console.log("Server returned: ",e.target.responseText);
      }
  };
  var filename = new Date().toISOString();
  var fd=new FormData();
  fd.append("audio_data",blob, filename);
  console.log(blob);
  xhr.open("POST","/testFlask/saveAudio",true);
  xhr.send(fd);
}

Python

@app.route("/saveAudio", methods=['GET','POST'])
def hello2():
    if request.method =='POST':
        #form = cgi.FieldStorage()
        fname = request.files['audio_data'].filename
        blob = request.files['audio_data']
        #blob = open("/home/ubuntu/abc/testing.wav").read()
        name = "output.wav"
        #f = open("/home/ubuntu/pabc/audio/text.txt", 'wb')
        #f.close()
        audio = wave.open('/home/ubuntu/abc/audio/'+fname+'.wav', 'wb')

        audio.setnchannels(1)
        audio.setsampwidth(1)
        audio.setframerate(8000)
        audio.setnframes(1)
        audio.writeframes(blob.read())
        audio.close()
        return blob.read()

0 个答案:

没有答案