#!/usr/bin/python # # mp3swapartist.py # # swap mp3 artist and album artist tags # # I set up my whole music collection using a directory structure # based on artist/album/trackno - title. Now I realise that # album artist is the proper tag to use for that structure. # # Copyright (C) 2013 Jeremy Tammik # import glob, os, re, sys from mutagen.mp3 import MP3 from mutagen.id3 import TPE1, TPE2 import eyeD3 def swap_artist( filepath ): "Swap mp3 artist and album artist tags." assert filepath.lower().endswith( '.mp3' ) try: audio = MP3( filepath ) old_artist = unicode( audio['TPE1'] ) assert 0 < len( old_artist ) old_album_artist = '' if audio.has_key( 'TPE2' ): old_album_artist = unicode( audio['TPE2'] ) s = "original artist '%s', album artist '%s'" % (old_artist, old_album_artist) if 0 == len( old_album_artist ): # copy artist to album artist s += ' - added album artist' audio.tags.add( TPE2( encoding=3, text=old_artist ) ) audio.tags.save() elif old_artist != old_album_artist: # swap artist and album artist s += ' swapped' audio.tags.add( TPE2( encoding=3, text=old_artist ) ) audio.tags.add( TPE1( encoding=3, text=old_album_artist ) ) audio.tags.save() else: s += ' retained' print filepath + ':', s except StandardError, err: print 'Error:', str( err ), "in '%s'" % filepath def main(): "Walk a directory tree and swap all mp3 artist and album artist tags." #dir = '.' dir = '/$User/Music/lovers_rock/' for root, dirs, files in os.walk( dir ): for filename in files: if filename.lower().endswith( '.mp3' ): filepath = os.path.join( root, filename ) swap_artist( filepath ) if __name__ == "__main__": main()