For a while I’ve wanted to make my own script to convert FLAC files into MP3s, an odd aspiration perhaps, but an aspiration nonetheless. For years I’ve been using one which I downloaded from somewhere ages ago, I can’t remember where, but it didn’t quite do what I wanted, which meant I usually had to spend some time altering tags after converting. The one I’ve made isn’t as advanced as many others you can find on the web, but it does exactly what I want. It’s tailor-made for me, by me! To understand why it does what it does you have to understand how I store my music, so bear with me. In my home folder I have a “Music” folder which has all my MP3s and I have a “Music [FLAC]” folder which stores all my FLAC files (unsurprisingly). In both of these folders, music is organised by artist then by album. Everything I have in FLAC I’ve also converted to MP3 for use with my MP3 player, and devices which don’t support FLAC, like my Xbox.
When I acquire new FLAC files I put them in the right place, and then manually edit the tags if necessary, I’m a bit OCD about making sure they’re right. If necessary I also change the file-name so it’s of the form “track-number track-name.flac” and remove any “nasty” characters from it, which I consider to be everything except numbers, letters and parentheses (only this kind of parentheses though). I also like to add ReplayGain data to the FLAC files, so they all play at the same level when the right software is used, and find a picture of the album artwork, which I place in the folder and rename to “cover.jpg”, most software will then display it while playing. I then convert them to MP3, put the new files in the right place and then apply mp3gain to them, so they play back at the same level on any MP3 player. I copy across the “cover.jpg” too.
My script does pretty much all of the above paragraph for me, except the first sentence. So all I have to do now is put the FLAC files in the right place, edit the tags if needed (something which I prefer to do manually anyway), then run my script from within the folder. At the moment the script doesn’t take any parameters, it uses the tags in the FLAC files to set up the file-names the way I like them, and always puts the created MP3s in the folder: “/home/Username/Music/Artist/Album/”, using the data from the tags to get the artist and album correct. So I suppose in many ways the script is quite simple. The only complicated part was coming up with the regular expressions required to get hold of the right data for the tags, and to strip out the unwanted characters. I’d never used regular expressions before, in fact while reading about writing scripts in the past I tended to skip over parts that mentioned regular expressions and the sed program, as they are topics unto themselves. But now I’ve learned a bit about them I’m sure my new knowledge will be useful again in the future, perhaps like in this XKCD.
Here’s a link to the script if you want to use it/look at it: flactomp3.sh. For it to work you need to have flac, lame, metaflac and mp3gain installed, it doesn’t check so be careful, just type
which lame etc. into a terminal and if they are installed it will tell you, if they’re not it will say nothing. There’s a couple of constants you can change near the top, which you can use to make it suit your needs a bit better. It’s explained in the file. To use it you need to place it in “/usr/local/bin/” and make it executable. So in a terminal on Ubuntu you would need to navigate to wherever you saved it to, then do:
sudo mv flactomp3.sh /usr/local/bin/ followed by:
sudo chmod +x /usr/local/bin/flactomp3.sh. To use it you just need to navigate to the folder of FLAC files you want to convert and enter:
flactomp3.sh. Tap return and your files will be converted exactly as I have described above. Make sure the tags in all the FLAC files are exactly as you want them before you start though, because otherwise there could be some nasty side-effects. At some point I may add some more to it, so it accepts input and output folders for example, but I don’t want to overcomplicate it.