Class: ShiftSubtitle

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

Direct Known Subclasses

ShiftSubtitleCli

Instance Method Summary collapse

Instance Method Details

#shift_srt(input_srt, output_srt, shift_seconds) ⇒ Object

Takes an SRT IO stream (such as File) and shifts it by shift_seconds (e.g. -2.5)



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/shift_subtitle.rb', line 6

def shift_srt(input_srt, output_srt, shift_seconds)
  input_srt.each_line do | line |
    srt_time_regex = /^(\d\d:\d\d:\d\d,\d\d\d)( --> )(\d\d:\d\d:\d\d,\d\d\d)/ 
    if (line =~ srt_time_regex)
      start_time = shift_timestamp($1, shift_seconds)
      end_time = shift_timestamp($3, shift_seconds)
      line.gsub!(srt_time_regex, start_time + $2 + end_time)        
    end
    output_srt.write line
  end
end

#shift_timestamp(srt_time_string, shift_seconds) ⇒ Object

Takes an SRT timestamp (‘01:01:23,500’) and shifts it by shift seconds (-2.5)



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shift_subtitle.rb', line 19

def shift_timestamp(srt_time_string, shift_seconds)
  if (srt_time_string =~ /(\d\d):(\d\d):(\d\d),(\d\d\d)/)
    time = Time.parse('2000-01-01 ' + srt_time_string + 'UTC') + shift_seconds
    hours = time.hour
    case
      when time.day == 31 then hours = 0; time = Time.at(0).utc # Never go below 00:00:00,000
      when time.day > 1 then hours += (time.day - 1) * 24 # Support times > 23 hours
    end
    "#{'%02d'%hours}:#{'%02d'%time.min}:#{'%02d'%time.sec},#{'%03d'%(time.usec/1000)}"
  else
    raise ArgumentError, "unexpected SRT timestamp format " + srt_time_string
  end
end