如何从javascript发送音频blob到python? [英] How to send audio blob from javascript to python?

查看:151
本文介绍了如何从javascript发送音频blob到python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JS中的音频blob发送到python脚本(在服务器上运行)。
My JS ajax ..看起来像这样。

I want to send a audio blob from JS to python script (which runs on server). My JS ajax .. looks something like this.

var fileType = 'audio';
var fileName = 'output.wav';
var formData = new FormData();
formData.append(fileType + '-filename', fileName);
formData.append(fileType + '-blob', blob);
    $.ajax({
    type: 'POST',
    url: 'http://localhost/python/audio.py',
    data: {audio:formData},
        success: function(response) {
        alert(respose);
    }
   }); 

我的python脚本看起来像这样。

and my python script looks like this.

#!/usr/bin/python3
print("Content-Type: text/html")
print()
import ssl
import cgi
import wave
import contextlib
form = cgi.FieldStorage()
fname = form.getvalue("audio", "error")
with contextlib.closing(wave.open(fname,'r')) as f:
    frames = f.getnframes()
    rate = f.getframerate()
    duration = frames / float(rate)
    print(duration)

现在,我只是测试,所以它应该告诉我音频文件的持续时间。 blob是通过record.js生成的。

Right now, I am just testing, so it should get me the duration of the audio file. The blob is generated through record.js

这不起作用,因为python无法识别文件。任何解决方案?

This is not working, as the python is unable to identify the file. Any solutions?

PS:我正在使用Xampp Server,在本地主机上运行。

PS: I am using Xampp Server, to run on local host.

作为回应Wojtek Marczenko:错误是

In response to Wojtek Marczenko: The error is

[Mon Apr 04 18:26:09.537912 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: Traceback (most recent call last):: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.537978 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:   File "/home/shashank/project/dutchman/python/audio.py", line 10, in <module>: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.538002 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:     with contextlib.closing(wave.open(fname,'r')) as f:: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.538024 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:   File "/usr/lib/python3.5/wave.py", line 499, in open: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.538036 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:     return Wave_read(f): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.538056 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:   File "/usr/lib/python3.5/wave.py", line 163, in __init__: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.538065 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:     self.initfp(f): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.538086 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:   File "/usr/lib/python3.5/wave.py", line 128, in initfp: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.538097 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:     self._file = Chunk(file, bigendian = 0): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.538110 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:   File "/usr/lib/python3.5/chunk.py", line 61, in __init__: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.538119 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:     self.chunkname = file.read(4): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html
[Mon Apr 04 18:26:09.538132 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: AttributeError: 'NoneType' object has no attribute 'read': /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html


推荐答案

看起来你没有正确发送将blob作为表单字段。将blob附加到FormData的正确方法是 formData.append(fileType,blob,fileName); 。此外,您应该只附加formData而不是将其嵌套在另一个对象中:

It looks like you're not properly sending the blob as form field. The proper way to attach blob to FormData would be formData.append(fileType, blob, fileName);. Also you should attach just the formData instead of nesting it in another object:

var formData = new FormData();
formData.append(fileType, blob, fileName);
$.ajax({
    type: 'POST',
    url: 'http://localhost/python/audio.py',
    data: formData,
    processData: false,  // prevent jQuery from converting the data
    contentType: false,  // prevent jQuery from overriding content type
    success: function(response) {
        alert(response);
    }
});

来源:
https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
http://www.mattlunn .me.uk / blog / 2012/05 / sending-formdata -with-jquery-ajax /

在python方面,你需要使用CGI模块根据python文档(不能发布更多链接)。我认为正确的方法是这样的:

On the python side, you need to use the CGI module according to the python docs (can't post more links). I believe the proper way would be like this:

form = cgi.FieldStorage()
fname = form["audio"].filename
print "Got filename:", fname  # in case of problems see if this looks ok

with contextlib.closing(wave.open(fname,'r')) as f:
    ...

这篇关于如何从javascript发送音频blob到python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆