Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to load default subtitles according to user settings on the global player #1306

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
00519e7
Add function to preload subtitles
therealguy90 Jun 12, 2023
8690aea
Add function to apply default subtitles on load
therealguy90 Jun 12, 2023
1f82a68
Fix "None" subtitles not bing selectable
therealguy90 Jun 12, 2023
c42f37e
Fix subtitle selector not selecting "None"
therealguy90 Jun 12, 2023
45367f2
Fix subtitle selector to allow selecting "None"
therealguy90 Jun 12, 2023
4d02d85
Added check for subtitles on first load
therealguy90 Jun 12, 2023
20b92c3
Cleanup debug commands.
therealguy90 Jun 12, 2023
9bf6b23
Fix subtitle selection window video player reload
therealguy90 Jun 13, 2023
6fa8355
Merge branch 'jellyfin:unstable' into unstable
therealguy90 Jun 17, 2023
e847c46
Modfy subtitle selection behavior
therealguy90 Jun 17, 2023
05df4b3
Code cleanup, load subtitles on next episode
therealguy90 Jun 17, 2023
ad9a136
Code cleanup, load subtitles on next episode
therealguy90 Jun 17, 2023
03b6496
Merge branch 'jellyfin:unstable' into unstable
therealguy90 Jun 17, 2023
4daea08
revert argument order
therealguy90 Jun 23, 2023
ee51222
Merge branch 'unstable' of https://github.com/Jmalayao/jellyfin-roku …
therealguy90 Jun 23, 2023
87c1ded
Merge branch 'jellyfin:unstable' into unstable
therealguy90 Jun 23, 2023
09cfea9
Merge branch 'jellyfin:unstable' into unstable
therealguy90 Jun 30, 2023
7d250fe
Merge branch 'jellyfin:unstable' into unstable
therealguy90 Jul 14, 2023
9342a3b
Merge branch 'jellyfin:unstable' into unstable
therealguy90 Jul 28, 2023
b737474
Merge branch 'jellyfin:unstable' into unstable
therealguy90 Aug 9, 2023
5653acb
Merge branch 'jellyfin:unstable' into unstable
therealguy90 Aug 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 170 additions & 3 deletions components/ItemGrid/LoadVideoContentTask.brs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import "pkg:/source/utils/deviceCapabilities.brs"
sub init()
m.user = AboutMe()
m.top.functionName = "loadItems"
currentItem = m.global.queueManager.callFunc("getCurrentItem")
m.top.selectedSubtitleIndex = defaultSubtitleTrackFromVid(currentItem.id)

end sub

sub loadItems()
Expand Down Expand Up @@ -51,7 +54,6 @@ sub loadItems()
end sub

function LoadItems_VideoPlayer(id as string, mediaSourceId = invalid as dynamic, audio_stream_idx = 1 as integer, subtitle_idx = -1 as integer, forceTranscoding = false as boolean) as dynamic

video = {}
video.id = id
video.content = createObject("RoSGNode", "ContentNode")
Expand All @@ -66,7 +68,6 @@ function LoadItems_VideoPlayer(id as string, mediaSourceId = invalid as dynamic,
end function

sub LoadItems_AddVideoContent(video as object, mediaSourceId as dynamic, audio_stream_idx = 1 as integer, subtitle_idx = -1 as integer, forceTranscoding = false as boolean)

meta = ItemMetaData(video.id)

if not isValid(meta)
Expand Down Expand Up @@ -140,7 +141,8 @@ sub LoadItems_AddVideoContent(video as object, mediaSourceId as dynamic, audio_s
end if


' 'TODO: allow user selection of subtitle track before playback initiated, for now set to no subtitles
' 'TODO: allow user selection of subtitle track before playback initiated, for manual subtitle selection
' Now uses behavior of JF video players, uses user subtitle settings

video.directPlaySupported = m.playbackInfo.MediaSources[0].SupportsDirectPlay
fully_external = false
Expand Down Expand Up @@ -339,6 +341,171 @@ sub addNextEpisodesToQueue(showID)
end if
end sub

' Identify the default subtitle track for a given video id
' returns the server-side track index for the appriate subtitle
function defaultSubtitleTrackFromVid(video_id) as integer
meta = ItemMetaData(video_id)
if isValid(meta) and isValid(meta.json) and isValid(meta.json.mediaSources)
subtitles = sortSubtitles(meta.id, meta.json.MediaSources[0].MediaStreams)
default_text_subs = defaultSubtitleTrack(subtitles["all"], true) ' Find correct subtitle track (forced text)
if default_text_subs <> -1
return default_text_subs
else
if get_user_setting("playback.subs.onlytext") = "false"
return defaultSubtitleTrack(subtitles["all"]) ' if no appropriate text subs exist, allow non-text
else
return -1
end if
end if
end if
' No valid mediaSources (i.e. LiveTV)
return -1
end function

' Identify the default subtitle track
' if "requires_text" is true, only return a track if it is textual
' This allows forcing text subs, since roku requires transcoding of non-text subs
' returns the server-side track index for the appriate subtitle
function defaultSubtitleTrack(sorted_subtitles, require_text = false) as integer
if m.user.Configuration.SubtitleMode = "None" or sorted_subtitles.Count() = 0
return -1 ' No subtitles desired: select none
end if

filtered_subtitles = []

if require_text 'Filter all non-text subtitles if require_text is true
for each subtitle in sorted_subtitles
if require_text and subtitle.IsTextSubtitleStream
filtered_subtitles.push(subtitle)
end if
end for
else
filtered_subtitles = sorted_subtitles
end if
sub_idx = -1 ' "none"

' When set to "Default," the media player will follow the default subtitle track specified.
' If there is no default/forced track, it will choose the first available subtitle track.
' Language preferences are considered only when multiple options are available, as per Jellyfin.
if m.user.Configuration.SubtitleMode = "Default"
forced_subs = []
for each item in filtered_subtitles ' Follow subtitle precedence. Search for Forced subtitles first.
if item.isForced
forced_subs.push(item)
exit for
end if
end for
if forced_subs.Count() >= 0
if forced_subs.Count() > 1 or m.user.Configuration.SubtitleLanguagePreference <> ""
sub_idx = forced_subs[0].index
' Failsafe for multiple Forced subtitles. Skips this check and uses the first found index
' if preference is set to "Any Language"
for each item in forced_subs
if m.user.Configuration.SubtitleLanguagePreference = item.Track.Language
sub_idx = item.index
exit for
end if
end for
else
sub_idx = forced_subs[0].index
end if
end if

if sub_idx = -1
default_subs = []
for each item in filtered_subtitles ' Search for Default subtitles if no Forced are found
if item.isForced
default_subs.push(item)
exit for
end if
end for
if default_subs.Count() >= 0
if default_subs.Count() > 1 or m.user.Configuration.SubtitleLanguagePreference <> ""
sub_idx = default_subs[0].index
' Failsafe for multiple Forced subtitles. Skips this check and uses the first found index
' if preference is set to "Any Language"
for each item in default_subs
if m.user.Configuration.SubtitleLanguagePreference = item.Track.Language
sub_idx = item.index
exit for
end if
end for
else
sub_idx = default_subs[0].index
end if
end if
end if
if sub_idx = -1 ' If no Default or Forced subtitles are found, use first available
sub_idx = filtered_subtitles[0].Index
end if
end if

'When "Only Forced" is selected, the media player will exclusively display forced subtitles, if available.
if m.user.Configuration.SubtitleMode = "OnlyForced"
forced_subs = []
for each item in filtered_subtitles ' Search for all Forced subtitles
if item.isForced
forced_subs.push(item)
exit for
end if
end for
if forced_subs.Count() >= 0
if forced_subs.Count() > 1 or m.user.Configuration.SubtitleLanguagePreference <> ""
sub_idx = forced_subs[0].index
' Failsafe for multiple Forced subtitles. Skips this check and uses the first found index
' if preference is set to "Any Language"
for each item in forced_subs
if m.user.Configuration.SubtitleLanguagePreference = item.Track.Language
sub_idx = item.index
exit for
end if
end for
else
sub_idx = forced_subs[0].index
end if
end if
' Unlike Default, Only Forced will only load Forced subtitles, therefore, it will default to None
' if no Forced flags are found.
end if


' "Always Play" means that subtitles matching the language
' preference will be loaded regardless of the audio language, as per Jellyfin.
' However, for our purposes, it is better to find a matching track language and disregard Default/Forced
' flags. If no matching languages are found, use the first track.
' The default logic is very similar to Default.
if m.user.Configuration.SubtitleMode = "Always"
for each item in filtered_subtitles
if m.user.Configuration.SubtitleLanguagePreference = item.Track.Language
sub_idx = item.index
exit for
end if
end for
if sub_idx = -1
sub_idx = filtered_subtitles[0].index
end if
end if

' For "Smart", we will default to using the first available preferred language.
' This is to avoid comparing the audio language to the subtitle language.
' Left this as a TODO in case someone wants to pick this up from here
' If none are found, use the first available.
if m.user.Configuration.SubtitleMode = "Smart"
for each item in filtered_subtitles
if m.user.Configuration.SubtitleLanguagePreference = item.Track.Language
sub_idx = item.index
exit for
end if
end for
if sub_idx = -1
sub_idx = filtered_subtitles[0].index
end if
end if


return sub_idx
end function

'Checks available subtitle tracks and puts subtitles in forced, default, and non-default/forced but preferred language at the top
function sortSubtitles(id as string, MediaStreams)
tracks = { "forced": [], "default": [], "normal": [], "text": [] }
Expand Down
1 change: 0 additions & 1 deletion components/manager/ViewCreator.brs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ sub onSelectSubtitlePressed()

for each item in m.view.fullSubtitleData
item.type = "subtitleselection"

if m.view.selectedSubtitle <> -1
' Subtitle is a track within the file
if item.index = m.view.selectedSubtitle
Expand Down
1 change: 1 addition & 0 deletions components/video/VideoPlayerView.brs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sub init()
m.LoadMetaDataTask.selectedAudioStreamIndex = m.currentItem.selectedAudioStreamIndex
m.LoadMetaDataTask.observeField("content", "onVideoContentLoaded")
m.LoadMetaDataTask.control = "RUN"
m.top.selectedSubtitle = m.LoadMetaDataTask.selectedSubtitleIndex

m.playbackTimer = m.top.findNode("playbackTimer")
m.bufferCheckTimer = m.top.findNode("bufferCheckTimer")
Expand Down