Skip to content

Commit

Permalink
Infer subtitle codec from delivery URL
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Dec 25, 2024
1 parent d349b79 commit df2aeea
Show file tree
Hide file tree
Showing 6 changed files with 28 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,19 @@
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

/**
* Return the media type for the codec found in this media stream. First tries to infer the media type from the streams delivery URL and
* falls back to the original stream codec.
*/
fun getSubtitleMediaStreamCodec(stream: MediaStream): String {
val codec = requireNotNull(stream.codec)
val codecMediaType = getFfmpegSubtitleMimeType(codec, "").ifBlank { null }

val urlSubtitleExtension = stream.deliveryUrl?.toUri()?.lastPathSegment?.split('.')?.last()
val urlExtensionMediaType = urlSubtitleExtension?.let { getFfmpegSubtitleMimeType(it, "") }?.ifBlank { null }

return urlExtensionMediaType ?: codecMediaType ?: urlSubtitleExtension ?: 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 df2aeea

Please sign in to comment.