From 6e5e14444e5f20e528d63d1b2d35642f14a115ca Mon Sep 17 00:00:00 2001
From: Jinho Kim <54465744+jkim2492@users.noreply.github.com>
Date: Sun, 5 Feb 2023 03:07:01 -0500
Subject: [PATCH] Optimize parseVTT
Change back to gettofile
Fix default font in captionTask
Update captionTask.brs
---
components/JFVideo.brs | 7 +--
components/JFVideo.xml | 1 +
components/captionTask.brs | 88 +++++++++++++++++++-------------------
components/captionTask.xml | 1 +
source/Main.brs | 3 +-
5 files changed, 51 insertions(+), 49 deletions(-)
diff --git a/components/JFVideo.brs b/components/JFVideo.brs
index 3fbedc4f..aaab4860 100644
--- a/components/JFVideo.brs
+++ b/components/JFVideo.brs
@@ -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
diff --git a/components/JFVideo.xml b/components/JFVideo.xml
index 0089f053..fa338b28 100644
--- a/components/JFVideo.xml
+++ b/components/JFVideo.xml
@@ -23,6 +23,7 @@
+
diff --git a/components/captionTask.brs b/components/captionTask.brs
index b6056196..54845669 100644
--- a/components/captionTask.brs
+++ b/components/captionTask.brs
@@ -10,32 +10,34 @@ sub init()
m.reader = createObject("roUrlTransfer")
m.font = CreateObject("roSGNode", "Font")
m.tags = CreateObject("roRegex", "{\\an\d*}|<.*?>|<.*?>", "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
diff --git a/components/captionTask.xml b/components/captionTask.xml
index 167b9936..20bef482 100644
--- a/components/captionTask.xml
+++ b/components/captionTask.xml
@@ -5,6 +5,7 @@
+
diff --git a/source/Main.brs b/source/Main.brs
index be36cef6..0d84e620 100644
--- a/source/Main.brs
+++ b/source/Main.brs
@@ -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.