2023-01-27 00:03:09 +00:00
|
|
|
sub init()
|
|
|
|
m.top.observeField("url", "fetchCaption")
|
2023-02-02 03:16:25 +00:00
|
|
|
m.top.currentCaption = []
|
2023-01-27 00:03:09 +00:00
|
|
|
m.top.currentPos = 0
|
|
|
|
|
|
|
|
m.captionTimer = m.top.findNode("captionTimer")
|
|
|
|
m.captionTimer.ObserveField("fire", "updateCaption")
|
|
|
|
|
|
|
|
m.captionList = []
|
|
|
|
m.reader = createObject("roUrlTransfer")
|
|
|
|
m.font = CreateObject("roSGNode", "Font")
|
2023-02-04 16:41:00 +00:00
|
|
|
m.tags = CreateObject("roRegex", "{\\an\d*}|<.*?>|<.*?>", "s")
|
2023-02-05 08:07:01 +00:00
|
|
|
|
2023-02-07 23:37:03 +00:00
|
|
|
' Caption Style
|
|
|
|
m.fontSizeDict = { "Default": 60, "Large": 60, "Extra Large": 70, "Medium": 50, "Small": 40 }
|
2023-02-10 17:18:17 +00:00
|
|
|
m.percentageDict = { "Default": 1.0, "100%": 1.0, "75%": 0.75, "50%": 0.5, "25%": 0.25, "Off": 0 }
|
2023-02-07 23:37:03 +00:00
|
|
|
m.textColorDict = { "Default": &HFFFFFFFF, "White": &HFFFFFFFF, "Black": &H000000FF, "Red": &HFF0000FF, "Green": &H008000FF, "Blue": &H0000FFFF, "Yellow": &HFFFF00FF, "Magenta": &HFF00FFFF, "Cyan": &H00FFFFFF }
|
2023-02-10 17:18:17 +00:00
|
|
|
m.bgColorDict = { "Default": &H000000FF, "White": &HFFFFFFFF, "Black": &H000000FF, "Red": &HFF0000FF, "Green": &H008000FF, "Blue": &H0000FFFF, "Yellow": &HFFFF00FF, "Magenta": &HFF00FFFF, "Cyan": &H00FFFFFF }
|
2023-02-07 23:37:03 +00:00
|
|
|
|
|
|
|
m.settings = CreateObject("roDeviceInfo")
|
|
|
|
m.fontSize = m.fontSizeDict[m.settings.GetCaptionsOption("Text/Size")]
|
|
|
|
m.textColor = m.textColorDict[m.settings.GetCaptionsOption("Text/Color")]
|
|
|
|
m.textOpac = m.percentageDict[m.settings.GetCaptionsOption("Text/Opacity")]
|
2023-02-10 17:18:17 +00:00
|
|
|
m.bgColor = m.bgColorDict[m.settings.GetCaptionsOption("Background/Color")]
|
|
|
|
m.bgOpac = m.percentageDict[m.settings.GetCaptionsOption("Background/Opacity")]
|
2023-02-04 16:41:00 +00:00
|
|
|
setFont()
|
2023-01-31 23:26:20 +00:00
|
|
|
end sub
|
|
|
|
|
2023-02-04 16:41:00 +00:00
|
|
|
sub setFont()
|
2023-01-31 23:26:20 +00:00
|
|
|
fs = CreateObject("roFileSystem")
|
2023-03-26 00:18:39 +00:00
|
|
|
|
|
|
|
if fs.Exists("tmp:/font")
|
|
|
|
m.font.uri = "tmp:/font"
|
2023-02-07 23:37:03 +00:00
|
|
|
m.font.size = m.fontSize
|
2023-02-19 18:30:48 +00:00
|
|
|
else
|
2023-03-26 01:52:12 +00:00
|
|
|
m.font = "font:LargeSystemFont"
|
2023-01-31 23:26:20 +00:00
|
|
|
end if
|
2023-01-27 00:03:09 +00:00
|
|
|
end sub
|
|
|
|
|
|
|
|
sub fetchCaption()
|
2023-02-19 18:30:48 +00:00
|
|
|
m.captionTimer.control = "stop"
|
|
|
|
re = CreateObject("roRegex", "(http.*?\.vtt)", "s")
|
|
|
|
url = re.match(m.top.url)[0]
|
|
|
|
if url <> invalid
|
|
|
|
m.reader.setUrl(url)
|
|
|
|
text = m.reader.GetToString()
|
|
|
|
m.captionList = parseVTT(text)
|
|
|
|
m.captionTimer.control = "start"
|
|
|
|
else
|
2023-01-27 00:03:09 +00:00
|
|
|
m.captionTimer.control = "stop"
|
|
|
|
end if
|
|
|
|
end sub
|
|
|
|
|
2023-02-19 18:30:48 +00:00
|
|
|
function newlabel(txt)
|
2023-01-31 23:26:20 +00:00
|
|
|
label = CreateObject("roSGNode", "Label")
|
|
|
|
label.text = txt
|
|
|
|
label.font = m.font
|
2023-03-26 01:52:12 +00:00
|
|
|
label.font.size = m.fontSize
|
2023-02-10 17:18:17 +00:00
|
|
|
label.color = m.textColor
|
|
|
|
label.opacity = m.textOpac
|
2023-01-31 23:26:20 +00:00
|
|
|
return label
|
|
|
|
end function
|
|
|
|
|
2023-02-10 17:18:17 +00:00
|
|
|
function newLayoutGroup(labels)
|
2023-01-31 23:26:20 +00:00
|
|
|
newlg = CreateObject("roSGNode", "LayoutGroup")
|
|
|
|
newlg.appendchildren(labels)
|
|
|
|
newlg.horizalignment = "center"
|
|
|
|
newlg.vertalignment = "bottom"
|
|
|
|
return newlg
|
|
|
|
end function
|
|
|
|
|
2023-02-10 17:18:17 +00:00
|
|
|
function newRect(lg)
|
|
|
|
rectLG = CreateObject("roSGNode", "LayoutGroup")
|
|
|
|
rectxy = lg.BoundingRect()
|
|
|
|
rect = CreateObject("roSGNode", "Rectangle")
|
|
|
|
rect.color = m.bgColor
|
|
|
|
rect.opacity = m.bgOpac
|
|
|
|
rect.width = rectxy.width + 50
|
|
|
|
rect.height = rectxy.height
|
|
|
|
if lg.getchildCount() = 0
|
|
|
|
rect.width = 0
|
|
|
|
rect.height = 0
|
|
|
|
end if
|
2023-02-19 18:30:48 +00:00
|
|
|
rectLG.translation = [0, -rect.height / 2]
|
2023-02-10 17:18:17 +00:00
|
|
|
rectLG.horizalignment = "center"
|
2023-02-19 18:30:48 +00:00
|
|
|
rectLG.vertalignment = "center"
|
2023-02-10 17:18:17 +00:00
|
|
|
rectLG.appendchild(rect)
|
|
|
|
return rectLG
|
|
|
|
end function
|
|
|
|
|
2023-01-31 23:26:20 +00:00
|
|
|
|
2023-03-26 00:18:39 +00:00
|
|
|
sub updateCaption()
|
2023-02-02 03:16:25 +00:00
|
|
|
m.top.currentCaption = []
|
2023-02-19 18:30:48 +00:00
|
|
|
if LCase(m.top.playerState) = "playingon"
|
2023-01-27 00:03:09 +00:00
|
|
|
m.top.currentPos = m.top.currentPos + 100
|
2023-01-31 23:26:20 +00:00
|
|
|
texts = []
|
2023-01-27 00:03:09 +00:00
|
|
|
for each entry in m.captionList
|
2023-02-04 16:41:00 +00:00
|
|
|
if entry["start"] <= m.top.currentPos and m.top.currentPos < entry["end"]
|
|
|
|
t = m.tags.replaceAll(entry["text"], "")
|
|
|
|
texts.push(t)
|
2023-01-27 00:03:09 +00:00
|
|
|
end if
|
|
|
|
end for
|
2023-01-31 23:26:20 +00:00
|
|
|
labels = []
|
|
|
|
for each text in texts
|
|
|
|
labels.push(newlabel (text))
|
|
|
|
end for
|
2023-02-10 17:18:17 +00:00
|
|
|
lines = newLayoutGroup(labels)
|
|
|
|
rect = newRect(lines)
|
|
|
|
m.top.currentCaption = [rect, lines]
|
2023-02-19 18:30:48 +00:00
|
|
|
else if LCase(m.top.playerState.right(1)) = "w"
|
|
|
|
m.top.playerState = m.top.playerState.left(len (m.top.playerState) - 1)
|
2023-01-27 00:03:09 +00:00
|
|
|
end if
|
|
|
|
end sub
|
|
|
|
|
2023-02-05 08:07:01 +00:00
|
|
|
function isTime(text)
|
|
|
|
return text.right(1) = chr(31)
|
2023-01-27 00:03:09 +00:00
|
|
|
end function
|
|
|
|
|
2023-02-05 08:07:01 +00:00
|
|
|
function toMs(t)
|
|
|
|
t = t.replace(".", ":")
|
|
|
|
t = t.left(12)
|
|
|
|
timestamp = t.tokenize(":")
|
|
|
|
return 3600000 * timestamp[0].toint() + 60000 * timestamp[1].toint() + 1000 * timestamp[2].toint() + timestamp[3].toint()
|
2023-02-04 16:41:00 +00:00
|
|
|
end function
|
|
|
|
|
2023-02-05 08:07:01 +00:00
|
|
|
function parseVTT(lines)
|
|
|
|
lines = lines.replace(" --> ", chr(31) + chr(10))
|
|
|
|
lines = lines.split(chr(10))
|
|
|
|
curStart = -1
|
|
|
|
curEnd = -1
|
|
|
|
entries = []
|
2023-01-27 00:03:09 +00:00
|
|
|
|
2023-02-05 08:07:01 +00:00
|
|
|
for i = 0 to lines.count() - 1
|
2023-02-04 16:41:00 +00:00
|
|
|
if isTime(lines[i])
|
2023-02-05 08:07:01 +00:00
|
|
|
curStart = toMs (lines[i])
|
|
|
|
curEnd = toMs (lines[i + 1])
|
|
|
|
i += 1
|
|
|
|
else if curStart <> -1
|
|
|
|
trimmed = lines[i].trim()
|
|
|
|
if trimmed <> chr(0)
|
|
|
|
entry = { "start": curStart, "end": curEnd, "text": trimmed }
|
|
|
|
entries.push(entry)
|
|
|
|
end if
|
2023-02-04 16:41:00 +00:00
|
|
|
end if
|
2023-01-27 00:03:09 +00:00
|
|
|
end for
|
2023-02-05 08:07:01 +00:00
|
|
|
return entries
|
2023-01-30 00:52:10 +00:00
|
|
|
end function
|