Class: VideoConverter::Converter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_concurrency: 2, batch_size: 20, conversion_delay: 0.5) ⇒ Converter

Returns a new instance of Converter.



9
10
11
12
13
# File 'lib/video_converter.rb', line 9

def initialize(max_concurrency: 2, batch_size: 20, conversion_delay: 0.5)
  @max_concurrency = max_concurrency
  @batch_size = batch_size
  @conversion_delay = conversion_delay
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



7
8
9
# File 'lib/video_converter.rb', line 7

def batch_size
  @batch_size
end

#conversion_delayObject

Returns the value of attribute conversion_delay.



7
8
9
# File 'lib/video_converter.rb', line 7

def conversion_delay
  @conversion_delay
end

#max_concurrencyObject

Returns the value of attribute max_concurrency.



7
8
9
# File 'lib/video_converter.rb', line 7

def max_concurrency
  @max_concurrency
end

Instance Method Details

#convert_file(source_file, target_file) ⇒ Object



15
16
17
18
# File 'lib/video_converter.rb', line 15

def convert_file(source_file, target_file)
  movie = FFMPEG::Movie.new(source_file)
  movie.transcode(target_file)
end

#convert_files_in_batch(files, source_ext, target_ext) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/video_converter.rb', line 40

def convert_files_in_batch(files, source_ext, target_ext)
  executor = Concurrent::ThreadPoolExecutor.new(
    min_threads: 1,
    max_threads: max_concurrency,
    max_queue: files.size,
    fallback_policy: :caller_runs
  )

  files.each do |file|
    executor.post do
      new_filename = file.gsub(source_ext, target_ext)
      if !File.exist?(new_filename)
        convert_file(file, new_filename)
        puts "Converted: #{file} -> #{new_filename}"
        FileUtils.rm(file) # Remove the original file after conversion
      else
        puts "Skipped conversion (already exists): #{file}"
      end
    end
  end

  executor.shutdown
  executor.wait_for_termination
end

#convert_single_file(source_file, target_file = nil, target_format = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/video_converter.rb', line 65

def convert_single_file(source_file, target_file = nil, target_format = nil)
  if File.exist?(source_file)
    if target_file.nil? && !target_format.nil?
      target_file = source_file.gsub(File.extname(source_file), target_format)
    end

    convert_file(source_file, target_file)
    puts "Converted: #{source_file} -> #{target_file}"
    FileUtils.rm(source_file) # Remove the original file after conversion
  else
    puts "File not found: #{source_file}"
  end
end

#get_source_files_recursive(dir, source_ext) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/video_converter.rb', line 24

def get_source_files_recursive(dir, source_ext)
  source_files = []
  Dir.foreach(dir) do |file|
    next if file == '.' || file == '..'

    current_path = File.join(dir, file)
    if File.directory?(current_path)
      source_files.concat(get_source_files_recursive(current_path, source_ext)) # Recursively call for subdirectories
    elsif file.end_with?(source_ext)
      source_files << current_path
    end
  end

  source_files
end

#rename_and_convert_files(dir, source_ext, target_ext) ⇒ Object



20
21
22
# File 'lib/video_converter.rb', line 20

def rename_and_convert_files(dir, source_ext, target_ext)
  convert_files_in_batch(get_source_files_recursive(dir, source_ext), source_ext, target_ext)
end