Skip to content

Instantly share code, notes, and snippets.

@aphyr
Created February 18, 2016 16:24
Show Gist options
  • Save aphyr/4a7b71eece3c92599135 to your computer and use it in GitHub Desktop.
Save aphyr/4a7b71eece3c92599135 to your computer and use it in GitHub Desktop.
#!/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