Skip to content

Commit

Permalink
Infer subtitle codec from delivery URL if unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Dec 24, 2024
1 parent dae017c commit 528731e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.jellyfin.androidtv.data.compat.StreamInfo;
import org.jellyfin.androidtv.preference.UserPreferences;
import org.jellyfin.androidtv.preference.constant.ZoomMode;
import org.jellyfin.playback.media3.exoplayer.mapping.SubtitleKt;
import org.jellyfin.sdk.api.client.ApiClient;
import org.jellyfin.sdk.model.api.MediaStream;
import org.jellyfin.sdk.model.api.MediaStreamType;
Expand Down Expand Up @@ -346,7 +345,7 @@ public void setMediaStreamInfo(ApiClient api, StreamInfo streamInfo) {
Uri subtitleUri = Uri.parse(api.createUrl(mediaStream.getDeliveryUrl(), Collections.emptyMap(), Collections.emptyMap(), true));
MediaItem.SubtitleConfiguration subtitleConfiguration = new MediaItem.SubtitleConfiguration.Builder(subtitleUri)
.setId("JF_EXTERNAL:" + String.valueOf(mediaStream.getIndex()))
.setMimeType(SubtitleKt.getFfmpegSubtitleMimeType(mediaStream.getCodec()))
.setMimeType(VideoManagerHelperKt.getSubtitleMediaStreamCodec(mediaStream))
.setLanguage(mediaStream.getLanguage())
.setLabel(mediaStream.getDisplayTitle())
.setSelectionFlags(getSubtitleSelectionFlags(mediaStream))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jellyfin.androidtv.ui.playback

import androidx.core.net.toUri
import org.jellyfin.playback.media3.exoplayer.mapping.getFfmpegSubtitleMimeType
import org.jellyfin.sdk.model.api.MediaStream

fun getSubtitleMediaStreamCodec(stream: MediaStream): String {
val codec = requireNotNull(stream.codec)

return getFfmpegSubtitleMimeType(
codec = codec,
// Try to infer the codec from the delivery URL extension
fallback = stream.deliveryUrl?.toUri()?.lastPathSegment?.split('.')?.last()?.let(::getFfmpegSubtitleMimeType) ?: codec,
)
}
4 changes: 2 additions & 2 deletions playback/media3/exoplayer/src/main/kotlin/mapping/audio.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import androidx.media3.common.MimeTypes
import androidx.media3.common.util.UnstableApi

@OptIn(UnstableApi::class)
fun getFfmpegAudioMimeType(codec: String) = codec.lowercase().let { codec ->
fun getFfmpegAudioMimeType(codec: String, fallback: String = codec) = codec.lowercase().let { codec ->
ffmpegAudioMimeTypes[codec]
?: MimeTypes.getAudioMediaMimeType(codec)
?: codec
?: fallback
}

val ffmpegAudioMimeTypes = mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import androidx.media3.common.MimeTypes
import androidx.media3.common.util.UnstableApi

@OptIn(UnstableApi::class)
fun getFfmpegContainerMimeType(codec: String) = codec.lowercase().let { codec ->
fun getFfmpegContainerMimeType(codec: String, fallback: String = codec) = codec.lowercase().let { codec ->
ffmpegContainerMimeTypes[codec]
?: ffmpegVideoMimeTypes[codec]
?: ffmpegAudioMimeTypes[codec]
?: MimeTypes.getMediaMimeType(codec)
?: codec
?: fallback
}

@OptIn(UnstableApi::class)
Expand Down
4 changes: 2 additions & 2 deletions playback/media3/exoplayer/src/main/kotlin/mapping/subtitle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import androidx.media3.common.MimeTypes
import androidx.media3.common.util.UnstableApi

@OptIn(UnstableApi::class)
fun getFfmpegSubtitleMimeType(codec: String): String = codec.lowercase().let { codec ->
fun getFfmpegSubtitleMimeType(codec: String, fallback: String = codec): String = codec.lowercase().let { codec ->
ffmpegSubtitleMimeTypes[codec]
?: MimeTypes.getTextMediaMimeType(codec)
?: codec
?: fallback
}

@OptIn(UnstableApi::class)
Expand Down
4 changes: 2 additions & 2 deletions playback/media3/exoplayer/src/main/kotlin/mapping/video.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import androidx.media3.common.MimeTypes
import androidx.media3.common.util.UnstableApi

@OptIn(UnstableApi::class)
fun getFfmpegVideoMimeType(codec: String) = codec.lowercase().let { codec ->
fun getFfmpegVideoMimeType(codec: String, fallback: String = codec) = codec.lowercase().let { codec ->
ffmpegVideoMimeTypes[codec]
?: MimeTypes.getVideoMediaMimeType(codec)
?: codec
?: fallback
}

@OptIn(UnstableApi::class)
Expand Down

0 comments on commit 528731e

Please sign in to comment.