From 2dc4c15dd8e18a2d47e67420f4de19d83ecb21ba Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 16 Jul 2023 17:11:27 +0200 Subject: [PATCH] Add support for extracting audio track types Co-authored-By: AudricV <74829229+AudricV@users.noreply.github.com> --- .../github/libretube/api/obj/PipedStream.kt | 4 +- .../github/libretube/helpers/DashHelper.kt | 53 +++++++++++++++---- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt b/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt index 3a67084d2..22ba3d385 100644 --- a/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt +++ b/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt @@ -24,7 +24,9 @@ data class PipedStream( val fps: Int? = null, val audioTrackName: String? = null, val audioTrackId: String? = null, - val contentLength: Long = -1 + val contentLength: Long = -1, + val audioTrackType: String? = null, + val audioTrackLocale: String? = null ) { private fun getQualityString(fileName: String): String { return "${fileName}_${quality?.replace(" ", "_")}_$format." + diff --git a/app/src/main/java/com/github/libretube/helpers/DashHelper.kt b/app/src/main/java/com/github/libretube/helpers/DashHelper.kt index 2d8853755..de382facf 100644 --- a/app/src/main/java/com/github/libretube/helpers/DashHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/DashHelper.kt @@ -19,8 +19,10 @@ object DashHelper { private data class AdapSetInfo( val mimeType: String, + val formats: MutableList = mutableListOf(), val audioTrackId: String? = null, - val formats: MutableList = mutableListOf() + val audioTrackType: String? = null, + val audioLocale: String? = null ) fun createManifest( @@ -75,7 +77,6 @@ object DashHelper { adapSetInfos.add( AdapSetInfo( stream.mimeType!!, - null, mutableListOf(stream) ) ) @@ -94,8 +95,10 @@ object DashHelper { adapSetInfos.add( AdapSetInfo( stream.mimeType!!, + mutableListOf(stream), stream.audioTrackId, - mutableListOf(stream) + stream.audioTrackType, + stream.audioTrackLocale ) ) } @@ -105,10 +108,22 @@ object DashHelper { adapSetElement.setAttribute("mimeType", adapSet.mimeType) adapSetElement.setAttribute("startWithSAP", "1") adapSetElement.setAttribute("subsegmentAlignment", "true") + if (adapSet.audioTrackId != null) { adapSetElement.setAttribute("lang", adapSet.audioTrackId.substring(0, 2)) + } else if (adapSet.audioLocale != null) { + adapSetElement.setAttribute("lang", adapSet.audioLocale) } + val roleElement = doc.createElement("Role") + roleElement.setAttribute("schemeIdUri", "urn:mpeg:dash:role:2011") + roleElement.setAttribute( + "value", + getRoleValueFromAudioTrackType(adapSet.audioTrackType) + ) + + adapSetElement.appendChild(roleElement) + val isVideo = adapSet.mimeType.contains("video") if (isVideo) { @@ -162,18 +177,38 @@ object DashHelper { val baseUrl = doc.createElement("BaseURL") baseUrl.appendChild(doc.createTextNode(ProxyHelper.unwrapUrl(stream.url!!, rewriteUrls))) - val segmentBase = doc.createElement("SegmentBase") + representation.appendChild(audioChannelConfiguration) + representation.appendChild(baseUrl) + representation.appendChild(createSegmentBaseElement(doc, stream)) + + return representation + } + + private fun createSegmentBaseElement( + document: Document, + stream: PipedStream + ): Element { + val segmentBase = document.createElement("SegmentBase") segmentBase.setAttribute("indexRange", "${stream.indexStart}-${stream.indexEnd}") - val initialization = doc.createElement("Initialization") + val initialization = document.createElement("Initialization") initialization.setAttribute("range", "${stream.initStart}-${stream.initEnd}") segmentBase.appendChild(initialization) - representation.appendChild(audioChannelConfiguration) - representation.appendChild(baseUrl) - representation.appendChild(segmentBase) + return segmentBase + } - return representation + private fun getRoleValueFromAudioTrackType(audioTrackType: String?): String { + if (audioTrackType == null) { + return "main" + } + + return when (audioTrackType.lowercase()) { + "descriptive" -> "description" + "dubbed" -> "dub" + "original" -> "main" + else -> "alternate" + } } private fun createVideoRepresentation(