Class: SrtShifter::Subtitle

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Subtitle

Returns a new instance of Subtitle.



6
7
8
# File 'lib/srtshifter/subtitle.rb', line 6

def initialize(options)
	@opts = options
end

Instance Method Details

#convert_time(time) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/srtshifter/subtitle.rb', line 41

def convert_time(time)
  time_diff = time_in_seconds(@opts[:time])
  if @opts[:operation] == "SUB" and time_diff > 0
    time_diff *= -1
  end

  current_time = Time.parse(time)
  new_time = Time.at(current_time.to_f + time_diff)
  "#{new_time.strftime('%H:%M:%S')},#{sprintf("%.3d",new_time.usec/1000)}"
end

#shiftObject



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
# File 'lib/srtshifter/subtitle.rb', line 10

def shift

	start_time = Time.now

	output_file = File.open(@opts[:output], 'w')

	lines = IO.readlines(@opts[:input])

	lines.each do |line|

		time_line = line.force_encoding("iso-8859-1")
		time_match = time_line.scan(/(\d{2}:\d{2}:\d{2},\d{3}) --\> (\d{2}:\d{2}:\d{2},\d{3})/)
      time_match.flatten!
		
		if time_match[0].nil? || time_match[1].nil?
			output_file.write(line)
		else
			new_start_time = convert_time(time_match[0])
    				new_end_time = convert_time(time_match[1])
    				new_line = "#{new_start_time} --> #{new_end_time}\n"

    				output_file.write(new_line)
		end
	end

	end_time = Time.now

	puts "\nFile created with success!"
	puts "Elapsed time: #{end_time-start_time} seconds."
end