Convert m4a to mp3

I’ve been moving a bunch of my brothers music out of iTunes for him so he can use it with portable players besides iPod. Unfortunately he encoded a lot of his cd’s in .m4a format. I found a decent utility for converting to mp3 (and other formats) http://www.bonkenc.org/

Unfortunately when you point Bonk at a directory full of m4a’s it crashes on certain files for some reason (encoding issues probably.) After the crash you need to setup all of your settings and add the files again, which is really time consuming and annoying.

To make this easier I whipped up a short python script that calls Bonk for you on each file, if it runs into a bad file it will rename the file for you so you don’t try to process it again.

Here’s the python code:

import os
import pprint
import subprocess
curDir = os.getcwd() # The current directory. This should contain your .m4a files
pathToBonk = "C:\\Program Files\\BonkEnc\\becmd.exe" #Where the becmd.exe file lives
problemFiles = [] #A list of files that failed conversion
#
for item in os.listdir(curDir):
	if item.upper().endswith('.M4A'):
		fullPath = os.path.join(curDir,item)
		cmd = '"%s" -e LAME -d "%s" "%s"' #The command to convert a single file
		cmd = cmd % (pathToBonk, curDir, fullPath)
		val = subprocess.call(cmd)
		if val == 0: #Successfull conversion, delete the original
			os.remove(fullPath)
		else:
			problemFiles.append(fullPath)
			print 'Problem converting %s' % item
			os.rename(fullPath, fullPath + ".BAD")
print 'These files had problems converting and have been renamed with .BAD extensions:'
pprint.pprint(problemFiles)

NOTES: This will delete the .m4a file after converting it. If you want to keep your old files for some reason make sure to run this on a copy of your files. IE in a different directory.

Posted: June 6th, 2007
Categories: Code, Technical, audio, m4a, mp3, python, snippet, utility
Tags:
Comments: 6 Comments.
Comments
Comment from Kyle Callahan - June 14, 2007 at 3:57 pm

Does all this python shit work on OS X?

Comment from Joshua Bloom - June 17, 2007 at 7:12 pm

It sure does Kyle. Thats one of the upsides to python development, it works across most platforms pretty easily.

In this case though I’m using Python to automate a windows specific application, so this particular use would need to be updated for Mac usage.

I should be getting my new work machine on Tuesday, so if I end up with a Mac you’ll see some more focus on Mac centric stuff here.

-Josh

Comment from teneisha - September 6, 2007 at 8:59 am

oooh, i have been looking for something to convert my .m4a files…

Comment from Liron - January 2, 2010 at 4:02 pm

Hi,
Great Code.
If I want to convert my files to a lower bitrate- what changes do I need to do in the Command Line Parameters?
Also, if I want to convert aac or other formats to .Mp3 and not only .M4a, what changes do I need to do?

where do i find all the Command Line Parameters?

Thanks in advance,
Liron.

Comment from jbloom - January 8, 2010 at 2:12 pm

Hi Liron,

Unfortunately I’m not working on MS Windows anymore so I don’t have any insight into the command line params for Bonk.

Best,
Josh

Comment from Luke Carrier - March 31, 2010 at 8:09 am

I liked this idea so much that I wrote a script based on it that’s a little more complete. If anyone’s interested, take a look at:

http://lukecarrier.me/2010/03/13/automatically-convert-your-itunes-library-to-mp3-with-bonkenc-and-python/

Thanks for a great idea!