How I take mp3 out of an iPod with correct filenames

Yesterday I was trying to take mp3 out of an iPod Nano, but I realize that the file names are encrypted:

~/Desktop/iPodMusic/$ ls
AEHR.mp3 BXRT.mp3 CKZA.mp3 EAFC.mp3 GBFB.mp3 HSEC.mp3 IRUB.mp3 QPDQ.mp3

However, the mp3 tags are still there when I checked using the mp3info command:

~/Desktop/iPodMusic/$ mp3info AEHR.mp3
File: AEHR.mp3
Title: Til Kingdom Come Track: 13
Artist: Coldplay
Album: X&Y Year: 2005
Comment: Genre: Rock [17]

So I wrote a script in Ruby to rename those files according to the Title and Artist retrieved using the mp3info.

The main flow of the program goes like this

  1. Obtain the encrypted file names of the mp3 into an array
  2. Run mp3info against each encrypted file names to obtain the mp3 tags
  3. Use RegExp to parse the retrieved mp3 information into Artist and Title
  4. Rename the file name accordingly

Code:

1. Obtain the encrypted file names of the mp3 into an array

song_names_arr = `ls`
song_names_arr = song_names_arr.split
# song_names_arr now contains ["AEHR.mp3","BXRT.mp3","CKZA.mp3", ...]

Enclosing code with ` enable us to run shell command in Ruby code and return the result as a string. Now, we have the encrypted file names in array.

2. Write a function to obtain the mp3 tags information using mp3info

def obtain_mp3tags(encrypted_name)
    return `mp3info #{encrypted_name}`
end

# obtain_mp3tags("AEHR.mp3") will return:
#   "File: AEHR.mp3
#   Title: Til Kingdom Come Track: 13
#   Artist: Coldplay
#   Album: X&Y Year: 2005
#   Comment: Genre: Rock[17]"

3. Write functions to parse the mp3 information to retrieve Artist and Title of an mp3:

def extract_artist(aString)
    regex = Regexp.new(/Artist:.*$/)
    matchdata = regex.match(aString)
    if matchdata
        s = matchdata[0].gsub(/Artist:/,"")
        s.gsub!(/^\s*/,"")
        s.gsub!(/\s*$/,"")
    else
        puts "NO MATCH"
        s=""
    end
    return s
end
# extract_artist(obtain_mp3tags("AEHR.mp3")) will return "Coldplay"

def extract_title(aString)
    regex = Regexp.new(/Title:.*Track/)
    matchdata = regex.match(aString)
    if matchdata
        s = matchdata[0].gsub(/Title:/,"")
        s.gsub!(/Track/,"")
        s.gsub!(/^\s*/,"")
        s.gsub!(/\s*$/,"")
    else
        puts "NO MATCH"
        s=""
    end
    return s
end

# extract_title(obtain_mp3tags("AEHR.mp3")) will return "Til Kingdom Come"

4. Now we have obtained the Artist and Title of an mp3, we can proceed to rename it:

`mv #{encrypted_song_name} "#{new_song_name}"`

Take note of the ” “, we need to enclose the new song name, if not, the mv command will give error when the new song name contains special characters such as whitespace, bracket, etc.

5. Putting everything together:

# Obtaining the encrypted file names into an array
  song_names_arr = `ls`
  song_names_arr = song_names_arr.split

  song_names_arr.each{ |e_song_name|

# obtain the mp3 information from the mp3 tags
  mp3_tags = obtain_mp3tags(e_song_name)
# check if the mp3 tag exist, if true, extract the Artist and Title
  if ((extract_artist(mp3_tags).length>0)||(extract_title(mp3_tags).length>0))
     new_song_name = extract_artist(mp3_tags) + " - " +extract_title(mp3_tags)+".mp3"
     # Rename the encrypted file names with the new song name
     `mv #{e_song_name} "#{new_song_name}"`
  end
  }

End result:

$ ls
Adele - First Love.mp3
Adele - He Won't Go.mp3
Adele - Hiding My Heart.mp3
Adele - That's It, I Quit, I'm Moving.mp3
Al Green - Tired Of Being Alone.mp3
Alanis Morissette - Hand in My Pocket.mp3
Alanis Morissette - You've Already Won Me Over.mp3
Amy Winehouse - .mp3
Amy Winehouse - Back To Black.mp3
Amy Winehouse - Cherry.mp3
Amy Winehouse - Our Day Will Come.mp3
....

Musics finally taken out with correct filenames! 😀

Advertisements

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