my question is similar to Get.wav file length or duration .
MediaRecorder
in js frontend to record audioI want to apply a length limit of 30 seconds , but all I have is a blob, should be stored in 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
).
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:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.