min file size, handle files not in dirs
This commit is contained in:
parent
9f7869c1c4
commit
5ee63fa28f
1 changed files with 68 additions and 29 deletions
|
|
@ -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,8 +29,17 @@ def main
|
|||
end
|
||||
|
||||
def extract_archives(dir)
|
||||
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)
|
||||
|
|
@ -41,11 +51,19 @@ def extract_archives(dir)
|
|||
Dir.chdir(pwd)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def encode_and_add_to_itunes(dir)
|
||||
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}..."
|
||||
|
|
@ -64,7 +82,6 @@ def encode_and_add_to_itunes(dir)
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def add_to_itunes(path)
|
||||
FileUtils.cp(path, ADD_TO_ITUNES_DIR)
|
||||
|
|
@ -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__
|
||||
|
|
|
|||
Loading…
Reference in a new issue