I'm working on an audio-related project that connects with Django backend via rest api. Part of the front-end requires to display waveforms of associated mp3 files and for this, it in turn requires optimized data of each mp3 file in form of an array, which the front-end (javascript) then processes and converts to a waveform. I can pick the associated mp3 file from backend storage, the problem is converting it into an array which I can serve to the front-end api. I have tried several methods but none seem to be working. I tried this How to read a MP3 audio file into a numpy array / save a numpy array to MP3? which leaves my computer hanging until I forced it to restart by holding the power button down. I have a working ffmpeg and so, I have also tried this Trying to convert an mp3 file to a Numpy Array, and ffmpeg just hangs which continues to raise TypeError on np.fromstring(data[data.find("data")+4:], np.int16)
. I can't actually say what the problem is and I really hope someone can help. Thank you in advance!
EDIT This is the django view for retrieving the waveform data:
NB: I've only included useful codes as I'm typing with my mobile phone.
def waveform(self, request, ptype, id):
project = Project.objects.get(pk=id)
audio = project.audio
mp3_path = os.path.join(cdn_dir, audio)
cmd = ['ffmpeg', '-i', mp3_path, '-f', 'wav', '-']
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, creationflags=0x8000000)
data = p.communicate()[0]
array = np.fromstring(data[data.find("data")+4:], np.int16)
return Response(array)
The TypeError I get is this:
TypeError: argument should be integer or bytes-like object, not "str"