Skip to content

Instantly share code, notes, and snippets.

@adamvduke
Created November 3, 2012 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamvduke/4005226 to your computer and use it in GitHub Desktop.
Save adamvduke/4005226 to your computer and use it in GitHub Desktop.
Get rid of crap files in your music library and convert crap formats to mp3
KEEP_EXTS = [".mp3",".jpg",".mid",".MP3",".wma",".m4a",".JPG",".mpg",".JPEG",".m4v",".mp4",".mov",".flac"]
UNWANTED_EXTS = [".ini",".db",".txt",".md5",".nfo",".pls",".info",".sfv",".NFO",".SFV",".url",".bal",".ogg",".php",".m3u",".LOG",".sls",".log",".pdf"]
CONVERT_EXTS = [".wma"]
def files(path)
Dir.glob("#{path}/**/*")
end
def find_files(path)
files(path).map do |file|
file if KEEP_EXTS.include?(File.extname(file))
end.compact
end
def delete_unwanted_files(path)
files(path).each do |file|
if (UNWANTED_EXTS.include? File.extname(file))
puts "Removing #{file}"
File.delete(file)
end
end
end
def output_file(path)
base_name = File.basename(path, ".*")
dir_name = File.dirname(path)
"#{dir_name}/#{base_name}.mp3"
end
def convert_files(paths)
paths.each do |path|
if CONVERT_EXTS.include?(File.extname(path))
output_file = output_file(path)
`ffmpeg -loglevel quiet -y -i "#{path}" -acodec mp3 -ab 128k "#{output_file}"`
if $?.exitstatus == 0
puts "Removing #{path}\n"
File.delete(path)
end
end
end
end
path = ARGV[0]
delete_unwanted_files(path)
files = find_files(path)
convert_files(files)
@KAllan357
Copy link

Neat!

You could eliminate your double quote hell in those arrays by using the array literal.

%w{.mp3 .jpg .mid .MP3}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment