Class: WebVTT::Cue

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cue = nil) ⇒ Cue

Returns a new instance of Cue.



11
12
13
14
# File 'lib/vtt/cue.rb', line 11

def initialize(cue = nil)
  @content = cue
  @style = {}
end

Instance Attribute Details

#endObject

Returns the value of attribute end.



9
10
11
# File 'lib/vtt/cue.rb', line 9

def end
  @end
end

#identifierObject

Returns the value of attribute identifier.



9
10
11
# File 'lib/vtt/cue.rb', line 9

def identifier
  @identifier
end

#startObject

Returns the value of attribute start.



9
10
11
# File 'lib/vtt/cue.rb', line 9

def start
  @start
end

#styleObject

Returns the value of attribute style.



9
10
11
# File 'lib/vtt/cue.rb', line 9

def style
  @style
end

#textObject

Returns the value of attribute text.



9
10
11
# File 'lib/vtt/cue.rb', line 9

def text
  @text
end

Class Method Details

.parse(cue) ⇒ Object

读取内容



17
18
19
20
21
# File 'lib/vtt/cue.rb', line 17

def self.parse(cue)	    	
  cue = Cue.new(cue)
  cue.parse
  return cue
end

.timestamp_in_sec(timestamp) ⇒ Object

时间戳转换成秒



34
35
36
37
38
39
40
# File 'lib/vtt/cue.rb', line 34

def self.timestamp_in_sec(timestamp)
  mres = timestamp.match(/([0-9]{2}):([0-9]{2}):([0-9]{2}\.[0-9]{3})/)
  sec = mres[3].to_f # 秒
  sec += mres[2].to_f * 60 #分
  sec += mres[1].to_f * 60 * 60 #小时
  return sec
end

Instance Method Details

#end_in_secObject

结束时间转换成fixnum类型



48
49
50
# File 'lib/vtt/cue.rb', line 48

def end_in_sec
  @end.to_f
end

#lengthObject

持续时间



53
54
55
# File 'lib/vtt/cue.rb', line 53

def length
  @end.to_f - @start.to_f
end

#offset_by(offset_secs) ⇒ Object

时间添加



58
59
60
61
# File 'lib/vtt/cue.rb', line 58

def offset_by( offset_secs )
  @start += offset_secs
  @end   += offset_secs
end

#parseObject

切割文字内容 把文字内容二次加工



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vtt/cue.rb', line 64

def parse
  lines = @content.split("\n").map(&:strip)
  return if lines[0] =~ /NOTE/

  if !lines[0].include?("-->")
    @identifier = lines[0]
    lines.shift
  end

  if lines.empty?
    return
  end

  if lines[0].match(/(([0-9]{2}:)?[0-9]{2}:[0-9]{2}\.[0-9]{3}) -+> (([0-9]{2}:)?[0-9]{2}:[0-9]{2}\.[0-9]{3})(.*)/)
    @start = Timestamp.new $1
    @end = Timestamp.new $3
    @style = Hash[$5.strip.split(" ").map{|s| s.split(":").map(&:strip) }]
  end
  @text = lines[1..-1].join("\n")
end

#start_in_secObject

开始时间转换成fixnum类型



43
44
45
# File 'lib/vtt/cue.rb', line 43

def start_in_sec
  @start.to_f
end

#to_webvttObject

把文字内容写入



24
25
26
27
28
29
30
31
# File 'lib/vtt/cue.rb', line 24

def to_webvtt
  res = ""
  if @identifier
    res << "#{@identifier}\n"
  end
  res << "#{@content}".strip 
  res
end