Class: Mts::Autojoin::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mts/autojoin.rb

Constant Summary collapse

TWO_GIGABYTES =
2040000000
FOUR_GIGABYTES =
4172000000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Runner

Returns a new instance of Runner.



14
15
16
17
18
19
# 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

Instance Attribute Details

#full_pathObject

Returns the value of attribute full_path.



11
12
13
# File 'lib/mts/autojoin.rb', line 11

def full_path
  @full_path
end

#grouped_video_filesObject

Returns the value of attribute grouped_video_files.



11
12
13
# File 'lib/mts/autojoin.rb', line 11

def grouped_video_files
  @grouped_video_files
end

#max_file_sizeObject (readonly)

Returns the value of attribute max_file_size.



12
13
14
# File 'lib/mts/autojoin.rb', line 12

def max_file_size
  @max_file_size
end

#video_filesObject

Returns the value of attribute video_files.



11
12
13
# File 'lib/mts/autojoin.rb', line 11

def video_files
  @video_files
end

Instance Method Details

#check_folderObject



21
22
23
24
25
26
27
# 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_filesObject



29
30
31
32
33
34
35
# 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_executeObject



62
63
64
65
66
67
68
69
70
71
# 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_metafilesObject



77
78
79
# File 'lib/mts/autojoin.rb', line 77

def delete_metafiles
  Dir.glob('*.meta').each { |f| File.delete(f) }
end

#execute_concat_command(file, number) ⇒ Object



73
74
75
# 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_filesObject



48
49
50
51
52
53
54
55
56
57
58
59
# 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!Object



81
82
83
84
85
86
87
88
# 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_sizeObject

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



39
40
41
42
43
44
45
46
# 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