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
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vtt2ass/application.rb', line 12

def initialize(input, options)
    @input = input ? input.gsub('\\', '/').delete_suffix('/') : "."
    @output = options[:output] ? options[:output].gsub('\\', '/').delete_suffix('/') : nil
    @width = 1920
    @height = 1080
    @font_family = options[:font_family] ? options[:font_family] : 'Open Sans Semibold'
    @font_size = options[:font_size] ? options[:font_size] : 52
    if options[:title] then
        @title = options[:title]
    end
    @quiet = options[:quiet]
    if options[:css] then
        @css = options[:css].gsub('\\', '/').delete_suffix('/')
    end
    @line_offset = options[:line_offset]
end

Instance Method Details

#convert(input_path) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/vtt2ass/application.rb', line 45

def convert(input_path)
    ass_file = vtt_to_ass(input_path)
    if (not @output.nil?) then
        ass_file.write_to_file(@output + '/' + File.basename(input_path).gsub('.vtt', '.ass'))
    end
    puts ass_file.to_s unless @quiet
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.



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

def start
    if File.directory?(@input) then
        Dir["#{@input}/*.vtt"].each do |file_path|
            convert(file_path)
        end
    elsif File.file?(@input) then
        convert(@input)
    else
        puts 'Error: input file or directory does not exist.'
    end
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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vtt2ass/application.rb', line 56

def vtt_to_ass(file_path)
    base_file_name = File.basename(file_path).gsub('.vtt', '')
    css_file = nil
    if defined?(@css) and File.directory?(@css) then
        css_file = "#{@css}/#{base_file_name}.css"
    elsif File.file?("#{file_path.gsub('.vtt', '')}.css") then
        css_file = "#{file_path.gsub('.vtt', '')}.css"
    else
        css_file = @css
    end
    vtt_file = VTTFile.new(file_path, @width, @height)
    ass_file = ASSFile.new(
        (defined?(@title) ? @title : base_file_name),
        @width,
        @height,
        css_file
    )
    ass_file.convert_vtt_to_ass(vtt_file, @font_family, @font_size, @line_offset)
    return ass_file
end