Class: ShiftSubtitle::CLI

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

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/shift_subtitle/cli.rb', line 6

def self.execute(stdout, arguments=[])

  options = {}

  optparse = OptionParser.new do |opts|
    opts.banner = "Usage: shift_subtitle.rb [options] input_file output_file"

    options[:operation] = ""
    opts.on('-p', '--operation OPERATION', 'add or sub') do |operation|
      options[:operation] = operation
    end

    options[:time] = ""
    opts.on('-t', '--time TIME', 'how much time to shift subtitles by') do |time|
      options[:time] = time
    end

    options[:start] = 1
    opts.on('-s', '--start_from [START]', 'where to start shifting subtitles from') do |start|
      options[:start] = start
    end

    options[:input_file] = ""
    opts.on('-i', '--input_file INPUT_FILE', 'the original subtitles') do |input_file|
      options[:input_file] = input_file
    end

    options[:output_file] = 
    opts.on('-o', '--output_file OUTPUT_FILE', 'where to save results') do |output_file|
      options[:output_file] = output_file
    end
  end

  optparse.parse!

  @time_shift_ms = options[:time].gsub(",", "").to_i
  @input_file_path = options[:input_file]
  @output_file_path = options[:output_file]
  @operation = options[:operation]
  @start = options[:start].to_i
  
  @subtitles = SRTParser.new(@input_file_path).subtitles
  
  output_file = File.new(@output_file_path, "w+")

  @subtitles.each do |subtitle|
    if @operation == "add"
      subtitle.shift_time_range_forward(@time_shift_ms) if subtitle.id >= @start
    elsif @operation == "sub"
      subtitle.shift_time_range_backward(@time_shift_ms) if subtitle.id >= @start
    end
    output_file.puts subtitle.to_s
    output_file.puts "\n"
  end

  output_file.close
end