class ASSLine

This class defines an ASS subtile line.

Attributes

style[R]
text[R]
time_end[R]
time_start[R]

Public Class Methods

new(style, time_start, time_end, text) click to toggle source

This method creates an instance of an ASSLine.

  • 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.

# File lib/vtt2ass/ASSLine.rb, line 15
def initialize(style, time_start, time_end, text)
    @style = style
    @time_start = convertTime(time_start)
    @time_end = convertTime(time_end)
    @text = convertToAssText(text)
end

Public Instance Methods

convertTime(time) click to toggle source

This method validates the time format and sends the matching time to be converted

  • Requires str, a VTT formatted time string.

# File lib/vtt2ass/ASSLine.rb, line 55
def convertTime(time)
    mTime = time.match(/([\d:]*)\.?(\d*)/)
    return toSubsTime(mTime[0])
end
convertToAssText(text) click to toggle source

This method replaces characters and tags to ASS compatible characters and tags.

  • Requires text, a string of VTT formated text as input.

# File lib/vtt2ass/ASSLine.rb, line 32
def convertToAssText(text)
    decoder = HTMLEntities.new()
    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 decoder.decode(text)
end
padTimeNum(sep, input, pad) click to toggle source

This 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.

# File lib/vtt2ass/ASSLine.rb, line 91
def padTimeNum(sep, input, pad)
    return sep + (input.to_s).rjust(pad, '0')
end
toSubsTime(str) click to toggle source

This method converts time from VTT format to the ASS format.

  • Requires str, a VTT formatted time string.

# File lib/vtt2ass/ASSLine.rb, line 64
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
to_s() click to toggle source

This method assigns the object values and outputs an ASS dialogue line.

# File lib/vtt2ass/ASSLine.rb, line 24
def to_s
    return "Dialogue: 0,#{@time_start},#{@time_end},#{@style},,0,0,0,,#{@text}"
end