Optimize parseVTT

Change back to gettofile

Fix default font in captionTask

Update captionTask.brs
This commit is contained in:
Jinho Kim 2023-02-05 03:07:01 -05:00
parent 305ac54d8d
commit 6e5e14444e
5 changed files with 51 additions and 49 deletions

View File

@ -35,14 +35,16 @@ sub init()
m.captionGroup.createchildren(9, "LayoutGroup")
m.captionTask = createObject("roSGNode", "captionTask")
m.captionTask.observeField("currentCaption", "updateCaption")
m.captionTask.observeField("useThis", "checkCaptionMode")
m.top.observeField("currentSubtitleTrack", "loadCaption")
m.top.observeField("globalCaptionMode", "toggleCaption")
m.top.suppressCaptions = True
toggleCaption()
end sub
sub loadCaption()
m.top.suppressCaptions = m.captionTask.useThis
m.captionTask.url = m.top.currentSubtitleTrack
end sub
@ -124,7 +126,7 @@ end sub
' When Video Player state changes
sub onPositionChanged()
m.captionTask.currentPos = Cint(m.top.position * 1000)
m.captionTask.currentPos = Int(m.top.position * 1000)
m.dialog = m.top.getScene().findNode("dialogBackground")
if not isValid(m.dialog)
checkTimeToDisplayNextEpisode()
@ -134,7 +136,6 @@ end sub
'
' When Video Player state changes
sub onState(msg)
m.captionTask.playerState = m.top.state + m.top.globalCaptionMode
' When buffering, start timer to monitor buffering process
if m.top.state = "buffering" and m.bufferCheckTimer <> invalid

View File

@ -23,6 +23,7 @@
<field id="mediaSourceId" type="string" />
<field id="audioIndex" type="integer" />
<field id="runTime" type="integer" />
</interface>
<script type="text/brightscript" uri="JFVideo.brs" />
<script type="text/brightscript" uri="pkg:/source/utils/misc.brs" />

View File

@ -10,32 +10,34 @@ sub init()
m.reader = createObject("roUrlTransfer")
m.font = CreateObject("roSGNode", "Font")
m.tags = CreateObject("roRegex", "{\\an\d*}|&lt;.*?&gt;|<.*?>", "s")
setFont()
end sub
sub setFont()
fs = CreateObject("roFileSystem")
fontlist = fs.Find("tmp:/", "font\.(otf|ttf)")
fontlist = fs.Find("tmp:/", "font")
if fontlist.count() > 0
m.font.uri = "tmp:/" + fontlist[0]
else
m.font = "font:LargeBoldSystemFont"
m.font.size = 60
m.top.useThis = True
end if
m.font.size = 60
end sub
sub fetchCaption()
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
if m.top.useThis
m.captionTimer.control = "stop"
re = CreateObject("roRegex", "(http.*?\.vtt)", "s")
url = re.match(m.top.url)[0]
?url
if url <> invalid
m.reader.setUrl(url)
text = m.reader.GetToString()
m.captionList = parseVTT(text)
m.captionTimer.control = "start"
else
m.captionTimer.control = "stop"
end if
end if
end sub
@ -71,6 +73,8 @@ sub updateCaption ()
texts = []
for each entry in m.captionList
if entry["start"] <= m.top.currentPos and m.top.currentPos < entry["end"]
' ?m.top.currentPos
' ?entry
t = m.tags.replaceAll(entry["text"], "")
texts.push(t)
end if
@ -98,40 +102,36 @@ sub updateCaption ()
end if
end sub
function ms(t) as integer
tt = t.tokenize(":")
return 3600000 * val(tt[0]) + 60000 * val(tt[1]) + 1000 * val(tt[2]) + val(t.right(3))
end function
function getstart(text)
return ms(text.left(12))
end function
function getend(text)
return ms(text)
end function
function isTime(text)
return text.mid(13, 3) = "-->"
return text.right(1) = chr(31)
end function
function parseVTT(text)
captionList = []
lines = text.tokenize(Chr(0))[0]
lines = lines.tokenize(Chr(10))
size = lines.count()
curStart = 0
curEnd = 0
for i = 0 to size - 1
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()
end function
function parseVTT(lines)
lines = lines.replace(" --> ", chr(31) + chr(10))
lines = lines.split(chr(10))
curStart = -1
curEnd = -1
entries = []
for i = 0 to lines.count() - 1
if isTime(lines[i])
curStart = ms (lines[i].left(12))
curEnd = ms(lines[i].mid(17, 12))
else
entry = { "start": curStart, "end": curEnd, "text": lines[i].trim() }
captionList.push(entry)
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
end if
end for
return captionList
return entries
end function

View File

@ -5,6 +5,7 @@
<field id="currentCaption" type="roArray" />
<field id="playerState" type="string" value="stopped" />
<field id="currentPos" type="int" />
<field id="useThis" type="boolean" />
</interface>
<script type="text/brightscript" uri="captionTask.brs" />
<script type="text/brightscript" uri="pkg:/source/utils/config.brs" />

View File

@ -47,8 +47,7 @@ sub Main (args as dynamic) as void
filename = re.match(filename)
if filename.count() > 0
filename = filename[1]
ext = right(filename, 4)
APIRequest("FallbackFont/Fonts/" + filename).asyncgettofile("tmp:/font" + ext)
APIRequest("FallbackFont/Fonts/" + filename).gettofile("tmp:/font")
end if
' Only show the Whats New popup the first time a user runs a new client version.