Created
May 21, 2015 22:38
-
-
Save jetpks/8f585473d6301b35722c to your computer and use it in GitHub Desktop.
Find m4a files, and make them m4r files.
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 | |
require 'FileUtils' | |
def main | |
config = parse_args | |
files = find_m4a(config[:src]) | |
mangled = mangle_names(files, config[:dst]) | |
move_files(mangled) | |
end | |
def move_files(mangled) | |
mangled.each do |src, dst| | |
begin | |
FileUtils.mv(src, dst) | |
rescue Exception => e | |
log("ERROR: Problem moving #{src} to #{dst}", true) | |
log("ERROR: #{e.message}", true) | |
exit(101) | |
end | |
log("✓ #{dst}") | |
end | |
end | |
def mangle_names(files, dst) | |
ret = {} | |
files.each do |file| | |
ret[file] = dst + '/' + file.split('/')[-1].gsub(/m4a$/, 'm4r') | |
end | |
ret | |
end | |
def find_m4a(src) | |
`find #{src} -name \*m4a -print0`.split("\0") | |
end | |
def parse_args | |
if ARGV.length != 2 then | |
log "ERROR: Invalid number of arguments.", true | |
usage | |
exit(42) | |
end | |
config = { :src => ARGV[0], :dst => ARGV[1] } | |
# Ensure source and dest dirs both exist | |
if not File.directory?(config[:src]) then | |
log "ERROR: source directory doesn't exist: #{config[:src]}" | |
usage | |
exit(43) | |
end | |
if not File.directory?(config[:dst]) then | |
Dir.mkdir(config[:dst]) | |
end | |
return config | |
end | |
def log(msg, error=false) | |
if error then | |
STDERR.puts(msg) | |
return | |
end | |
puts msg | |
end | |
def usage | |
log "" | |
log "# Usage" | |
log " #{__FILE__} path/to/m4a/files path/to/destination/directory" | |
log "# Example" | |
log " #{__FILE__} . ." | |
log " #{__FILE__} /home/user/music /home/user/ringtones" | |
log "# Description" | |
log " Recurses through given directory looking for m4a files to turn into" | |
log " m4r (Apple ringtone) files." | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment