Sunday, November 17, 2024
Google search engine
HomeLanguagesConvert mp3 to wav using Python

Convert mp3 to wav using Python

In this article, we are going to discuss various methods to convert mp3 to wave file format using Python.

Method 1:

First We Need To Install ffmpeg. It Is A Free Open Source Software Project Consist of a Large Suite Of Libraries And Programs For Handling Video, Audio, And Other Multimedia Files.

sudo apt-get install ffmpeg 

So First Let Us Install pydub. This is an Audio Manipulation Module. Python Provides a Module Called pydub to Work With Audio Files. pydub is a Python library to work with only .wav files.

sudo apt-get install -y python-pydub

Program:

Python3




# import required modules
from os import path
from pydub import AudioSegment
  
# assign files
input_file = "hello.mp3"
output_file = "result.wav"
  
# convert mp3 file to wav file
sound = AudioSegment.from_mp3(input_file)
sound.export(output_file, format="wav")


Output:

Here you can see there is a python script And hello.mp3 file which converts it into a result.wav file.

The pydub module uses either ffmpeg or avconf programs to do the actual conversion. So you do have to install ffmpeg to make this work. But if you don’t need pydub for anything else, you can just use the built-in subprocess module to call a convertor program like ffmpeg which is shown in the below method.

Method 2:

It is a simple two-line script or code to convert a mp3 file to wav file.

Here we don’t need the pydub module, we can use builtin the subprocess module to call converter program ffmpeg as shown below:

Program:

Python3




# import required modules
import subprocess
  
# convert mp3 to wav file
subprocess.call(['ffmpeg', '-i', 'hello.mp3',
                 'converted_to_wav_file.wav'])


Output:

As you can see the wave format is generated.

RELATED ARTICLES

Most Popular

Recent Comments