如何将音频blob从javascript发送到python
How to send audio blob from javascript to python?
我想将一个音频blob从JS发送到python脚本(在服务器上运行)。我的JS ajax。。看起来像这样。
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脚本是这样的。
#!/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 生成的
这不起作用,因为python无法识别文件。有什么解决方案吗?
PS:我正在使用Xampp服务器,在本地主机上运行。
回应Wojtek Marczenko:错误是
[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,而不是将其嵌套在另一个对象中:
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/appendhttp://www.mattlunn.me.uk/blog/2012/05/sending-formdata-with-jquery-ajax/
在python方面,您需要根据python文档使用CGI模块(不能发布更多链接)。我相信正确的方法是这样的:
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:
...
我在检索音频字节时出错,结果是光标问题,所以要小心。
FormData请求中的文件可以在request.files
访问,然后您可以选择包含在FormData中的文件,例如request.files['audio']
。
因此,现在如果您想访问文件的实际字节,在我们使用.stream
的"音频"中,您应该首先确保您的光标指向第一个字节,而不是文件的末尾,在这种情况下,您将获得空字节。
因此,一个很好的方法:
file = request.files['audio']
file.stream.seek(0)
audio = file.read()
- 为什么在python中使用异常比javascript更容易接受
- 如何访问Web2py'中python代码中的JavaScript变量;s视图
- 调用Javascript中的Python函数,读取静态文件(Flask)
- 如何使用Python/Selenium网络驱动程序处理Angularjs/Javascript下拉列表
- 如何在新创建的模块中从Odoo v8中的Javascript文件中调用python函数
- 创建一个html表单并使用javascript访问中的python脚本
- 当Python在Javascript中发送时,值将转换为UNICODE
- 将dicts数组从python传递到Javascript
- python到javascript正则表达式
- Python Javascript哈希库,以确保JSON对象在传输过程中不会损坏
- Python javascript builder
- Python/JavaScript/HTML 动态创建计算机网络图
- Selenium - Python - Javascript 下拉菜单选项值
- 自动调优/ Python / Javascript
- Selenium Python Javascript Execution
- Selenium python javascript弹出窗口
- Selenium RC / Python / JavaScript .aspx /循环语句
- Python/Javascript数字精度
- Python - JavaScript 网页抓取与硒不能正常工作
- Python/Javascript/AJAX 实时搜索帮助