Class: Captions::Base

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

Direct Known Subclasses

SRT, VTT

Constant Summary

Constants included from Util

Util::TIMECODE_REGEX

Instance Method Summary collapse

Methods included from Util

#convert_frame_rate, #convert_to_msec, #msec_to_timecode, #sanitize

Constructor Details

#initialize(file = nil, fps = 25) ⇒ Base

Creates new instance of parser Usage:

p = Captions::Base.new(nil, 25)

This creates new cue-list with no file object associated with it. ‘parse` method cannot be called if no file path is specified.

p = Captions::Base.new('file_path', 25)

This parses the file specified in the file path ‘parse` method will be defined in any one of the classes which extends this base class. This parses the file in the fps specified. If a subtitle file has to be parsed in different fps, it can be passed as a parameter. FPS parameter plays an important role if the start-time or end-time of a subtitle is mentioned in frames (i.e) HH:MM:SS:FF (frames)



26
27
28
29
# File 'lib/captions/base.rb', line 26

def initialize(file=nil, fps=25)
  @cue_list = CueList.new(fps)
  @file = File.new(file, 'r:bom|utf-8') if file
end

Instance Method Details

#cues(&block) ⇒ Object

This returns the subtitle which are parsed. A block can also be passed to filter the cues based on following parameters. Usage:

p.cues
p.cues { |c| c.start_time > 1000 }
p.cues { |c| c.end_time > 1000 }
p.cues { |c| c.duration > 1000 }

Filters based on the condition and returns new set of cues



68
69
70
71
72
73
74
75
76
# File 'lib/captions/base.rb', line 68

def cues(&block)
  if block_given?
    base = self.class.new()
    base.cues = fetch_result(&block)
    return base.cues
  else
    @cue_list
  end
end

#cues=(cue_list) ⇒ Object

This overrides the existing cuelist which has been populated with a new cuelist. This is mainly used in export of one format to another.



34
35
36
# File 'lib/captions/base.rb', line 34

def cues=(cue_list)
  @cue_list = CueList.new(@cue_list.fps, cue_list)
end

#filter(&block) ⇒ Object

This filters the subtitle based on the condition and returns a new object. A condition is mandatory to filter subtitles Usage:

p.filter
p.filter { |c| c.start_time > 1000 }
p.filter { |c| c.end_time > 1000 }
p.filter { |c| c.duration > 1000 }

Filters and returns a Captions class rather than a cue list



87
88
89
90
91
92
93
# File 'lib/captions/base.rb', line 87

def filter(&block)
  if block_given?
    base = self.class.new()
    base.cues = fetch_result(&block)
    return base
  end
end

#increase_duration_by(diff, &block) ⇒ Object

Increases duration of subtitles by ‘n` milliseconds Usage:

p.increase_duration_by(1000)
p.increase_duration_by("00:00:02.000")
p.increase_duration_by(1000) { |c| c.start_time > 2000 }

This increases duration of subtiltes by the time passed.



118
119
120
121
122
123
# File 'lib/captions/base.rb', line 118

def increase_duration_by(diff, &block)
  msec = sanitize(diff, frame_rate)
  fetch_result(&block).each do |cue|
    cue.duration += msec
  end
end

#move_by(diff, &block) ⇒ Object

Moves subtitles by ‘n` milliseconds Usage:

p.move_by(1000)
p.move_by("00:00:02.000")
p.move_by(1000) { |c| c.start_time > 2000 }

This changes start-time and end-time of subtiltes by the time passed.



103
104
105
106
107
108
109
# File 'lib/captions/base.rb', line 103

def move_by(diff, &block)
  msec = sanitize(diff, frame_rate)
  fetch_result(&block).each do |cue|
    cue.start_time += msec
    cue.end_time += msec
  end
end

#set_frame_rate(rate) ⇒ Object

A subtitle is parsed with 25 fps by default. This default value can be changed when creating a new parser class. When the subtitle is being parsed, it takes the value mentioned when the class is created. Even after the subtitle is parsed, frame rate (fps) can be changed using this method. Usage:

p = Captions::Base.new('file_path', 25)
p.parse
p.set_frame_rate(29.97)

This method changes all the subtitle which are parsed to the new frame rate.



54
55
56
# File 'lib/captions/base.rb', line 54

def set_frame_rate(rate)
  @cue_list.frame_rate = rate
end