require "id3tag" require "fileutils" puts "enter absolute path to music folder:" indir = gets.chomp # Example: "/home/shivam/Music/Music/F01" indir = indir.sub(/\/$/, "") + "/*" puts "enter extraction path (absolute):" outdir = gets.chomp # Example: "/tmp/songs" outdir = outdir.sub(/\/$/, "") + "/" files = Dir[indir] files.reject! { |file| file !~ /.mp3/ } meta_array = [] files.each { |file| hash = {} ID3Tag.read(File.open(file, "rb")) do |tag| hash[:artist] = tag.artist hash[:title] = tag.title hash[:album] = tag.album hash[:path] = file end meta_array << hash } albums = meta_array.group_by { |a| a[:album] } begin albums.each { |album| album_path = outdir + album[0].gsub(/\//,"-") FileUtils.mkdir_p album_path album.last.each { |hash| title = (hash[:title] || "Unknown Track #{rand(1000)}").gsub(/\//,"-") FileUtils.cp hash[:path], (album_path + "/" + title).sub(/\.mp3$/,"") + ".mp3" } } end rescue => e puts e.inspect puts @hash.inspect end