Sha256: 00099ab9df4b734f47d649e031c4c188c3bb2af853a5c9c285723152e6f50c8a

Contents?: true

Size: 1.77 KB

Versions: 6

Compression:

Stored size: 1.77 KB

Contents

module Subber::Formatter
  class Srt < Base
    LF_REGEX = /\n/
    CRLF = "\r\n"

    class << self
      # @param subtitles [Array<Subber::Subtitle>]
      # @return [String]
      #
      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

      # @param file_content [String]
      # @return [String]
      #
      def add_window_line_break(file_content)
        file_content.gsub(LF_REGEX, CRLF)
      end

      # @param subtitle [Subber::Subtitle]
      # @return [String]
      #
      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

      # @param subtitle [Subber::Subtitle]
      # @return [String]
      #
      def build_counter(subtitle)
        subtitle.counter.to_s
      end

      # @param subtitle [Subber::Subtitle]
      # @return [String]
      #
      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

      # @param subtitle [Subber::Subtitle]
      # @return [String]
      #
      def build_content(subtitle)
        subtitle.content
      end

      # @param ms_time [Integer] Time in milliseconds
      # @return [String] Formatted time
      #
      def convert_ms_to_time(ms_time)
        seconds = ms_time / 1000.0
        Time.at(seconds).utc.strftime("%H:%M:%S,%L")
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
subber-0.1.9 lib/subber/formatter/srt.rb
subber-0.1.8 lib/subber/formatter/srt.rb
subber-0.1.4 lib/subber/formatter/srt.rb
subber-0.1.3 lib/subber/formatter/srt.rb
subber-0.1.2 lib/subber/formatter/srt.rb
subber-0.1.1 lib/subber/formatter/srt.rb