Follow up to #122 for selecting subtitles tracks (#124)

Handles selecting subtitle tracks when transcoding now too

Also fixes the adjustment if there's many external subtitles
This commit is contained in:
damontecres 2025-10-31 13:56:42 -04:00 committed by GitHub
parent 1508fa1ea3
commit e9e5aee01c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -80,7 +80,6 @@ import org.jellyfin.sdk.model.api.BaseItemKind
import org.jellyfin.sdk.model.api.DeviceProfile import org.jellyfin.sdk.model.api.DeviceProfile
import org.jellyfin.sdk.model.api.MediaSegmentDto import org.jellyfin.sdk.model.api.MediaSegmentDto
import org.jellyfin.sdk.model.api.MediaSegmentType import org.jellyfin.sdk.model.api.MediaSegmentType
import org.jellyfin.sdk.model.api.MediaSourceInfo
import org.jellyfin.sdk.model.api.MediaStreamType import org.jellyfin.sdk.model.api.MediaStreamType
import org.jellyfin.sdk.model.api.PlayMethod import org.jellyfin.sdk.model.api.PlayMethod
import org.jellyfin.sdk.model.api.PlaybackInfoDto import org.jellyfin.sdk.model.api.PlaybackInfoDto
@ -560,7 +559,7 @@ class PlaybackViewModel
if (tracks.groups.isNotEmpty()) { if (tracks.groups.isNotEmpty()) {
applyTrackSelections( applyTrackSelections(
player, player,
source, source.supportsDirectPlay,
audioIndex, audioIndex,
subtitleIndex, subtitleIndex,
externalSubtitleCount, externalSubtitleCount,
@ -1026,17 +1025,24 @@ suspend fun <T> onMain(block: suspend CoroutineScope.() -> T) = withContext(Disp
@OptIn(UnstableApi::class) @OptIn(UnstableApi::class)
private fun applyTrackSelections( private fun applyTrackSelections(
player: Player, player: Player,
source: MediaSourceInfo, supportsDirectPlay: Boolean,
audioIndex: Int?, audioIndex: Int?,
subtitleIndex: Int?, subtitleIndex: Int?,
externalSubtitleCount: Int, externalSubtitleCount: Int,
subtitleIsExternal: Boolean, subtitleIsExternal: Boolean,
) { ) {
if (source.supportsDirectPlay) { if (subtitleIndex != null && subtitleIndex >= 0 && (subtitleIsExternal || supportsDirectPlay)) {
if (subtitleIndex != null && subtitleIndex >= 0) {
val indexToFind =
subtitleIndex + if (subtitleIsExternal) 0 else (if (externalSubtitleCount > 0) -1 else 1)
val chosenTrack = val chosenTrack =
if (subtitleIsExternal) {
player.currentTracks.groups.firstOrNull { group ->
group.type == C.TRACK_TYPE_TEXT && group.isSupported &&
(0..<group.mediaTrackGroup.length)
.mapNotNull {
group.getTrackFormat(it).id
}.any { it.endsWith("e:$subtitleIndex") }
}
} else {
val indexToFind = subtitleIndex - externalSubtitleCount + 1
player.currentTracks.groups.firstOrNull { group -> player.currentTracks.groups.firstOrNull { group ->
group.type == C.TRACK_TYPE_TEXT && group.isSupported && group.type == C.TRACK_TYPE_TEXT && group.isSupported &&
(0..<group.mediaTrackGroup.length) (0..<group.mediaTrackGroup.length)
@ -1044,7 +1050,9 @@ private fun applyTrackSelections(
group.getTrackFormat(it).idAsInt group.getTrackFormat(it).idAsInt
}.contains(indexToFind) }.contains(indexToFind)
} }
Timber.v("Chosen subtitle ($subtitleIndex/$indexToFind) track: $chosenTrack") }
Timber.v("Chosen subtitle ($subtitleIndex) track: $chosenTrack")
chosenTrack?.let { chosenTrack?.let {
player.trackSelectionParameters = player.trackSelectionParameters =
player.trackSelectionParameters player.trackSelectionParameters
@ -1064,16 +1072,18 @@ private fun applyTrackSelections(
.setTrackTypeDisabled(C.TRACK_TYPE_TEXT, true) .setTrackTypeDisabled(C.TRACK_TYPE_TEXT, true)
.build() .build()
} }
if (audioIndex != null) { if (audioIndex != null && supportsDirectPlay) {
val indexToFind =
audioIndex - externalSubtitleCount + 1
val chosenTrack = val chosenTrack =
player.currentTracks.groups.firstOrNull { group -> player.currentTracks.groups.firstOrNull { group ->
group.type == C.TRACK_TYPE_AUDIO && group.isSupported && group.type == C.TRACK_TYPE_AUDIO && group.isSupported &&
(0..<group.mediaTrackGroup.length) (0..<group.mediaTrackGroup.length)
.map { .map {
group.getTrackFormat(it).idAsInt group.getTrackFormat(it).idAsInt
}.contains(audioIndex - externalSubtitleCount + 1) // Indexes are 1 based }.contains(indexToFind)
} }
Timber.v("Chosen audio track: $chosenTrack") Timber.v("Chosen audio ($audioIndex/$indexToFind) track: $chosenTrack")
chosenTrack?.let { chosenTrack?.let {
player.trackSelectionParameters = player.trackSelectionParameters =
player.trackSelectionParameters player.trackSelectionParameters
@ -1088,4 +1098,3 @@ private fun applyTrackSelections(
} }
} }
} }
}