How to convert m4a to wav in python?

5.1k Views Asked by At

I used this code to convert the m4a file that I have and the audio file is in the same directory as my code, but the code can't find the file.

m4a_file = '1.m4a'
wav_filename = 'output.wav'

sound = AudioSegment.from_file(m4a_file, format='m4a')
sound.export(wav_filename, format='wav')
1

There are 1 best solutions below

2
Ali Massoud On

First, make sure you have ffmpeg installed:

sudo apt install ffmpeg

Then check if you have ffprobe, if not:

pip install ffprobe

pydub needs ffmpeg to work correctly.

lastly, download pydub:

pip install pydub

Then run the code you have, or this sample code:

from pydub import AudioSegment

m4a_file = 'sample-1.m4a' # I have downloaded sample audio from this link https://getsamplefiles.com/sample-audio-files/m4a
wav_filename = 'output.wav'

sound = AudioSegment.from_file(m4a_file, format='m4a')
file_handle = sound.export(wav_filename, format='wav')

I hope this helps!