Class: SRT::File
- Inherits:
-
Object
- Object
- SRT::File
- Defined in:
- lib/srt/file.rb
Instance Attribute Summary collapse
Class Method Summary collapse
- .parse(input, options = {}) ⇒ Object
- .parse_file(srt_file) ⇒ Object
- .parse_string(srt_data) ⇒ Object
-
.split_srt_data(srt_data) ⇒ Object
Ruby often gets the wrong encoding for a file and will throw errors on ‘split` for invalid byte sequences.
Instance Method Summary collapse
- #append(options) ⇒ Object
- #errors ⇒ Object
- #split(options) ⇒ Object
- #timeshift(options) ⇒ Object
- #to_s(time_str_function = :time_str) ⇒ Object
- #to_webvtt ⇒ Object
Instance Attribute Details
#lines ⇒ Object
227 228 229 |
# File 'lib/srt/file.rb', line 227 def lines @lines ||= [] end |
Class Method Details
.parse(input, options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/srt/file.rb', line 3 def self.parse(input, = {}) @debug = .fetch(:debug, false) if input.is_a?(String) parse_string(input) elsif input.is_a?(::File) parse_file(input) else raise "Invalid input. Expected a String or File, got #{input.class.name}." end end |
.parse_file(srt_file) ⇒ Object
14 15 16 |
# File 'lib/srt/file.rb', line 14 def self.parse_file(srt_file) parse_string ::File.open(srt_file, 'rb') { |f| srt_file.read } end |
.parse_string(srt_data) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/srt/file.rb', line 18 def self.parse_string(srt_data) result = new line = Line.new split_srt_data(srt_data).each_with_index do |str, index| begin if str.strip.empty? result.lines << line unless line.empty? line = Line.new elsif !line.error if line.sequence.nil? line.sequence = str.to_i elsif line.start_time.nil? if mres = str.match(/(?<start_timecode>[^[[:space:]]]+) -+> (?<end_timecode>[^[[:space:]]]+) ?(?<display_coordinates>X1:\d+ X2:\d+ Y1:\d+ Y2:\d+)?/) if (line.start_time = Parser.timecode(mres["start_timecode"])) == nil line.error = "#{index}, Invalid formatting of start timecode, [#{mres["start_timecode"]}]" $stderr.puts line.error if @debug end if (line.end_time = Parser.timecode(mres["end_timecode"])) == nil line.error = "#{index}, Invalid formatting of end timecode, [#{mres["end_timecode"]}]" $stderr.puts line.error if @debug end if mres["display_coordinates"] line.display_coordinates = mres["display_coordinates"] end else line.error = "#{index}, Invalid Time Line formatting, [#{str}]" $stderr.puts line.error if @debug end else line.text << str.strip end end rescue line.error = "#{index}, General Error, [#{str}]" $stderr.puts line.error if @debug end end result end |
.split_srt_data(srt_data) ⇒ Object
Ruby often gets the wrong encoding for a file and will throw errors on ‘split` for invalid byte sequences. This chain of fallback encodings lets us get something that works.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/srt/file.rb', line 65 def self.split_srt_data(srt_data) begin srt_data.split(/\n/) + ["\n"] rescue begin srt_data = srt_data.unpack("C*").pack("U*") srt_data.force_encoding('utf-8').split(/\n/) + ["\n"] rescue srt_data.force_encoding('iso-8859-1').split(/\n/) + ["\n"] end end end |
Instance Method Details
#append(options) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/srt/file.rb', line 78 def append() if .length == 1 && .values[0].class == self.class reshift = Parser.timecode(.keys[0]) || (lines.last.end_time + Parser.timespan(.keys[0])) renumber = lines.last.sequence .values[0].lines.each do |line| lines << line.clone lines.last.sequence += renumber lines.last.start_time += reshift lines.last.end_time += reshift end end self end |
#errors ⇒ Object
231 232 233 |
# File 'lib/srt/file.rb', line 231 def errors lines.collect { |l| l.error if l.error }.compact end |
#split(options) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/srt/file.rb', line 94 def split() = { :timeshift => true, :renumber => true }.merge() split_points = [] if ([:at]) split_points = [[:at]].flatten.map{ |timecode| Parser.timecode(timecode) }.sort elsif ([:every]) interval = Parser.timecode([:every]) max = lines.last.end_time (interval..max).step(interval){ |t| split_points << t } end if (split_points.count > 0) split_offsprings = [File.new] reshift = 0 renumber = 0 lines.each do |line| if split_points.empty? || line.end_time <= split_points.first cloned_line = line.clone cloned_line.sequence -= renumber if [:renumber] if [:timeshift] cloned_line.start_time -= reshift cloned_line.end_time -= reshift end split_offsprings.last.lines << cloned_line elsif line.start_time < split_points.first cloned_line = line.clone cloned_line.sequence -= renumber if [:renumber] if [:timeshift] cloned_line.start_time -= reshift cloned_line.end_time = split_points.first - reshift end split_offsprings.last.lines << cloned_line renumber = line.sequence - 1 reshift = split_points.first split_points.delete_at(0) split_offsprings << File.new cloned_line = line.clone cloned_line.sequence -= renumber if [:renumber] if [:timeshift] cloned_line.start_time = 0 cloned_line.end_time -= reshift end split_offsprings.last.lines << cloned_line else renumber = line.sequence - 1 reshift = split_points.first split_points.delete_at(0) split_offsprings << File.new cloned_line = line.clone cloned_line.sequence -= renumber if [:renumber] if [:timeshift] cloned_line.start_time -= reshift cloned_line.end_time -= reshift end split_offsprings.last.lines << cloned_line end end end split_offsprings end |
#timeshift(options) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/srt/file.rb', line 163 def timeshift() if .length == 1 if [:all] && (seconds = Parser.timespan([:all])) lines.each do |line| line.start_time += seconds line.end_time += seconds end elsif (original_framerate = Parser.framerate(.keys[0])) && (target_framerate = Parser.framerate(.values[0])) time_ratio = original_framerate / target_framerate lines.each do |line| line.start_time *= time_ratio line.end_time *= time_ratio end end elsif .length == 2 origins, targets = .keys, .values [0,1].each do |i| if origins[i].is_a?(String) && Parser.id(origins[i]) origins[i] = lines[Parser.id(origins[i]) - 1].start_time elsif origins[i].is_a?(String) && Parser.timecode(origins[i]) origins[i] = Parser.timecode(origins[i]) end if targets[i].is_a?(String) && Parser.timecode(targets[i]) targets[i] = Parser.timecode(targets[i]) elsif targets[i].is_a?(String) && Parser.timespan(targets[i]) targets[i] = origins[i] + Parser.timespan(targets[i]) end end time_rescale_factor = (targets[1] - targets[0]) / (origins[1] - origins[0]) time_rebase_shift = targets[0] - origins[0] * time_rescale_factor lines.each do |line| line.start_time = line.start_time * time_rescale_factor + time_rebase_shift line.end_time = line.end_time * time_rescale_factor + time_rebase_shift end end if lines.reject! { |line| line.end_time < 0 } lines.sort_by! { |line| line.sequence } lines.each_with_index do |line, index| line.sequence = index + 1 line.start_time = 0 if line.start_time < 0 end end end |
#to_s(time_str_function = :time_str) ⇒ Object
212 213 214 |
# File 'lib/srt/file.rb', line 212 def to_s(time_str_function=:time_str) lines.map { |l| l.to_s(time_str_function) }.join("\n") end |
#to_webvtt ⇒ Object
216 217 218 219 220 221 222 223 |
# File 'lib/srt/file.rb', line 216 def to_webvtt header = <<eos WEBVTT X-TIMESTAMP-MAP=MPEGTS:900000,LOCAL:00:00:00.000 eos header + to_s(:webvtt_time_str) end |