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

View File

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

View File

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

View File

@ -5,6 +5,7 @@
<field id="currentCaption" type="roArray" /> <field id="currentCaption" type="roArray" />
<field id="playerState" type="string" value="stopped" /> <field id="playerState" type="string" value="stopped" />
<field id="currentPos" type="int" /> <field id="currentPos" type="int" />
<field id="useThis" type="boolean" />
</interface> </interface>
<script type="text/brightscript" uri="captionTask.brs" /> <script type="text/brightscript" uri="captionTask.brs" />
<script type="text/brightscript" uri="pkg:/source/utils/config.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) filename = re.match(filename)
if filename.count() > 0 if filename.count() > 0
filename = filename[1] filename = filename[1]
ext = right(filename, 4) APIRequest("FallbackFont/Fonts/" + filename).gettofile("tmp:/font")
APIRequest("FallbackFont/Fonts/" + filename).asyncgettofile("tmp:/font" + ext)
end if end if
' Only show the Whats New popup the first time a user runs a new client version. ' Only show the Whats New popup the first time a user runs a new client version.