I tried to load an audio file into librosa. I wrote this code:
import librosa
import librosa.display
import IPython.display
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.font_manager as fm
audio_path = 'C:/Users/ddolcju/PycharmProjects/pitch_project/rec.mp3'
y, sr = librosa.load(audio_path)
But I got the error “File contains data in an unknown format”. I searched online and was told to install ffmpeg. After I installed ffmpeg, the error message still appears. What’s wrong?
The error message “File contains data in an unknown format” indicates that the audio file may not be in a compatible format with librosa. To fix this issue, you can use ffmpeg to convert the audio file to a WAV or FLAC format, which are compatible with librosa. Here’s the modified code:
import librosa
import librosa.display
import IPython.display
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.font_manager as fm
import os
audio_path = 'C:/Users/ddolcju/PycharmProjects/pitch_project/rec.mp3'
# convert audio file to WAV format using ffmpeg
if not os.path.exists('temp.wav'):
os.system(f'ffmpeg -i "{audio_path}" temp.wav')
# load the converted audio file
y, sr = librosa.load('temp.wav')
# delete the temporary file
os.remove('temp.wav')