Created
April 5, 2011 17:45
-
-
Save arturopuente/904092 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Lo de la extensión es curioso, sin quitarla/reagregarla deja los archivos con la extensión capitalizada (.Mp3), que me parece menos bonito que .mp3 :P
Por eso acá captura sólo la primera letra y a eso aplica la conversión :P
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Por ejemplo:
Claro que puedes perder algo de legibilidad. En tracknumber es casi lo mismo (una linea). En trackname si ahorras líneas.