Pages

Feb 4, 2012

Renaming MP3 files with ruby


Recently, my friend accidentally deleted my files in my external hard drive. Recovering process is quite time consuming since there is a lot of files in there. To make it short, I finally got back most of my mp3 collection, but the file naming need some restoration.

$ ls
total 13026192
drwxr-xr-x  1566 root  staff     53244 Feb  4 20:23 .
drwxr-xr-x     3 root  staff       102 Feb  4 20:18 ..
-rw-r--r--     1 root  staff   5980962 May 24  2011 f100018967.mp3
-rw-r--r--     1 root  staff   2331842 May 24  2011 f100248623.mp3
-rw-r--r--     1 root  staff   1072506 May 24  2011 f100379151.mp3
-rw-r--r--     1 root  staff   1464448 May 24  2011 f100479767.mp3
-rw-r--r--     1 root  staff   1083520 May 24  2011 f100743063.mp3
-rw-r--r--     1 root  staff   3950443 May 24  2011 f100938119.mp3
-rw-r--r--     1 root  staff   4244632 May 24  2011 f101036079.mp3
-rw-r--r--     1 root  staff   3887104 May 24  2011 f101232487.mp3
-rw-r--r--     1 root  staff   4339809 May 24  2011 f101396631.mp3
-rw-r--r--     1 root  staff   4583424 May 24  2011 f101527175.mp3
-rw-r--r--     1 root  staff    147968 May 24  2011 f101727759.mp3
...
...

$ file f100743063.mp3 
f100743063.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, 
layer III, v1, 128 kbps, 44.1 kHz, JntStereo


$ hexdump -C f95958423.mp3 | head -n 8
00000000  49 44 33 03 00 00 00 00  1f 76 54 43 4f 4e 00 00  |ID3......vTCON..|
00000010  00 05 00 00 00 28 31 32  29 43 4f 4d 4d 00 00 00  |.....(12)COMM...|
00000020  0d 00 00 00 65 6e 67 00  50 41 52 4b 2d 45 4e 47  |....eng.PARK-ENG|
00000030  54 49 54 32 00 00 00 14  00 00 00 54 69 6b 75 73  |TIT2.......Tikus|
00000040  2d 74 69 6b 75 73 20 6b  61 6e 74 6f 72 00 54 50  |-tikus kantor.TP|
00000050  45 31 00 00 00 0b 00 00  00 49 77 61 6e 20 46 61  |E1.......Iwan Fa|
00000060  6c 73 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |ls..............|
00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

Since some of the mp3 files still preserve its mp3 tag, I decided to make some tools to grep the tag and rename the file according to that tag.

$ gem install ruby-mp3info

#!/usr/bin/env ruby
# You need to require this..
require 'rubygems'
require 'mp3info'

# This variable will hold your first argument as directory
# that contains mp3 files
dir = ARGV[0]
dir = File.expand_path(dir)

# Scan for files with .mp3 extension
mp3_list =  Dir.glob("#{dir}/*.mp3")

# Initialize variable 'artist' and 'song'
artist = ''
song = ''

# Loop all the list of files inside the array
mp3_list.each do |mp3_file|
        Mp3Info.open(mp3_file) do |mp3|
                if mp3.tag.artist.nil?
                  # If artist tag not found, set as 'Unknown'
                        artist =  "Unknown"
                else
                  # If found, assign to variable artist.
                  # replace character '/' to '\' to avoid
                  # problem in renaming the file.
                        artist = mp3.tag.artist.gsub(/\//,"\\")
                end

                if mp3.tag.title.nil?
                  # If title tag not found, set as 'Unknown'
                        song =  "Unknown"
                else
                  # If found, assign to variable song.
                  # replace character '/' to '\' to avoid
                  # problem in renaming the file.
                        song =  mp3.tag.title.gsub(/\//,"\\")
                end

    # combine as a new path
                new_name = "#{dir}/#{artist} - #{song}#{File.extname(mp3_file)}"

                unless mp3_file == new_name
                  # If looping filename != newly combined path,
                  # rename the file
                        puts new_name
                        File.rename(mp3_file, new_name)
                end
        end
end


Credit: http://ruby-mp3info.rubyforge.org/