Module: Titlekit::SRT
- Defined in:
- lib/titlekit/parsers/srt.rb
Defined Under Namespace
Classes: Subtitle, Subtitles, Timecode
Class Method Summary collapse
- .export(subtitles) ⇒ Object
-
.import(string) ⇒ Object
Parses the supplied string and returns the results.
-
.master(subtitles) ⇒ Object
Master the subtitles for best possible usage of the format’s features.
Class Method Details
.export(subtitles) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/titlekit/parsers/srt.rb', line 107 def self.export(subtitles) result = '' subtitles.each_with_index do |subtitle, index| result << (index+1).to_s result << "\n" result << SRT.build_timecode(subtitle[:start]) result << ' --> ' result << SRT.build_timecode(subtitle[:end]) result << "\n" result << subtitle[:lines] result << "\n\n" end return result end |
.import(string) ⇒ Object
Parses the supplied string and returns the results.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/titlekit/parsers/srt.rb', line 37 def self.import(string) Treetop.load(File.join(__dir__, 'srt')) parser = SRTParser.new syntax_tree = parser.parse(string) if syntax_tree return syntax_tree.build else failure = "failure_index #{parser.failure_index}\n" failure += "failure_line #{parser.failure_line}\n" failure += "failure_column #{parser.failure_column}\n" failure += "failure_reason #{parser.failure_reason}\n" raise failure end end |
.master(subtitles) ⇒ Object
Master the subtitles for best possible usage of the format’s features.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/titlekit/parsers/srt.rb', line 57 def self.master(subtitles) tracks = subtitles.map { |subtitle| subtitle[:track] }.uniq if tracks.length == 1 # maybe styling? aside that: nada más! elsif tracks.length >= 2 mastered_subtitles = [] # Determine timeframes with a discrete state cuts = subtitles.map { |s| [s[:start], s[:end]] }.flatten.uniq.sort frames = [] cuts.each_cons(2) do |pair| frames << { start: pair[0], end: pair[1] } end frames.each do |frame| intersecting = subtitles.select do |subtitle| (subtitle[:end] == frame[:end] || subtitle[:start] == frame[:start] || (subtitle[:start] < frame[:start] && subtitle[:end] > frame[:end])) end if intersecting.any? intersecting.sort_by! { |subtitle| tracks.index(subtitle[:track]) } subtitle = {} subtitle[:id] = mastered_subtitles.length+1 subtitle[:start] = frame[:start] subtitle[:end] = frame[:end] # Combine two or more than three simultaneous tracks by # stacking them directly, with different colors. colored_lines = intersecting.map do |subtitle| color = DEFAULT_PALETTE[tracks.index(subtitle[:track]) % DEFAULT_PALETTE.length] "<font color=\"##{color}\">#{subtitle[:lines]}</font>" end subtitle[:lines] = colored_lines.join("\n") mastered_subtitles << subtitle end end subtitles.replace(mastered_subtitles) end end |