Skip to content

Commit

Permalink
Custom subtitles added to options
Browse files Browse the repository at this point in the history
Custom subtitles can be enabled via * > Settings > Playback > Use Custom Subtitles

Fallback font downloaded only if it is enabled in system settings

Cleaned up various code
  • Loading branch information
jkim2492 committed Feb 19, 2023
1 parent facec48 commit bd5a055
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 63 deletions.
57 changes: 27 additions & 30 deletions components/JFVideo.brs
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,32 @@ sub init()
m.captionTask.observeField("useThis", "checkCaptionMode")
m.top.observeField("currentSubtitleTrack", "loadCaption")
m.top.observeField("globalCaptionMode", "toggleCaption")

m.top.suppressCaptions = True
toggleCaption()

if get_user_setting("playback.subs.custom") = "false"
m.top.suppressCaptions = false
else
m.top.suppressCaptions = true
toggleCaption()
end if
end sub

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

sub toggleCaption()
m.captionTask.playerState = m.top.state + m.top.globalCaptionMode
if m.top.globalCaptionMode = "On"
m.captionTask.playerState = m.captionTask.playerState + "Wait"
if LCase(m.top.globalCaptionMode) = "on"
m.captionTask.playerState = m.top.state + m.top.globalCaptionMode + "w"
m.captionGroup.visible = true
else
m.captionGroup.visible = false
end if
end sub

sub updateCaption ()
while m.captionGroup.removeChildIndex(0)
end while
m.captionGroup.removeChildrenIndex(m.captionGroup.getChildCount(), 0)
m.captionGroup.appendChildren(m.captionTask.currentCaption)
end sub

Expand All @@ -71,10 +73,6 @@ sub onContentChange()

m.top.observeField("position", "onPositionChanged")

' If video content type is not episode, remove position observer
' if m.top.content.contenttype <> 4
' m.top.unobserveField("position")
' end if
end sub

sub onNextEpisodeDataLoaded()
Expand All @@ -86,12 +84,11 @@ end sub
'
' Runs Next Episode button animation and sets focus to button
sub showNextEpisodeButton()
if m.top.content.contenttype = 4
if not m.nextEpisodeButton.visible
m.showNextEpisodeButtonAnimation.control = "start"
m.nextEpisodeButton.setFocus(true)
m.nextEpisodeButton.visible = true
end if
if m.top.content.contenttype <> 4 then return
if not m.nextEpisodeButton.visible
m.showNextEpisodeButtonAnimation.control = "start"
m.nextEpisodeButton.setFocus(true)
m.nextEpisodeButton.visible = true
end if
end sub

Expand All @@ -111,24 +108,24 @@ end sub

' Checks if we need to display the Next Episode button
sub checkTimeToDisplayNextEpisode()
if m.top.content.contenttype = 4
if m.top.content.contenttype <> 4 then return

if int(m.top.position) >= (m.top.runTime - 30)
showNextEpisodeButton()
updateCount()
return
end if
if int(m.top.position) >= (m.top.runTime - 30)
showNextEpisodeButton()
updateCount()
return
end if

if m.nextEpisodeButton.visible or m.nextEpisodeButton.hasFocus()
m.nextEpisodeButton.visible = false
m.nextEpisodeButton.setFocus(false)
end if
if m.nextEpisodeButton.visible or m.nextEpisodeButton.hasFocus()
m.nextEpisodeButton.visible = false
m.nextEpisodeButton.setFocus(false)
end if
end sub

' When Video Player state changes
sub onPositionChanged()
m.captionTask.currentPos = Int(m.top.position * 1000)
' Check if dialog is open
m.dialog = m.top.getScene().findNode("dialogBackground")
if not isValid(m.dialog)
checkTimeToDisplayNextEpisode()
Expand Down
38 changes: 18 additions & 20 deletions components/captionTask.brs
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,27 @@ sub setFont()
if fontlist.count() > 0
m.font.uri = "tmp:/" + fontlist[0]
m.font.size = m.fontSize
m.top.useThis = True
else
reg = CreateObject("roFontRegistry")
m.font = reg.GetDefaultFont(m.fontSize, false, false)
end if
end sub

sub fetchCaption()
if m.top.useThis
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
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

function newlabel(txt):
function newlabel(txt)
label = CreateObject("roSGNode", "Label")
label.text = txt
label.font = m.font
Expand Down Expand Up @@ -82,16 +81,17 @@ function newRect(lg)
rect.width = 0
rect.height = 0
end if
rectLG.translation = [0, -rect.height / 2]
rectLG.horizalignment = "center"
rectLG.vertalignment = "bottom"
rectLG.vertalignment = "center"
rectLG.appendchild(rect)
return rectLG
end function


sub updateCaption ()
m.top.currentCaption = []
if m.top.playerState = "playingOn"
if LCase(m.top.playerState) = "playingon"
m.top.currentPos = m.top.currentPos + 100
texts = []
for each entry in m.captionList
Expand All @@ -107,10 +107,8 @@ sub updateCaption ()
lines = newLayoutGroup(labels)
rect = newRect(lines)
m.top.currentCaption = [rect, lines]
else if right(m.top.playerState, 4) = "Wait"
m.top.playerState = "playingOn"
else
m.top.currentCaption = []
else if LCase(m.top.playerState.right(1)) = "w"
m.top.playerState = m.top.playerState.left(len (m.top.playerState) - 1)
end if
end sub

Expand Down
1 change: 0 additions & 1 deletion components/captionTask.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<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
13 changes: 10 additions & 3 deletions settings/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
"settingName": "playback.subs.onlytext",
"type": "bool",
"default": "false"
},
{
"title": "Use Custom Subtitles",
"description": "Use custom subtitles instead of Roku default subtitles",
"settingName": "playback.subs.custom",
"type": "bool",
"default": "false"
}
]
},
Expand Down Expand Up @@ -129,11 +136,11 @@
"default": "false"
},
{
"title":"Disable Community Rating for Episodes",
"title": "Disable Community Rating for Episodes",
"description": "If enabled, the star and community rating for episodes of a TV show will be removed. This is to prevent spoilers of an upcoming good/bad episode.",
"settingName": "ui.tvshows.disableCommunityRating",
"type":"bool",
"default":"false"
"type": "bool",
"default": "false"
}
]
},
Expand Down
14 changes: 8 additions & 6 deletions source/Main.brs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ sub Main (args as dynamic) as void
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]
APIRequest("FallbackFont/Fonts/" + filename).gettofile("tmp:/font")
if parseJSON(APIRequest("/System/Configuration/encoding").GetToString())["EnableFallbackFont"] = true
re = CreateObject("roRegex", "Name.:.(.*?).,.Size", "s")
filename = APIRequest("FallbackFont/Fonts").GetToString()
filename = re.match(filename)
if filename.count() > 0
filename = filename[1]
APIRequest("FallbackFont/Fonts/" + filename).gettofile("tmp:/font")
end if
end if

' Only show the Whats New popup the first time a user runs a new client version.
Expand Down
3 changes: 0 additions & 3 deletions source/utils/Subtitles.brs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ function setupSubtitle(video, subtitles, subtitle_idx = -1) as integer

if selectedSubtitle.IsEncoded
' With encoded subtitles, turn off captions
?"Check2"

video.globalCaptionMode = "Off"
else
' If this is a text-based subtitle, set relevant settings for roku captions
Expand Down Expand Up @@ -178,7 +176,6 @@ sub turnoffSubtitles()
video = m.scene.focusedChild.focusedChild
current = video.SelectedSubtitle
video.SelectedSubtitle = -1
?"Check3"
video.globalCaptionMode = "Off"
m.device.EnableAppFocusEvent(false)
' Check if Enoded subtitles are being displayed, and turn off
Expand Down

0 comments on commit bd5a055

Please sign in to comment.