back from the hiatus…

Been busy with a paying project that I won’t be posting about here. 

Anyway, this week, I dusted off my Python to write a script to convert all the .WAV files in a tree-structured music library to .MP3 files in parallel structure.  It uses the pydub library (which wraps ffmpeg) to do the heavy lifting.  If you’re looking to do something similar, maybe this library will save you a few minutes.

#wav2mp3.py

import os
from pydub import AudioSegment
from pydub.utils import mediainfo

top_wav_dir = "./WAVS"
top_mp3_dir = "./MP3S"

for root, dirs, files in os.walk(top_wav_dir):
if len(dirs) == 0:
# if there are no subdirs, it's an album...begin

# get the relative location of these wav files
relpath = os.path.relpath(root, start=top_wav_dir)

# setup a "mirror" location for the mp3 files
destination_path = os.path.join(top_mp3_dir, relpath)
if not os.path.exists(destination_path):
print("making new directory", destination_path)
os.makedirs(destination_path)
else:
print("saving to existing directory", destination_path)

# iterate over the .wav files
for filename in files:
if filename.split(".")[-1] == 'wav':
new_filename = filename[0:-4] + ".mp3"
this_wave_filename = os.path.join(root, filename)
this_mp3_filename = os.path.join(destination_path, new_filename)
if not os.path.isfile(this_mp3_filename):
print(" converting", filename)

# open the file
this_track = AudioSegment.from_wav(this_wave_filename, parameters=[])
media_info = mediainfo(this_wave_filename)
this_track.export(this_mp3_filename, format="mp3", bitrate="192k", tags=media_info['TAG'])
else:
print(" MP3 FILE EXISTS!!....skipping")
print()
Updated: December 7, 2018 — 5:05 am

Leave a Reply

Your email address will not be published. Required fields are marked *