class Subber::Formatter::Srt

Constants

CRLF
LF_REGEX

Public Class Methods

format(subtitles) click to toggle source

@param subtitles [Array<Subber::Subtitle>] @return [String]

# File lib/subber/formatter/srt.rb, line 10
def format(subtitles)
  subtitle_texts =
    subtitles.map do |subtitle|
      convert_subtitle_to_text(subtitle)
    end

  file_content = subtitle_texts.join
  file_content = add_window_line_break(file_content)
  file_content
end

Private Class Methods

add_window_line_break(file_content) click to toggle source

@param file_content [String] @return [String]

# File lib/subber/formatter/srt.rb, line 26
def add_window_line_break(file_content)
  file_content.gsub(LF_REGEX, CRLF)
end
build_content(subtitle) click to toggle source

@param subtitle [Subber::Subtitle] @return [String]

# File lib/subber/formatter/srt.rb, line 63
def build_content(subtitle)
  subtitle.content
end
build_counter(subtitle) click to toggle source

@param subtitle [Subber::Subtitle] @return [String]

# File lib/subber/formatter/srt.rb, line 46
def build_counter(subtitle)
  subtitle.counter.to_s
end
build_time_range(subtitle) click to toggle source

@param subtitle [Subber::Subtitle] @return [String]

# File lib/subber/formatter/srt.rb, line 53
def build_time_range(subtitle)
  start_time = convert_ms_to_time(subtitle.start_time)
  end_time = convert_ms_to_time(subtitle.end_time)

  "#{start_time} --> #{end_time}"
end
convert_ms_to_time(ms_time) click to toggle source

@param ms_time [Integer] Time in milliseconds @return [String] Formatted time

# File lib/subber/formatter/srt.rb, line 70
def convert_ms_to_time(ms_time)
  seconds = ms_time / 1000.0
  Time.at(seconds).utc.strftime("%H:%M:%S,%L")
end
convert_subtitle_to_text(subtitle) click to toggle source

@param subtitle [Subber::Subtitle] @return [String]

# File lib/subber/formatter/srt.rb, line 33
def convert_subtitle_to_text(subtitle)
  subtitle_text = [
    build_counter(subtitle),
    build_time_range(subtitle),
    build_content(subtitle),
  ].join("\n")

  "#{subtitle_text}\n\n"
end