Home of Rob
Menu
Sorting MP3 Files
Ok, sometimes I come across mp3 files that have well-written names and id3 tags filled in (that is, my mp3 player displays artist/title/album information about the tracks) but they're all in a single directory. Or they're scattered about the place - the same artist cropping up in multiple places where they shouldn't.
Alternatively you may have some mp3 files that have the correct id3 tag information, but the name has become garbled. This would be the case if you copy files from your iPod onto your computer. For reasons best known only to Apple, they scatter the mp3 files randomly around in directories on your iPod and rename them to a random collection of letters (and numbers?).
If you want to sort this out, then the following script is for you. For the iPod users out there, you can search for *.mp3 on your iPod (including hidden files/folders, if you're on windows), and then copy them all to a single directory. Then copy this script in, run it, and it'll sort all your files!
The Script
The following is a python script that I quickly put together recently. It's not particularly sophisticated, but does the job I needed it to. It'll search the current directory for *.mp3 files, and for each one it extracts artist and album information. It then moves the file to ./artist/album/
cwd = os.getcwd() for file in files: clean_album = album.replace("'","") if not os.path.exists( album_dir ): destination_file = file.replace( "'", "" ) print "Success:", Success Python comes readily installed on Linux machines, but you may need to install the ID3 parsing module. For windows users, you'll need to install python. This is straight-forward, just head over to the python download repository and follow instructions there. #!/usr/bin/env python
import os
from mutagen.easyid3 import EasyID3
files = os.listdir(cwd)
Failures=0
Success=0
if file.endswith(".mp3"):
try:
#print "Processing: ", file
audio=EasyID3(file)
album=audio['album'][0]
artist=audio['artist'][0]
if len(album) > 0 and len(artist) > 0:
clean_artist = artist.replace("'","")
clean_artist = clean_artist.replace( " ", "_" )
artist_dir = os.path.join( cwd, clean_artist )
if not os.path.exists( artist_dir ):
os.mkdir(artist_dir)
clean_album = clean_album.replace( " ", "_" )
album_dir = os.path.join( artist_dir, clean_album )
os.mkdir( album_dir )
destination_file = destination_file.replace( " ", "_" )
destination = os.path.join( album_dir, destination_file )
#print 'mv "%s" "%s"' % (file, destination)
os.system( 'mv "%s" "%s"' % (file, destination) )
Success+=1
except Exception,e:
print "Failed: ", str(e)
print "album_dir:", album_dir
print "destination_file:", destination_file
print
Failures+=1
print "Failures:", FailuresNotes: