feat: Embedded subs in downloaded transcode

This commit is contained in:
nomadics9 2024-07-18 05:20:34 +03:00
parent fbf4c185f0
commit 09427e1de0
3 changed files with 66 additions and 2 deletions

View file

@ -28,6 +28,7 @@ import com.nomadics9.ananas.models.toFindroidTrickplayInfoDto
import com.nomadics9.ananas.models.toFindroidUserDataDto
import com.nomadics9.ananas.repository.JellyfinRepository
import org.jellyfin.sdk.model.api.EncodingContext
import org.jellyfin.sdk.model.api.MediaStreamType
import timber.log.Timber
import java.io.File
import java.util.UUID
@ -118,6 +119,7 @@ class DownloaderImpl(
database.insertSource(source.toFindroidSourceDto(item.id, path.path.orEmpty()))
database.insertUserData(item.toFindroidUserDataDto(jellyfinRepository.getUserId()))
downloadExternalMediaStreams(item, source, storageIndex)
downloadEmbeddedMediaStreams(item, source, storageIndex)
if (trickplayInfo != null) {
downloadTrickplayData(item.id, source.id, trickplayInfo)
}
@ -147,6 +149,7 @@ class DownloaderImpl(
database.insertSource(source.toFindroidSourceDto(item.id, path.path.orEmpty()))
database.insertUserData(item.toFindroidUserDataDto(jellyfinRepository.getUserId()))
downloadExternalMediaStreams(item, source, storageIndex)
downloadEmbeddedMediaStreams(item, source, storageIndex)
if (trickplayInfo != null) {
downloadTrickplayData(item.id, source.id, trickplayInfo)
}
@ -341,6 +344,45 @@ class DownloaderImpl(
}
}
private fun downloadEmbeddedMediaStreams(
item: FindroidItem,
source: FindroidSource,
storageIndex: Int = 0
) {
val storageLocation = context.getExternalFilesDirs(null)[storageIndex]
val subtitleStreams = source.mediaStreams.filter { !it.isExternal && it.type == MediaStreamType.SUBTITLE && it.path != null }
for (mediaStream in subtitleStreams) {
var deliveryUrl = mediaStream.path!!
if (mediaStream.codec == "webvtt") {
deliveryUrl = deliveryUrl.replace("Stream.srt", "Stream.vtt")
}
val id = UUID.randomUUID()
val streamPath = Uri.fromFile(
File(
storageLocation,
"downloads/${item.id}.${source.id}.$id.download"
)
)
database.insertMediaStream(
mediaStream.toFindroidMediaStreamDto(
id,
source.id,
streamPath.path.orEmpty()
)
)
val request = DownloadManager.Request(Uri.parse(deliveryUrl))
.setTitle(mediaStream.title)
.setAllowedOverMetered(appPreferences.downloadOverMobileData)
.setAllowedOverRoaming(appPreferences.downloadWhenRoaming)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN)
.setDestinationUri(streamPath)
val downloadId = downloadManager.enqueue(request)
database.setMediaStreamDownloadId(id, downloadId)
}
}
private suspend fun downloadTrickplayData(
itemId: UUID,
sourceId: String,

View file

@ -577,7 +577,7 @@ constructor(
else -> MimeTypes.TEXT_UNKNOWN
}
)
.setLanguage(mediaStream.language?.ifBlank { "Unknown" })
.setLanguage(mediaStream.language.ifBlank { "Unknown" })
.setLabel("Embedded")
.build()
}

View file

@ -136,7 +136,27 @@ class PlayerViewModel @Inject internal constructor(
} else {
mediaSources[mediaSourceIndex]
}
val externalSubtitles = mediaSource.mediaStreams
val externalSubtitles = if (mediaSource.type == FindroidSourceType.LOCAL) {
mediaSource.mediaStreams
.filter { mediaStream ->
mediaStream.type == MediaStreamType.SUBTITLE && !mediaStream.path.isNullOrBlank()
}
.map { mediaStream ->
ExternalSubtitle(
mediaStream.title,
mediaStream.language,
Uri.parse(mediaStream.path!!),
when (mediaStream.codec) {
"subrip" -> MimeTypes.APPLICATION_SUBRIP
"webvtt" -> MimeTypes.APPLICATION_SUBRIP
"pgs" -> MimeTypes.APPLICATION_PGS
"ass" -> MimeTypes.TEXT_SSA
else -> MimeTypes.TEXT_UNKNOWN
},
)
}
}else {
mediaSource.mediaStreams
.filter { mediaStream ->
mediaStream.isExternal && mediaStream.type == MediaStreamType.SUBTITLE && !mediaStream.path.isNullOrBlank()
}
@ -148,11 +168,13 @@ class PlayerViewModel @Inject internal constructor(
when (mediaStream.codec) {
"subrip" -> MimeTypes.APPLICATION_SUBRIP
"webvtt" -> MimeTypes.APPLICATION_SUBRIP
"pgs" -> MimeTypes.APPLICATION_PGS
"ass" -> MimeTypes.TEXT_SSA
else -> MimeTypes.TEXT_UNKNOWN
},
)
}
}
val trickplayInfo = when (this) {
is FindroidSources -> {
this.trickplayInfo?.get(mediaSource.id)?.let {