Created
November 3, 2012 00:27
-
-
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
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Neat!
You could eliminate your double quote hell in those arrays by using the array literal.
%w{.mp3 .jpg .mid .MP3}