min file size, handle files not in dirs

This commit is contained in:
Sami Samhuri 2014-09-27 09:14:00 -07:00
parent 9f7869c1c4
commit 5ee63fa28f

View file

@ -3,6 +3,7 @@
require 'fileutils'
ADD_TO_ITUNES_DIR = File.expand_path('~/Music/iTunes/iTunes Media/Automatically Add to iTunes.localized')
MIN_SIZE = 50 * 1024 * 1024
def main
root_dir =
@ -28,40 +29,56 @@ def main
end
def extract_archives(dir)
Dir.foreach(dir) do |filename|
next if filename == '.' || filename == '..'
path = File.join(dir, filename)
if File.directory?(path)
extract_archives(path)
elsif filename =~ /\.rar$/
pwd = Dir.pwd
Dir.chdir(dir)
puts "* Extracting #{filename}..."
`unrar x '#{filename}'`
Dir.chdir(pwd)
if File.directory?(dir)
Dir.foreach(dir) do |filename|
next if filename == '.' || filename == '..'
_extract_archives(dir, filename)
end
else
_extract_archives(*File.split(dir))
end
end
def _extract_archives(dir, filename)
path = File.join(dir, filename)
if File.directory?(path)
extract_archives(path)
elsif filename =~ /\.rar$/
pwd = Dir.pwd
Dir.chdir(dir)
puts "* Extracting #{filename}..."
`unrar x '#{filename}'`
Dir.chdir(pwd)
end
end
def encode_and_add_to_itunes(dir)
Dir.foreach(dir) do |filename|
next if filename == '.' || filename == '..'
path = File.join(dir, filename)
if File.directory?(path)
# puts "* Descending into #{path}..."
encode_and_add_to_itunes(path)
if File.directory?(dir)
Dir.foreach(dir) do |filename|
next if filename == '.' || filename == '..'
_encode_and_add_to_itunes(dir, filename)
end
else
_encode_and_add_to_itunes(*File.split(dir))
end
end
def _encode_and_add_to_itunes(dir, filename)
path = File.join(dir, filename)
if File.directory?(path)
# puts "* Descending into #{path}..."
encode_and_add_to_itunes(path)
else
encoded_path = encode(File.expand_path(dir), filename)
if encoded_path == :noencode
puts "* No encoding required for #{path}, adding to iTunes"
add_to_itunes(path)
elsif encoded_path
puts "* Encoded as #{encoded_path}, adding to iTunes"
add_to_itunes(encoded_path)
File.unlink(encoded_path)
else
encoded_path = encode(File.expand_path(dir), filename)
if encoded_path == :noencode
puts "* No encoding required for #{path}, adding to iTunes"
add_to_itunes(path)
elsif encoded_path
puts "* Encoded as #{encoded_path}, adding to iTunes"
add_to_itunes(encoded_path)
File.unlink(encoded_path)
else
# skipped or failed
end
# skipped or failed
end
end
end
@ -72,6 +89,12 @@ def add_to_itunes(path)
end
def encode(dir, filename)
path = File.join(dir, filename)
size = File.stat(path).size
if size < MIN_SIZE
return
end
ext = File.extname(filename)
encoded_path =
case ext
@ -117,7 +140,7 @@ def encode_video(dir, filename, ext)
encoded_filename = encoded_filename(filename, ext)
encoded_path = File.join('/tmp', encoded_filename)
if File.exists?(encoded_path)
# TODO: option to skill all or remove
# TODO: option to skip all or remove
puts "* Skipping #{filename}, it is already encoded"
encoded_path
else
@ -141,4 +164,20 @@ def encoded_filename(filename, ext)
filename.sub(/#{ext}$/, '.mp4')
end
SIZE_SUFFIXES = %w[bytes KB MB GB TB PB EB]
def human_size(n)
suffix_idx = 0
while n > 1023 && suffix_idx < SIZE_SUFFIXES.length - 1
n /= 1024.0
suffix_idx += 1
end
suffix = SIZE_SUFFIXES[suffix_idx]
if n - n.to_i < 0.01
n = n.to_i
else
n = "%0.2f" % n
end
"#{n} #{suffix}"
end
main if $0 == __FILE__