从Blob Audio Django创建WAV文件 [英] Create a wav file from blob audio django

查看:38
本文介绍了从Blob Audio Django创建WAV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在客户端,我正在发送Blob音频(wav)文件.在服务器端,我正在尝试将Blob文件转换为音频wav文件.我做了以下事情:

On the client side, I am sending a blob audio (wav) file. On the server side, I am trying to convert the blob file to an audio wav file. I did the following:

blob = request.FILES['file']
name = "TEST.wav"
audio = wave.open(name, 'wb')
audio.setnchannels(1)
audio.writeframes(blob.read())

我认为转换blob类似于将blob图像转换为jpeg文件,但是在这种假设下非常不正确.那行不通;我收到一个错误-错误:未指定样品宽度."然后,我使用了setsampwidth()并以介于1和4之间的任意数字(在查看wave.py源文件之后...我不知道为什么字节必须介于1和4之间).此后,将引发另一个错误-错误:未指定采样率."如何指定采样率?

I thought that converting the blob would be similar to converting a blob image to a jpeg file, but was very incorrect in that assumption. That didn't work; I get an error - "Error: sample width not specified." I then used setsampwidth() and tossed in an arbitrary number between 1 and 4 (after looking at the wave.py source file...I don't know why the bytes have to be between 1 and 4). After that another error is thrown - "Error: sampling rate not specified." How do I specify the sampling rate?

setnchannels(),setsampwidth()方法有什么作用?我可以通过Blob生成WAV文件吗?

What does the setnchannels(), setsampwidth() methods do? Is there an "easy" way I generate the wav file from the blob?

推荐答案

以前,我从未做过..但是,在我的测试中,以下脚本对我来说效果很好.. (音频输出与原始文件不同) .

Previously, I never do it before.. but, in my test this script below is worked well for me.. (But the audio output isn't same like original file).

>>> nchannels = 2
>>> sampwidth = 2
>>> framerate = 8000
>>> nframes = 100
>>> 
>>> import wave
>>> 
>>> name = 'output.wav'
>>> audio = wave.open(name, 'wb')
>>> audio.setnchannels(nchannels)
>>> audio.setsampwidth(sampwidth)
>>> audio.setframerate(framerate)
>>> audio.setnframes(nframes)
>>> 
>>> blob = open("original.wav").read() # such as `blob.read()`
>>> audio.writeframes(blob)
>>> 

我在 https://stackoverflow.com/a/3637480/6396981 中找到了这种方法

最后,通过使用 1 更改 nchannels sampwidth 的值.并且我得到了与原始文件相同的音频.

Finally, by changing the value of nchannels and sampwidth with 1. and I got an audio that same with original file.

nchannels = 1
sampwidth = 1
framerate = 8000
nframes = 1

Python2 下测试,并收到错误 UnicodeDecodeError:'utf-8'编解码器无法解码位置4的字节0x95:无效的起始字节 Python3 上.

Tested under Python2, and got an error UnicodeDecodeError: 'utf-8' codec can't decode byte 0x95 in position 4: invalid start byte on Python3.

这篇关于从Blob Audio Django创建WAV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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