From fbf8e0a499f51d2aed093b3564e548e1d5db3a10 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 30 Jun 2026 16:21:46 -0400 Subject: [PATCH] Attempt to reduce errors in post-live DVR videos --- CHANGELOG.md | 1 + .../Services/Innertube/PlayerEndpoint.bs | 63 +---------- .../Services/Innertube/PostLiveDvrInfo.bs | 106 ++++++++++++++++++ 3 files changed, 109 insertions(+), 61 deletions(-) create mode 100644 playlet-lib/src/components/Services/Innertube/PostLiveDvrInfo.bs diff --git a/CHANGELOG.md b/CHANGELOG.md index f4b316ce..b54072b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Retry condition issue in http client - Attempt to fix "page needs to be reloaded" edge case +- Attempt to reduce errors in post-live DVR videos ## [0.47.5] - 2026-06-28 diff --git a/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs b/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs index 92c05abb..57af3c57 100644 --- a/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs +++ b/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs @@ -4,6 +4,7 @@ import "pkg:/components/Services/Innertube/LiveStreamInfo.bs" import "pkg:/components/Services/Innertube/NodesParser.bs" import "pkg:/components/Services/Innertube/OTFSegmentInfo.bs" import "pkg:/components/Services/Innertube/PoTokens.bs" +import "pkg:/components/Services/Innertube/PostLiveDvrInfo.bs" import "pkg:/source/services/HttpClient.bs" import "pkg:/source/utils/ObjectUtils.bs" import "pkg:/source/utils/Types.bs" @@ -190,7 +191,7 @@ namespace Innertube if isPostLiveDvr LogDebug("Fetching Post-Live DVR info") - GetPostLiveDvrInfo(parsedResponse, adaptiveFormats, playerRequest.cpn, client, cancellation) + GetPostLiveDvrInfo(parsedResponse, adaptiveFormats, playerRequest.cpn, client, cancellation, payload) end if if isOTF @@ -747,64 +748,4 @@ namespace Innertube return false end function - function GetPostLiveDvrInfo(parsedResponse as object, adaptiveFormats as object, cpn as dynamic, client as Innertube.ClientType, cancellation as object) as void - if adaptiveFormats.Count() = 0 - LogWarn("GetPostLiveDvrInfo: No adaptive formats available") - return - end if - - url = adaptiveFormats[0]["url"] - if StringUtils.IsNullOrEmpty(url) - LogWarn("GetPostLiveDvrInfo: URL is empty") - return - end if - - queryComponents = UrlUtils.ParseQueryComponents(url) - queryComponents["rn"] = "0" - queryComponents["sq"] = "0" - if not StringUtils.IsNullOrEmpty(cpn) - queryComponents["cpn"] = cpn - end if - - url = UrlUtils.SetQueryParams(url, queryComponents) - - request = HttpClient.Head(url) - requestHeaders = Innertube.CreateHeaders(client) - requestHeaders["origin"] = "https://www.youtube.com" - requestHeaders["referer"] = "https://www.youtube.com" - requestHeaders["DNT"] = "?1" - request.Headers(requestHeaders) - - request.TryCount(2) - request.Cancellation(cancellation) - response = request.Await() - - if not response.IsSuccess() - LogError("Failed to fetch Post-Live DVR info", response.ErrorMessage()) - return - end if - - headers = response.Headers() - - if not headers.DoesExist("X-Head-Time-Millis") or not headers.DoesExist("X-Head-Seqnum") - msg = "GetPostLiveDvrInfo: Missing Post-Live DVR info in headers" - LogErrorNoTelemetry(msg, url, headers) - LogTelemetryEvent({ - level: "error" - message: msg + `\nVideoID: ${parsedResponse["videoId"]}\nUrl: ${url}\nHeaders: ${FormatJson(headers)}` - fingerprint: [msg] - }) - end if - - durationMs = ValidString(headers["X-Head-Time-Millis"]).ToInt() - segmentCount = ValidString(headers["X-Head-Seqnum"]).ToInt() - - if durationMs > 0 - parsedResponse["postLiveDvrDurationMs"] = durationMs - end if - if segmentCount > 0 - parsedResponse["postLiveDvrSegmentCount"] = segmentCount - end if - end function - end namespace diff --git a/playlet-lib/src/components/Services/Innertube/PostLiveDvrInfo.bs b/playlet-lib/src/components/Services/Innertube/PostLiveDvrInfo.bs new file mode 100644 index 00000000..ad45580a --- /dev/null +++ b/playlet-lib/src/components/Services/Innertube/PostLiveDvrInfo.bs @@ -0,0 +1,106 @@ +import "pkg:/components/Services/Innertube/Context.bs" +import "pkg:/source/services/HttpClient.bs" +import "pkg:/source/utils/StringUtils.bs" +import "pkg:/source/utils/UrlUtils.bs" + +namespace Innertube + + function GetPostLiveDvrInfo(parsedResponse as object, adaptiveFormats as object, cpn as dynamic, client as Innertube.ClientType, cancellation as object, playerResponse as object) as void + if adaptiveFormats.Count() = 0 + LogWarn("GetPostLiveDvrInfo: No adaptive formats available") + return + end if + + formatUrl = adaptiveFormats[0]["url"] + if StringUtils.IsNullOrEmpty(formatUrl) + LogWarn("GetPostLiveDvrInfo: URL is empty") + return + end if + + ' Query the live edge (no sq): that response is Cache-Control: no-cache, always + ' served fresh, so it carries X-Head-*. A finished stream's head is its final + ' segment, so X-Head-Seqnum is the total count and X-Head-Time-Millis the duration. + primary = FetchPostLiveDvrHead(formatUrl, invalid, cpn, client, cancellation) + headers = invalid + + if HasPostLiveDvrHead(primary) + headers = primary.headers + else + ' sq=0 is cacheable, so an edge can return it missing X-Head-* (#359); fall + ' back to it only when the live-edge query misses. + fallback = FetchPostLiveDvrHead(formatUrl, "0", cpn, client, cancellation) + diagnostics = PostLiveDvrDiagnostics(parsedResponse["videoId"], primary, fallback) + if HasPostLiveDvrHead(fallback) + headers = fallback.headers + LogTelemetryEvent({ + level: "warning" + message: `GetPostLiveDvrInfo: no-sq missing head, sq=0 fallback recovered\n${diagnostics}` + fingerprint: ["GetPostLiveDvrInfo: sq=0 fallback recovered"] + }) + else + ' Stuck: neither header path has the head. Attach the full /player response + ' so a fix is findable from telemetry alone - lengthSeconds + targetDurationSec + ' approximate count/duration today, and any other usable field is there too. + ' The blob folds into Sentry extra, keeping the message lean. + LogTelemetryEvent({ + level: "error" + message: `GetPostLiveDvrInfo: both no-sq and sq=0 missing head\n${diagnostics}` + fingerprint: ["GetPostLiveDvrInfo: both no-sq and sq=0 missing head"] + attachments: [{ + filename: "player_response.json" + content_type: "application/json" + data: FormatJson(playerResponse) + }] + }) + return + end if + end if + + durationMs = ValidString(headers["X-Head-Time-Millis"]).ToInt() + segmentCount = ValidString(headers["X-Head-Seqnum"]).ToInt() + + if durationMs > 0 + parsedResponse["postLiveDvrDurationMs"] = durationMs + end if + if segmentCount > 0 + parsedResponse["postLiveDvrSegmentCount"] = segmentCount + end if + end function + + function FetchPostLiveDvrHead(formatUrl as string, sq as dynamic, cpn as dynamic, client as Innertube.ClientType, cancellation as object) as object + queryComponents = UrlUtils.ParseQueryComponents(formatUrl) + queryComponents["rn"] = "0" + if sq <> invalid + queryComponents["sq"] = sq + end if + if not StringUtils.IsNullOrEmpty(cpn) + queryComponents["cpn"] = cpn + end if + url = UrlUtils.SetQueryParams(formatUrl, queryComponents) + + request = HttpClient.Head(url) + requestHeaders = Innertube.CreateHeaders(client) + requestHeaders["origin"] = "https://www.youtube.com" + requestHeaders["referer"] = "https://www.youtube.com" + requestHeaders["DNT"] = "?1" + request.Headers(requestHeaders) + request.TryCount(2) + request.Cancellation(cancellation) + response = request.Await() + + return { + url: url + statusCode: response.StatusCode() + headers: response.Headers() + } + end function + + function HasPostLiveDvrHead(result as object) as boolean + return IsAssociativeArray(result) and IsAssociativeArray(result.headers) and result.headers.DoesExist("X-Head-Time-Millis") and result.headers.DoesExist("X-Head-Seqnum") + end function + + function PostLiveDvrDiagnostics(videoId as dynamic, primary as object, fallback as object) as string + return `VideoID: ${ValidString(videoId)}` + Chr(10) + `URL (no-sq; sq=0 appends &sq=0): ${primary.url}` + Chr(10) + `no-sq HEAD status: ${primary.statusCode.ToStr()} headers: ${FormatJson(primary.headers)}` + Chr(10) + `sq=0 HEAD status: ${fallback.statusCode.ToStr()} headers: ${FormatJson(fallback.headers)}` + end function + +end namespace