[英]python get audio duration from blob
my question is similar to Get.wav file length or duration .我的问题类似于Get.wav 文件长度或持续时间。
MediaRecorder
in js frontend to record audioMediaRecorder
录制音频I want to apply a length limit of 30 seconds , but all I have is a blob, should be stored in django BinaryField
.我想应用30 seconds 的长度限制,但我只有一个 blob,应该存储在 django
BinaryField
中。
since in backend I don't want to create a file in each request, I only want to access the length of the audio, I am grateful for your suggestions.因为在后端我不想在每个请求中创建一个文件,我只想访问音频的长度,我很感激你的建议。
You want to use the io
package to give your in-memory data a file-like interface for other libraries (like wave
).您想使用
io
package 为您的内存数据提供与其他库(如wave
)类似的文件接口。
An example below:下面的一个例子:
from io import BytesIO
with open("sample.wav", "rb") as f:
blob = f.read()
data = BytesIO(blob)
data
can then be treated like any wav file:然后可以将
data
视为任何 wav 文件:
import wave
import contextlib
with contextlib.closing(wave.open(data,'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
print(duration)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.