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'
|
require 'fileutils'
|
||||||
|
|
||||||
ADD_TO_ITUNES_DIR = File.expand_path('~/Music/iTunes/iTunes Media/Automatically Add to iTunes.localized')
|
ADD_TO_ITUNES_DIR = File.expand_path('~/Music/iTunes/iTunes Media/Automatically Add to iTunes.localized')
|
||||||
|
MIN_SIZE = 50 * 1024 * 1024
|
||||||
|
|
||||||
def main
|
def main
|
||||||
root_dir =
|
root_dir =
|
||||||
|
|
@ -28,8 +29,17 @@ def main
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract_archives(dir)
|
def extract_archives(dir)
|
||||||
|
if File.directory?(dir)
|
||||||
Dir.foreach(dir) do |filename|
|
Dir.foreach(dir) do |filename|
|
||||||
next if filename == '.' || 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)
|
path = File.join(dir, filename)
|
||||||
if File.directory?(path)
|
if File.directory?(path)
|
||||||
extract_archives(path)
|
extract_archives(path)
|
||||||
|
|
@ -41,11 +51,19 @@ def extract_archives(dir)
|
||||||
Dir.chdir(pwd)
|
Dir.chdir(pwd)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def encode_and_add_to_itunes(dir)
|
def encode_and_add_to_itunes(dir)
|
||||||
|
if File.directory?(dir)
|
||||||
Dir.foreach(dir) do |filename|
|
Dir.foreach(dir) do |filename|
|
||||||
next if filename == '.' || 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)
|
path = File.join(dir, filename)
|
||||||
if File.directory?(path)
|
if File.directory?(path)
|
||||||
# puts "* Descending into #{path}..."
|
# puts "* Descending into #{path}..."
|
||||||
|
|
@ -64,7 +82,6 @@ def encode_and_add_to_itunes(dir)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def add_to_itunes(path)
|
def add_to_itunes(path)
|
||||||
FileUtils.cp(path, ADD_TO_ITUNES_DIR)
|
FileUtils.cp(path, ADD_TO_ITUNES_DIR)
|
||||||
|
|
@ -72,6 +89,12 @@ def add_to_itunes(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def encode(dir, filename)
|
def encode(dir, filename)
|
||||||
|
path = File.join(dir, filename)
|
||||||
|
size = File.stat(path).size
|
||||||
|
if size < MIN_SIZE
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
ext = File.extname(filename)
|
ext = File.extname(filename)
|
||||||
encoded_path =
|
encoded_path =
|
||||||
case ext
|
case ext
|
||||||
|
|
@ -117,7 +140,7 @@ def encode_video(dir, filename, ext)
|
||||||
encoded_filename = encoded_filename(filename, ext)
|
encoded_filename = encoded_filename(filename, ext)
|
||||||
encoded_path = File.join('/tmp', encoded_filename)
|
encoded_path = File.join('/tmp', encoded_filename)
|
||||||
if File.exists?(encoded_path)
|
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"
|
puts "* Skipping #{filename}, it is already encoded"
|
||||||
encoded_path
|
encoded_path
|
||||||
else
|
else
|
||||||
|
|
@ -141,4 +164,20 @@ def encoded_filename(filename, ext)
|
||||||
filename.sub(/#{ext}$/, '.mp4')
|
filename.sub(/#{ext}$/, '.mp4')
|
||||||
end
|
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__
|
main if $0 == __FILE__
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue