Backup your iPod – Python Script

I had this in a folder and I thought I might as well share it. iPods are lovely machines indeed, but they are a little bit like black holes: songs/videos go in, and they won’t come out. Or at least with the same degree of simplicity. Anyways, since the iPod is nothing but a fancy USB stick, you can always copy the songs yourself. However, it scrambles their names which makes it, if you have something like 3000 songs, a rather herculean task to rename everyone of them properly. Thankfully thought, most songs you have on your iPod ( at least mine were ) have ID3 tags with all the information you need to rename them and actually rebuild the library.

You just need a script to read the contents of your iPod, fetch the tags of each song, and progressively copy them. This is a very very very crude approach, that works only for MP3s ( forget those FLACs or MP4s ). It worked wonders for me. 30GB of music backed up perfectly :) You need the mutagen library to make it work. And I don’t know if it works on Windows either.

To use do something like: python this_script.py /my/iPod/Contents/Folder  /path/to/backup/songs/to

import os, sys, shutil
from mutagen.easyid3 import EasyID3

# script.py <iPod Folder> <Target Backup Folder>

ori_loc, backup_loc = sys.argv[1:]

os.chdir(os.path.join(ori_loc))

# Walk through folders
number = 0
for root, folders, songs in os.walk('.'):

 for song in songs:
 if song[0] != '.':
 if song.split('.')[-1].upper() == 'MP3':
 audio = EasyID3(os.path.join(root, song))
 try:
 album = audio['album'][0].replace('/', '_')
 artist = audio['artist'][0].replace('/', '_')
 title = audio['title'][0].replace('/', '_')
 except Exception, e:
 print "Error copying %s %s: %s" %(root, song, e)
 album = 'Unknown'
 artist = 'Unknown'
 title = 'Unknown_'+str(number)
 number +=1

 # Copy it to the backup folder
 dest_folder = os.path.abspath(os.path.join(backup_loc, artist))

 if not os.path.exists(dest_folder):
 os.mkdir(dest_folder)

 dest_folder = os.path.join(dest_folder, album)
 if not os.path.exists(dest_folder):
 os.mkdir(dest_folder)

 if not os.path.exists(os.path.join(dest_folder, title+'.mp3')):
 shutil.copy(os.path.join(root, song), os.path.join(dest_folder, title+'.mp3'))

print 0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s