class Captions::Base

Public Class Methods

new(file=nil, fps=25) click to toggle source

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)

# 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

Public Instance Methods

cues(&block) click to toggle source

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

# 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) click to toggle source

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

# File lib/captions/base.rb, line 34
def cues=(cue_list)
  @cue_list = CueList.new(@cue_list.fps, cue_list)
end
filter(&block) click to toggle source

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

# 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) click to toggle source

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.

# 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) click to toggle source

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.

# 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) click to toggle source

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.

# File lib/captions/base.rb, line 54
def set_frame_rate(rate)
  @cue_list.frame_rate = rate
end

Private Instance Methods

base_dump(file) { |file| ... } click to toggle source

This is the base method through which all subtitle exports has to be done. When a `file_path` is passed to this base method, it opens the file. Other logic about how the file should be written is defined inside `dump` method in one of the subclass.

# File lib/captions/base.rb, line 151
def base_dump(file)
  begin
    File.open(file, 'w') do |file|
      yield(file)
    end
  end
  return true
end
base_parser() { || ... } click to toggle source

This is the base method through which all subtitle file parsing should be done. This throws error if no `@file` is found. This also closes the file once the file has be parsed. All other logic like when the subtitle has to be inserted into the list, when to set the start and end time will be defined inside `parse` method.

# File lib/captions/base.rb, line 136
def base_parser
  raise UnknownFile, "No subtitle file specified" if @file.nil?
  begin
    yield
  ensure
    @file.close
  end
  return true
end
fetch_result(&block) click to toggle source

This accepts a block and returns the result based on the condition passed. This acts on the cuelist whenever a block is passed it does select with the condition passed and returns the result. When no block is passed, it returns the entire cuelist.

# File lib/captions/base.rb, line 171
def fetch_result(&block)
  if block_given?
    return @cue_list.select(&block)
  else
    return @cue_list
  end
end
frame_rate() click to toggle source

This returns the frame-rate which was used for parsing the subtitles

# File lib/captions/base.rb, line 162
def frame_rate
  @cue_list.fps
end