-
Notifications
You must be signed in to change notification settings - Fork 0
feat(player): discover external audio tracks #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
core/model/src/main/kotlin/com/miruplay/tv/model/ExternalAudioTracks.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package com.miruplay.tv.model | ||
|
|
||
| import java.net.URLDecoder | ||
| import java.nio.charset.StandardCharsets | ||
|
|
||
| fun buildExternalAudioTracks(paths: List<String>): List<ExternalAudioTrack> = | ||
| paths | ||
| .map(String::trim) | ||
| .filter(String::isNotBlank) | ||
| .distinct() | ||
| .map { path -> | ||
| ExternalAudioTrack( | ||
| language = sidecarLanguageFromPath(path), | ||
| title = externalAudioFileName(path), | ||
| path = path, | ||
| ) | ||
| } | ||
|
|
||
| fun matchingExternalAudioPaths( | ||
| videoPath: String, | ||
| siblingPaths: Iterable<String>, | ||
| ): List<String> { | ||
| val videoStem = MediaPathConventions.stem(videoPath).lowercase() | ||
| if (videoStem.isBlank()) return emptyList() | ||
| return siblingPaths | ||
| .asSequence() | ||
| .filter { candidate -> candidate != videoPath && candidate.isSupportedExternalAudioPath() } | ||
| .filter { candidate -> | ||
| val audioStem = MediaPathConventions.stem(candidate).lowercase() | ||
| audioStem == videoStem || SIDECAR_SUFFIX_SEPARATORS.any { separator -> | ||
| audioStem.startsWith("$videoStem$separator") | ||
| } | ||
| } | ||
| .distinct() | ||
| .sortedBy(String::lowercase) | ||
| .toList() | ||
| } | ||
|
|
||
| internal fun sidecarLanguageFromPath(path: String): String { | ||
| val stem = MediaPathConventions.stem(externalAudioFileName(path)).replace('_', '-') | ||
| val candidate = SIDECAR_LANGUAGE_SUFFIX.find(stem) | ||
| ?.groupValues | ||
| ?.get(1) | ||
| ?.lowercase() | ||
| ?: return "und" | ||
| return when (candidate) { | ||
| "chs", "sc" -> "zh-Hans" | ||
| "cht", "tc" -> "zh-Hant" | ||
| "chi", "zho" -> "zh" | ||
| "eng" -> "en" | ||
| "jpn" -> "ja" | ||
| else -> candidate.split('-', limit = 2).let { parts -> | ||
| if (parts.size == 1) parts[0] | ||
| else parts[0] + "-" + if (parts[1].length == 2) parts[1].uppercase() | ||
| else parts[1].replaceFirstChar(Char::uppercase) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun String.isSupportedExternalAudioPath(): Boolean = | ||
| externalAudioFileName(this).substringAfterLast('.', "").lowercase() in SUPPORTED_EXTERNAL_AUDIO_EXTENSIONS | ||
|
|
||
| private fun externalAudioFileName(path: String): String { | ||
| val pathWithoutUrlSuffix = if ("://" in path) path.substringBefore('?').substringBefore('#') else path | ||
| val encodedName = pathWithoutUrlSuffix.substringAfterLast('/').substringAfterLast('\\') | ||
| return runCatching { | ||
| URLDecoder.decode(encodedName.replace("+", "%2B"), StandardCharsets.UTF_8) | ||
| }.getOrDefault(encodedName) | ||
| } | ||
|
|
||
| private val SUPPORTED_EXTERNAL_AUDIO_EXTENSIONS = setOf( | ||
| "aac", "ac3", "dts", "eac3", "flac", "m4a", "mka", "mp3", "ogg", "opus", "wav", | ||
| ) | ||
| private val SIDECAR_LANGUAGE_SUFFIX = Regex( | ||
| "(?:^|[.\\s\\-\\[])([a-zA-Z]{2,3}(?:-[a-zA-Z]{2,4})?)\\]?$", | ||
| ) | ||
| private val SIDECAR_SUFFIX_SEPARATORS = listOf(".", " ", "_", "-", "[") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
core/model/src/test/kotlin/com/miruplay/tv/model/ExternalAudioTracksTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.miruplay.tv.model | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
|
|
||
| class ExternalAudioTracksTest { | ||
| @Test | ||
| fun `matching audio accepts same stem and language suffixes`() { | ||
| val video = "/Show/Episode 01.mkv" | ||
|
|
||
| assertEquals( | ||
| listOf( | ||
| "/Show/Episode 01.en.flac", | ||
| "/Show/Episode 01.ja.ac3", | ||
| "/Show/Episode 01.m4a", | ||
| "/Show/Episode 01.zh-Hans.mka", | ||
| ), | ||
| matchingExternalAudioPaths( | ||
| video, | ||
| listOf( | ||
| video, | ||
| "/Show/Episode 01.m4a", | ||
| "/Show/Episode 01.ja.ac3", | ||
| "/Show/Episode 01.en.flac", | ||
| "/Show/Episode 01.zh-Hans.mka", | ||
| "/Show/Episode 01.en.srt", | ||
| "/Show/Episode 010.flac", | ||
| "/Show/Episode 02.flac", | ||
| ), | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `audio model derives language without treating title words as language`() { | ||
| val tracks = buildExternalAudioTracks( | ||
| listOf( | ||
| "/Show/Episode 01.zh-Hant.flac", | ||
| "/Show/Episode 01 Commentary.opus", | ||
| ), | ||
| ) | ||
|
|
||
| assertEquals(listOf("zh-Hant", "und"), tracks.map { it.language }) | ||
| assertEquals("Episode 01.zh-Hant.flac", tracks.first().title) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Preserve sidecar language and title metadata through both playback backends.
ExternalAudioTrack.languageis derived during discovery but does not reach the displayed backend track. Exo readsFormat.language, and embedded MPV reads native track metadata. A file namedEpisode.en.ac3without embedded tags will therefore appear asundor unnamed in the audio menu.player-core/src/main/kotlin/com/miruplay/tv/player/ExoPlaybackController.kt#L289-L297: preserve each external track language and title in the Exo audio-track representation.player-core/src/main/kotlin/com/miruplay/tv/player/ExoPlaybackController.kt#L1561-L1561: pass external-audio metadata, not only paths, to embedded MPV.player-mpv-android/src/main/kotlin/is/xyz/mpv/MiruMpvSurfaceView.kt#L276-L278: apply the supplied language and title when adding the native audio track, or merge the metadata when publishingTrackInfo.Add a backend test for a sidecar with a language suffix and no embedded audio tags.
📍 Affects 2 files
player-core/src/main/kotlin/com/miruplay/tv/player/ExoPlaybackController.kt#L289-L297(this comment)player-core/src/main/kotlin/com/miruplay/tv/player/ExoPlaybackController.kt#L1561-L1561player-mpv-android/src/main/kotlin/is/xyz/mpv/MiruMpvSurfaceView.kt#L276-L278🤖 Prompt for AI Agents