Move font download to Main.brs

Optimize parseVTT performance
This commit is contained in:
Jinho Kim 2023-02-04 11:41:00 -05:00
parent 3b7f7d7f82
commit 1ee4ef8d12
4 changed files with 45 additions and 44 deletions

View File

@ -134,6 +134,7 @@ 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,8 +23,6 @@
<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" />
<field id="captionVisible" type="boolean" />
</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

@ -9,29 +9,20 @@ sub init()
m.captionList = [] m.captionList = []
m.reader = createObject("roUrlTransfer") m.reader = createObject("roUrlTransfer")
m.font = CreateObject("roSGNode", "Font") m.font = CreateObject("roSGNode", "Font")
fetchFont() m.tags = CreateObject("roRegex", "{\\an\d*}|&lt;.*?&gt;|<.*?>", "s")
setFont()
end sub end sub
sub fetchFont() sub setFont()
fs = CreateObject("roFileSystem") fs = CreateObject("roFileSystem")
fontlist = fs.Find("tmp:/", ".*\.(otf|ttf)") fontlist = fs.Find("tmp:/", "font\.(otf|ttf)")
if fontlist.count() = 0 if fontlist.count() > 0
re = CreateObject("roRegex", "Name.:.(.*?).,.Size", "s")
m.filename = APIRequest("FallbackFont/Fonts").GetToString()
m.filename = re.match(m.filename)
if m.filename.count() <> 0
m.filename = m.filename[1]
APIRequest("FallbackFont/Fonts/" + m.filename).gettofile("tmp:/" + m.filename)
m.font.uri = "tmp:/" + m.filename
m.font.size = 60
else
m.font = "font:LargeBoldSystemFont"
end if
else
m.font.uri = "tmp:/" + fontlist[0] m.font.uri = "tmp:/" + fontlist[0]
m.font.size = 60 else
m.font = "font:LargeBoldSystemFont"
end if end if
m.font.size = 60
end sub end sub
sub fetchCaption() sub fetchCaption()
@ -79,8 +70,9 @@ sub updateCaption ()
m.top.currentPos = m.top.currentPos + 100 m.top.currentPos = m.top.currentPos + 100
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"]
texts.push(entry["text"]) t = m.tags.replaceAll(entry["text"], "")
texts.push(t)
end if end if
end for end for
labels = [] labels = []
@ -99,7 +91,7 @@ sub updateCaption ()
lglist[8].getchild(q).color = &HFFFFFFFF lglist[8].getchild(q).color = &HFFFFFFFF
end for end for
m.top.currentCaption = lglist m.top.currentCaption = lglist
else if m.top.playerState = "playingOnWait" else if right(m.top.playerState, 4) = "Wait"
m.top.playerState = "playingOn" m.top.playerState = "playingOn"
else else
m.top.currentCaption = [] m.top.currentCaption = []
@ -107,37 +99,37 @@ sub updateCaption ()
end sub end sub
function ms(t) as integer function ms(t) as integer
r = CreateObject("roRegex", ":|\.", "") tt = t.tokenize(":")
l = r.split(t) return 3600000 * val(tt[0]) + 60000 * val(tt[1]) + 1000 * val(tt[2]) + val(t.right(3))
return 3600000 * val(l[0]) + 60000 * val(l[1]) + 1000 * val(l[2]) + val(l[3])
end function end function
function splitLines(text)
r = CreateObject("roRegex", chr(10), "")
return r.split(text) function getstart(text)
return ms(text.left(12))
end function end function
function strip(text) as string function getend(text)
leading = CreateObject("roRegex", "^\s+", "") return ms(text)
trailing = CreateObject("roRegex", "\s+$", "") end function
text = leading.replaceall(text, "")
text = trailing.replaceall(text, "") function isTime(text)
return text return text.mid(13, 3) = "-->"
end function end function
function parseVTT(text) function parseVTT(text)
timestamp = "(\d\d:\d\d:\d\d\.\d\d\d) --> (\d\d:\d\d:\d\d\.\d\d\d)"
re = CreateObject("roRegex", timestamp + " region.*", "")
timeList = re.matchall (text)
textList = re.split(text)
textList.shift()
captionList = [] captionList = []
for i = 0 to textList.count() - 1 lines = text.tokenize(Chr(0))[0]
textLines = splitLines(strip (textList[i])) lines = lines.tokenize(Chr(10))
for each line in textLines size = lines.count()
entry = { "start": ms(timeList[i][1]), "end": ms(timeList[i][2]), "text": strip(line), "color": "" } for i = 2 to size - 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) captionList.push(entry)
end for end if
end for end for
return captionList return captionList
end function end function

View File

@ -41,6 +41,16 @@ sub Main (args as dynamic) as void
m.scene.observeField("exit", m.port) m.scene.observeField("exit", m.port)
' Downloads and stores a fallback font to tmp:/
re = CreateObject("roRegex", "Name.:.(.*?).,.Size", "s")
filename = APIRequest("FallbackFont/Fonts").GetToString()
filename = re.match(filename)
if filename.count() > 0
filename = filename[1]
ext = right(filename, 4)
APIRequest("FallbackFont/Fonts/" + filename).gettofile("tmp:/font" + ext)
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.
if appInfo.GetVersion() <> get_setting("LastRunVersion") if appInfo.GetVersion() <> get_setting("LastRunVersion")
' Ensure the user hasn't disabled Whats New popups ' Ensure the user hasn't disabled Whats New popups