Convert all mp3 into Wav (and viceversa) with Ffmpeg and Python

Ffmpeg

First install ffmpeg, a free reliable software to manipulate audio and video. We’ve talked about this software a loto of times in this blog. In combination with Python is very powerful and handy to use, once you understand how it works, being it just a command line interface software. You want find any GUI for this, but with help of Python we can learn how to do some very useful stuff. Go and check the posts about ffmpeg at the end of this post.

From mp3 to wav

You can easily transform all your mp3 files in wav with the following code.

import os
import glob
# files                                                                         
lst = glob.glob("*.mp3")
print(lst)

for file in lst:
# convert wav to mp3
	os.system(f"""ffmpeg -i {file} -acodec pcm_u8 -ar 22050 {file[:-4]}.wav""")         

From wav to mp3

Just reverse it and you will do the opposite

import os
import glob
# files                                                                         
lst = glob.glob("*.wav")
print(lst)

for file in lst:
# convert wav to mp3
	os.system(f"""ffmpeg -i {file} -vn -ar 44100 -ac 2 -b:a 192k {file[:-4]}.mp3""")

How to install window version of Ffmpeg

Go to the Ffmpeg site and download the window version. Let’s say you want the 64 bit version. After you download the zipped file, unzip it and put it somewhere in your hard drive. Copy the path to your bin directory of ffmpeg and open the System properties:

Click on environment variables (variabili d’ambiente in italiano).

Then:

Click on Path, and

Double click on the old path or click on new and paste di path to the bin directory. Press ok. You’re done.

Check the version of ffmpeg in the cmd

Ffmpeg & Python for videos

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.