I received a wav file from the front end and got a FileStorage object with request.files.get('file'). How can I load it in librosa?
audio = request.files.get('file')
data, sampling_rate = librosa.load(audio)
and the error below showed
TypeError: expected str, bytes or os.PathLike object, not FileStorage
I tried to use read() function
audio = request.files.get('file').read()
but it still doesn't work
ValueError: _getfinalpathname: embedded null character in path
CodePudding user response:
librosa uses soundfile and audioread to read audio, but expects a path to the audio file. If you want to read audio from file-like objects you can use soundfile as well. You can find the documentation for this here.
import soundfile as sf
import io
# ...
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
file = request.files.get('file')
if file:
tmp = io.BytesIO(file.read())
data, samplerate = sf.read(tmp)
# ...
return render_template('upload.html')