Class: ASSSubtitle
- Inherits:
-
Object
- Object
- ASSSubtitle
- Defined in:
- lib/vtt2ass/ASSSubtitle.rb
Overview
This class defines an ASS subtile line.
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#style ⇒ Object
readonly
Returns the value of attribute style.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#time_end ⇒ Object
readonly
Returns the value of attribute time_end.
-
#time_start ⇒ Object
readonly
Returns the value of attribute time_start.
Instance Method Summary collapse
-
#convertTime(time) ⇒ Object
This method validates the time format and sends the matching time to be converted.
-
#convertToAssText(text) ⇒ Object
This method replaces characters and tags to ASS compatible characters and tags.
-
#initialize(style, time_start, time_end, params, text) ⇒ ASSSubtitle
constructor
This method creates an instance of an ASSSubtitle.
-
#padTimeNum(sep, input, pad) ⇒ Object
The method pads text so that time numbers are a fixed number of digit.
-
#to_s ⇒ Object
This method assigns the object values and outputs an ASS dialogue line.
-
#toSubsTime(str) ⇒ Object
This method converts time from VTT format to the ASS format.
Constructor Details
#initialize(style, time_start, time_end, params, text) ⇒ ASSSubtitle
This method creates an instance of an ASSSubtitle.
-
Requires a
style
name as input. -
Requires
time_start
, a VTT formatted timestamp as input. -
Requires
time_start
, a VTT formatted timestamp as input. -
Requires
text
, a VTT formatted string as input.
13 14 15 16 17 18 19 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 13 def initialize(style, time_start, time_end, params, text) @style = style @time_start = convertTime(time_start) @time_end = convertTime(time_end) @params = params @text = convertToAssText(text) end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
4 5 6 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 4 def params @params end |
#style ⇒ Object (readonly)
Returns the value of attribute style.
4 5 6 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 4 def style @style end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
4 5 6 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 4 def text @text end |
#time_end ⇒ Object (readonly)
Returns the value of attribute time_end.
4 5 6 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 4 def time_end @time_end end |
#time_start ⇒ Object (readonly)
Returns the value of attribute time_start.
4 5 6 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 4 def time_start @time_start end |
Instance Method Details
#convertTime(time) ⇒ Object
This method validates the time format and sends the matching time to be converted
-
Requires
str
, a VTT formatted time string.
53 54 55 56 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 53 def convertTime(time) mTime = time.match(/([\d:]*)\.?(\d*)/) return toSubsTime(mTime[0]) end |
#convertToAssText(text) ⇒ Object
This method replaces characters and tags to ASS compatible characters and tags.
-
Requires
text
, a string of VTT formated text as input.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 31 def convertToAssText(text) text = text .gsub(/\r/, '') .gsub(/\n/, '\\N') .gsub(/\\n/, '\\N') .gsub(/\\N +/, '\\N') .gsub(/ +\\N/, '\\N') .gsub(/(\\N)+/, '\\N') .gsub(/<b[^>]*>([^<]*)<\/b>/) { |s| "{\\b1}#{$1}{\\b0}" } .gsub(/<i[^>]*>([^<]*)<\/i>/) { |s| "{\\i1}#{$1}{\\i0}" } .gsub(/<u[^>]*>([^<]*)<\/u>/) { |s| "{\\u1}#{$1}{\\u0}" } .gsub(/<c[^>]*>([^<]*)<\/c>/) { |s| $1 } .gsub(/<[^>]>/, '') .gsub(/\\N$/, '') .gsub(/ +$/, '') return text end |
#padTimeNum(sep, input, pad) ⇒ Object
The method pads text so that time numbers are a fixed number of digit.
-
Requires
sep
, a string separator. -
Requires
input
, an integer. -
Requires
pad
, an integer for the number of digits to be padded.
89 90 91 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 89 def padTimeNum(sep, input, pad) return sep + (input.to_s).rjust(pad, '0') end |
#to_s ⇒ Object
This method assigns the object values and outputs an ASS dialogue line.
23 24 25 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 23 def to_s return "Dialogue: 0,#{@time_start},#{@time_end},#{@style},,0,0,0,,#{@text}" end |
#toSubsTime(str) ⇒ Object
This method converts time from VTT format to the ASS format.
-
Requires
str
, a VTT formatted time string.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/vtt2ass/ASSSubtitle.rb', line 62 def toSubsTime(str) n = [] x = str.split(/[:.]/).map { |x| x.to_i } msLen = 2 hLen = 1 x[3] = '0.' + (x[3].to_s).rjust(3, '0') sx = x[0]*60*60 + x[1]*60 + x[2] + x[3].to_f sx = ("%.2f" % sx).split('.') n.unshift(padTimeNum('.', sx[1], msLen)) sx = sx[0].to_f n.unshift(padTimeNum(':', (sx % 60).to_i, 2)) n.unshift(padTimeNum(':', (sx / 60).floor % 60, 2)) n.unshift(padTimeNum('', (sx / 3600).floor % 60, hLen)) return n.join('') end |