Ex­plor­ing Soft­ware: Us­ing Python to Man­age MP3 Tags

The au­thor loves to play around and ex­per­i­ment with soft­ware and share the re­sults of his work with read­ers. He has wide and var­ied tastes and MP3 mu­sic is one of them.

OpenSource For You - - Contents - Anil Seth

It is nice to be able to play the songs you like more of­ten. Amarok was the first player I came across that al­lowed the cre­ation of dy­namic playlists us­ing a rat­ing tag. For ex­am­ple, you could have a playlist with half the songs with a rat­ing of 4 or more and the re­main­ing could be any choice from your li­brary. This was many years ago, when it was Amarok ver­sion 3. The rat­ing was kept in the data­base of the mu­sic player and I put in the ef­fort to rate at least the songs I loved to hear of­ten.

Some­where down the line, the data­base was lost. I can’t re­mem­ber whether it hap­pened when up­grad­ing the Amarok player to ver­sion 4 or when I switched the desk­top. Any­way, I never got around to set­ting the rat­ings again.

There is now an ID3 tag which stores the rat­ing. How­ever, it is far too te­dious to change MP3 tags us­ing a mu­sic player, es­pe­cially if the mu­sic li­brary is a mess, with in­con­sis­tent file and direc­tory names.

Can you use the ID3 tags to make the li­brary bet­ter or­gan­ised?

You may de­cide to name each mu­sic file us­ing the artist and the song’s ti­tle. How­ever, it is pos­si­ble that not all songs in your li­brary have the proper tags, es­pe­cially if you have digi­tised them your­self from an­cient CDs.

So, you could de­cide on the fol­low­ing strat­egy:

1. Cre­ate a CSV file with the file name and the se­lected ID3 tags as col­umns.

2. Use a spread­sheet to mod­ify the col­umns.

3. Up­date the ID3 tags us­ing the mod­i­fied CSV file.

4. Re­name all the MP3 files us­ing the ID3 tags.

There are a num­ber of Python mod­ules to mod­ify the tags in MP3 files. Two com­mon pack­ages are Mu­ta­gen and eyeD3. Mu­ta­gen has the ad­van­tage that it works with mul­ti­ple file types, in­clud­ing MP3, OGG and FLAC, while eyeD3 is meant for MP3 files only.

Get­ting started

On Fe­dora, in­stall the fol­low­ing pack­ages: $sudo dnf in­stall python2-mu­ta­gen, python-eyed3

The fol­low­ing is the min­i­mum code for run­ning any of the three op­tions. You pass the name of the op­tion (get_­tags, up­date_­tags or re­name) and the start­ing direc­tory. #!/usr/bin/python im­port sys,os from os.path im­port join if __­name__ == ‘__­main__': try: op­tion = eval(sys.argv[1]) if len(sys.argv) > 2:

path = sys.argv[2] else:

path = ‘.' op­tion(path) ex­cept:

print(“Pa­ram­e­ters: one of [get_­tags,up­date_ tags,re­name] path(de­fault .)”)

Cre­at­ing the CSV file

The ba­sic code for get_­tags is as fol­lows. You it­er­ate over the files and se­lect each MP3 file. You will need to call the func­tion get_­file_­tags to get the tags you need. This func­tion is the one that will de­pend on the Python mod­ule you use. Two other ver­sions are given later.

def get_m­p3_­file(path):

“”” Walk through all files and se­lect mp3 files.

Com­mon for all op­tions””” for root,dirnames,file­names in os.walk(path): for f in file­names: if len(f) > 4 and f[­4:].lower() == ‘.mp3’:

yield join(root,f) def get_­tags(path):

“”” Cre­ate a dic­tio­nary with file name as the key and val­ues are a tu­ple of the de­sired tag val­ues Save the dic­tio­nary as csv file suit­able for a spread­sheet””” re­sult = {} for fn in get_m­p3_­file(path):

re­sult[fn]=get_­file_­tags(fn) out=open(‘mp3.csv’,’w’) for f in re­sult:

out.write(“%s;%s;%s;%s\n”%((f,) + re­sult[f]))

Shown be­low is the code for get_­file_­tags if you’re us­ing the Mu­ta­gen mod­ule:

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.