Skip to content

Commit

Permalink
Optimize parseVTT
Browse files Browse the repository at this point in the history
Change back to gettofile

Fix default font in captionTask

Update captionTask.brs
  • Loading branch information
jkim2492 committed Feb 6, 2023
1 parent 305ac54 commit 6e5e144
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 47 deletions.
7 changes: 4 additions & 3 deletions components/JFVideo.brs
Original file line number Diff line number Diff line change
Expand Up @@ -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

end sub

sub loadCaption()
m.top.suppressCaptions = m.captionTask.useThis
m.captionTask.url = m.top.currentSubtitleTrack
end sub

Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions components/JFVideo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
84 changes: 42 additions & 42 deletions components/captionTask.brs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
function isTime(text)
return text.right(1) = chr(31)
end function

function getend(text)
return ms(text)
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 isTime(text)
return text.mid(13, 3) = "-->"
end function
function parseVTT(lines)
lines = lines.replace(" --> ", chr(31) + chr(10))
lines = lines.split(chr(10))
curStart = -1
curEnd = -1
entries = []

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
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
1 change: 1 addition & 0 deletions components/captionTask.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
3 changes: 1 addition & 2 deletions source/Main.brs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6e5e144

Please sign in to comment.