|
| 1 | +sub init() |
| 2 | + m.top.observeField("url", "fetchCaption") |
| 3 | + m.top.currentCaption = [] |
| 4 | + m.top.currentPos = 0 |
| 5 | + |
| 6 | + m.captionTimer = m.top.findNode("captionTimer") |
| 7 | + m.captionTimer.ObserveField("fire", "updateCaption") |
| 8 | + |
| 9 | + m.captionList = [] |
| 10 | + m.reader = createObject("roUrlTransfer") |
| 11 | + m.font = CreateObject("roSGNode", "Font") |
| 12 | + m.tags = CreateObject("roRegex", "{\\an\d*}|<.*?>|<.*?>", "s") |
| 13 | + |
| 14 | + ' Caption Style |
| 15 | + m.fontSizeDict = { "Default": 60, "Large": 60, "Extra Large": 70, "Medium": 50, "Small": 40 } |
| 16 | + m.percentageDict = { "Default": 1.0, "100%": 1.0, "75%": 0.75, "50%": 0.5, "25%": 0.25, "Off": 0 } |
| 17 | + m.textColorDict = { "Default": &HFFFFFFFF, "White": &HFFFFFFFF, "Black": &H000000FF, "Red": &HFF0000FF, "Green": &H008000FF, "Blue": &H0000FFFF, "Yellow": &HFFFF00FF, "Magenta": &HFF00FFFF, "Cyan": &H00FFFFFF } |
| 18 | + m.bgColorDict = { "Default": &H000000FF, "White": &HFFFFFFFF, "Black": &H000000FF, "Red": &HFF0000FF, "Green": &H008000FF, "Blue": &H0000FFFF, "Yellow": &HFFFF00FF, "Magenta": &HFF00FFFF, "Cyan": &H00FFFFFF } |
| 19 | + |
| 20 | + m.settings = CreateObject("roDeviceInfo") |
| 21 | + m.fontSize = m.fontSizeDict[m.settings.GetCaptionsOption("Text/Size")] |
| 22 | + m.textColor = m.textColorDict[m.settings.GetCaptionsOption("Text/Color")] |
| 23 | + m.textOpac = m.percentageDict[m.settings.GetCaptionsOption("Text/Opacity")] |
| 24 | + m.bgColor = m.bgColorDict[m.settings.GetCaptionsOption("Background/Color")] |
| 25 | + m.bgOpac = m.percentageDict[m.settings.GetCaptionsOption("Background/Opacity")] |
| 26 | + setFont() |
| 27 | +end sub |
| 28 | + |
| 29 | +sub setFont() |
| 30 | + fs = CreateObject("roFileSystem") |
| 31 | + fontlist = fs.Find("tmp:/", "font") |
| 32 | + if fontlist.count() > 0 |
| 33 | + m.font.uri = "tmp:/" + fontlist[0] |
| 34 | + m.font.size = m.fontSize |
| 35 | + else |
| 36 | + reg = CreateObject("roFontRegistry") |
| 37 | + m.font = reg.GetDefaultFont(m.fontSize, false, false) |
| 38 | + end if |
| 39 | +end sub |
| 40 | + |
| 41 | +sub fetchCaption() |
| 42 | + m.captionTimer.control = "stop" |
| 43 | + re = CreateObject("roRegex", "(http.*?\.vtt)", "s") |
| 44 | + url = re.match(m.top.url)[0] |
| 45 | + if url <> invalid |
| 46 | + m.reader.setUrl(url) |
| 47 | + text = m.reader.GetToString() |
| 48 | + m.captionList = parseVTT(text) |
| 49 | + m.captionTimer.control = "start" |
| 50 | + else |
| 51 | + m.captionTimer.control = "stop" |
| 52 | + end if |
| 53 | +end sub |
| 54 | + |
| 55 | +function newlabel(txt) |
| 56 | + label = CreateObject("roSGNode", "Label") |
| 57 | + label.text = txt |
| 58 | + label.font = m.font |
| 59 | + label.color = m.textColor |
| 60 | + label.opacity = m.textOpac |
| 61 | + return label |
| 62 | +end function |
| 63 | + |
| 64 | +function newLayoutGroup(labels) |
| 65 | + newlg = CreateObject("roSGNode", "LayoutGroup") |
| 66 | + newlg.appendchildren(labels) |
| 67 | + newlg.horizalignment = "center" |
| 68 | + newlg.vertalignment = "bottom" |
| 69 | + return newlg |
| 70 | +end function |
| 71 | + |
| 72 | +function newRect(lg) |
| 73 | + rectLG = CreateObject("roSGNode", "LayoutGroup") |
| 74 | + rectxy = lg.BoundingRect() |
| 75 | + rect = CreateObject("roSGNode", "Rectangle") |
| 76 | + rect.color = m.bgColor |
| 77 | + rect.opacity = m.bgOpac |
| 78 | + rect.width = rectxy.width + 50 |
| 79 | + rect.height = rectxy.height |
| 80 | + if lg.getchildCount() = 0 |
| 81 | + rect.width = 0 |
| 82 | + rect.height = 0 |
| 83 | + end if |
| 84 | + rectLG.translation = [0, -rect.height / 2] |
| 85 | + rectLG.horizalignment = "center" |
| 86 | + rectLG.vertalignment = "center" |
| 87 | + rectLG.appendchild(rect) |
| 88 | + return rectLG |
| 89 | +end function |
| 90 | + |
| 91 | + |
| 92 | +sub updateCaption () |
| 93 | + m.top.currentCaption = [] |
| 94 | + if LCase(m.top.playerState) = "playingon" |
| 95 | + m.top.currentPos = m.top.currentPos + 100 |
| 96 | + texts = [] |
| 97 | + for each entry in m.captionList |
| 98 | + if entry["start"] <= m.top.currentPos and m.top.currentPos < entry["end"] |
| 99 | + t = m.tags.replaceAll(entry["text"], "") |
| 100 | + texts.push(t) |
| 101 | + end if |
| 102 | + end for |
| 103 | + labels = [] |
| 104 | + for each text in texts |
| 105 | + labels.push(newlabel (text)) |
| 106 | + end for |
| 107 | + lines = newLayoutGroup(labels) |
| 108 | + rect = newRect(lines) |
| 109 | + m.top.currentCaption = [rect, lines] |
| 110 | + else if LCase(m.top.playerState.right(1)) = "w" |
| 111 | + m.top.playerState = m.top.playerState.left(len (m.top.playerState) - 1) |
| 112 | + end if |
| 113 | +end sub |
| 114 | + |
| 115 | +function isTime(text) |
| 116 | + return text.right(1) = chr(31) |
| 117 | +end function |
| 118 | + |
| 119 | +function toMs(t) |
| 120 | + t = t.replace(".", ":") |
| 121 | + t = t.left(12) |
| 122 | + timestamp = t.tokenize(":") |
| 123 | + return 3600000 * timestamp[0].toint() + 60000 * timestamp[1].toint() + 1000 * timestamp[2].toint() + timestamp[3].toint() |
| 124 | +end function |
| 125 | + |
| 126 | +function parseVTT(lines) |
| 127 | + lines = lines.replace(" --> ", chr(31) + chr(10)) |
| 128 | + lines = lines.split(chr(10)) |
| 129 | + curStart = -1 |
| 130 | + curEnd = -1 |
| 131 | + entries = [] |
| 132 | + |
| 133 | + for i = 0 to lines.count() - 1 |
| 134 | + if isTime(lines[i]) |
| 135 | + curStart = toMs (lines[i]) |
| 136 | + curEnd = toMs (lines[i + 1]) |
| 137 | + i += 1 |
| 138 | + else if curStart <> -1 |
| 139 | + trimmed = lines[i].trim() |
| 140 | + if trimmed <> chr(0) |
| 141 | + entry = { "start": curStart, "end": curEnd, "text": trimmed } |
| 142 | + entries.push(entry) |
| 143 | + end if |
| 144 | + end if |
| 145 | + end for |
| 146 | + return entries |
| 147 | +end function |
0 commit comments