If I have an MP3 file how can I convert it to a WAV file? (preferably, using a pure python approach)
-
1Decompressing MP3s is not an appropriate task to implement in Python.– Glenn MaynardJun 16, 2010 at 0:46
-
3To the best of my knowledge, nobody has ever attempted to write an MP3 decoder in Python. The resulting decoder would be terribly slow, and there is no sense in the project anyway; the more natural thing would be to make a Python module that wraps a C library. Re-use the already-written and already-debugged C code, and don't try to reinvent the wheel. I love Python too, but there are some projects that are not appropriate for Python and an MP3 decoder is one.– stevehaJun 16, 2010 at 2:05
5 Answers
I maintain an open source library, pydub, which can help you out with that.
from pydub import AudioSegment
sound = AudioSegment.from_mp3("/path/to/file.mp3")
sound.export("/output/path/file.wav", format="wav")
One caveat: it uses ffmpeg to handle audio format conversions (except for wav files, which python handles natively).
note: you probably shouldn't do this conversion on GAE :/ even if it did support ffmpeg. EC2 would be a good match for the job though
-
You have no idea how much awesome this has made my project. Thank you! :)– MakotoFeb 22, 2013 at 5:43
-
Doesn't this convert wav to mp3? The question asks for mp3 to wav. Dec 20, 2013 at 5:12
-
@someone-or-other oops you're right, though it's just a matter of changing the formats mentioned in the code (I've fixed it now)– JiaaroDec 20, 2013 at 18:06
-
1this will give an error 3rd line should be sound.export("/output/path/file.wav", format="wav") Jan 11, 2018 at 4:42
-
2@NikhilWagh The most common reason to get that error is because the ffmpeg executable can't be found– JiaaroJun 11, 2018 at 18:11
This is working for me:
import subprocess
subprocess.call(['ffmpeg', '-i', 'audio.mp3',
'audio.wav'])
-
-
No matter how I do it it gives me "FileNotFoundError: [WinError 2] The system cannot find the file specified"– FrobotJan 19 at 16:25
-
I think I am right person to answer this question because I am student who tried hard to get answer for this question. I am giving answer for Windows users but I think this may work with MAC OS too. But apt for windows.
Lets discuss answers in steps:
first check for pydub and ffmpeg package. If you computer dont have these packages then install pydub in you command prompt
pip install pydub
Next and imp thing is ffmpeg package which converts images to different formats. For this you should manually install this package. Let me give you reason why when we can use pip for installing package. First pip installs the package but it will not stores the path to the system. So computer cant recognize this package path. For this I suggest you to install manually but how.... dont worry will give you steps.
STEP 1:
#Present link
This first link that you have paste it in google
https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip
#Use for future students
But people will have question now this link might work what about future. For that simple answer is
https://www.gyan.dev/ffmpeg/builds/
After typing this in google go to releases and download zip folder always don't download 7.zip.But thus is only when my first link will not work for future is any student search for answer.
STEP 2: After downloading the zip file from first step first link. Now make a folder in C drive. For this just click on my My PC, then OS(C:),make a new folder. Copy paste the zip file downloaded to this folder. Extract the zip file in this new folder. Now go into the folder and copy path of "bin" present in this folder from properties.
STEP 3:This is final step and imp one where you will set path. In search bar in your laptop search for "Edit the system environmental variables". Then click on "environmental variables" at bottom for path. Here they are two parts in screen system variables and user variables. Now you have to search for path "Path" in system variable is you want to use for whole system. Double click on "Path" in system variables. A window appears where you have to choose "New". Here copy paste the path of bin folder. Then click on Ok in all and close all tabs.
Step 4:Check for correct installation of ffmpeg. In command prompt type ffmpeg now you will get the list of paths and its features. This shows you have finished your installation.
Step 5 : Download a mp3 file. If your have downloaded python then open IDLE prompt. The click on new in File a note pad appears. One imp point to remember here is copy paste the mp3 file where you python code is stored. Example If I want to save the python file in Desktop the mp3 file should be stored in desktop. I think you go an idea. Now copy paste the code which I am using
import subprocess
subprocess.call(['ffmpeg', '-i', 'ind.mp3','ind1.wav'])
then click on run module
you will get the conversion.
Thank you
This answer might help you. If you want code and method for converting speech to text code and method you can post me. I wish this answer for 10 min may save you hours.
https://www.youtube.com/watch?v=vBb_eYThfRQ
use this video for path configuration or step 3 for reference but copy path to system variables not user because whole system can use this package then. If my language is bad don't mind I think it is understandable.
Install the module pydub
. This is an audio manipulation module for Python. This module can open many multimedia audio and video formats. You can install this module with pip
.
pip install pydub
If you have not installed ffmpeg yet, install it. You can use your package manager to do that.
For Ubuntu / Debian Linux:
apt-get install ffmpeg
When ready, execute the below code:
from os import path
from pydub import AudioSegment
# files
src = "transcript.mp3"
dst = "test.wav"
# convert wav to mp3
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")
Check this link for details.
For Those using windows 7 and above:
Step 1: This link will help you install ffmpeg: How to Install FFMPEG on Windows
Step 2: This code will help you convert multiple files from one format to another ( which of course is supported by ffmpeg)
import os
import subprocess
input_dir = r'C:\\Path\\To\\Your\\Input\\Directory\\'
output_dir = r'C:\\Path\\To\\Your\\Output\\Directory\\'
path_to_ffmpeg_exe = r'C:\\Path\\To\\ffmpeg-2022-YY-MM-git-blabla-full_build\\bin\\ffmpeg.exe'
files_list = []
for path in os.listdir(input_dir):
if os.path.isfile(os.path.join(input_dir, path)):
files_list.append(path)
for file_nm in files_list:
print(file_nm)
subprocess.call([path_to_ffmpeg_exe, '-i', os.path.join(input_dir, file_nm), os.path.join(output_dir, str(file_nm.split(".")[0] + ".wav"))])
-
For Step 1, provided link is not accessible right, all you need to do is check out the official builds & releases here; github.com/BtbN/FFmpeg-Builds/releases download the latest one, At the time of writing this, the latest build is from github.com/BtbN/FFmpeg-Builds/releases/download/latest/….– BenMay 31 at 6:08