Class: Captions::Cue

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/captions/cue.rb

Constant Summary collapse

ALIGNMENT =

Text Properties supported

"alignment"
COLOR =
"color"
POSITION =
"position"
TEXT_PROPERTIES =

List of Text Properties

[ALIGNMENT, COLOR, POSITION]

Constants included from Util

Util::TIMECODE_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#convert_frame_rate, #convert_to_msec, #msec_to_timecode, #sanitize

Constructor Details

#initialize(cue_number = nil) ⇒ Cue

Creates a new Cue class Each cue denotes a subtitle.



17
18
19
20
21
22
23
24
# File 'lib/captions/cue.rb', line 17

def initialize(cue_number = nil)
  self.text = nil
  self.start_time = nil
  self.end_time = nil
  self.duration = nil
  self.number = cue_number
  self.properties = {}
end

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



13
14
15
# File 'lib/captions/cue.rb', line 13

def duration
  @duration
end

#end_timeObject

Returns the value of attribute end_time.



13
14
15
# File 'lib/captions/cue.rb', line 13

def end_time
  @end_time
end

#numberObject

Returns the value of attribute number.



13
14
15
# File 'lib/captions/cue.rb', line 13

def number
  @number
end

#propertiesObject

Returns the value of attribute properties.



13
14
15
# File 'lib/captions/cue.rb', line 13

def properties
  @properties
end

#start_timeObject

Returns the value of attribute start_time.



13
14
15
# File 'lib/captions/cue.rb', line 13

def start_time
  @start_time
end

#textObject

Returns the value of attribute text.



13
14
15
# File 'lib/captions/cue.rb', line 13

def text
  @text
end

Instance Method Details

#<=>(other_cue) ⇒ Object



89
90
91
# File 'lib/captions/cue.rb', line 89

def <=>(other_cue)
  self.start_time <=> other_cue.start_time
end

#add_text(text) ⇒ Object

Adds text. If text is already found, new-line is appended.



81
82
83
84
85
86
87
# File 'lib/captions/cue.rb', line 81

def add_text(text)
  if self.text.nil?
    self.text = text
  else
    self.text += "\n" + text
  end
end

#change_frame_rate(old_rate, new_rate) ⇒ Object

Changes start-time, end-time and duration based on new frame-rate



74
75
76
77
78
# File 'lib/captions/cue.rb', line 74

def change_frame_rate(old_rate, new_rate)
  self.start_time = convert_frame_rate(self.start_time, old_rate, new_rate)
  self.end_time = convert_frame_rate(self.end_time, old_rate, new_rate)
  self.duration = convert_frame_rate(self.duration, old_rate, new_rate)
end

#serialize(fps) ⇒ Object

Serializes the values set for the cue. Converts start-time, end-time and duration to milliseconds If duration is not found, it will be calculated based on start-time and end-time.

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/captions/cue.rb', line 55

def serialize(fps)
  raise InvalidSubtitle, "Subtitle should have start time" if self.start_time.nil?
  raise InvalidSubtitle, "Subtitle shold have end time" if self.end_time.nil?

  begin
    ms_per_frame = (1000.0 / fps)
    self.start_time = convert_to_msec(self.start_time, ms_per_frame)
    self.end_time = convert_to_msec(self.end_time, ms_per_frame)
    if duration.nil?
      self.duration = self.end_time - self.start_time
    else
      self.duration = convert_to_msec(self.duration, ms_per_frame)
    end
  rescue
    raise InvalidSubtitle, "Cannot calculate start-time or end-time"
  end
end

#set_time(start_time, end_time, duration = nil) ⇒ Object

Sets the time for the cue. Both start-time and end-time can be passed together. This just assigns the value passed.



29
30
31
32
33
# File 'lib/captions/cue.rb', line 29

def set_time(start_time, end_time, duration = nil)
  self.start_time = start_time
  self.end_time = end_time
  self.duration = duration
end