Skip to content

Commit 1ee4ef8

Browse files
committed
Move font download to Main.brs
Optimize parseVTT performance
1 parent 3b7f7d7 commit 1ee4ef8

File tree

4 files changed

+45
-44
lines changed

4 files changed

+45
-44
lines changed

components/JFVideo.brs

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ end sub
134134
'
135135
' When Video Player state changes
136136
sub onState(msg)
137+
137138
m.captionTask.playerState = m.top.state + m.top.globalCaptionMode
138139
' When buffering, start timer to monitor buffering process
139140
if m.top.state = "buffering" and m.bufferCheckTimer <> invalid

components/JFVideo.xml

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
<field id="mediaSourceId" type="string" />
2424
<field id="audioIndex" type="integer" />
2525
<field id="runTime" type="integer" />
26-
27-
<field id="captionVisible" type="boolean" />
2826
</interface>
2927
<script type="text/brightscript" uri="JFVideo.brs" />
3028
<script type="text/brightscript" uri="pkg:/source/utils/misc.brs" />

components/captionTask.brs

+34-42
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,20 @@ sub init()
99
m.captionList = []
1010
m.reader = createObject("roUrlTransfer")
1111
m.font = CreateObject("roSGNode", "Font")
12-
fetchFont()
12+
m.tags = CreateObject("roRegex", "{\\an\d*}|&lt;.*?&gt;|<.*?>", "s")
13+
setFont()
1314
end sub
1415

1516

16-
sub fetchFont()
17+
sub setFont()
1718
fs = CreateObject("roFileSystem")
18-
fontlist = fs.Find("tmp:/", ".*\.(otf|ttf)")
19-
if fontlist.count() = 0
20-
re = CreateObject("roRegex", "Name.:.(.*?).,.Size", "s")
21-
m.filename = APIRequest("FallbackFont/Fonts").GetToString()
22-
m.filename = re.match(m.filename)
23-
if m.filename.count() <> 0
24-
m.filename = m.filename[1]
25-
APIRequest("FallbackFont/Fonts/" + m.filename).gettofile("tmp:/" + m.filename)
26-
m.font.uri = "tmp:/" + m.filename
27-
m.font.size = 60
28-
else
29-
m.font = "font:LargeBoldSystemFont"
30-
end if
31-
else
19+
fontlist = fs.Find("tmp:/", "font\.(otf|ttf)")
20+
if fontlist.count() > 0
3221
m.font.uri = "tmp:/" + fontlist[0]
33-
m.font.size = 60
22+
else
23+
m.font = "font:LargeBoldSystemFont"
3424
end if
25+
m.font.size = 60
3526
end sub
3627

3728
sub fetchCaption()
@@ -79,8 +70,9 @@ sub updateCaption ()
7970
m.top.currentPos = m.top.currentPos + 100
8071
texts = []
8172
for each entry in m.captionList
82-
if entry["start"] <= m.top.currentPos and m.top.currentPos <= entry["end"]
83-
texts.push(entry["text"])
73+
if entry["start"] <= m.top.currentPos and m.top.currentPos < entry["end"]
74+
t = m.tags.replaceAll(entry["text"], "")
75+
texts.push(t)
8476
end if
8577
end for
8678
labels = []
@@ -99,45 +91,45 @@ sub updateCaption ()
9991
lglist[8].getchild(q).color = &HFFFFFFFF
10092
end for
10193
m.top.currentCaption = lglist
102-
else if m.top.playerState = "playingOnWait"
94+
else if right(m.top.playerState, 4) = "Wait"
10395
m.top.playerState = "playingOn"
10496
else
10597
m.top.currentCaption = []
10698
end if
10799
end sub
108100

109101
function ms(t) as integer
110-
r = CreateObject("roRegex", ":|\.", "")
111-
l = r.split(t)
112-
return 3600000 * val(l[0]) + 60000 * val(l[1]) + 1000 * val(l[2]) + val(l[3])
102+
tt = t.tokenize(":")
103+
return 3600000 * val(tt[0]) + 60000 * val(tt[1]) + 1000 * val(tt[2]) + val(t.right(3))
113104
end function
114105

115-
function splitLines(text)
116-
r = CreateObject("roRegex", chr(10), "")
117-
return r.split(text)
106+
107+
108+
function getstart(text)
109+
return ms(text.left(12))
118110
end function
119111

120-
function strip(text) as string
121-
leading = CreateObject("roRegex", "^\s+", "")
122-
trailing = CreateObject("roRegex", "\s+$", "")
123-
text = leading.replaceall(text, "")
124-
text = trailing.replaceall(text, "")
125-
return text
112+
function getend(text)
113+
return ms(text)
114+
end function
115+
116+
function isTime(text)
117+
return text.mid(13, 3) = "-->"
126118
end function
127119

128120
function parseVTT(text)
129-
timestamp = "(\d\d:\d\d:\d\d\.\d\d\d) --> (\d\d:\d\d:\d\d\.\d\d\d)"
130-
re = CreateObject("roRegex", timestamp + " region.*", "")
131-
timeList = re.matchall (text)
132-
textList = re.split(text)
133-
textList.shift()
134121
captionList = []
135-
for i = 0 to textList.count() - 1
136-
textLines = splitLines(strip (textList[i]))
137-
for each line in textLines
138-
entry = { "start": ms(timeList[i][1]), "end": ms(timeList[i][2]), "text": strip(line), "color": "" }
122+
lines = text.tokenize(Chr(0))[0]
123+
lines = lines.tokenize(Chr(10))
124+
size = lines.count()
125+
for i = 2 to size - 1
126+
if isTime(lines[i])
127+
curStart = ms (lines[i].left(12))
128+
curEnd = ms(lines[i].mid(17, 12))
129+
else
130+
entry = { "start": curStart, "end": curEnd, "text": lines[i].trim() }
139131
captionList.push(entry)
140-
end for
132+
end if
141133
end for
142134
return captionList
143135
end function

source/Main.brs

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ sub Main (args as dynamic) as void
4141

4242
m.scene.observeField("exit", m.port)
4343

44+
' Downloads and stores a fallback font to tmp:/
45+
re = CreateObject("roRegex", "Name.:.(.*?).,.Size", "s")
46+
filename = APIRequest("FallbackFont/Fonts").GetToString()
47+
filename = re.match(filename)
48+
if filename.count() > 0
49+
filename = filename[1]
50+
ext = right(filename, 4)
51+
APIRequest("FallbackFont/Fonts/" + filename).gettofile("tmp:/font" + ext)
52+
end if
53+
4454
' Only show the Whats New popup the first time a user runs a new client version.
4555
if appInfo.GetVersion() <> get_setting("LastRunVersion")
4656
' Ensure the user hasn't disabled Whats New popups

0 commit comments

Comments
 (0)