Skip to content

Instantly share code, notes, and snippets.

@oubiwann
Created October 14, 2012 01:27
Show Gist options
  • Save oubiwann/3886894 to your computer and use it in GitHub Desktop.
Save oubiwann/3886894 to your computer and use it in GitHub Desktop.
WMA to MP3 Conversion for Mac OS X
#!/usr/bin/python
import os
import re
import subprocess
import sys
# script configuration
if sys.platform == "darwin":
MPLAYER_PATH = os.path.join(
"/Applications/Non-Standard/Audio and Video",
"MPlayer OS X 2.app/Contents/Resources/mplayer.app/Contents/MacOS")
# lame was manually installed into /usr/bin
LAME_PATH="/usr/bin"
elif sys.platform == "linux2":
MPLAYER_PATH = "/usr/bin"
LAME_PATH="/usr/bin"
MPLAYER = os.path.join(MPLAYER_PATH, "mplayer")
LAME = os.path.join(LAME_PATH, "lame")
DUMP_FILE = "audiodump.wav"
BACKUP_DIR = "wma"
WORKING_DIR = "/tmp"
def make_audio_dump(filename):
"""
Use mplayer to dump the audio contents of the .wma files as .wav files.
"""
command = ("\"%s\" -nosound -vo null -vc dummy -af resample=44100 -aid 1 "
"-ao pcm:waveheader \"%s\"" % (MPLAYER, filename))
subprocess.call(command, shell=True)
def convert_wav_to_mp3(input, output):
"""
Use lame to convert the .wav files to .mp3 files. Remove the raw .wav file
when done.
"""
command = "\"%s\" -b 256 -h \"%s\" -o \"%s\"" % (LAME, input, output)
subprocess.call(command, shell=True)
os.unlink(input)
def convert_wma_to_mp3(wma_filename):
"""
Given a .wma filename, get a filename for the new .mp3 file based on this,
convert the original to a .wav and then that to an .mp3 file.
"""
mp3_filename = re.sub("\.wma$", ".mp3", wma_filename)
make_audio_dump(wma_filename)
convert_wav_to_mp3(DUMP_FILE, mp3_filename)
def has_wma_files(filenames):
"""
Given a list of filenames, check to see if any of them have the .wma file
extension. If so, return a true value; otherwise, a false one.
"""
for filename in filenames:
if filename.endswith(".wma"):
return True
return False
def convert_wma_files(path):
"""
Walk a given file system directory and all its child directories in order
to find .wma files. If found, convert them to .mp3 files and backup the
originals.
"""
for dir, subdirs, filenames in os.walk(path):
# we don't want to convert files that have already been converted
if os.path.basename(dir) == BACKUP_DIR:
continue
# if there's nothing to do, move on
if not has_wma_files(filenames):
continue
# define and create the backup dir, if it hasn't been already
backup_dir = os.path.join(dir, BACKUP_DIR)
if not os.path.exists(backup_dir):
os.mkdir(backup_dir)
for filename in sorted(filenames):
# on mac os x samba shares, sometimes ._*.wma files are present;
# skip these
if filename.startswith("."):
continue
if filename.endswith(".wma"):
print "Dumping audio for %s ..." % (filename)
wma_filename = os.path.join(dir, filename)
wma_backup = os.path.join(backup_dir, filename)
convert_wma_to_mp3(wma_filename)
os.rename(wma_filename, wma_backup)
if __name__ == "__main__":
path = sys.argv[1]
os.chdir(WORKING_DIR)
convert_wma_files(path)
@sancadia
Copy link

There are lots of WMA to MP3 Converter on the market and most of them are compatible with Mac OS X.

I used to use one of them to convert WMA to MP3, WAV, AAC, M4A, etc. Its name is Faasoft WMA to MP3 Converter. Here is the guide to inform you how to use this program to convert WMA to MP3 at: http://www.faasoft.com/articles/wma-to-mp3-mac-and-windows.html

Hope it can help you too.

@aandreigromov
Copy link

I use Avdshare Audio Converter to convert WMA to MP3 on my mac computer.
In fact, it also has windows version. It also helps to convert wma to other audio format like flac, m4a, mp3, ogg, aac, wav etc.
It even can convert audio to wma format.
It also helps to extract audio from video files.
Here is the step by step guide https://www.avdshare.com/wma-converter-for-mac-and-windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment