class Mts::Autojoin::Runner

Constants

FOUR_GIGABYTES
TWO_GIGABYTES

Attributes

full_path[RW]
grouped_video_files[RW]
max_file_size[R]
video_files[RW]

Public Class Methods

new(path) click to toggle source
# File lib/mts/autojoin.rb, line 14
def initialize(path)
  @full_path = File.expand_path(path || '/')
  @video_files = []
  @max_file_size = TWO_GIGABYTES
  @grouped_video_files = Hash.new { |h,k| h[k] = [] }
end

Public Instance Methods

check_folder() click to toggle source
# File lib/mts/autojoin.rb, line 21
def check_folder
  if File.directory?(full_path)
    puts "MTS files will be checked at '#{full_path}'"
  else
    abort 'You did not provide a valid folder'
  end
end
check_video_files() click to toggle source
# File lib/mts/autojoin.rb, line 29
def check_video_files
  Dir.entries(full_path).sort.each do |file|
    next if (file == '.' || file == '..' || !['.mts','.MTS'].include?(File.extname(file)))
    @video_files << [file, File.size(File.expand_path(file, full_path))]
  end
  abort "No MTS files found at '#{full_path}'" if @video_files.empty?
end
create_file_list_and_execute() click to toggle source
# File lib/mts/autojoin.rb, line 62
def create_file_list_and_execute
  @grouped_video_files.each do |group_number, filenames|
    File.open("file-list-#{group_number}.meta","w") do |tmpfile|
      filenames.each do |filename|
        tmpfile.puts("file '" + File.expand_path(filename, full_path) + "'")
      end
    end
    execute_concat_command("file-list-#{group_number}.meta", group_number)
  end
end
delete_metafiles() click to toggle source
# File lib/mts/autojoin.rb, line 77
def delete_metafiles
  Dir.glob('*.meta').each { |f| File.delete(f) }
end
execute_concat_command(file, number) click to toggle source
# File lib/mts/autojoin.rb, line 73
def execute_concat_command(file, number)
  Kernel.system "ffmpeg -f concat -safe 0 -i #{file} -c copy video-output-#{number}.mts"
end
group_video_files() click to toggle source
# File lib/mts/autojoin.rb, line 48
def group_video_files
  current_filegroup = 1

  @video_files.each do |file, size|
    @grouped_video_files[current_filegroup] += [file]
    if size < max_file_size
      current_filegroup += 1
    end
  end

  @grouped_video_files
end
run!() click to toggle source
# File lib/mts/autojoin.rb, line 81
def run!
  check_folder
  check_video_files
  set_max_file_size
  group_video_files
  create_file_list_and_execute
  delete_metafiles
end
set_max_file_size() click to toggle source

Update max_file_size to FOUR_GIGABYTES if there is at least one file equal or greather than 4 gigabytes

# File lib/mts/autojoin.rb, line 39
def set_max_file_size
  @video_files.each do |file, size|
    if size >= FOUR_GIGABYTES
      @max_file_size = FOUR_GIGABYTES
      break
    end
  end
end