Module: Titlekit::SSA
- Defined in:
- lib/titlekit/parsers/ssa.rb
Defined Under Namespace
Classes: Events, ScriptInfo, Subtitles, V4PStyles
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
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 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/titlekit/parsers/ssa.rb', line 144 def self.export(subtitles) result = '' result << "[Script Info]\nScriptType: v4.00\n\n" result << "[V4 Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\n" result << "Style: Default,Arial,16,16777215,16777215,16777215,-2147483640,0,0,1,3,0,2,70,70,40,0,0\n" result << "Style: Middle,Arial,16,16777215,16777215,16777215,-2147483640,0,0,1,3,0,10,70,70,40,0,0\n" result << "Style: Top,Arial,16,16777215,16777215,16777215,-2147483640,0,0,1,3,0,6,70,70,40,0,0\n" DEFAULT_PALETTE.each do |color| # reordered_color = "" # reordered_color << color[4..5] # reordered_color << color[2..3] # reordered_color << color[0..1] processed_color = (color[4..5]+color[2..3]+color[0..1]).to_i(16) result << "Style: #{color},Arial,16,#{processed_color},#{processed_color},#{processed_color},-2147483640,0,0,1,3,0,2,70,70,40,0,0\n" end result << "\n" # Close styles section result << "[Events]\nFormat: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n" subtitles.each do |subtitle| fields = [ 'Dialogue: 0', # Format: Marked SSA.build_timecode(subtitle[:start]), # Start SSA.build_timecode(subtitle[:end]), # End subtitle[:style] || 'Default', # Style '', # Name '0000', # MarginL '0000', # MarginR '0000', # MarginV '',# Effect subtitle[:lines].gsub("\n", '\N') # Text ] result << (fields.join(',') + "\n") end return result end |
.import(string) ⇒ Object
Parses the supplied string and returns the results.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/titlekit/parsers/ssa.rb', line 66 def self.import(string) Treetop.load(File.join(__dir__, 'ssa')) parser = SSAParser.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.
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 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 |
# File 'lib/titlekit/parsers/ssa.rb', line 86 def self.master(subtitles) tracks = subtitles.map { |subtitle| subtitle[:track] }.uniq if tracks.length == 1 # maybe styling? aside that: nada más! elsif (2..3).include?(tracks.length) subtitles.each do |subtitle| case tracks.index(subtitle[:track]) when 0 subtitle[:style] = 'Default' when 1 subtitle[:style] = 'Top' when 2 subtitle[:style] = 'Middle' end end elsif tracks.length >= 4 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]) } intersecting.each do |subtitle| new_subtitle = {} new_subtitle[:id] = mastered_subtitles.length+1 new_subtitle[:start] = frame[:start] new_subtitle[:end] = frame[:end] color = DEFAULT_PALETTE[tracks.index(subtitle[:track]) % DEFAULT_PALETTE.length] new_subtitle[:style] = color new_subtitle[:lines] = subtitle[:lines] mastered_subtitles << new_subtitle end end end subtitles.replace(mastered_subtitles) end end |