Convert FLACs to MP3s

Here is a simple python script which will convert FLAC files to MP3s. You need the following tools to make it work:
– flac
– lame
– python-mutagen

#!/usr/bin/python2.5
# coding: utf-8
import sys,os
from optparse import OptionParser
from mutagen.flac import FLAC
from mutagen.easyid3 import EasyID3

usage = "usage: %prog [options] file.flac file.mp3"
parser = OptionParser(usage)
parser.add_option("-l", "--lamelevel", dest="lamelevel",
                  help=" --Lame encoding levels, defalut=V0")
(options, args) = parser.parse_args()
if len(args) != 2:
    parser.error("incorrect number of arguments")
if options.lamelevel:
    ll=options.lamelevel
else:
    ll="V0"

lameOptions={
        "320" : "-b 320 --ignore-tag-errors",
        "V0" : "-V 0 --vbr-new --ignore-tag-errors",
        "V2" : "-V 2 --vbr-new --ignore-tag-errors"
        }

#Perform the conversion
lampO=lameOptions.get(ll,lameOptions["V0"])
cnv_comm="flac -dc \"%s\" |lame %s --add-id3v2 - \"%s\" 2>&1" % (args[0], lampO,
os.system(cnv_comm)


#Copy the tags
id3tags=set(('album', 'title', 'lyricist', 'date', 'version', 'composer',
    'genre', 'tracknumber', 'artist'))

mp3tag=EasyID3(args[1])
mp3tag.clear()

flactag=FLAC(args[0])
for t in flactag.tags:
    lc=t[0].lower()
    if lc in id3tags:
        mp3tag[lc]=t[1]

mp3tag.save()
This entry was posted in Linux, PYTHON. Bookmark the permalink.

Leave a comment