Pragmattica

January 17, 2008

Convert iTunes M4A files to MP3 on Linux

Filed under: Linux — bnsmith @ 3:00 am

And Keep Some of the Tags!

Update: I have just finished creating a GUI front-end for the conversion program described below. This new program can be installed easily through a package and should be easier to use, so I suggest you follow this link and try the directions there first.

I love iTunes. It was the first web retailer to sell music from the major labels with no DRM. On the night that iTunes Plus first became available, I stayed up late, trying to find some albums that I wanted, just because I wanted to see the folks at iTunes and EMI rewarded monetarily, and wanted to do my bit. Of course, iTunes Plus music comes in non-DRM-encumbered M4A format, which doesn’t play on many portable MP3 players, and doesn’t play very well on others.

There are plenty of posts describing how to convert M4A files to MP3, but most of them completely ignore the information about the artist, album, and so on embedded in the file as tags. I don’t know about you, but I think tags are important. I don’t want to sift through 500 tracks named “unknown” on my MP3 player.

The program that I’ve created will convert a directory full of M4A files to MP3, preserving the artist, album, song name and track number tags. Hopefully this should be enough to play a whole album, in its proper order, with no confusion.

These instructions are specifically for Ubuntu 7.10, but should work on other distros with some modifications.

The first step is to install some required libraries. Open a terminal and type:

 sudo apt-get install id3v2 mplayer lame python-mutagen

Next download my program here, and extract it. Suppose that your iTunes music is in “/home/youruser/itunes”. In your terminal, go to the directory where you extracted the program and enter this command:

./ConvertToMP3.py /home/youruser/itunes

The original files will be left untouched, and new MP3s should be put into a new directory called “/home/youruser/itunes_mp3″. If you have any problems, don’t feel bad about leaving a comment asking for help. In fact, even if you don’t need help, maybe you could just leave a comment anyway. It would be the first comment I’ve ever gotten. ;)

About these ads

94 Comments

  1. Thanks for sharing!

    Comment by scott klarr — January 17, 2008 @ 9:38 am

  2. Does not work
    ERROR: The file ‘/home/username/Folder Name/Long Folder Name/cd: 1: can’t cd to /home/username/Folder’ does not exist. This should never happen; there must be an error in the program. Sorry :)

    So I tried folders with one word:
    merk@ubuntu-desktop:~/Applications$ ./ConvertToMP3.py /home/merk/Music/tm
    Attempting to convert M4A files in ‘/home/merk/Music/tm’
    ERROR: The file ‘/home/username/Music/tm/find: paths must precede expression’ does not exist. This should never happen; there must be an error in the program. Sorry :)

    Comment by Anonymous — January 18, 2008 @ 12:08 pm

  3. Thanks for finding that bug, Anonymous! I’ve uploaded a new version that should fix at least the first of the two issues that you’ve encountered. I was able to replicate the problem with long folder names, but the second problem is a bit more of a mystery. Are you using Ubuntu 7.10 or 7.04?

    Comment by bnsmith — January 18, 2008 @ 2:06 pm

  4. 7.10, I tried the new version but I still get the same second error

    Comment by Merk — January 19, 2008 @ 5:43 pm

  5. Merk, I’m really sorry, but I still haven’t managed to reproduce your issue on my home computers. I’d still like to find out what’s going wrong, though. Perhaps you could help me out by running the following commands and posting their results:
    find --version
    echo $SHELL
    echo $COLORTERM
    /bin/bash --version
    gnome-terminal --version
    python --version
    cd "/home/merk/Music/tm"; find . -type f -iname *.m4a -print

    Comment by bnsmith — January 19, 2008 @ 8:35 pm

  6. AHA! I think I’ve managed to reproduce the problem! Don’t worry about running those commands that I mentioned. I’m going to overhaul the program so that this won’t ever happen again. I’ll post a comment when the new version is complete.

    Comment by bnsmith — January 19, 2008 @ 9:11 pm

  7. I’ve uploaded a new version. The part of the program that was causing problems has been completely rewritten. Merk, I really hope that it will work properly now and do what you need it to do. I’m MUCH more confident in the soundness of the code.

    Comment by bnsmith — January 20, 2008 @ 3:57 am

  8. Attempting to convert M4A files in '/home/merk/Music/tm/cd1'
    Traceback (most recent call last):
      File "./ConvertToMP3.py", line 73, in 
        tagInfo = MP4(src)
      File "/usr/lib/python2.5/site-packages/mutagen/__init__.py", line 75, in __init__
        self.load(filename, *args, **kwargs)
      File "/usr/lib/python2.5/site-packages/mutagen/mp4.py", line 646, in load
        try: self.info = MP4Info(atoms, fileobj)
      File "/usr/lib/python2.5/site-packages/mutagen/mp4.py", line 575, in __init__
        for trak in list(atoms["moov"].findall("trak")):
      File "/usr/lib/python2.5/site-packages/mutagen/mp4.py", line 172, in __getitem__
        raise KeyError, "%s not found" % names[0]
    mutagen.mp4.MP4StreamInfoError: 'moov not found'
    

    Is there maybe a problem with the file names of the m4as? In my case they’re really long and contain non-alphanumeric characters like – and ‘

    merk@ubuntu-desktop:~/Music/tm$ find --version
    GNU find version 4.2.31
    Built using GNU gnulib version 2007-02-24
    Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION 
    merk@ubuntu-desktop:~/Music/tm$ echo $SHELL
    /bin/bash
    merk@ubuntu-desktop:~/Music/tm$ echo $COLORTERM
    gnome-terminal
    merk@ubuntu-desktop:~/Music/tm$ /bin/bash --version
    GNU bash, version 3.2.25(1)-release (i486-pc-linux-gnu)
    Copyright (C) 2005 Free Software Foundation, Inc.
    merk@ubuntu-desktop:~/Music/tm$ gnome-terminal --version
    GNOME gnome-terminal 2.18.2
    merk@ubuntu-desktop:~/Music/tm$ python --version
    Python 2.5.1
    merk@ubuntu-desktop:~/Music/tm$ cd "/home/merk/Music/tm"; find . -type f -iname *.m4a -print
    find: paths must precede expression
    Usage: find [-H] [-L] [-P] [path...] [expression]
    

    Comment by Merk — January 20, 2008 @ 9:05 pm

  9. I have now uploaded yet another version. After some investigation, I found that the error seems to have occurred with a library called ‘mutagen’ that has to do with reading the artist, album… tags from M4A files. This might mean that at least one of your M4A files has corrupted tags or something. Try running the new program on a test directory with 20 or so files, and see how many of them are listed as having errors at the end. If it’s only one file, it’s probably just something wrong with that file specifically. If it’s all of the files, then I’m probably doing something wrong with the way I’m reading the tags. Or there could be a bug in the tag-reading library. Anyway, the default Ubuntu version of that tag-reading library is 1.11, and the newest version of the library is 1.13. If all of your files won’t convert, let me know and I’ll post instructions on how to update the library to the newest version, which might have this bug fixed.

    Comment by bnsmith — January 21, 2008 @ 3:05 am

  10. Well it created the mp3s this time. But they were all 0 bytes, I think it may have to do with bad id3 tags. Some of the files wouldn’t even convert at all.

    Comment by Merk — January 21, 2008 @ 4:34 am

  11. Merk, I think I have an idea for figuring out the bug you’re encountering once and for all. I need to get some of the M4A files that you’re trying to convert. Are they DRM-free M4As from iTunes Plus? If they are, let me know the titles of a couple of failing songs and I’ll buy them off of iTunes as well. If they’re from some other music site, just let me know and I’ll sign up. If you ripped them from CDs or converted them to M4As from another format, I’ll try to replicate your original procedure. If I can get a hold of just a couple of your files, then I’m sure I can fix this (without having to bug you anymore to try running this and that command).

    Comment by bnsmith — January 23, 2008 @ 2:14 pm

  12. This worked like a charm! Thanks!

    Comment by Benton Barnett — January 29, 2008 @ 12:14 am

  13. Hmmm, doesn’t work for me:

    $ ./ConvertToMP3.py /home/james/music
    File “./ConvertToMP3.py”, line 80
    title = tagInfo.tags['\xa9nam'][0] if tagInfo.tags.has_key(‘\xa9nam’) else “”
    ^
    SyntaxError: invalid syntax

    Comment by James — February 16, 2008 @ 9:21 am

  14. James, I’m sorry that it isn’t working for you. I’d be happy to try to figure out what’s wrong, though. If you’d still like to convert your music and haven’t found some alternate method already, perhaps you could look at the commands that I listed in comment #5 up above, and post their results.

    Comment by bnsmith — February 16, 2008 @ 10:01 pm

  15. Does not work!
    I just tried it and it abended with errors:

    Writing LAME Tag…done
    ReplayGain: -1.4dB
    Traceback (most recent call last):
    File “./ConvertToMP3.py”, line 100, in
    subprocess.check_call([u'id3v2', u'-t', title, dest])
    File “/usr/lib64/python2.5/subprocess.py”, line 456, in check_call
    retcode = call(*popenargs, **kwargs)
    File “/usr/lib64/python2.5/subprocess.py”, line 443, in call
    return Popen(*popenargs, **kwargs).wait()
    File “/usr/lib64/python2.5/subprocess.py”, line 593, in __init__
    errread, errwrite)
    File “/usr/lib64/python2.5/subprocess.py”, line 1051, in _execute_child
    raise child_exception
    OSError: [Errno 2] No such file or directory

    Comment by Christian — February 17, 2008 @ 10:42 pm

  16. Hi Christian, I tried a little test and was able to get the same error message as you by un-installing the ‘id3v2′ program. I think that the error you got indicates that either the program isn’t installed or isn’t accessible. If you’re using Ubuntu, you can install the prerequisites with this command:

    sudo apt-get install id3v2 mplayer lame python-mutagen

    Comment by bnsmith — February 18, 2008 @ 12:19 am

  17. Hi bnsmith, thanks a lot for the debugging. I am using fedora 7 and the script is now running fine.

    An additional question: Comparing the tags of the m4a and the mp3 it look like the info of the “artist” and the “genre” gets lost. Any workaround?

    Thanks Christian

    Comment by Christian — February 18, 2008 @ 9:13 pm

  18. Hello again Christian. The genre getting lost is currently expected behavior, as I haven’t written any code to transfer it across. But the artist getting lost is strange… I thought that I was retrieving the artist from the M4A and putting it into the MP3. There’s a chance that the M4A files that you have may be a bit different from the ones I used, and maybe the artist is in a different tag from the one I expect it to be in. Are the files that you’re trying to convert from iTunes Plus? The reason I ask is that I’m hoping you could give me the title and artist of an M4A that isn’t working properly, and then I can go and buy it from iTunes Plus myself and then figure out what’s going wrong. As for the genre problem, if you make use of the genres, I’ll see if I can find some time to add some code for that in the next week or so.

    Comment by bnsmith — February 19, 2008 @ 12:57 am

  19. Very nice program! Thanks for sharing this! I converted my m4a library, and it worked flawlessly!

    Comment by Andrew — February 21, 2008 @ 8:15 am

  20. Wonderful Program. Thanks for sharing this. It worked great. I did have to update my tags, but no big deal.

    Comment by SongofSongs — February 22, 2008 @ 1:41 am

  21. hi bnsmith,

    thanks for your comment and your willingness to improve the script. Please send me a direct email, than I send you the m4p with tag problems.

    Comment by Christian — February 23, 2008 @ 8:23 am

  22. Just What I Need… Thanks so much for this little tool… Had my collection converting within a minute!

    Comment by aheyde — February 24, 2008 @ 1:30 am

  23. Hey,

    Works nicely, though it’d be nice if it had a restart and a little better dir tracking so you could do your itunes lib and maintain artist/album/ distinctions. You can do something like
    ls -d /home/you/Music/*/* | xargs -i python CovertToMp3.py “{}”

    though I had to do:
    ls -d /home/andy/Music/[0124]*/* | perl -pe ‘$_ = quotemeta($_); s/\\$//’ | xargs -i python ConvertToMp3.py ‘{}’

    as some albums and names had quotes, ‘?’ ‘&’ etc in them

    I do see:
    Can’t open joystick device /dev/input/js0: No such file or directory
    Can’t init input joystick
    mplayer: could not connect to socket
    mplayer: No such file or directory
    Failed to open LIRC support. You will not be able to use your remote control.

    might be an mplayer option to stop that. Being able to go to OrgVorbis or something would be nice

    Pretty slick!

    Comment by Andy Bach — March 5, 2008 @ 9:28 pm

  24. Hey there. A while back you posted on a simple script i posted up to convert without tag information.. However I have now written a shell script that will do this and only needs LAME and FAAD2 installed :)

    http://www.scottklarr.com/topic/138/skwizard—linux-m4a-converter-and-file-rename-tool/

    Comment by Scott Klarr — March 7, 2008 @ 8:28 am

  25. [...] Convert iTunes M4A files to MP3 on Linux « Pragmattica Reimplementazione in Python di uno script di conversione da M4A a MP3. Utile perchè riporta i tag del file di origine in quello convertito. (tags: mp3 aac linux shell scripting scripts script) [...]

    Pingback by links for 2008-03-12 « Emanuele Cipolla — March 12, 2008 @ 9:24 pm

  26. I get the following error:

    Traceback (most recent call last):
    File “./ConvertToMP3.py”, line 113, in
    subprocess.check_call([u'id3v2', u'-g', genre, dest])
    File “/usr/lib/python2.5/subprocess.py”, line 456, in check_call
    retcode = call(*popenargs, **kwargs)
    File “/usr/lib/python2.5/subprocess.py”, line 443, in call
    return Popen(*popenargs, **kwargs).wait()
    File “/usr/lib/python2.5/subprocess.py”, line 593, in __init__
    errread, errwrite)
    File “/usr/lib/python2.5/subprocess.py”, line 1135, in _execute_child
    raise child_exception
    TypeError: execv() arg 2 must contain only strings

    Any help would be appreciated

    Comment by kwisher — March 17, 2008 @ 12:46 am

  27. Does your program work on m4p files also or just m4a files?

    Comment by kwisher — March 17, 2008 @ 1:00 am

  28. Hi Kwisher. I recently added Genre support to the conversion script, and it looks like one of the songs you have has some differences in the genre tagging compared to the test songs that I used. If you don’t care about genre tags, the quickest way to get back on track would be to try the older version, downloadable here:

    http://converttomp3.googlecode.com/files/ConvertToMP3.zip

    Of course, I’d like to figure out what my bug is, so if you could let me know what song it failed on, then I could buy it on iTunes and fix the bug properly. Thanks so much for your help.

    Comment by bnsmith — March 17, 2008 @ 1:01 am

  29. Kwisher, this program only works on M4A files that aren’t protected by DRM. If I understand correctly, M4P files have special Apple encryption that I couldn’t even attempt to crack.

    Comment by bnsmith — March 17, 2008 @ 1:03 am

  30. bnsmith,

    Looks like your previous version is working. I would gladly send you a couple of the “problem” files that wouldn’t work with your latest version. I entered my email address when I posted this response. Are you the moderator here and can you see my email addy? If so, drop me a email and I will send you a few songs. In what file order does your program process the files? I noticed the later version failed after creating one mp3 file.

    Thanks for providing this solution.

    Kevin Wisher

    Comment by kwisher — March 18, 2008 @ 2:07 am

  31. I’ve updated the link in the blog post to point to a new version that should fix the aforementioned genre problem. As always, let me know if you find an M4A file that won’t convert, or if you have any other problems.

    Comment by bnsmith — March 20, 2008 @ 1:35 am

  32. This script is awesome, though you should point out that it works recursively on sub directories as well. Instructions work without modification on kubuntu 8.04 alpha 6. this most recent update of the script (about two hours ago) has solved all my problems thus far. Better still I have a practical python script to help me understand the language better!

    Thank you for devoting the time to creating a quality solution
    I may be interested in porting it to OSX in the future

    cheers

    -PMelse

    Comment by PMelse — March 20, 2008 @ 7:59 am

  33. the script doesnt work if there is a comma in a sub-directory name e.g.

    /home/username/music/blah, album name

    the “mp3″ version of the directory is created but it cannot locate the outfile

    Comment by justinf — March 26, 2008 @ 1:19 pm

  34. I uploaded a new version that should fix the comma issue. Thanks for catching that!

    Comment by bnsmith — March 26, 2008 @ 10:14 pm

  35. [...] M4A Files to MP3, With an Easy-To-Use GUI Filed under: Linux — bnsmith @ 9:29 pm A few months ago, I described the steps needed to convert DRM-free iTunes M4A files into MP3 files using Ubuntu Linux. The procedure [...]

    Pingback by Convert iTunes M4A Files to MP3, With an Easy-To-Use GUI « Pragmattica — March 31, 2008 @ 9:29 pm

  36. Thank you for your tool.

    After ConvertToMP3.py runs, I have a new directory of MP3 files that were converted from MP4. However, the files that were already MP3 in the original iTunes database are not copied to the new directory.

    This means that in my MP3 player, I must use the original iTunes database together with the output of ConvertToMP3.py. How do you suggest that I use these two directories together?

    Separately, is ConvertToMP3.py reentrant? That is, can I convert an iTunes database, add more songs to the iTunes database, and then re-run onvertToMP3.py to convert just the new songs?

    Comment by Voom — April 21, 2008 @ 12:43 am

  37. Hi Voom. You’re right that the MP3s from the original iTunes database aren’t copied to the new directory. To do something like that, I think that it would be possible to create a bash or python script that would search for MP3s the same way that I currently search for M4As, and then copy them to the new directory. However, I don’t have any code that does this at the moment. Perhaps I can add this as an option to ConvertToMP3.py.

    As for your second question, the script is definitely not re-entrant at all. Though, that is a good idea. Thanks for the suggestion. I can see how people would like to be able to run the conversion again and then just only convert the new files rather than all of them. As with the other thing, it’ll take some time to implement.

    Comment by bnsmith — April 21, 2008 @ 9:01 pm

  38. Am new to Linux, so I was very excited to see this solution. For my classical music I use iTunes grouping feature (so the first movement of a piece stays together with the 2nd, 3rd and 4th when I shuffle). This means that I’ve spent tens if not hundreds of hours getting grouping information into itunes. Any chance of preserving that info with this program?

    Comment by Sash — May 1, 2008 @ 8:53 am

  39. Sash, I’m pretty sure that I can add “Grouping” support to the program. I’ve done some looking and it appears that both M4A and MP3 files support a tag for this purpose. There is an ID3v2.3 tag named “TIT1″ with “Grouping” as the description, based on this page: http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/ID3.html. So I can try to add this in.

    Voom, I’d like to apologize for being so lazy lately. I do plan on implementing the two new features that you suggested, as well as the grouping thing. It’s just been hard to find the time in the last few weeks.

    Comment by bnsmith — May 1, 2008 @ 10:17 pm

  40. It works for me!
    Thanks :-)

    Comment by Alex — May 5, 2008 @ 2:34 pm

  41. Voom, I’ve implemented one of the features you suggested (command-line only). When running ConvertToMP3.py on a directory containing both MP3 and M4A files, just add the “–copymp3s” argument:

    ./ConvertToMP3.py /home/you/iTunes\ Music /home/you/mydestination --copymp3s

    This should do all the conversion as usual and also copy existing MP3 files over to the new directory, so you can copy it wholesale to your player.

    Comment by bnsmith — May 7, 2008 @ 10:10 pm

  42. Voom, I’ve finished the second feature that you suggested. It should now be possible to run the program multiple times with the same source and destination directories and have it only convert the new files that it finds. As before, this is currently implemented in the command-line version only. If anyone wants to use these features in the GUI package, let me know and I’ll make a new deb file. Sash, your feature is coming up next. I only hope that the portable MP3 player that you use has good enough support for the more obscure MP3 tags. I’ll know more once I do some fiddling.

    Comment by bnsmith — May 8, 2008 @ 8:20 pm

  43. TO USERS AND BNSMITH:
    * Note that the “–copymp3s” argument above is being formatted into a single long hyphen by WordPress. It’s “minus minus copymp3s”
    * I added a start on user documentation for ConvertToMP3 to the Google Code Wiki for ConvertToMP3 at http://code.google.com/p/converttomp3/wiki/Documentation

    TO BSMITH:
    * Nice job on the enhancements. You referred to yourself as “lazy”, but I think not. I’m sys admin for a 13 year old Linux user, and ConvertToMP3 is a key tool in preventing my daughter from sliding into Windows virus/spyware/DRM/lock-in hell.
    * Today I tested your updates. They work.
    * Now, here’s a feature request that is just for your amusement–my conversion is done so I do not need this any more. I presume you are using a single thread right now. How about if you took advantage of multiple CPUs? ConvertToMP3 is particularly easy to partition, so it would be a fun, instructive exercise for you.

    Comment by Voom — May 10, 2008 @ 4:51 pm

  44. Thanks so much for the encouragement, Voom, and good catch on that formatting error. I have gone back and edited the comment so that the two dashes are distinct. That multi-threading idea is pretty good. Think how much faster a multi-threaded version would be for someone with a quad-core CPU. I think that those will become more common in the next few years. I am also seriously considering adding ogg support (of course, I still have that feature to add for Sash first). I’ve taken a look at the documentation, and it looks great! Using svn to easily grab the latest version is very clever.

    Comment by bnsmith — May 10, 2008 @ 6:53 pm

  45. Sash, I’ve finished adding ‘Grouping’ tag support to the program. It was a fairly major change, so I’m a little nervous about linking to this new version in the blog-post above. Instead, you can download it here. I would appreciate it if you could give this version a quick test on a couple of albums, and drop a comment here to let me know if it works correctly. Please note that this is the command-line version only. If this feature works correctly, then I’ll probably go ahead and update the deb package for the GUI version to contain these new features as well. I hope this works for you!

    Comment by bnsmith — May 14, 2008 @ 8:35 pm

  46. Was using a simple bash script that did not preserve tag info…then found your script that seems to work. However, file size with your conversion seems to grow.

    Original .m4a song = 2.7MB
    Bash conversion .mp3 = 2.7MB
    Your conversion .mp3 = 4.0MB

    Any ideas?

    Comment by dbc — June 22, 2008 @ 10:45 am

  47. I should have clarified my previous post (#46) in that I converted several files with the same result, i.e. the Bash script .mp3 was about the same size as the original .m4a, whereas the .mp3 with your script was larger. I understand that mpeg layer 4 AAC compression can result in smaller files than an mp3 (assuming similar sounding audio, i.e. 128kbps mp3 and similar m4a bandwidth limitations), but was unsure why the bash script (using LAME and FAAD) resulted in an .mp3 file the same size as the .m4a original, whereas your phython script resulted in a larger file. I don’t necessarily mind the larger file (this is not a complaint), but just trying to understand. Remember, the bash script did not preserve the tag info (album, artist, genre, etc) and I understand that this takes some space in the file…but it was odd that the bash generated .mp3 files were the same size as the .m4a whereas the .mp3 from your script are 2 – 4 MB larger.

    Comment by dbc — June 22, 2008 @ 11:55 am

  48. Hi dbc. I’m not completely sure myself if this is the reason, but my best theory is that it has something to do with the MP3 encoding quality. When I fill the command-line parameters into lame in my script, I put in the options to make an MP3 with the absolute maximum possible quality. And no, I don’t know exactly why I do this. I guess when I was programming it, I just sort of said to myself “hard-drive space is cheap, why not?”

    If you want lower quality MP3s, just let me know. It would be pretty trivial to change the parameter or add an option to select the quality or something.

    Comment by bnsmith — June 22, 2008 @ 12:19 pm

  49. Hello bnsmith, Your script works very well for me. I think I may know the difference in filesize…and I believe your reply is correct. With the original bash script I was using it created the .mp3 at 128kbps. With your script the audio was encoded at 192kbps. This 1.5x difference in encoding rate is the same as the 1.5x difference in file size (2.7MB to 4MB). I don’t mind the 192kbps as I agree space is cheap. I was just trying to understand the difference and could not believe the tag information (artist, album, etc.) was it…which it is not.
    A couple of questions:
    (1). Where is the “album artwork” stored…is it part of the audio file or a companion file with a different extension?
    (2). Now I have to convert some aiff to mp3, do you know if this is a simple variation of your script?
    Thanks again for a wonderful job at creating this script.

    Comment by dbc — June 25, 2008 @ 10:16 am

  50. It’s people like yourself that only serve to improve the general level of hope for both linux users and the open source community. You have my thanks.

    Comment by Nahtura — July 22, 2008 @ 11:07 am

  51. Thanks a lot. Thanks to your program, I was able to convert easily some important mp4 I had in my collection.

    Comment by henrique — July 23, 2008 @ 6:29 pm

  52. here is a little script i wrote to converting all the m4a’s in the itunes folder. i just got tired of cding into folders and having to type ConvertToMP3.py, so i made a script using the ConvertToMP3.py script. just move the ConvertToMP3.py script to /usr/bin and then run the following script:

    #!/bin/bash
    for i in //iTunes\ Music/*; do

    echo $i
    for k in “$i”; do
    ConvertToMP3.py “$k”
    echo $k
    done
    done

    just replace with your current path for itunes. edit the script however you want. hopefully it can be of use to someone else.

    Comment by kyle — August 23, 2008 @ 4:50 pm

  53. Bah. looks as though the blog does not use the greater than and less than signs with words in it. the script should read.

    #!/bin/bash
    for i in /path/iTunes\ Music/*; do

    echo $i
    for k in “$i”; do
    ConvertToMP3.py “$k”
    echo $k
    done
    done

    replace path with your path to itunes music folder.

    Comment by kyle — August 23, 2008 @ 4:53 pm

  54. Kyle, did you want the script to put the MP3 files into the same folder as the original M4A files? I had designed the ConvertToMP3.py script to accept a folder name as the argument and then recursively convert all of the M4As in that directory and all of the sub-directories, putting the results into a new folder called “ORIGINALNAME_mp3″. But if you want the results just to go into the same directory as the original files, perhaps I could implement a command-line argument to do that for you. Is that what you wanted? Or have I completely misunderstood?

    Comment by bnsmith — August 23, 2008 @ 5:54 pm

  55. Having the results go into the same directory as the original was what I was hoping for. I was converting the files for a client, so I did not want to confuse them with different file names. (They are not very skilled computer users.)

    Comment by kyle — August 27, 2008 @ 7:25 am

  56. Awesome utility and much needed! Thanks for posting it. Have you got this in a Debian package format yet?

    Comment by Ben — October 2, 2008 @ 5:43 am

  57. I just updated the code with a patch from the other thread for the GUI version. Both the command-line zip file and the debian package have been updated to contain the latest available features. Ben, if you want to install the program from a Debian package, follow the link in the “Update” paragraph at the top of the post.

    Comment by bnsmith — October 8, 2008 @ 3:51 pm

  58. Thanks for this program, I let it run overnight on my Suse 11 system and came back this morning to find my collection converted, all 1,684 files, 249 directories and no problems what so ever. keep up the good work.

    Comment by Peter — October 14, 2008 @ 4:16 pm

  59. Thanks for ending my long search for an easy way to replace the pesky .m4a files that peppered my music folder and refused to play on my MP3 player. Great program, worked like a charm.

    Comment by John — October 28, 2008 @ 10:39 pm

  60. Install and run find first time, WOW ….

    But the second time I tried it, here what I have as message :
    ————————————————————-
    danpel@hpserveur:/music/En_travail/Scripts/Convert_M4A_to_MP3$ ./ConvertToMP3.py /music/En_travail/A_convertir/
    Attempting to convert M4A files in ‘/music/En_travail/A_convertir’
    Traceback (most recent call last):
    File “./ConvertToMP3.py”, line 197, in
    main()
    File “./ConvertToMP3.py”, line 98, in main
    m4as = getFilesEnding(convertdir, “.m4a”)
    File “./ConvertToMP3.py”, line 46, in getFilesEnding
    for current, dirs, files in os.walk(targetDir, True):
    File “/usr/lib/python2.5/os.py”, line 293, in walk
    if isdir(join(top, name)):
    File “/usr/lib/python2.5/posixpath.py”, line 65, in join
    path += ‘/’ + b
    UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0x8a in position 35: ordinal not in range(128)
    ———————————————————————–
    I<m confuse since It was working like god the first time.

    Thanks for any help.

    Comment by danpel — December 17, 2008 @ 6:27 pm

  61. It seems that a bug has been introduced with the most recent version. Go to this URL:

    http://code.google.com/p/converttomp3/downloads/list

    and download an older version, perhaps 0.6.2.

    Once you find a version that works, perhaps you could drop another comment and let me know which one, so I can compare the old and new versions to figure out what the failure is.

    Comment by bnsmith — December 17, 2008 @ 8:45 pm

  62. Hi there,

    I followed what you ask. Here is the result for all the version.
    ——————————————————————
    danpel@hpserveur:/music/En_travail/Scripts/Convert_M4A_to_MP3$ ./ConvertToMP3061.py /music/En_travail/A_convertir/
    Attempting to convert M4A files in ‘/music/En_travail/A_convertir’
    The destination directory ‘/music/En_travail/A_convertir_mp3′ already exists. Conversion will be skipped for any MP3s that already exist.
    Traceback (most recent call last):
    File “./ConvertToMP3061.py”, line 96, in
    m4as = getFilesEnding(convertdir, “.m4a”)
    File “./ConvertToMP3061.py”, line 45, in getFilesEnding
    for current, dirs, files in os.walk(targetDir, True):
    File “/usr/lib/python2.5/os.py”, line 293, in walk
    if isdir(join(top, name)):
    File “/usr/lib/python2.5/posixpath.py”, line 65, in join
    path += ‘/’ + b
    UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0×82 in position 10: ordinal not in range(128)
    danpel@hpserveur:/music/En_travail/Scripts/Convert_M4A_to_MP3$ ./ConvertToMP3062.py /music/En_travail/A_convertir/
    Attempting to convert M4A files in ‘/music/En_travail/A_convertir’
    The destination directory ‘/music/En_travail/A_convertir_mp3′ already exists. Conversion will be skipped for any MP3s that already exist.
    Traceback (most recent call last):
    File “./ConvertToMP3062.py”, line 191, in
    main()
    File “./ConvertToMP3062.py”, line 98, in main
    m4as = getFilesEnding(convertdir, “.m4a”)
    File “./ConvertToMP3062.py”, line 46, in getFilesEnding
    for current, dirs, files in os.walk(targetDir, True):
    File “/usr/lib/python2.5/os.py”, line 293, in walk
    if isdir(join(top, name)):
    File “/usr/lib/python2.5/posixpath.py”, line 65, in join
    path += ‘/’ + b
    UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0×82 in position 10: ordinal not in range(128)
    danpel@hpserveur:/music/En_travail/Scripts/Convert_M4A_to_MP3$ ./ConvertToMP3063.py /music/En_travail/A_convertir/
    Attempting to convert M4A files in ‘/music/En_travail/A_convertir’
    The destination directory ‘/music/En_travail/A_convertir_mp3′ already exists. Conversion will be skipped for any MP3s that already exist.
    Traceback (most recent call last):
    File “./ConvertToMP3063.py”, line 197, in
    main()
    File “./ConvertToMP3063.py”, line 98, in main
    m4as = getFilesEnding(convertdir, “.m4a”)
    File “./ConvertToMP3063.py”, line 46, in getFilesEnding
    for current, dirs, files in os.walk(targetDir, True):
    File “/usr/lib/python2.5/os.py”, line 293, in walk
    if isdir(join(top, name)):
    File “/usr/lib/python2.5/posixpath.py”, line 65, in join
    path += ‘/’ + b
    UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0×82 in position 10: ordinal not in range(128)
    danpel@hpserveur:/music/En_travail/Scripts/Convert_M4A_to_MP3$
    ————————————————————
    It seam to work if I only but some folder and not others !?!
    Here is the list of folder and files, maybe this could help.
    —————————————————————
    `– Beau Dommage
    `– Beau Dommage
    |– 01 La nouvelle saison.m4a
    |– 02 Le retour du flson.m4a
    |– 03 Tout simplement jaloux?.m4a
    |– 04 Quand Rose va chez son fils.m4a
    |– 06 Du milieu du pont Jacques-Cartier.m4a
    |– 07 Rive-Sud.m4a
    |– 09 J’aimais l’hiver.m4a
    `– 11 Marcher tout seul la nuit sur une.m4a
    ———————————————————

    friendly yours.

    Comment by danpel — December 19, 2008 @ 12:57 pm

  63. Many thanks for a very handy app.

    Comment by Tom — December 31, 2008 @ 1:52 am

  64. I get the following:

    scott@scott:~$ ./ConvertToMP3.py iTunes/iTunes\ Music/Tommy\ Emmanuel/Only/
    Attempting to convert M4A files in ‘/home/scott/iTunes/iTunes Music/Tommy Emmanuel/Only’
    Traceback (most recent call last):
    File “./ConvertToMP3.py”, line 197, in
    main()
    File “./ConvertToMP3.py”, line 98, in main
    m4as = getFilesEnding(convertdir, “.m4a”)
    File “./ConvertToMP3.py”, line 46, in getFilesEnding
    for current, dirs, files in os.walk(targetDir, True):
    File “/usr/lib/python2.5/os.py”, line 293, in walk
    if isdir(join(top, name)):
    File “/usr/lib/python2.5/posixpath.py”, line 65, in join
    path += ‘/’ + b
    UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0×82 in position 8: ordinal not in range(128)

    Comment by Scott Dudley — February 24, 2009 @ 5:40 pm

  65. you rock. thank you for sharing your m4a to mp3 python script! It works perfectly for me.

    Comment by Nate — March 31, 2009 @ 11:03 pm

  66. Recently, personal circumstances have prevented me from doing much software development, but I’m hoping to get back into it, starting with this utility. For all those who have encountered the UnicodeDecodeError problem, I’ve stumbled on a clue in this thread:

    http://www.gossamer-threads.com/lists/python/python/700967

    Apparently, this conversion utility works for some people and not others due to issues with locales. If you have this problem, perhaps you could run “echo $LANG” on the command line and post the result. This could be a thorny problem to resolve, but I’m working on it.

    Comment by bnsmith — April 16, 2009 @ 12:23 pm

  67. Great script. Thanks

    Comment by João Marcelo — May 3, 2009 @ 6:34 pm

  68. Thanks for the support, everyone! I’m learning more about different encodings of strings, Unicode and internationalization in the hopes of fixing that bug. It is quite the adventure, but I think I have an idea that might work. The things I’ve learned might just make it into a blog post.

    Comment by bnsmith — May 4, 2009 @ 9:58 pm

  69. Thanks for all your work on this. It works excellent.
    ———–
    Debian – Lenny – amd64

    Comment by bmc5311 — May 27, 2009 @ 7:35 pm

  70. Why would you want to do this? For one, there are AAC plug-ins for gstreamer. Secondly, who would want to convert their entire audio library from one lossy format to another when support for AAC playback exists natively?

    Comment by Dissenter — June 8, 2009 @ 9:26 am

  71. @Dissenter: The main thing that motivated me to write this was a portable player that had poor support for AAC. It would play the songs, but lost the album-order and played them in alphabetical order instead (that bug really annoyed me). You’re right that it’s not so important on desktop Linux, though.

    Comment by bnsmith — June 8, 2009 @ 11:45 am

  72. Fantastic script! It “may” not be important on desktop Linux, but it sure is when my car has an mp3 cd player ;) Many thanks for making this available for all of us!

    Comment by Laura H — August 13, 2009 @ 10:18 am

  73. Hey bnsmith. This is an amazing script. I was looking for something earlier today to convert a bunch of albums I have in Apple Lossless (m4a) to mp3 with tags, and was happy to find your script.

    However, I’ve noticed a few problems prevented me from using it. You set the mp3 encoding to a low-ish 192cbr, and while that’s good for 128kbps AAC, most iTunes plus files are now 256kbps. Also, I noticed you don’t provide for a few essential iTunes tags like: total number of tracks, disk numbers, Composer, Album Artist, and the “part of a compilation” flag. Those last two are specially important so soundtrack albums show grouped on the player and not as split tracks under each artist. Since I’ve gathered here you are a busy man, I took the liberty of changing the script myself and adding support for those tags and mapping them to their mp3 counterparts.

    As a bonus I’ve fixed a small issue where you mapped the Year tag to the long deprecated TYER frame, which is invalid under id3v2.4, and although still works in some software players, is very frowned upon. Oh, and I’ve put artwork support too, so if there’s album covers in the m4a files they’ll get also passed along to the mp3s (at the cost of adding ImageMagick dependency to the script). Nothing a ‘sudo apt-get install imagemagick’ won’t do for most people.

    Check it out, I’ve put it on: http://dl.getdropbox.com/u/455517/ConvertToMP3.py

    If you like any of those changes, you can use them however you see fit. I’ve tried to comment on the code wherever I made a change, but please clean up any programming bad practices, this is the first time I’ve ever wrote a line of python. Once again, thank you very much for this script.

    Comment by Ollie Neto — September 20, 2009 @ 11:56 pm

    • Wow! That’s amazing work. Isn’t open-source wonderful?

      There are a couple of ways that we could go about this. First, I put the code for this program up at code.google.com/p/converttomp3/. If you’d like, I could add you as a project owner and you could check changes in with SVN. If you’re not up for that, it’s no problem. I’ll get your changes checked in to the trunk as soon as I get a spare moment. What do you think?

      Comment by bnsmith — September 21, 2009 @ 9:31 pm

  74. Thanks!!! very useful script.

    Comment by Oscar Duarte — December 28, 2009 @ 10:19 am

  75. Just another profound thanks for this excellent script, Brad. It does alarm one at first with some apparently meaningless error messages (“mplayer: could not connect to socket”, “mplayer: No such file or directory”), and then appears to just hang, but a little patience is rewarded with the desired restult. Great job!

    Also thanks to Ollie Neto – I’m using your variant to convert a bunch of files now.

    Comment by MikeL — December 29, 2009 @ 6:32 pm

  76. 1. I tried to use the GUI version. It did not like my 64bit Ubuntu.
    2. I rip audio books and would like 64bit and I rip music and would like 320bit. I’m not a programmer. Maybe your could put a variable with the rate setting at the top of your code, so I could easily change the rate to my liking.

    Thanks -Steve

    Comment by Steve — February 3, 2010 @ 11:54 am

  77. run the original script and when it says could not find .m4a go into your prefered file searcher and rename the file from m4a to mp3 and it works eg:

    song.m4a

    rename that song after converting to:

    song.mp3

    Comment by andrew — March 16, 2010 @ 1:32 pm

  78. it worked like a charm on 9.10
    only I had to issue the command like

    python ConvertToMP3.py /home/youruser/itunes

    instead of

    ./ConvertToMP3.py /home/youruser/itunes

    even after I had made the file executable,

    thanks for sharing!

    Comment by F@b — May 7, 2010 @ 3:12 am

  79. Hello!
    Thank you very much for such little convenient tool!
    I had been looking for a program to extract album covers from M4A files and your program was the first one which did the trick!
    So, as you see, your program may be useful even for people who does not need m4a -> mp3 conversion itself. :) Thanks again.

    P.S. And by the way, a separate “Thank you” for this program is a free software (GPL’ed). Respect ;)

    Comment by vladimir — August 2, 2010 @ 2:22 pm

  80. Thanks for helping other break free from m4a!

    I was nervous about doing my collection recursively, but it worked flawlessly. Thanks!

    I tried hacking it to do ogg/vorbis files. It kinda worked, but the tags wouldn’t extract.

    And my cheap mp3 player only supports id3v1. Dual id3 support (v1 & v2) would be cool.

    Sorry to suggest so much. I’m not complaining. You’ve helped so much. Thanks.

    Comment by Anonymous — October 21, 2010 @ 9:03 am

  81. Work like a charm. Thanks mate!

    Comment by Matt Wilkinson — October 26, 2010 @ 6:03 am

  82. Worked with Ubuntu 10.10 – thank you.

    Comment by Freedom — November 28, 2010 @ 6:17 am

  83. I keep on getting errors like the one below. It kicks my USB HD in to “read only” Using Ubuntu 10.10, any ideas?

    Exiting… (End of file)
    Can’t init outfile ‘/media/USB300GB/mp3/iTunes Media/Music/The View/Hats Off To The Buskers/05 Skag Trendy.mp3′
    Traceback (most recent call last):
    File “./ConvertToMP3.py”, line 197, in
    main()
    File “./ConvertToMP3.py”, line 129, in main
    os.makedirs(os.path.split(dest)[0])
    File “/usr/lib/python2.6/os.py”, line 150, in makedirs
    makedirs(head, mode)
    File “/usr/lib/python2.6/os.py”, line 157, in makedirs
    mkdir(name, mode)
    OSError: [Errno 30] Read-only file system: ‘/media/USB300GB/mp3/iTunes Media/Music/Andrew Litton_ London Philharmonic Orche’
    freedom@freedom:~$

    Comment by Freedom — November 28, 2010 @ 6:36 am

  84. The application errors out with the following error. It would be helpful if it would output the file that it is having problems with and/or skip over the file rather than failing.

    Traceback (most recent call last):
    File “./ConvertToMP3.py”, line 197, in
    main()
    File “./ConvertToMP3.py”, line 134, in main
    title = getTag(tagInfo, ‘\xa9nam’)
    File “./ConvertToMP3.py”, line 32, in getTag
    if tagInfo.tags.has_key(primaryTag):
    AttributeError: ‘NoneType’ object has no attribute ‘has_key’

    Comment by Brian — December 13, 2010 @ 10:26 pm

  85. It runs under Ubuntu 9.04 (Jaunty), but produces mp3 files with just the id3 tags – no audio.

    So, i wrote something similar in shell. It’s lightly tested. It needs
    sudo apt-get install id3v2 lame faad

    I call it m4a2mp3dir
    Paste this into a file, and
    chmod +x m4a2mp3dir
    Call it by connecting to the directory, and running m4a2mp3dir


    #!/bin/bash
    # Convert the current directory of .M4a or .m4a files to .mp3
    # Rename any files named .M4A to .m4a
    A=`ls -1 | grep .M4A$`
    if [ -n "$A" ] ; then
    for i in *.M4A; do
    mv $i `basename $i .M4a`.m4a
    done
    fi
    # If there are any .m4a files...
    A=`ls -1 | grep .m4a$`
    if [ -n "$A" ] ; then
    # ...convert them to mp3.
    for i in *.m4a; do
    A=`basename $i .m4a`
    faad -i $i > $A.txt 2>&1
    # echo "Converting: ${i%.m4a}.mp3"
    # chatty original version
    # faad -q -o - "$i" | lame - "${i%.m4a}.mp3"
    faad -q -o - "$i" | lame - --tt $A --quiet -q 0 "${i%.m4a}.mp3"
    T=`grep album $A.txt | cut -d: -f2`
    if [ "x$T" != "x" ] ; then
    # Wish id3tag had something to turn off the noise.
    id3tag "-A$T" $A.mp3 > /dev/null 2>&1
    fi
    T=`grep title $A.txt | cut -d: -f2`
    if [ "x$T" != "x" ] ; then
    id3tag "-s$T" $A.mp3 > /dev/null 2>&1
    else
    id3tag "-s$A" $A.mp3 > /dev/null 2>&1
    fi
    T=`grep artist $A.txt | cut -d: -f2`
    if [ "x$T" != "x" ] ; then
    id3tag "-a$T" $A.mp3 > /dev/null 2>&1
    fi
    T=`grep writer $A.txt | cut -d: -f2`
    if [ "x$T" != "x" ] ; then
    id3tag "-C$T" $A.mp3 > /dev/null 2>&1 # desc
    fi
    T=`grep tool $A.txt | cut -d: -f2`
    if [ "x$T" != "x" ] ; then
    id3tag "-c$T" $A.mp3 > /dev/null 2>&1 # comment
    fi
    # rm -f $A.txt
    done
    fi

    Comment by Stephen — December 27, 2010 @ 7:08 pm

  86. [...] Convert iTunes M4A files to MP3 on Linux В« Pragmattica Jan 17, 2008 … Convert iTunes M4A files to MP3 on Linux В« Pragmattica Reimplementazione in Python di uno script di … [...]

    Pingback by transfer mp4 to mp3 linux | Buy it! — April 11, 2011 @ 4:22 pm

  87. Thanks so much friend!

    Comment by Anonymous — August 13, 2011 @ 8:12 pm

  88. I congratulate you about ConvetToMp3! Great job!

    Please, I read your python script and I translate it to brazilian portuguese. Do you receive translations in your project?
    If “yes”, I going to translate it to esperanto language, too!

    Comment by josberto — September 7, 2011 @ 11:30 am

  89. Great! Thank you for the handy script!

    Comment by Anonymous — November 25, 2011 @ 12:28 pm

  90. similar project: http://code.google.com/p/ftransc/

    Comment by Anonymous — December 19, 2011 @ 4:52 pm

  91. On Fedora 14 it works (needed “yum install id3v2 mplayer lame python-mutagen” first), but it does not copy the ID3 info from my m4a files to the newly created mp3 files. I’m really looking for an automatic way to do this copy, since I have >100 m4a files and don’t want to do it manually. Ugh.

    Comment by Inky Reveal (@envoy510) — December 31, 2011 @ 1:13 pm

  92. I am pretty sure that this information will certainly aid all those iTunes users who are looking forward to convert M4 format files into standard MP3 files on Linux platform. You have presented a truly useful piece of information.

    Comment by james — January 31, 2013 @ 1:15 pm

  93. Thanks bud. Nice work.

    Comment by Clinton Baldridge — July 5, 2013 @ 4:44 pm


RSS feed for comments on this post.

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: