From f475745236941c08d0d3b0a856497dfd37017306 Mon Sep 17 00:00:00 2001 From: LuoH-AN <157256842+LuoH-AN@users.noreply.github.com> Date: Sun, 15 Nov 2026 08:16:00 +0800 Subject: [PATCH 1/5] fix(playback): use an authenticated player client when signed in --- .../kotlinytmusicscraper/extractor/Extractor.android.kt | 4 +++- .../maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt index d7eddb8f1..16f0f248d 100644 --- a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt +++ b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt @@ -33,6 +33,8 @@ actual class Extractor { actual fun logIn(cookie: String?) { ServiceList.YouTube.tokens = cookie ?: "" + // The visionos player request is anonymous; TV requests carry the saved login session. + NewPipe.setYoutubePlayerClient(if (cookie.isNullOrEmpty()) "visionos" else "tv_samsung") } actual fun newPipePlayer(videoId: String): List> { @@ -332,4 +334,4 @@ actual class Extractor { return (DownloadProgress.failed("Error")) } } -} \ No newline at end of file +} diff --git a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt index 34ae04b61..d863f8b90 100644 --- a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt +++ b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt @@ -28,6 +28,8 @@ actual class Extractor { actual fun logIn(cookie: String?) { ServiceList.YouTube.tokens = cookie ?: "" + // The visionos player request is anonymous; TV requests carry the saved login session. + NewPipe.setYoutubePlayerClient(if (cookie.isNullOrEmpty()) "visionos" else "tv_samsung") } actual fun newPipePlayer(videoId: String): List> { @@ -163,4 +165,4 @@ actual class Extractor { filePath: String, track: SongItem, ): DownloadProgress = DownloadProgress.AUDIO_DONE -} \ No newline at end of file +} From 76d6b0a2b271c0a94243f0df57ddd858eedcb9bf Mon Sep 17 00:00:00 2001 From: LuoH-AN <157256842+LuoH-AN@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:00:10 +0800 Subject: [PATCH 2/5] fix(playback): avoid discarding audio on transient or unrelated URL checks --- .../extractor/BraveNewPipeUtils.kt | 45 ++++++++++++------- .../extractor/Extractor.android.kt | 11 ++--- .../extractor/NewPipeUtils.kt | 3 ++ .../maxrave/kotlinytmusicscraper/YouTube.kt | 38 ++++++---------- .../extractor/BraveNewPipeUtils.kt | 45 ++++++++++++------- .../extractor/Extractor.jvm.kt | 11 ++--- .../extractor/NewPipeUtils.kt | 3 ++ 7 files changed, 91 insertions(+), 65 deletions(-) diff --git a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt index 1164f1c65..b564c82a2 100644 --- a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt +++ b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt @@ -2,6 +2,7 @@ package com.maxrave.kotlinytmusicscraper.extractor import com.maxrave.common.ITAG import com.maxrave.kotlinytmusicscraper.models.YouTubeClient +import com.maxrave.logger.Logger import okhttp3.OkHttpClient import okhttp3.RequestBody.Companion.toRequestBody import org.schabi.newpipe.extractor.downloader.Downloader @@ -11,12 +12,8 @@ import org.schabi.newpipe.extractor.exceptions.ReCaptchaException import java.io.IOException import java.net.Proxy -internal fun List>.hasRequiredItags(): Boolean { - val itags = this.mapTo(HashSet()) { it.first } - val hasAudio = ITAG.AUDIO.any { it in itags } - val hasVideo = ITAG.VIDEO.any { it in itags } - return hasAudio && hasVideo -} +internal fun List>.hasPlayableAudio(): Boolean = + any { it.first in ITAG.AUDIO && it.second.isNotBlank() } private val streamHealthCheckClient: OkHttpClient by lazy { OkHttpClient @@ -25,14 +22,19 @@ private val streamHealthCheckClient: OkHttpClient by lazy { } /** - * Pick one random URL whose itag belongs to [ITAG.AUDIO] or [ITAG.VIDEO] and HEAD it. Returns true - * only when the response code is in the 200..299 range. We only health-check those URLs because - * they are the ones the player will actually use; lower-quality extras can stay unverified. + * Check a standard audio URL first. A video or Premium-only format must not decide whether + * the whole song can play. Only a definitive rejection warrants trying another decoder; + * a timeout or server error leaves validation to the player's actual media request. */ -internal fun List>.headCheckRandomStream(): Boolean { - val required = ITAG.AUDIO + ITAG.VIDEO - val candidate = this.filter { it.first in required }.randomOrNull() ?: return false - return runCatching { +internal fun List>.headCheckAudioStream( + videoId: String, + tier: String, +): Boolean { + val audioPriority = listOf(ITAG.AUDIO_OPUS_MEDIUM, ITAG.AUDIO_OPUS_LOW, ITAG.AUDIO_OPUS_HIGH, ITAG.AUDIO_AAC_HIGH) + val candidate = + filter { it.first in ITAG.AUDIO && it.second.isNotBlank() } + .minByOrNull { audioPriority.indexOf(it.first) } ?: return false + return try { val request = okhttp3.Request .Builder() @@ -40,9 +42,22 @@ internal fun List>.headCheckRandomStream(): Boolean { .url(candidate.second) .build() streamHealthCheckClient.newCall(request).execute().use { response -> - response.code in 200..299 + val rejected = response.code in 400..499 && response.code !in setOf(405, 408, 429) + if (response.code !in 200..299) { + Logger.w( + "Extractor", + "PipePipe[$tier] audio HEAD for $videoId itag=${candidate.first}: HTTP ${response.code}, rejected=$rejected", + ) + } + !rejected } - }.getOrDefault(false) + } catch (e: IOException) { + Logger.w( + "Extractor", + "PipePipe[$tier] audio HEAD inconclusive for $videoId itag=${candidate.first}: ${e.javaClass.simpleName}; trying playback", + ) + true + } } class BraveNewPipeDownloaderImpl( diff --git a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt index 16f0f248d..6e3e9bddd 100644 --- a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt +++ b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt @@ -27,6 +27,8 @@ actual class Extractor { actual fun init() { NewPipe.init(newPipeDownloader) + // PipePipe defaults to five seconds, which is too short for slower VPN routes. + ServiceList.YouTube.setLoadingTimeout(30) BraveNewPipe.init(braveNewPipeDownloader) YoutubeApiDecoder.setLocalDecoder(faradayDecoder) } @@ -119,12 +121,11 @@ actual class Extractor { streamsList.mapNotNull { (it.itagItem?.id ?: return@mapNotNull null) to it.content } - if (!pipeResult.hasRequiredItags()) { - Logger.d(TAG, "PipePipe[$tier] missing required itags for $videoId (got=${pipeResult.map { it.first }})") + if (!pipeResult.hasPlayableAudio()) { + Logger.w(TAG, "PipePipe[$tier] missing playable audio itags for $videoId (got=${pipeResult.map { it.first }})") return null } - if (!pipeResult.headCheckRandomStream()) { - Logger.d(TAG, "PipePipe[$tier] stream URL HEAD check failed (non 2xx) for $videoId") + if (!pipeResult.headCheckAudioStream(videoId, tier)) { // A rejected URL is the one symptom of a stale player table: the signature is // well-formed and still wrong, so nothing threw on the way here. Only the on-device // table can go stale, so only that tier is worth invalidating. @@ -133,7 +134,7 @@ actual class Extractor { } val label = if (tier == LOCAL_TIER) faradayDecoder.lastOutcomeLabel else REMOTE_TIER ExtractSource.record(videoId, "PipePipe · $label") - Logger.d(TAG, "extract source=PipePipe[$tier] itags=${pipeResult.map { it.first }} for $videoId") + Logger.i(TAG, "extract source=PipePipe[$tier] itags=${pipeResult.map { it.first }} for $videoId") return pipeResult } catch (e: Throwable) { Logger.w(TAG, "PipePipe[$tier] extractor failed for $videoId: ${e.message}") diff --git a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt index aedf10b89..fdf0cfab2 100644 --- a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt +++ b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt @@ -17,6 +17,7 @@ import okhttp3.OkHttpClient import okhttp3.RequestBody.Companion.toRequestBody import java.io.IOException import java.net.Proxy +import java.util.concurrent.TimeUnit class NewPipeDownloaderImpl( proxy: Proxy?, @@ -25,6 +26,8 @@ class NewPipeDownloaderImpl( OkHttpClient .Builder() .proxy(proxy) + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) .build() @Throws(IOException::class, ReCaptchaException::class) diff --git a/service/kotlinYtmusicScraper/src/commonMain/kotlin/com/maxrave/kotlinytmusicscraper/YouTube.kt b/service/kotlinYtmusicScraper/src/commonMain/kotlin/com/maxrave/kotlinytmusicscraper/YouTube.kt index b462286f6..7d145afdd 100644 --- a/service/kotlinYtmusicScraper/src/commonMain/kotlin/com/maxrave/kotlinytmusicscraper/YouTube.kt +++ b/service/kotlinYtmusicScraper/src/commonMain/kotlin/com/maxrave/kotlinytmusicscraper/YouTube.kt @@ -1376,7 +1376,6 @@ class YouTube { tempRes: PlayerResponse, preferredAudioLanguage: String? = null, ): PlayerResponse? { - val listUrlSig = mutableListOf() var decodedSigResponse: PlayerResponse? var sigResponse: PlayerResponse? Logger.d(TAG, "YouTube TempRes ${tempRes.playabilityStatus}") @@ -1450,32 +1449,21 @@ class YouTube { }, ), ) - listUrlSig.addAll( - ( - decodedSigResponse - .streamingData - ?.adaptiveFormats - ?.mapNotNull { it.url } - ?.toMutableList() ?: mutableListOf() - ).apply { - decodedSigResponse - .streamingData - ?.formats - ?.mapNotNull { it.url } - ?.let { addAll(it) } - }, - ) - listUrlSig.forEach { - Logger.d(TAG, "YouTube NewPipe URL $it") - } - val randomUrl = listUrlSig.randomOrNull() ?: return null - if (listUrlSig.isNotEmpty() && !is403Url(randomUrl)) { - Logger.d(TAG, "YouTube NewPipe Found URL $randomUrl") - return decodedSigResponse - } else { - Logger.d(TAG, "YouTube NewPipe No URL Found") + // PipePipe already checks an audio URL before returning streams. A second random + // HEAD here could discard valid audio because an unrelated video URL is rejected. + val hasAudio = + decodedSigResponse.streamingData?.adaptiveFormats?.any { + it.itag in ITAG.AUDIO && !it.url.isNullOrBlank() + } == true + val hasManifest = + decodedSigResponse.streamingData?.formats?.any { + it.url?.let(::isManifestUrl) == true + } == true + if (!hasAudio && !hasManifest) { + Logger.w(TAG, "NewPipe returned no mapped audio or manifest for $videoId") return null } + return decodedSigResponse } fun isManifestUrl(url: String): Boolean = url.contains(".m3u8") || url.contains(".mpd") || url.contains("manifest") diff --git a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt index 7694410fb..b3a2fced9 100644 --- a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt +++ b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt @@ -2,6 +2,7 @@ package com.maxrave.kotlinytmusicscraper.extractor import com.maxrave.common.ITAG import com.maxrave.kotlinytmusicscraper.models.YouTubeClient +import com.maxrave.logger.Logger import okhttp3.OkHttpClient import okhttp3.RequestBody.Companion.toRequestBody import org.schabi.newpipe.extractor.downloader.Downloader @@ -11,12 +12,8 @@ import org.schabi.newpipe.extractor.exceptions.ReCaptchaException import java.io.IOException import java.net.Proxy -internal fun List>.hasRequiredItags(): Boolean { - val itags = this.mapTo(HashSet()) { it.first } - val hasAudio = ITAG.AUDIO.any { it in itags } - val hasVideo = ITAG.VIDEO.any { it in itags } - return hasAudio && hasVideo -} +internal fun List>.hasPlayableAudio(): Boolean = + any { it.first in ITAG.AUDIO && it.second.isNotBlank() } private val streamHealthCheckClient: OkHttpClient by lazy { OkHttpClient @@ -25,14 +22,19 @@ private val streamHealthCheckClient: OkHttpClient by lazy { } /** - * Pick one random URL whose itag belongs to [ITAG.AUDIO] or [ITAG.VIDEO] and HEAD it. Returns true - * only when the response code is in the 200..299 range. We only health-check those URLs because - * they are the ones the player will actually use; lower-quality extras can stay unverified. + * Check a standard audio URL first. A video or Premium-only format must not decide whether + * the whole song can play. Only a definitive rejection warrants trying another decoder; + * a timeout or server error leaves validation to the player's actual media request. */ -internal fun List>.headCheckRandomStream(): Boolean { - val required = ITAG.AUDIO + ITAG.VIDEO - val candidate = this.filter { it.first in required }.randomOrNull() ?: return false - return runCatching { +internal fun List>.headCheckAudioStream( + videoId: String, + tier: String, +): Boolean { + val audioPriority = listOf(ITAG.AUDIO_OPUS_MEDIUM, ITAG.AUDIO_OPUS_LOW, ITAG.AUDIO_OPUS_HIGH, ITAG.AUDIO_AAC_HIGH) + val candidate = + filter { it.first in ITAG.AUDIO && it.second.isNotBlank() } + .minByOrNull { audioPriority.indexOf(it.first) } ?: return false + return try { val request = okhttp3.Request .Builder() @@ -40,9 +42,22 @@ internal fun List>.headCheckRandomStream(): Boolean { .url(candidate.second) .build() streamHealthCheckClient.newCall(request).execute().use { response -> - response.code in 200..299 + val rejected = response.code in 400..499 && response.code !in setOf(405, 408, 429) + if (response.code !in 200..299) { + Logger.w( + "Extractor", + "PipePipe[$tier] audio HEAD for $videoId itag=${candidate.first}: HTTP ${response.code}, rejected=$rejected", + ) + } + !rejected } - }.getOrDefault(false) + } catch (e: IOException) { + Logger.w( + "Extractor", + "PipePipe[$tier] audio HEAD inconclusive for $videoId itag=${candidate.first}: ${e.javaClass.simpleName}; trying playback", + ) + true + } } class BraveNewPipeDownloaderImpl( diff --git a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt index d863f8b90..8c64af2a2 100644 --- a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt +++ b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt @@ -22,6 +22,8 @@ actual class Extractor { actual fun init() { NewPipe.init(newPipeDownloader) + // PipePipe defaults to five seconds, which is too short for slower VPN routes. + ServiceList.YouTube.setLoadingTimeout(30) BraveNewPipe.init(braveNewPipeDownloader) YoutubeApiDecoder.setLocalDecoder(faradayDecoder) } @@ -118,12 +120,11 @@ actual class Extractor { val manifest = streamInfo.dashMpdUrl.takeIf { !it.isNullOrEmpty() } ?: streamInfo.hlsUrl if (!manifest.isNullOrEmpty()) temp.add(96 to manifest) val pipeResult = temp.toList() - if (!pipeResult.hasRequiredItags()) { - Logger.d(TAG, "PipePipe[$tier] missing required itags for $videoId (got=${pipeResult.map { it.first }})") + if (!pipeResult.hasPlayableAudio()) { + Logger.w(TAG, "PipePipe[$tier] missing playable audio itags for $videoId (got=${pipeResult.map { it.first }})") return null } - if (!pipeResult.headCheckRandomStream()) { - Logger.d(TAG, "PipePipe[$tier] stream URL HEAD check failed (non 2xx) for $videoId") + if (!pipeResult.headCheckAudioStream(videoId, tier)) { // A rejected URL is the one symptom of a stale player table: the signature is // well-formed and still wrong, so nothing threw on the way here. Only the on-device // table can go stale, so only that tier is worth invalidating. @@ -132,7 +133,7 @@ actual class Extractor { } val label = if (tier == LOCAL_TIER) faradayDecoder.lastOutcomeLabel else REMOTE_TIER ExtractSource.record(videoId, "PipePipe · $label") - Logger.d(TAG, "extract source=PipePipe[$tier] itags=${pipeResult.map { it.first }} for $videoId") + Logger.i(TAG, "extract source=PipePipe[$tier] itags=${pipeResult.map { it.first }} for $videoId") return pipeResult } catch (e: Throwable) { Logger.w(TAG, "PipePipe[$tier] extractor failed for $videoId: ${e.message}") diff --git a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt index aedf10b89..fdf0cfab2 100644 --- a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt +++ b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt @@ -17,6 +17,7 @@ import okhttp3.OkHttpClient import okhttp3.RequestBody.Companion.toRequestBody import java.io.IOException import java.net.Proxy +import java.util.concurrent.TimeUnit class NewPipeDownloaderImpl( proxy: Proxy?, @@ -25,6 +26,8 @@ class NewPipeDownloaderImpl( OkHttpClient .Builder() .proxy(proxy) + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) .build() @Throws(IOException::class, ReCaptchaException::class) From 6b1dbf633ae3e2895c536ba346e28fe566c316c5 Mon Sep 17 00:00:00 2001 From: LuoH-AN <157256842+LuoH-AN@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:18:16 +0800 Subject: [PATCH 3/5] fix(playback): keep supplementary web formats from replacing TV audio --- .../extractor/BraveNewPipeUtils.kt | 15 ++++-- .../extractor/Extractor.android.kt | 2 +- .../extractor/NewPipeUtils.kt | 13 +++-- .../extractor/SupplementaryPlayerResponse.kt | 52 +++++++++++++++++++ .../extractor/BraveNewPipeUtils.kt | 15 ++++-- .../extractor/Extractor.jvm.kt | 2 +- .../extractor/NewPipeUtils.kt | 13 +++-- 7 files changed, 94 insertions(+), 18 deletions(-) create mode 100644 service/kotlinYtmusicScraper/src/commonMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/SupplementaryPlayerResponse.kt diff --git a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt index b564c82a2..331716d27 100644 --- a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt +++ b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt @@ -26,7 +26,7 @@ private val streamHealthCheckClient: OkHttpClient by lazy { * the whole song can play. Only a definitive rejection warrants trying another decoder; * a timeout or server error leaves validation to the player's actual media request. */ -internal fun List>.headCheckAudioStream( +internal fun List>.checkAudioStream( videoId: String, tier: String, ): Boolean { @@ -38,23 +38,30 @@ internal fun List>.headCheckAudioStream( val request = okhttp3.Request .Builder() - .head() + .get() + .header("Range", "bytes=0-0") .url(candidate.second) .build() + val clientName = request.url.queryParameter("c") ?: "unknown" streamHealthCheckClient.newCall(request).execute().use { response -> val rejected = response.code in 400..499 && response.code !in setOf(405, 408, 429) if (response.code !in 200..299) { Logger.w( "Extractor", - "PipePipe[$tier] audio HEAD for $videoId itag=${candidate.first}: HTTP ${response.code}, rejected=$rejected", + "PipePipe[$tier] audio probe for $videoId itag=${candidate.first} client=$clientName: HTTP ${response.code}, rejected=$rejected", ) } + if (response.code in 200..299) { + // Read just one byte, then close the response even if the CDN ignores Range. + response.body?.source()?.readByte() + Logger.i("Extractor", "PipePipe[$tier] audio probe passed for $videoId itag=${candidate.first} client=$clientName") + } !rejected } } catch (e: IOException) { Logger.w( "Extractor", - "PipePipe[$tier] audio HEAD inconclusive for $videoId itag=${candidate.first}: ${e.javaClass.simpleName}; trying playback", + "PipePipe[$tier] audio probe inconclusive for $videoId itag=${candidate.first}: ${e.javaClass.simpleName}; trying playback", ) true } diff --git a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt index 6e3e9bddd..9b3878e7c 100644 --- a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt +++ b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt @@ -125,7 +125,7 @@ actual class Extractor { Logger.w(TAG, "PipePipe[$tier] missing playable audio itags for $videoId (got=${pipeResult.map { it.first }})") return null } - if (!pipeResult.headCheckAudioStream(videoId, tier)) { + if (!pipeResult.checkAudioStream(videoId, tier)) { // A rejected URL is the one symptom of a stale player table: the signature is // well-formed and still wrong, so nothing threw on the way here. Only the on-device // table can go stale, so only that tier is worth invalidating. diff --git a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt index fdf0cfab2..d12eb5e32 100644 --- a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt +++ b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt @@ -39,7 +39,7 @@ class NewPipeDownloaderImpl( throw ReCaptchaException("reCaptcha Challenge requested", request.url()) } - return response.toNewPipeResponse() + return response.toNewPipeResponse(request) } @Throws(IOException::class, ReCaptchaException::class) @@ -71,7 +71,7 @@ class NewPipeDownloaderImpl( ) return } - callback?.onSuccess(response.toNewPipeResponse()) + callback?.onSuccess(response.toNewPipeResponse(request)) } catch (e: Exception) { callback?.onError(e) } finally { @@ -83,8 +83,13 @@ class NewPipeDownloaderImpl( return cancellable } - private fun okhttp3.Response.toNewPipeResponse(): Response { - val rawBytes = body?.bytes() ?: ByteArray(0) + private fun okhttp3.Response.toNewPipeResponse(extractorRequest: Request): Response { + val rawBytes = + filterSupplementaryPlayerResponse( + extractorRequest.url(), + extractorRequest.dataToSend(), + body?.bytes() ?: ByteArray(0), + ) return Response( code, message, diff --git a/service/kotlinYtmusicScraper/src/commonMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/SupplementaryPlayerResponse.kt b/service/kotlinYtmusicScraper/src/commonMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/SupplementaryPlayerResponse.kt new file mode 100644 index 000000000..a9393486a --- /dev/null +++ b/service/kotlinYtmusicScraper/src/commonMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/SupplementaryPlayerResponse.kt @@ -0,0 +1,52 @@ +package com.maxrave.kotlinytmusicscraper.extractor + +import com.maxrave.common.ITAG +import com.maxrave.logger.Logger +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonArray +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.contentOrNull +import kotlinx.serialization.json.intOrNull + +/** + * PipePipe scans WEB_REMIX before its configured player and keeps the first similar stream. + * Limit the supplementary response to the Premium audio it was intended to supply, so its + * ordinary web streams cannot replace the TV streams with URLs that require a web PoToken. + */ +internal fun filterSupplementaryPlayerResponse( + requestUrl: String, + requestBody: ByteArray?, + responseBody: ByteArray, +): ByteArray { + if (!requestUrl.startsWith("https://music.youtube.com/youtubei/v1/player") || requestBody == null) { + return responseBody + } + return try { + val request = Json.parseToJsonElement(requestBody.decodeToString()) as? JsonObject ?: return responseBody + val context = request["context"] as? JsonObject + val client = context?.get("client") as? JsonObject + if ((client?.get("clientName") as? JsonPrimitive)?.contentOrNull != "WEB_REMIX") return responseBody + val response = Json.parseToJsonElement(responseBody.decodeToString()) as? JsonObject ?: return responseBody + val streamingData = response["streamingData"] as? JsonObject ?: return responseBody + val premiumItags = setOf(ITAG.AUDIO_AAC_HIGH, ITAG.AUDIO_OPUS_HIGH) + var removed = 0 + val filtered = streamingData.toMutableMap() + for (key in listOf("adaptiveFormats", "formats")) { + val formats = streamingData[key] as? JsonArray ?: continue + val premium = formats.filter { format -> + val itag = ((format as? JsonObject)?.get("itag") as? JsonPrimitive)?.intOrNull + itag != null && itag in premiumItags + } + removed += formats.size - premium.size + filtered[key] = JsonArray(premium) + } + if (removed == 0) return responseBody + val videoId = (request["videoId"] as? JsonPrimitive)?.contentOrNull + Logger.i("Extractor", "WEB_REMIX supplement for $videoId: removed $removed ordinary formats; keeping Premium audio") + JsonObject(response + ("streamingData" to JsonObject(filtered))).toString().encodeToByteArray() + } catch (e: IllegalArgumentException) { + Logger.w("Extractor", "Could not filter WEB_REMIX supplement: ${e::class.simpleName}") + responseBody + } +} diff --git a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt index b3a2fced9..626dfec24 100644 --- a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt +++ b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt @@ -26,7 +26,7 @@ private val streamHealthCheckClient: OkHttpClient by lazy { * the whole song can play. Only a definitive rejection warrants trying another decoder; * a timeout or server error leaves validation to the player's actual media request. */ -internal fun List>.headCheckAudioStream( +internal fun List>.checkAudioStream( videoId: String, tier: String, ): Boolean { @@ -38,23 +38,30 @@ internal fun List>.headCheckAudioStream( val request = okhttp3.Request .Builder() - .head() + .get() + .header("Range", "bytes=0-0") .url(candidate.second) .build() + val clientName = request.url.queryParameter("c") ?: "unknown" streamHealthCheckClient.newCall(request).execute().use { response -> val rejected = response.code in 400..499 && response.code !in setOf(405, 408, 429) if (response.code !in 200..299) { Logger.w( "Extractor", - "PipePipe[$tier] audio HEAD for $videoId itag=${candidate.first}: HTTP ${response.code}, rejected=$rejected", + "PipePipe[$tier] audio probe for $videoId itag=${candidate.first} client=$clientName: HTTP ${response.code}, rejected=$rejected", ) } + if (response.code in 200..299) { + // Read just one byte, then close the response even if the CDN ignores Range. + response.body?.source()?.readByte() + Logger.i("Extractor", "PipePipe[$tier] audio probe passed for $videoId itag=${candidate.first} client=$clientName") + } !rejected } } catch (e: IOException) { Logger.w( "Extractor", - "PipePipe[$tier] audio HEAD inconclusive for $videoId itag=${candidate.first}: ${e.javaClass.simpleName}; trying playback", + "PipePipe[$tier] audio probe inconclusive for $videoId itag=${candidate.first}: ${e.javaClass.simpleName}; trying playback", ) true } diff --git a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt index 8c64af2a2..b0a547884 100644 --- a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt +++ b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt @@ -124,7 +124,7 @@ actual class Extractor { Logger.w(TAG, "PipePipe[$tier] missing playable audio itags for $videoId (got=${pipeResult.map { it.first }})") return null } - if (!pipeResult.headCheckAudioStream(videoId, tier)) { + if (!pipeResult.checkAudioStream(videoId, tier)) { // A rejected URL is the one symptom of a stale player table: the signature is // well-formed and still wrong, so nothing threw on the way here. Only the on-device // table can go stale, so only that tier is worth invalidating. diff --git a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt index fdf0cfab2..d12eb5e32 100644 --- a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt +++ b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/NewPipeUtils.kt @@ -39,7 +39,7 @@ class NewPipeDownloaderImpl( throw ReCaptchaException("reCaptcha Challenge requested", request.url()) } - return response.toNewPipeResponse() + return response.toNewPipeResponse(request) } @Throws(IOException::class, ReCaptchaException::class) @@ -71,7 +71,7 @@ class NewPipeDownloaderImpl( ) return } - callback?.onSuccess(response.toNewPipeResponse()) + callback?.onSuccess(response.toNewPipeResponse(request)) } catch (e: Exception) { callback?.onError(e) } finally { @@ -83,8 +83,13 @@ class NewPipeDownloaderImpl( return cancellable } - private fun okhttp3.Response.toNewPipeResponse(): Response { - val rawBytes = body?.bytes() ?: ByteArray(0) + private fun okhttp3.Response.toNewPipeResponse(extractorRequest: Request): Response { + val rawBytes = + filterSupplementaryPlayerResponse( + extractorRequest.url(), + extractorRequest.dataToSend(), + body?.bytes() ?: ByteArray(0), + ) return Response( code, message, From 8c8b3009f74a8727428cf820e2a56f874b9dd234 Mon Sep 17 00:00:00 2001 From: LuoH-AN <157256842+LuoH-AN@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:33:25 +0800 Subject: [PATCH 4/5] fix(playback): recover audio without metadata and preserve fallback login --- .../extractor/BraveNewPipeUtils.kt | 120 +++++++++++++----- .../extractor/Extractor.android.kt | 19 ++- .../extractor/BraveNewPipeUtils.kt | 120 +++++++++++++----- .../extractor/Extractor.jvm.kt | 20 ++- 4 files changed, 196 insertions(+), 83 deletions(-) diff --git a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt index 331716d27..650d24a8e 100644 --- a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt +++ b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt @@ -3,6 +3,7 @@ package com.maxrave.kotlinytmusicscraper.extractor import com.maxrave.common.ITAG import com.maxrave.kotlinytmusicscraper.models.YouTubeClient import com.maxrave.logger.Logger +import dev.maxrave.pipepipe.extractor.services.youtube.YoutubeParsingHelper import okhttp3.OkHttpClient import okhttp3.RequestBody.Companion.toRequestBody import org.schabi.newpipe.extractor.downloader.Downloader @@ -11,6 +12,7 @@ import org.schabi.newpipe.extractor.downloader.Response import org.schabi.newpipe.extractor.exceptions.ReCaptchaException import java.io.IOException import java.net.Proxy +import java.util.concurrent.TimeUnit internal fun List>.hasPlayableAudio(): Boolean = any { it.first in ITAG.AUDIO && it.second.isNotBlank() } @@ -22,58 +24,106 @@ private val streamHealthCheckClient: OkHttpClient by lazy { } /** - * Check a standard audio URL first. A video or Premium-only format must not decide whether - * the whole song can play. Only a definitive rejection warrants trying another decoder; - * a timeout or server error leaves validation to the player's actual media request. + * Probe ordinary audio first. A rejected rendition does not rule out the other audio URLs. + * After recovering with another rendition, return that audio alone so format selection cannot + * choose an unverified fallback. Inconclusive network checks still allow actual playback. */ -internal fun List>.checkAudioStream( +internal fun List>.playableStreams( videoId: String, tier: String, -): Boolean { +): List> { val audioPriority = listOf(ITAG.AUDIO_OPUS_MEDIUM, ITAG.AUDIO_OPUS_LOW, ITAG.AUDIO_OPUS_HIGH, ITAG.AUDIO_AAC_HIGH) - val candidate = + val candidates = filter { it.first in ITAG.AUDIO && it.second.isNotBlank() } - .minByOrNull { audioPriority.indexOf(it.first) } ?: return false - return try { - val request = - okhttp3.Request - .Builder() - .get() - .header("Range", "bytes=0-0") - .url(candidate.second) - .build() - val clientName = request.url.queryParameter("c") ?: "unknown" - streamHealthCheckClient.newCall(request).execute().use { response -> - val rejected = response.code in 400..499 && response.code !in setOf(405, 408, 429) - if (response.code !in 200..299) { - Logger.w( - "Extractor", - "PipePipe[$tier] audio probe for $videoId itag=${candidate.first} client=$clientName: HTTP ${response.code}, rejected=$rejected", - ) - } - if (response.code in 200..299) { - // Read just one byte, then close the response even if the CDN ignores Range. - response.body?.source()?.readByte() - Logger.i("Extractor", "PipePipe[$tier] audio probe passed for $videoId itag=${candidate.first} client=$clientName") + .sortedBy { audioPriority.indexOf(it.first) } + .distinctBy { it.second } + var hadRejection = false + for (candidate in candidates) { + val accepted = try { + val request = + okhttp3.Request + .Builder() + .get() + .header("Range", "bytes=0-0") + .url(candidate.second) + .build() + val clientName = request.url.queryParameter("c") ?: "unknown" + streamHealthCheckClient.newCall(request).execute().use { response -> + val rejected = response.code in 400..499 && response.code !in setOf(405, 408, 429) + if (response.code !in 200..299) { + Logger.w( + "Extractor", + "PipePipe[$tier] audio probe for $videoId itag=${candidate.first} client=$clientName: HTTP ${response.code}, rejected=$rejected", + ) + } + if (response.code in 200..299) { + response.body?.source()?.readByte() + Logger.i("Extractor", "PipePipe[$tier] audio probe passed for $videoId itag=${candidate.first} client=$clientName") + } + !rejected } - !rejected + } catch (e: IOException) { + Logger.w( + "Extractor", + "PipePipe[$tier] audio probe inconclusive for $videoId itag=${candidate.first}: ${e.javaClass.simpleName}; trying playback", + ) + true + } + if (accepted) { + if (!hadRejection) return this + Logger.i("Extractor", "PipePipe[$tier] recovered audio for $videoId with itag=${candidate.first}") + val audioItags = ITAG.AUDIO + setOf(140, 249) + return filter { it.first !in audioItags || it.second == candidate.second } } - } catch (e: IOException) { - Logger.w( - "Extractor", - "PipePipe[$tier] audio probe inconclusive for $videoId itag=${candidate.first}: ${e.javaClass.simpleName}; trying playback", - ) - true + hadRejection = true } + return emptyList() } class BraveNewPipeDownloaderImpl( proxy: Proxy?, ) : Downloader() { + @Volatile + private var loginCookie: String? = null + + fun logIn(cookie: String?) { + loginCookie = cookie?.takeIf { it.isNotBlank() } + } + private val client = OkHttpClient .Builder() .proxy(proxy) + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .addNetworkInterceptor { chain -> + val request = chain.request() + val builder = request.newBuilder() + val cookie = loginCookie + val trustedHost = request.url.isHttps && request.url.host in setOf("www.youtube.com", "music.youtube.com") + if (trustedHost && cookie != null) { + // Brave's default downloader only sends consent cookies. Keep the fallback + // signed in too, with SAPISIDHASH bound to the actual YouTube origin. + builder.header("Cookie", cookie) + if (request.url.encodedPath.startsWith("/youtubei/v1/")) { + val origin = "https://${request.url.host}" + runCatching { YoutubeParsingHelper.getAuthorizationHeader(cookie, origin) } + .onSuccess { + builder.header("Authorization", it) + builder.header("Origin", origin) + builder.header("X-Origin", origin) + }.onFailure { + Logger.w("Extractor", "BravePipe login header unavailable: ${it.javaClass.simpleName}") + } + } + } else if (!trustedHost) { + // Also enforce this on redirects; credentials must never reach media, + // decoder, dislike or other third-party hosts. + builder.removeHeader("Cookie") + builder.removeHeader("Authorization") + } + chain.proceed(builder.build()) + } .build() @Throws(IOException::class, ReCaptchaException::class) diff --git a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt index 9b3878e7c..8aac4aee9 100644 --- a/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt +++ b/service/kotlinYtmusicScraper/src/androidMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.android.kt @@ -35,6 +35,7 @@ actual class Extractor { actual fun logIn(cookie: String?) { ServiceList.YouTube.tokens = cookie ?: "" + braveNewPipeDownloader.logIn(cookie) // The visionos player request is anonymous; TV requests carry the saved login session. NewPipe.setYoutubePlayerClient(if (cookie.isNullOrEmpty()) "visionos" else "tv_samsung") } @@ -114,9 +115,14 @@ actual class Extractor { tier: String, ): List>? { try { - val streamInfo = - StreamInfo.getInfo(ServiceList.YouTube, "https://music.youtube.com/watch?v=$videoId") - val streamsList = streamInfo.audioStreams + streamInfo.videoStreams + streamInfo.videoOnlyStreams + // The app already has metadata from WEB_REMIX. StreamInfo requires a title before + // extracting streams, so a TV response without a title can discard playable audio. + val streamExtractor = ServiceList.YouTube.getStreamExtractor("https://music.youtube.com/watch?v=$videoId") + streamExtractor.fetchPage() + val streamsList = + streamExtractor.audioStreams + + runCatching { streamExtractor.videoStreams }.getOrDefault(emptyList()) + + runCatching { streamExtractor.videoOnlyStreams }.getOrDefault(emptyList()) val pipeResult = streamsList.mapNotNull { (it.itagItem?.id ?: return@mapNotNull null) to it.content @@ -125,7 +131,8 @@ actual class Extractor { Logger.w(TAG, "PipePipe[$tier] missing playable audio itags for $videoId (got=${pipeResult.map { it.first }})") return null } - if (!pipeResult.checkAudioStream(videoId, tier)) { + val playableResult = pipeResult.playableStreams(videoId, tier) + if (playableResult.isEmpty()) { // A rejected URL is the one symptom of a stale player table: the signature is // well-formed and still wrong, so nothing threw on the way here. Only the on-device // table can go stale, so only that tier is worth invalidating. @@ -134,8 +141,8 @@ actual class Extractor { } val label = if (tier == LOCAL_TIER) faradayDecoder.lastOutcomeLabel else REMOTE_TIER ExtractSource.record(videoId, "PipePipe · $label") - Logger.i(TAG, "extract source=PipePipe[$tier] itags=${pipeResult.map { it.first }} for $videoId") - return pipeResult + Logger.i(TAG, "extract source=PipePipe[$tier] itags=${playableResult.map { it.first }} for $videoId") + return playableResult } catch (e: Throwable) { Logger.w(TAG, "PipePipe[$tier] extractor failed for $videoId: ${e.message}") return null diff --git a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt index 626dfec24..a444f8157 100644 --- a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt +++ b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/BraveNewPipeUtils.kt @@ -3,6 +3,7 @@ package com.maxrave.kotlinytmusicscraper.extractor import com.maxrave.common.ITAG import com.maxrave.kotlinytmusicscraper.models.YouTubeClient import com.maxrave.logger.Logger +import dev.maxrave.pipepipe.extractor.services.youtube.YoutubeParsingHelper import okhttp3.OkHttpClient import okhttp3.RequestBody.Companion.toRequestBody import org.schabi.newpipe.extractor.downloader.Downloader @@ -11,6 +12,7 @@ import org.schabi.newpipe.extractor.downloader.Response import org.schabi.newpipe.extractor.exceptions.ReCaptchaException import java.io.IOException import java.net.Proxy +import java.util.concurrent.TimeUnit internal fun List>.hasPlayableAudio(): Boolean = any { it.first in ITAG.AUDIO && it.second.isNotBlank() } @@ -22,58 +24,106 @@ private val streamHealthCheckClient: OkHttpClient by lazy { } /** - * Check a standard audio URL first. A video or Premium-only format must not decide whether - * the whole song can play. Only a definitive rejection warrants trying another decoder; - * a timeout or server error leaves validation to the player's actual media request. + * Probe ordinary audio first. A rejected rendition does not rule out the other audio URLs. + * After recovering with another rendition, return that audio alone so format selection cannot + * choose an unverified fallback. Inconclusive network checks still allow actual playback. */ -internal fun List>.checkAudioStream( +internal fun List>.playableStreams( videoId: String, tier: String, -): Boolean { +): List> { val audioPriority = listOf(ITAG.AUDIO_OPUS_MEDIUM, ITAG.AUDIO_OPUS_LOW, ITAG.AUDIO_OPUS_HIGH, ITAG.AUDIO_AAC_HIGH) - val candidate = + val candidates = filter { it.first in ITAG.AUDIO && it.second.isNotBlank() } - .minByOrNull { audioPriority.indexOf(it.first) } ?: return false - return try { - val request = - okhttp3.Request - .Builder() - .get() - .header("Range", "bytes=0-0") - .url(candidate.second) - .build() - val clientName = request.url.queryParameter("c") ?: "unknown" - streamHealthCheckClient.newCall(request).execute().use { response -> - val rejected = response.code in 400..499 && response.code !in setOf(405, 408, 429) - if (response.code !in 200..299) { - Logger.w( - "Extractor", - "PipePipe[$tier] audio probe for $videoId itag=${candidate.first} client=$clientName: HTTP ${response.code}, rejected=$rejected", - ) - } - if (response.code in 200..299) { - // Read just one byte, then close the response even if the CDN ignores Range. - response.body?.source()?.readByte() - Logger.i("Extractor", "PipePipe[$tier] audio probe passed for $videoId itag=${candidate.first} client=$clientName") + .sortedBy { audioPriority.indexOf(it.first) } + .distinctBy { it.second } + var hadRejection = false + for (candidate in candidates) { + val accepted = try { + val request = + okhttp3.Request + .Builder() + .get() + .header("Range", "bytes=0-0") + .url(candidate.second) + .build() + val clientName = request.url.queryParameter("c") ?: "unknown" + streamHealthCheckClient.newCall(request).execute().use { response -> + val rejected = response.code in 400..499 && response.code !in setOf(405, 408, 429) + if (response.code !in 200..299) { + Logger.w( + "Extractor", + "PipePipe[$tier] audio probe for $videoId itag=${candidate.first} client=$clientName: HTTP ${response.code}, rejected=$rejected", + ) + } + if (response.code in 200..299) { + response.body?.source()?.readByte() + Logger.i("Extractor", "PipePipe[$tier] audio probe passed for $videoId itag=${candidate.first} client=$clientName") + } + !rejected } - !rejected + } catch (e: IOException) { + Logger.w( + "Extractor", + "PipePipe[$tier] audio probe inconclusive for $videoId itag=${candidate.first}: ${e.javaClass.simpleName}; trying playback", + ) + true } - } catch (e: IOException) { - Logger.w( - "Extractor", - "PipePipe[$tier] audio probe inconclusive for $videoId itag=${candidate.first}: ${e.javaClass.simpleName}; trying playback", - ) - true + if (accepted) { + if (!hadRejection) return this + Logger.i("Extractor", "PipePipe[$tier] recovered audio for $videoId with itag=${candidate.first}") + val audioItags = ITAG.AUDIO + setOf(140, 249) + return filter { it.first !in audioItags || it.second == candidate.second } + } + hadRejection = true } + return emptyList() } class BraveNewPipeDownloaderImpl( proxy: Proxy?, ) : Downloader() { + @Volatile + private var loginCookie: String? = null + + fun logIn(cookie: String?) { + loginCookie = cookie?.takeIf { it.isNotBlank() } + } + private val client = OkHttpClient .Builder() .proxy(proxy) + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .addNetworkInterceptor { chain -> + val request = chain.request() + val builder = request.newBuilder() + val cookie = loginCookie + val trustedHost = request.url.isHttps && request.url.host in setOf("www.youtube.com", "music.youtube.com") + if (trustedHost && cookie != null) { + // Brave's default downloader only sends consent cookies. Keep the fallback + // signed in too, with SAPISIDHASH bound to the actual YouTube origin. + builder.header("Cookie", cookie) + if (request.url.encodedPath.startsWith("/youtubei/v1/")) { + val origin = "https://${request.url.host}" + runCatching { YoutubeParsingHelper.getAuthorizationHeader(cookie, origin) } + .onSuccess { + builder.header("Authorization", it) + builder.header("Origin", origin) + builder.header("X-Origin", origin) + }.onFailure { + Logger.w("Extractor", "BravePipe login header unavailable: ${it.javaClass.simpleName}") + } + } + } else if (!trustedHost) { + // Also enforce this on redirects; credentials must never reach media, + // decoder, dislike or other third-party hosts. + builder.removeHeader("Cookie") + builder.removeHeader("Authorization") + } + chain.proceed(builder.build()) + } .build() @Throws(IOException::class, ReCaptchaException::class) diff --git a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt index b0a547884..a090ee580 100644 --- a/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt +++ b/service/kotlinYtmusicScraper/src/jvmMain/kotlin/com/maxrave/kotlinytmusicscraper/extractor/Extractor.jvm.kt @@ -30,6 +30,7 @@ actual class Extractor { actual fun logIn(cookie: String?) { ServiceList.YouTube.tokens = cookie ?: "" + braveNewPipeDownloader.logIn(cookie) // The visionos player request is anonymous; TV requests carry the saved login session. NewPipe.setYoutubePlayerClient(if (cookie.isNullOrEmpty()) "visionos" else "tv_samsung") } @@ -109,22 +110,27 @@ actual class Extractor { tier: String, ): List>? { try { - val streamInfo = - StreamInfo.getInfo(ServiceList.YouTube, "https://music.youtube.com/watch?v=$videoId") - val streamsList = streamInfo.audioStreams + streamInfo.videoStreams + streamInfo.videoOnlyStreams + // Metadata is already available in the app; a missing TV title must not stop audio extraction. + val streamExtractor = ServiceList.YouTube.getStreamExtractor("https://music.youtube.com/watch?v=$videoId") + streamExtractor.fetchPage() + val streamsList = + streamExtractor.audioStreams + + runCatching { streamExtractor.videoStreams }.getOrDefault(emptyList()) + + runCatching { streamExtractor.videoOnlyStreams }.getOrDefault(emptyList()) val temp = streamsList .mapNotNull { (it.itagItem?.id ?: return@mapNotNull null) to it.content }.toMutableList() - val manifest = streamInfo.dashMpdUrl.takeIf { !it.isNullOrEmpty() } ?: streamInfo.hlsUrl + val manifest = runCatching { streamExtractor.dashMpdUrl.takeIf { it.isNotEmpty() } ?: streamExtractor.hlsUrl }.getOrNull() if (!manifest.isNullOrEmpty()) temp.add(96 to manifest) val pipeResult = temp.toList() if (!pipeResult.hasPlayableAudio()) { Logger.w(TAG, "PipePipe[$tier] missing playable audio itags for $videoId (got=${pipeResult.map { it.first }})") return null } - if (!pipeResult.checkAudioStream(videoId, tier)) { + val playableResult = pipeResult.playableStreams(videoId, tier) + if (playableResult.isEmpty()) { // A rejected URL is the one symptom of a stale player table: the signature is // well-formed and still wrong, so nothing threw on the way here. Only the on-device // table can go stale, so only that tier is worth invalidating. @@ -133,8 +139,8 @@ actual class Extractor { } val label = if (tier == LOCAL_TIER) faradayDecoder.lastOutcomeLabel else REMOTE_TIER ExtractSource.record(videoId, "PipePipe · $label") - Logger.i(TAG, "extract source=PipePipe[$tier] itags=${pipeResult.map { it.first }} for $videoId") - return pipeResult + Logger.i(TAG, "extract source=PipePipe[$tier] itags=${playableResult.map { it.first }} for $videoId") + return playableResult } catch (e: Throwable) { Logger.w(TAG, "PipePipe[$tier] extractor failed for $videoId: ${e.message}") return null From b377958e271f8fae5325629fa762d5c2009547fe Mon Sep 17 00:00:00 2001 From: LuoH-AN <157256842+LuoH-AN@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:39:27 +0800 Subject: [PATCH 5/5] fix(playback): fall back to available audio when Premium twins are absent --- .../maxrave/data/repository/StreamRepositoryImpl.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/data/src/commonMain/kotlin/com/maxrave/data/repository/StreamRepositoryImpl.kt b/data/src/commonMain/kotlin/com/maxrave/data/repository/StreamRepositoryImpl.kt index 083226568..bbd798aca 100644 --- a/data/src/commonMain/kotlin/com/maxrave/data/repository/StreamRepositoryImpl.kt +++ b/data/src/commonMain/kotlin/com/maxrave/data/repository/StreamRepositoryImpl.kt @@ -169,11 +169,9 @@ internal class StreamRepositoryImpl( ?: formatList.find { !it.isAudio && it.url.isNullOrEmpty().not() } val audioTwinItag = ITAG.highQualityTwinOf(itag) val audioFormat = - formatList.find { it.itag == itag } ?: if (audioTwinItag != null) { - formatList.find { it.itag == audioTwinItag } - } else { - formatList.find { it.isAudio && it.url.isNullOrEmpty().not() } - } + formatList.find { it.itag == itag } + ?: audioTwinItag?.let { twin -> formatList.find { it.itag == twin } } + ?: formatList.find { it.isAudio && it.url.isNullOrEmpty().not() } var format = if (isVideo) { videoFormat @@ -410,4 +408,4 @@ internal class StreamRepositoryImpl( } } } -} \ No newline at end of file +}