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(options) ⇒ Application

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



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vtt2ass/Application.rb', line 15

def initialize(options)
    @input = options[:input] ? options[:input].gsub('\\', '/').delete_suffix('/') : "."
    @output = options[:output] ? options[:output].gsub('\\', '/').delete_suffix('/') : "."
    @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
end

Instance Method Details

#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.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vtt2ass/Application.rb', line 31

def start
    if File.directory?(@input) then
        Dir["#{@input}/*.vtt"].each do |file_path|
            vtt_to_ass(file_path).writeToFile(@output + '/' + File.basename(file_path).gsub('.vtt', '.ass'))
        end
    elsif File.file?(@input) then
        vtt_to_ass(@input).writeToFile(@output + '/' + File.basename(@input).gsub('.vtt', '.ass'))
    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.



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

def vtt_to_ass(file_path)
    vtt_file = VTTFile.new(file_path)
    ass_file = ASSFile.new(
        (defined?(@title) ? @title : File.basename(file_path).gsub('.vtt', '')),
        @width,
        @height
    )
    ass_file.convertVTTtoASS(vtt_file, @font_family, @font_size)
    return ass_file
end