Module: Titlekit::ASS

Defined in:
lib/titlekit/parsers/ass.rb

Defined Under Namespace

Classes: Events, ScriptInfo, Subtitles, V4PStyles

Class Method Summary collapse

Class Method Details

.export(subtitles) ⇒ Object



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
# File 'lib/titlekit/parsers/ass.rb', line 122

def self.export(subtitles)
  result = ''

  result << "[Script Info]\nScriptType: v4.00+\n\n"
  result << "[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n"
  result << "Style: Default,Arial,16,&H00FFFFFF,&H00FFFFFF,&H40000000,&H40000000,0,0,0,0,100,100,0,0.00,1,3,0,2,20,20,20,1\n"
  result << "Style: Top,Arial,16,&H00FFFFFF,&H00FFFFFF,&H40000000,&H40000000,0,0,0,0,100,100,0,0.00,1,3,0,8,20,20,20,1\n"
  result << "Style: Middle,Arial,16,&H00FFFFFF,&H00FFFFFF,&H40000000,&H40000000,0,0,0,0,100,100,0,0.00,1,3,0,5,20,20,20,1\n"

  DEFAULT_PALETTE.each do |color|
    processed_color = '&H00' + (color[4..5] + color[2..3] + color[0..1])
    result << "Style: #{color},Arial,16,#{processed_color},#{processed_color},&H40000000,&H40000000,0,0,0,0,100,100,0,0.00,1,3,0,2,20,20,20,1\n"
  end

  result << "\n" # Close styles section
  
  result << "[Events]\nFormat: Layer, Start, End, Style, Actor, 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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/titlekit/parsers/ass.rb', line 44

def self.import(string)
  Treetop.load(File.join(__dir__, 'ass'))
  parser = ASSParser.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.

Parameters:

  • subtitles (Array<Hash>)

    the subtitles to master



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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/titlekit/parsers/ass.rb', line 64

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 || tracks.length == 3

    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