Class: Appydave::Tools::SubtitleProcessor::Join::SRTMerger

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

Overview

Merges multiple subtitle arrays while maintaining timing and adding buffers

Instance Method Summary collapse

Constructor Details

#initialize(buffer_ms: 100) ⇒ SRTMerger

Returns a new instance of SRTMerger.



139
140
141
# File 'lib/appydave/tools/subtitle_processor/join.rb', line 139

def initialize(buffer_ms: 100)
  @buffer_ms = buffer_ms.to_f
end

Instance Method Details

#merge(subtitle_arrays) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/appydave/tools/subtitle_processor/join.rb', line 143

def merge(subtitle_arrays)
  return [] if subtitle_arrays.empty?

  merged = []
  current_end_time = 0.0

  subtitle_arrays.each do |subtitles|
    next if subtitles.empty?

    # Calculate offset needed for this batch of subtitles
    first_subtitle = subtitles.first
    offset_seconds = calculate_offset(current_end_time, first_subtitle.start_time)

    # Add adjusted subtitles to merged array
    subtitles.each do |subtitle|
      adjusted_subtitle = adjust_subtitle_timing(subtitle, offset_seconds)
      merged << adjusted_subtitle
    end

    # Update current_end_time for next batch
    current_end_time = merged.last.end_time
  end

  # Renumber subtitles sequentially
  merged.each_with_index do |subtitle, index|
    subtitle.instance_variable_set(:@index, index + 1)
  end

  merged
end