Class: Appydave::Tools::SubtitleMaster::Join::SRTParser

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/subtitle_manager/join.rb

Overview

Parses SRT files into structured subtitle objects

Defined Under Namespace

Classes: Subtitle

Instance Method Summary collapse

Instance Method Details

#parse(content) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/appydave/tools/subtitle_manager/join.rb', line 82

def parse(content)
  validate_content!(content)

  subtitles = []
  current_block = { text: [] }

  content.split("\n").each do |line|
    line = line.strip

    if line.empty?
      process_block(current_block, subtitles) if current_block[:index]
      current_block = { text: [] }
      next
    end

    if current_block[:index].nil?
      current_block[:index] = line.to_i
    elsif current_block[:timestamp].nil? && line.include?(' --> ')
      start_time, end_time = line.split(' --> ')
      current_block[:timestamp] = { start: start_time, end: end_time }
    else
      current_block[:text] << line
    end
  end

  # Process the last block if it exists
  process_block(current_block, subtitles) if current_block[:index]

  subtitles
end