Skip to content

Instantly share code, notes, and snippets.

@xenda
Forked from arturopuente/mp3_renamer.rb
Created April 13, 2011 13:37
Show Gist options
  • Save xenda/917551 to your computer and use it in GitHub Desktop.
Save xenda/917551 to your computer and use it in GitHub Desktop.
# gem install ruby-mp3info
require 'mp3info'
# Formato: # Pista [espacio] Nombre
# Ejemplo de filtros:
# Antes: 05 - Run Run Run - The Velvet Underground.mp3
# filters = ["- ", " - The Velvet Underground"]
# Después: 05 Run Run Run.mp3
def prepare(name)
filters = [""]
filters.each { |f| name = name.sub(f, "") }
ext = File.extname(name)
name = name.sub(ext, "")
name = name.gsub(/\b('?[a-z])/) { $1.capitalize }
name += ext
end
def get_tracknum(name)
name[0..name.index(" ")].to_i
end
def get_trackname(name)
ext = File.extname(name).length + 1
name[(name.index(" ") + 1)..(name.length - ext)]
end
Dir.entries(".").each do |file|
if File.file?(file) && File.extname(file).casecmp(".mp3") == 0
new_file = prepare(file)
File.rename(file, new_file)
Mp3Info.open(new_file) do |mp3|
mp3.tag.tracknum = get_tracknum(new_file)
mp3.tag.title = get_trackname(new_file)
# mp3.tag.artist = "The Velvet Underground"
# mp3.tag.album = "The Velvet Underground & Nico"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment