Created
February 18, 2016 16:24
-
-
Save aphyr/4a7b71eece3c92599135 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
#!/usr/bin/env ruby | |
# Takes a directory and turns music in it to mp3s; deleting originals. | |
require 'find' | |
require 'fileutils' | |
# Find files | |
files = [] | |
Find.find(ARGV.first) do |file| | |
if file =~ /\.(aac|mp4|m4a|wma|m4p|ogg|flac|wav)$/ | |
files << file | |
end | |
end | |
# Previous runs might have got somewhere | |
remaining = files.reject do |f| | |
File.exists?(f.sub(/\.[^\.]+$/, '.mp3')) | |
end | |
exit if files.empty? | |
# Convert | |
unless remaining.empty? | |
puts ['parallel', "soundconverter", "-b", "-m", "audio/mpeg", "-s", ".mp3", | |
':::', *remaining].join(" ") | |
system('parallel', "soundconverter", "-b", "-m", "audio/mpeg", "-s", ".mp3", | |
':::', *remaining) | |
if $? != 0 | |
puts "Encountered errors; not deleting anything" | |
exit! | |
end | |
end | |
# Delete | |
puts "Deleting files" | |
files.each do |file| | |
File.delete file | |
end | |
puts "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment