将录制的音频从浏览器持久保存为WAV文件到Python API

时间:2019-03-16 22:46:57

标签: python audio base64 wav mediarecorder

我正在做一个项目,要求我:

  1. 从浏览器录制音频(当前通过MediaRecorder API)
  2. 通过HTTP POST发送到Python API
  3. 将此音频数据保存为文件系统中的WAV文件

我目前正在将从浏览器记录的音频编码为base64字符串(从由音频块构成的Blob中编码):

start() {
  return new Promise(resolve => {
     navigator.mediaDevices.getUserMedia({ audio: true })
    .then(stream => {
      this.mediaRecorder = new MediaRecorder(stream);
      this.audioChunks = [];
   this.mediaRecorder.addEventListener('dataavailable', event => {
        this.audioChunks.push(event.data);
      });

      this.mediaRecorder.start();
    });
});

stop() {
   this.mediaRecorder.stop();
}


getData() {
   const blob = new Blob(this.audioChunks, { type: 'audio/wav;codecs=opus' });
   const reader = new FileReader();
   reader.readAsDataURL(blob);
   reader.onloadend = () => {
   const base64data = reader.result;
   this.http.post('http://localhost:5000/test', { audio: base64data }).subscribe(res => {
    console.log(res);
  });
};

}

将其发送到后端后,尝试通过解码base64字符串并使用Python'Wave'包来写入文件来保存音频。但是我只是在生成的文件中变得静态。

def write_to_file(base64_audio):
decoded = base64.standard_b64decode(base64_audio)
print(type(decoded))
with wave.open('temp/test.wav', 'wb') as wav:
    wav.setparams((2, 2, 16000, 0, 'NONE', 'NONE'))
    wav.writeframes(decoded)

任何有关如何解决此问题或改进流程的建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

Python

import base64
import wave

from flask import Flask, request

app = Flask(__name__)


@app.route("/test", methods=["POST"])
def hello():
    data = str(request.form)[59:][:-4].replace(" ", "+").encode()
    write_to_file(data)
    return "Hello World!"


def write_to_file(base64_audio):
    import base64
    wav_file = open("temp.wav", "wb")
    decode_string = base64.b64decode(base64_audio)
    wav_file.write(decode_string)


if __name__ == "__main__":
    app.run("0.0.0.0", 5000)

HTML

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body>
<input type="button" value="start" onclick="start()"></br>
<input type="button" value="stop" onclick="stop()"></br>
<input type="button" value="getData" onclick="getData()"></br>

<script type="text/javascript">
    function start() {
        return new Promise(resolve => {
            navigator.mediaDevices.getUserMedia({audio: true})
                .then(stream => {
                    this.mediaRecorder = new MediaRecorder(stream);
                    this.audioChunks = [];
                    this.mediaRecorder.addEventListener('dataavailable', event => {
                        this.audioChunks.push(event.data);
                    });

                    this.mediaRecorder.start();
                });
        });
    }

    function stop() {
        this.mediaRecorder.stop();
    }


    function getData() {
        let blob = new Blob(this.audioChunks, {type: 'audio/wav;codecs=opus'});
        let reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onloadend = () => {
            let base64data = reader.result;
            console.log(base64data)
            /*
            this.http.post(, {audio: base64data}).subscribe(res => {
                console.log(res);
            });
            */
            $.ajax({
                type: "POST",
                url: 'http://localhost:5000/test',
                data: base64data,
                success: function (data) {
                    alert(data);
                }
            });
        };
    }
</script>
</body>
</html>