[英]How to send audio blob from javascript to python?
I want to send a audio blob from JS to python script (which runs on server).我想将音频 blob 从 JS 发送到 python 脚本(在服务器上运行)。 My JS ajax .. looks something like this.我的 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);
}
});
and my python script looks like this.我的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)
Right now, I am just testing, so it should get me the duration of the audio file.现在,我只是在测试,所以它应该让我知道音频文件的持续时间。 The blob is generated through record.js blob 是通过 record.js 生成的
This is not working, as the python is unable to identify the file.这不起作用,因为 python 无法识别文件。 Any solutions?有什么解决办法吗?
PS: I am using Xampp Server, to run on local host. PS:我正在使用 Xampp Server,在本地主机上运行。
In response to Wojtek Marczenko: The error is回应 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
It looks like you're not properly sending the blob as form field.看起来您没有正确地将 blob 作为表单字段发送。 The proper way to attach blob to FormData would be formData.append(fileType, blob, fileName);
将 blob 附加到 FormData 的正确方法是formData.append(fileType, blob, fileName);
. . Also you should attach just the formData instead of nesting it in another object:此外,您应该只附加 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);
}
});
Sources: https://developer.mozilla.org/en-US/docs/Web/API/FormData/append http://www.mattlunn.me.uk/blog/2012/05/sending-formdata-with-jquery-ajax/资料来源: https ://developer.mozilla.org/en-US/docs/Web/API/FormData/append http://www.mattlunn.me.uk/blog/2012/05/sending-formdata-with-jquery -阿贾克斯/
On the python side, you need to use the CGI module according to the python docs (can't post more links).在 python 方面,您需要根据 python 文档使用 CGI 模块(不能发布更多链接)。 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:
...
I had an error in retrieving the audio bytes, it turned out to be a cursor problem so beware.我在检索音频字节时出错,结果是光标问题,所以要小心。
Files in a FormData request can be accessed at request.files
then you can select the file you included in the FormData eg request.files['audio']
.可以通过request.files
访问 FormData 请求中的文件,然后您可以选择包含在 FormData 中的文件,例如request.files['audio']
。
So now if you want to access the actual bytes of the file, in our case 'audio' using .stream
, you should make sure first that your cursor points to the first byte and not to the end of the file, in which case you will get empty bytes.所以现在如果你想访问文件的实际字节,在我们的例子中是使用.stream
的“音频”,你应该首先确保你的光标指向第一个字节而不是文件的末尾,在这种情况下你将得到空字节。
Hence, a good way to do it:因此,一个很好的方法:
file = request.files['audio']
file.stream.seek(0)
audio = file.read()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.