From 95dff3bc076e6f4f0a74353e907ffafd80c2b7fa Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sun, 21 Jun 2026 19:51:54 -0400 Subject: [PATCH] Attempt to fix low quality videos, by downgrading tv client version --- CHANGELOG.md | 1 + .../Services/Innertube/Constants.bs | 2 + .../components/Services/Innertube/Context.bs | 25 +++- .../Services/Innertube/InnertubeService.bs | 35 +++++- .../Services/Innertube/PlayerEndpoint.bs | 20 ++-- .../Innertube/TvClientDowngradeExperiment.bs | 108 ++++++++++++++++++ 6 files changed, 173 insertions(+), 18 deletions(-) create mode 100644 playlet-lib/src/components/Services/Innertube/TvClientDowngradeExperiment.bs diff --git a/CHANGELOG.md b/CHANGELOG.md index 342eedec..be7c3e01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Playlists with more than one page of videos no longer stop loading after the first ~100 videos - Report previous-run crashes to diagnostics - Live streams no longer fail with a 502 error on transient segment failures +- Attempt to fix low quality videos, by downgrading tv client version ## [0.47.1] - 2026-06-18 diff --git a/playlet-lib/src/components/Services/Innertube/Constants.bs b/playlet-lib/src/components/Services/Innertube/Constants.bs index 69084520..28e53f17 100644 --- a/playlet-lib/src/components/Services/Innertube/Constants.bs +++ b/playlet-lib/src/components/Services/Innertube/Constants.bs @@ -23,3 +23,5 @@ const INNERTUBE_TV_DEVICE_MODEL = "SmartTV" const INNERTUBE_TV_OS_NAME = "Tizen" const INNERTUBE_TV_OS_VERSION = "4.0.0.2" const INNERTUBE_TV_USER_AGENT = "Mozilla/5.0(SMART-TV; Linux; Tizen 4.0.0.2) AppleWebkit/605.1.15 (KHTML, like Gecko) SamsungBrowser/9.2 TV Safari/605.1.15" + +const INNERTUBE_TV_CLIENT_VERSION_DOWNGRADED = "5.20260114" diff --git a/playlet-lib/src/components/Services/Innertube/Context.bs b/playlet-lib/src/components/Services/Innertube/Context.bs index ff0900b9..410abd8d 100644 --- a/playlet-lib/src/components/Services/Innertube/Context.bs +++ b/playlet-lib/src/components/Services/Innertube/Context.bs @@ -8,6 +8,7 @@ namespace Innertube Web = "WEB" IOS = "IOS" Tv = "TV" + TvDowngraded = "TV_DOWNGRADED" end enum function CreateHeaders(client as Innertube.ClientType) as object @@ -35,6 +36,14 @@ namespace Innertube "x-youtube-client-name": INNERTUBE_TV_CLIENT_NAME "x-youtube-client-version": INNERTUBE_TV_CLIENT_VERSION } + else if client = ClientType.TvDowngraded + return { + "accept": "*/*" + "accept-language": "*" + "user-agent": INNERTUBE_TV_USER_AGENT + "x-youtube-client-name": INNERTUBE_TV_CLIENT_NAME + "x-youtube-client-version": INNERTUBE_TV_CLIENT_VERSION_DOWNGRADED + } else throw "Invalid client type" end if @@ -46,8 +55,8 @@ namespace Innertube contextClient = CreateContextClientWeb(deviceInfo) else if client = ClientType.IOS contextClient = CreateContextClientIOS(deviceInfo) - else if client = ClientType.Tv - contextClient = CreateContextClientTv(deviceInfo) + else if client = ClientType.Tv or client = ClientType.TvDowngraded + contextClient = CreateContextClientTv(deviceInfo, client) else throw "Invalid client type" end if @@ -58,8 +67,8 @@ namespace Innertube accessToken = OptionUtils.Get(options, "accessToken") videoId = OptionUtils.Get(options, "videoId") - useAccessToken = client = Innertube.ClientType.TV and not StringUtils.IsNullOrEmpty(accessToken) - useCtt = client = Innertube.ClientType.TV and IsAssociativeArray(credentialTransferToken) and not StringUtils.IsNullOrEmpty(videoId) + useAccessToken = (client = ClientType.Tv or client = ClientType.TvDowngraded) and not StringUtils.IsNullOrEmpty(accessToken) + useCtt = (client = ClientType.Tv or client = ClientType.TvDowngraded) and IsAssociativeArray(credentialTransferToken) and not StringUtils.IsNullOrEmpty(videoId) context = { "client": contextClient @@ -154,7 +163,11 @@ namespace Innertube } end function - function CreateContextClientTv(deviceInfo as object) as object + function CreateContextClientTv(deviceInfo as object, client as Innertube.ClientType) as object + tvClientVersion = INNERTUBE_TV_CLIENT_VERSION + if client = ClientType.TvDowngraded + tvClientVersion = INNERTUBE_TV_CLIENT_VERSION_DOWNGRADED + end if return { "hl": GetCurrentLocale(deviceInfo) "gl": GetContentRegion(deviceInfo) @@ -163,7 +176,7 @@ namespace Innertube "deviceModel": INNERTUBE_TV_DEVICE_MODEL "userAgent": INNERTUBE_TV_USER_AGENT "clientName": "TVHTML5" - "clientVersion": INNERTUBE_TV_CLIENT_VERSION + "clientVersion": tvClientVersion "osName": INNERTUBE_TV_OS_NAME "osVersion": INNERTUBE_TV_OS_VERSION "originalUrl": "https://www.youtube.com/tv" diff --git a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs index bcb11ebe..352d24ee 100644 --- a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs +++ b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs @@ -9,6 +9,7 @@ import "pkg:/components/Services/Innertube/ResolveUrlEndpoint.bs" import "pkg:/components/Services/Innertube/SearchEndpoint.bs" import "pkg:/components/Services/Innertube/SearchFilters.bs" import "pkg:/components/Services/Innertube/SubscriptionEndpoint.bs" +import "pkg:/components/Services/Innertube/TvClientDowngradeExperiment.bs" import "pkg:/source/services/HttpClient.bs" import "pkg:/source/utils/CancellationUtils.bs" import "pkg:/source/utils/ObjectUtils.bs" @@ -36,14 +37,18 @@ namespace InnertubeService end if playerResponseJson = invalid - client = Innertube.ClientType.Tv + client = Innertube.Experiment.InitialPlayerClient() totalPlayerRequests = 0 triedReloadRefresh = false + downgradeRetryAttempted = false + downgradeRetryArm = invalid + downgradeOutcomeReported = false while true - ' We shouldn't be making more than 2 requests: + ' We shouldn't be making more than 3 requests: ' 1. Make request with TV client. ' 2. Make request, but STS_MISMATCH error -> retry with new STS. - if totalPlayerRequests >= 2 + ' 3. useServerDrivenAbr -> retry with control/treatment client version to recover format URLs. + if totalPlayerRequests >= 3 LogError("Too many player requests, aborting.") return { error: "Too many player requests" @@ -51,7 +56,7 @@ namespace InnertubeService end if totalPlayerRequests += 1 playerRequest = Innertube.CreatePlayerRequest(client, options) - if not StringUtils.IsNullOrEmpty(accessToken) and client = Innertube.ClientType.Tv + if not StringUtils.IsNullOrEmpty(accessToken) and (client = Innertube.ClientType.Tv or client = Innertube.ClientType.TvDowngraded) playerRequest.Header("Authorization", "Bearer " + accessToken) end if playerRequest.Cancellation(cancellation) @@ -68,6 +73,10 @@ namespace InnertubeService end if if not playerResponse.IsSuccess() + if downgradeRetryArm <> invalid and not downgradeOutcomeReported + downgradeOutcomeReported = true + Innertube.Experiment.ReportDowngradeOutcome(downgradeRetryArm, Innertube.Experiment.DowngradeOutcome.RetryFailed, videoId, playerRequest, playerResponse) + end if parsedResponse = { error: playerResponse.ErrorMessage() } @@ -77,6 +86,24 @@ namespace InnertubeService playerResponseJson = playerResponse.Json() + if not downgradeRetryAttempted + downgradePlan = Innertube.Experiment.PlanTvClientDowngradeRetry(playerResponseJson) + if downgradePlan <> invalid + downgradeRetryAttempted = true + downgradeRetryArm = downgradePlan.arm + client = downgradePlan.client + LogInfo("[DOWNGRADE-RETRY] trigger arm=", downgradeRetryArm, " client=", client, " videoId=", videoId) + continue while + end if + else if downgradeRetryArm <> invalid and not downgradeOutcomeReported + downgradeOutcomeReported = true + outcome = Innertube.Experiment.DowngradeOutcome.NotRecovered + if Innertube.Experiment.ResponseHasUsableAdaptive(playerResponseJson) + outcome = Innertube.Experiment.DowngradeOutcome.Recovered + end if + Innertube.Experiment.ReportDowngradeOutcome(downgradeRetryArm, outcome, videoId, playerRequest, playerResponse) + end if + parsedResponse = Innertube.ParseInnertubePlayerResponse(playerRequest, playerResponse, client, cancellation) if parsedResponse.error = invalid diff --git a/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs b/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs index 24334ef3..a1cfa937 100644 --- a/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs +++ b/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs @@ -16,13 +16,13 @@ namespace Innertube accessToken = OptionUtils.Get(options, "accessToken") cpn = InnertubeService.CreateClientPlaybackNonce() - useAccessToken = client = Innertube.ClientType.TV and not StringUtils.IsNullOrEmpty(accessToken) + useAccessToken = (client = Innertube.ClientType.Tv or client = Innertube.ClientType.TvDowngraded) and not StringUtils.IsNullOrEmpty(accessToken) deviceInfo = CreateObject("roDeviceInfo") context = Innertube.CreateContext(client, deviceInfo, options) - if client = Innertube.ClientType.TV + if client = Innertube.ClientType.Tv or client = Innertube.ClientType.TvDowngraded referer = "https://www.youtube.com/tv#/watch?v=" + videoId else referer = "https://www.youtube.com/watch?v=" + videoId @@ -102,7 +102,7 @@ namespace Innertube adaptiveFormats = ValidArray(ObjectUtils.Dig(payload, ["streamingData", "adaptiveFormats"])) formats = ValidArray(ObjectUtils.Dig(payload, ["streamingData", "formats"])) - error = DecipherUrls(formats, adaptiveFormats, playerRequest.signatureTimestamp, cancellation) + error = DecipherUrls(formats, adaptiveFormats, playerRequest.signatureTimestamp, cancellation, client) if error <> invalid parsedResponse["error"] = error.error if error.DoesExist("errorCode") @@ -499,7 +499,7 @@ namespace Innertube return json["sts"] end function - function DecipherUrls(formats as object, adaptiveFormats as object, signatureTimestamp as dynamic, cancellation as object) as object + function DecipherUrls(formats as object, adaptiveFormats as object, signatureTimestamp as dynamic, cancellation as object, client = invalid as dynamic) as object if signatureTimestamp = invalid ' No need to decipher if signatureTimestamp is not available return invalid @@ -581,7 +581,7 @@ namespace Innertube for i = 0 to decipheredFormats.Count() - 1 fmt = formats[i] decipheredFmt = decipheredFormats[i] - fmt["url"] = ResetUrlClientVersion(decipheredFmt["url"]) + fmt["url"] = ResetUrlClientVersion(decipheredFmt["url"], client) end for decipheredAdaptiveFormats = json["adaptiveFormats"] @@ -595,7 +595,7 @@ namespace Innertube for i = 0 to decipheredAdaptiveFormats.Count() - 1 fmt = adaptiveFormats[i] decipheredFmt = decipheredAdaptiveFormats[i] - fmt["url"] = ResetUrlClientVersion(decipheredFmt["url"]) + fmt["url"] = ResetUrlClientVersion(decipheredFmt["url"], client) end for return invalid end function @@ -603,7 +603,7 @@ namespace Innertube ' YouTube.js sets cver to hardcoded values that might be different from ' What's used in Playlet. There's no indication that this makes a difference, ' but it makes sense to keep it consistent. - function ResetUrlClientVersion(url as dynamic) as dynamic + function ResetUrlClientVersion(url as dynamic, requestClient = invalid as dynamic) as dynamic if StringUtils.IsNullOrEmpty(url) return url end if @@ -611,7 +611,11 @@ namespace Innertube queryComponents = UrlUtils.ParseQueryComponents(url) client = ValidString(queryComponents["c"]) if client = "TVHTML5" - queryComponents["cver"] = INNERTUBE_TV_CLIENT_VERSION + if requestClient = Innertube.ClientType.TvDowngraded + queryComponents["cver"] = INNERTUBE_TV_CLIENT_VERSION_DOWNGRADED + else + queryComponents["cver"] = INNERTUBE_TV_CLIENT_VERSION + end if else if client = "IOS" queryComponents["cver"] = INNERTUBE_IOS_CLIENT_VERSION else if client = "WEB" diff --git a/playlet-lib/src/components/Services/Innertube/TvClientDowngradeExperiment.bs b/playlet-lib/src/components/Services/Innertube/TvClientDowngradeExperiment.bs new file mode 100644 index 00000000..57486084 --- /dev/null +++ b/playlet-lib/src/components/Services/Innertube/TvClientDowngradeExperiment.bs @@ -0,0 +1,108 @@ +' TV client-version downgrade experiment (recover VOD per-format URLs withheld under useServerDrivenAbr). +' Self-contained so it can be ripped out wholesale once the experiment concludes: delete this file and the +' call sites in InnertubeService.GetVideoMetadata (InitialPlayerClient, PlanTvClientDowngradeRetry, +' ResponseHasUsableAdaptive, ReportDowngradeOutcome) plus the downgradeRetry* / downgradeOutcomeReported locals, +' then drop ClientType.TvDowngraded + its branches if the downgrade isn't kept. +' +' DEBUG-only (compiled out of prod by #if DEBUG): +' DEBUG_FORCE_DOWNGRADED_CLIENT - downgrade the FIRST player request too, not just the retry. +' DEBUG_FORCE_DOWNGRADE_RETRY - force the downgrade-retry path on every non-live VOD. +#const DEBUG_FORCE_DOWNGRADE_RETRY = false +#const DEBUG_FORCE_DOWNGRADED_CLIENT = false + +import "pkg:/components/Services/Innertube/Context.bs" +import "pkg:/source/utils/ObjectUtils.bs" +import "pkg:/source/utils/StringUtils.bs" + +namespace Innertube.Experiment + + enum DowngradeArm + Control = "control_7x" + Treatment = "treatment_5x" + end enum + + enum DowngradeOutcome + Recovered = "recovered" + NotRecovered = "not_recovered" + RetryFailed = "retry_failed" + end enum + + ' Client for the first player request: Tv normally, TvDowngraded only under the debug flag. + function InitialPlayerClient() as Innertube.ClientType + #if DEBUG + #if DEBUG_FORCE_DOWNGRADED_CLIENT + return Innertube.ClientType.TvDowngraded + #end if + #end if + return Innertube.ClientType.Tv + end function + + ' Plan a one-shot retry only when the player signals useServerDrivenAbr AND no adaptive format has a usable + ' URL (the stuck-at-360p state - SABR can be signaled while URLs are still present, in which case we keep them). + ' Coin-flip control (same version - intermittency baseline) vs treatment (downgrade - the fix). + ' Returns invalid otherwise (URLs present, not SABR, live, or no streamingData). + function PlanTvClientDowngradeRetry(playerResponseJson as object) as object + if not IsAssociativeArray(ObjectUtils.Dig(playerResponseJson, ["streamingData"])) + return invalid + end if + if ValidBool(ObjectUtils.Dig(playerResponseJson, ["videoDetails", "isLive"])) + return invalid + end if + serverDrivenAbr = ValidBool(ObjectUtils.Dig(playerResponseJson, ["playerConfig", "mediaCommonConfig", "useServerDrivenAbr"])) + shouldRetry = serverDrivenAbr and not ResponseHasUsableAdaptive(playerResponseJson) + #if DEBUG + #if DEBUG_FORCE_DOWNGRADE_RETRY + shouldRetry = true + #end if + #end if + if not shouldRetry + return invalid + end if + arm = DowngradeArm.Control + retryClient = Innertube.ClientType.Tv + if Rnd(0) < 0.5 + arm = DowngradeArm.Treatment + retryClient = Innertube.ClientType.TvDowngraded + end if + return { + arm: arm + client: retryClient + } + end function + + ' True if any adaptive format carries a playable field (url / signatureCipher / cipher). + function ResponseHasUsableAdaptive(playerResponseJson as object) as boolean + adaptiveFormats = ObjectUtils.Dig(playerResponseJson, ["streamingData", "adaptiveFormats"]) + if not IsArray(adaptiveFormats) + return false + end if + for each format in adaptiveFormats + if IsAssociativeArray(format) and (not StringUtils.IsNullOrEmpty(format["url"]) or not StringUtils.IsNullOrEmpty(format["signatureCipher"]) or not StringUtils.IsNullOrEmpty(format["cipher"])) + return true + end if + end for + return false + end function + + ' One telemetry event per outcome; fingerprint splits GlitchTip into 6 buckets: + ' {control_7x, treatment_5x} x {recovered, not_recovered, retry_failed}. + ' Attaches the retry request + response so each bucket is diagnosable, not just a count. + function ReportDowngradeOutcome(arm as Innertube.Experiment.DowngradeArm, outcome as Innertube.Experiment.DowngradeOutcome, videoId as string, playerRequest as object, playerResponse as object) as void + LogInfo("[DOWNGRADE-RETRY] outcome arm=", arm, " outcome=", outcome, " videoId=", videoId) + LogTelemetryEvent({ + level: "warning" + message: "[DOWNGRADE-RETRY] arm=" + arm + " outcome=" + outcome + fingerprint: ["downgrade-retry", arm, outcome] + attachments: [{ + filename: "request.txt" + content_type: "text/plain" + data: playerRequest.ToCurlCommand() + }, { + filename: "response.json" + content_type: "application/json" + data: playerResponse.Text() + }] + }) + end function + +end namespace