Reputation: 31
General information:
I have a project in python that deals with audio classification; the backend is complete but I'm suffering with the front-end. The backend requirement (that I can't change) is that the audio file must be in wav format but I want users to be able to upload mp3 files as well. My front-end is a web server using python 2.7 and flask.
So, I basically want to convert mp3 to wav, but keep on getting errors.
(The complete code is at the bottom in case it is needed to understand the problem more clearly)
My attempts:
1- I used pydub library
I installed homebrew, libav, and ffmpeg
libav installation method: brew install libav --with-libvorbis --with-sdl --with-theora
ffmpeg installation method: brew install ffmpeg --with-libvorbis --with-ffplay --with-theora
Method1
sound = AudioSegment.from_file(filename[i], format="mp3") #filename[i]=nameOfFile
sound.export("input.wav", format="wav")
Method2
AudioSegment.from_file(filename[i], format="mp3").export("input.wav", format="wav")
=> Kept getting "file not found" and "cannot detect ffmpeg or avconv" runtime warning even though I installed ffmpeg and libav
=> Got same error above ("file not found") when I used "from_mp3" instead of "from_file"
=> Tried using "raw" instead of "mp3" and got "key error: sample_width" (couldn't find what this error meant)
Note: I made sure I'm in the right directory
2- Used subprocess
import subprocess
subprocess.call(["ffmpeg", "-i",filename[i],"inputAudio.wav"])
=> Got "OSError: No such file or directory"
I hope you can help me understand what the problem is and how to solve it...
Complete Code:
I have this at the top
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
os.chdir(APP_ROOT)
Inside the function that deals with the audio files
data = request.files.getlist('file') #get all uploaded audio files
fsize = len(data) #number of audio files
i = 0 #index counter
filename = ["" for x in range(fsize)] #LIST TO STORE EACH FILE'S NAME
audiofile = ["" for x in range(fsize)] #LIST TO STORE CLASSIFICATION RESULTS OF EACH FILE
#LOOP THROUGH EACH UPLOADED FILE
for file in data:
filename[i] = file.filename #ADD FILENAME TO LIST ABOVE
destination = str(APP_ROOT)
Problematic Part:
if file.filename.endswith(".mp3"):
from pydub import AudioSegment
t = destination + "/" + filename[i]
file.save(t) #SAVE UPLOADED MP3 FILE TO EXTRACT IT USING PYDUB
sound = AudioSegment.from_file(filename[i], format="mp3")
sound.export("input.wav", format="wav")
os.remove(t) #DELETE MP3 FILE, WE ONLY WANT WAV
destination += "/inputAudio.wav"
Code Continuation:
#STORE AUDIO FILE TO PREPARE FOR PROCESSING (CLASSIFICATION)
else:
destination += "/inputAudio.wav"
file.save(destination)
#FINAL STEP
audiofile[i]=Raudio.start() #AUDIO PROCESSING (CLASSIFICATION)
os.remove(destination) #DELETE AUDIO FILE TO PREVENT CLUTTERING OF FILES
i += 1 #INCREMENT FILE INDEX
Upvotes: 1
Views: 1550
Reputation: 31
Okay so I have found a solution to this problem.
I ended up downloading the FFmpeg binary from this site.
Then I just copy-pasted all of the contents of the downloaded file in the following path:
Macintosh HD/Library/Frameworks/Python.framework/Versions/2.7
This is also where the python exec is and I remember reading somewhere that the problem may be that ffmpeg couldn't be found in the path of python or something.
So.... It finally works! Thought I'd share the solution as this problem has taken up most of my weekend and I hope it can help someone else solve their issues,
Upvotes: 2