Class: Application

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

Overview

Main application class that manages all the operations.

Instance Method Summary collapse

Constructor Details

#initialize(input, options) ⇒ Application

Creates a new Application instance. It receives options that can define the input and output directories.



12
13
14
15
# File 'lib/vtt2ass/application.rb', line 12

def initialize(input, options)
  @options = options
  @input = sanitize_path(input)
end

Instance Method Details

#convert(input_path) ⇒ Object

This method launches the conversion process on the specified input file.

Raises:

  • (StandardError)


47
48
49
50
51
52
53
54
55
# File 'lib/vtt2ass/application.rb', line 47

def convert(input_path)
  output = sanitize_path(@options[:output])
  output = '.' if output.nil?
  raise StandardError, 'ERROR: Output directory does not exist.' unless File.directory?(output)

  ass_file = vtt_to_ass(input_path)
  ass_file.write_to_file("#{output}/#{File.basename(input_path).gsub('.vtt', '.ass')}") unless output.nil?
  puts ass_file.to_s unless @options[:quiet]
end

#sanitize_path(path) ⇒ Object

Replace backslashes from Windows paths to normal slashes. Deletes the trailing slash if there is one.



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

def sanitize_path(path)
  path&.gsub('\\', '/')&.delete_suffix('/')
end

#startObject

This method starts the application process. It sends the file_paths of VTT files in the input directory to convertFileToASS method and outputs the resulting ASS format to a new file.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vtt2ass/application.rb', line 28

def start
  if File.directory?(@input)
    Dir["#{@input}/*.vtt"].each do |file_path|
      convert(file_path)
    end
  elsif File.file?(@input)
    convert(@input)
  else
    raise StandardError, 'ERROR: Input file or directory does not exist.'
  end
rescue SystemExit, Interrupt
  puts 'ERROR: The application stopped unexpectedly. The conversion may not have been completed.'
rescue StandardError => e
  puts e.message
  puts e.backtrace
end

#vtt_to_ass(file_path) ⇒ Object

This method creates a new VTTFile object from the file path provided and convert its content inside a new ASSFile object.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vtt2ass/application.rb', line 60

def vtt_to_ass(file_path)
  base_file_name = File.basename(file_path).gsub('.vtt', '')
  css_file =
    if !@options[:css].nil? && File.directory?(@options[:css])
      "#{sanitize_path(@options[:css])}/#{base_file_name}.css"
    elsif File.file?("#{file_path.gsub('.vtt', '')}.css")
      "#{file_path.gsub('.vtt', '')}.css"
    else
      @options[:css]
    end
  vtt_file = VTTFile.new(file_path, @options[:width], @options[:height])
  ass_file = ASSFile.new(
    (@options[:title].nil? ? base_file_name : @options[:title]),
    @options[:width],
    @options[:height],
    css_file
  )
  ass_file.convert_vtt_to_ass(vtt_file, @options[:font_family], @options[:font_size],
                              { line: @options[:line_offset], caption: @options[:caption_offset] })
  ass_file
end