How to convert MP3 to WAV in Python

Each Answer to this Q is separated by one/two green lines.

If I have an MP3 file how can I convert it to a WAV file? (preferably, using a pure python approach)

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

This is working for me:

import subprocess
subprocess.call(['ffmpeg', '-i', 'audio.mp3',
                   'audio.wav'])

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 = "https://stackoverflow.com/questions/3049572/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"))])


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .