Sha256: 0e50e893afdba529dbfd81756cf0e72d7ae66da7d32117bb19525f85f1a2674c

Contents?: true

Size: 895 Bytes

Versions: 2

Compression:

Stored size: 895 Bytes

Contents

module SRT
  class Parser
    class << self
      def framerate(framerate_string)
        mres = framerate_string.match(/(?<fps>\d+((\.)?\d+))(fps)/)
        mres ? mres["fps"].to_f : nil
      end

      def id(id_string)
        mres = id_string.match(/#(?<id>\d+)/)
          mres ? mres["id"].to_i : nil
      end

      def timecode(timecode_string)
        mres = timecode_string.match(/(?<h>\d+):(?<m>\d+):(?<s>\d+)[,.]?(?<ms>\d+)?/)
        mres ? "#{mres["h"].to_i * 3600 + mres["m"].to_i * 60 + mres["s"].to_i}.#{mres["ms"]}".to_f : nil
      end

      def timespan(timespan_string)
        factors = {
          "ms" => 0.001,
          "s" => 1,
          "m" => 60,
          "h" => 3600
        }
        mres = timespan_string.match(/(?<amount>(\+|-)?\d+((\.)?\d+)?)(?<unit>ms|s|m|h)/)
        mres ? mres["amount"].to_f * factors[mres["unit"]] : nil
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
srt-0.1.5 lib/srt/parser.rb
srt-0.1.4 lib/srt/parser.rb