File size: 1,118 Bytes
63cab25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
module Whisper
  class Segment
    SRT_ESCAPES = {
      "&" => "&",
      "<" => "&lt;",
      ">" => "&gt;",
    }
    SRT_ESCAPES_RE = Regexp.union(SRT_ESCAPES.keys)
    private_constant :SRT_ESCAPES, :SRT_ESCAPES_RE

    def to_srt_cue
      "#{srt_start_time} --> #{srt_end_time}\n#{srt_text}\n"
    end

    def to_webvtt_cue
      "#{webvtt_start_time} --> #{webvtt_end_time}\n#{webvtt_text}\n"
    end

    private

    def time_to_a(time)
      sec, decimal_part = time.divmod(1000)
      min, sec = sec.divmod(60)
      hour, min = min.divmod(60)
      [hour, min, sec, decimal_part]
    end

    def srt_time(time)
      "%02d:%02d:%02d,%03d" % time_to_a(time)
    end

    def srt_start_time
      srt_time(start_time)
    end

    def srt_end_time
      srt_time(end_time)
    end

    def srt_text
      text.gsub(SRT_ESCAPES_RE, SRT_ESCAPES)
    end

    def webvtt_time(time)
      "%02d:%02d:%02d.%03d" % time_to_a(time)
    end

    def webvtt_start_time
      webvtt_time(start_time)
    end

    def webvtt_end_time
      webvtt_time(end_time)
    end

    alias webvtt_text srt_text
  end
end