<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[biolend83]]></title><description><![CDATA[biolend83]]></description><link>https://biolend83.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 28 Nov 2026 01:59:35 GMT</lastBuildDate><atom:link href="https://biolend83.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Implementing SABR in an independent YouTube client]]></title><description><![CDATA[How TubeHaus 1.7.0 turns YouTube’s SABR session into an ordinary file with byte ranges]]></description><link>https://biolend83.hashnode.dev/implementing-sabr-in-an-independent-youtube-client</link><guid isPermaLink="true">https://biolend83.hashnode.dev/implementing-sabr-in-an-independent-youtube-client</guid><category><![CDATA[SelfHosting]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Python]]></category><category><![CDATA[Open Source]]></category><dc:creator><![CDATA[biolend83]]></dc:creator><pubDate>Sat, 21 Nov 2026 01:49:15 GMT</pubDate><content:encoded><![CDATA[<p>YouTube's own web player no longer always gets a plain stream address. For a growing set of videos it holds a SABR session instead: a POST conversation in which the player says where it is, the server sends the next pieces, and a web proof-of-origin token rides along. Without that token, the answers stop after about a minute. Other clients that expect a URL they can range-request simply fail.</p>
<p>TubeHaus is a self-hosted YouTube client (Python server, AGPL-3.0) with native apps for Android, desktop and iPad (GPL-3.0). It is not affiliated with YouTube or Google. In 1.6 the server started talking to YouTube itself and dropped Invidious. In 1.7.0, released 20 November 2026, it added a SABR path so those web-player-only videos play again. The work lives in <code>server/sabr/sabr.mjs</code>, a Deno helper started by <code>server/tubehaus_server.py</code>.</p>
<p>This is an integration, not a from-scratch SABR stack. The helper bundles three MIT libraries: <code>googlevideo</code> (the <code>SabrStream</code> session and <code>buildSabrFormat</code>), <code>bgutils-js</code> (BotGuard and the web proof-of-origin minter), and <code>jsdom</code>. yt-dlp's EJS solver, already in <code>server/ejs</code>, solves the <code>n</code> parameter on the SABR URL. The original piece is what sits around those libraries: a small localhost HTTP service that turns SABR into an ordinary file, so every existing TubeHaus player can keep doing what it already does.</p>
<h2>Prepare, then forget SABR</h2>
<p>When no other client gives plain stream addresses — or when <code>HT_SABR=first</code> — the Python server asks the helper's <code>/prepare?v=</code> endpoint. The helper reads the watch page, takes <code>serverAbrStreamingUrl</code> and the ustreamer config from the player response, and solves <code>n</code>. It then rewrites each adaptive format's URL to <code>/companion/sabr?v=&amp;itag=</code> (and <code>xtags</code> when YouTube lists more than one track under the same itag). Muxed formats have no SABR path and are dropped. The web app, the apps and TVs receive the same shape of answer they always did: a list of files.</p>
<p>Playback is a byte-range GET to <code>/companion/sabr</code>, proxied to the helper's <code>/track</code>. The helper is the only process that speaks SABR.</p>
<h2>Tokens without an account</h2>
<p>A session needs a web proof-of-origin token bound to the video id. The helper mints one with no account and no cookies, following the same outline as googlevideo's downloader example. It fetches YouTube's home page, runs the BotGuard challenge in JSDOM (with a stub 2-D canvas, because JSDOM has none), sends the snapshot to YouTube's integrity-token endpoint, and builds a <code>WebPoMinter</code>. One good minter is reused for many videos until its TTL runs down.</p>
<p>YouTube often turns a fresh JSDOM minter down. The first SABR answer carries a protection status: 1 means the token was taken, 2 means it was not. About half of the minters JSDOM produces are rejected in practice. A rejected one is thrown away immediately and the fetch is retried; a good one is kept. Captions for these videos are fetched the same way, with a token on the timedtext request, because those caption addresses expire without one.</p>
<h2>A format is a sparse file</h2>
<p>Each SABR piece says where it belongs in YouTube's file (a byte offset) and, for media segments, the time it starts. The helper writes the bytes at that offset and records <code>[byte, milliseconds]</code> points. HTTP range requests are answered from the file. A range that is not on disk yet starts a SABR fetch at the time that byte plays, interpolated from nearby points, or from the average bitrate if nothing nearby has been seen. If the first media piece still starts after the wanted byte, the helper retries earlier — twenty seconds, then backing off — instead of forcing the player to wait for everything before the seek.</p>
<p>A few practical limits keep the disk in check. The helper reads at most 24 MB ahead of what a player has asked for, because the rest may never be wanted. A format unused for twenty minutes is deleted. Total cache size defaults to 3 GB (<code>HT_SABR_DISK_MB</code>). SABR is on by default (<code>HT_SABR=1</code>) as a fallback, can be tried first, or turned off. Deno is required.</p>
<h2>Why this shape</h2>
<p>The alternative was to teach every player SABR: the Shaka-based web player, the Android and Android TV apps, the desktop apps, the iOS app, and TV browsers. That would have been a much larger change, and a breaking one for anything else that already speaks the server's Invidious-shaped API. By stopping at a file with byte ranges, 1.7.0 leaves those clients alone. The helper is a localhost implementation detail.</p>
<p>The interesting part, if you are building another independent client, is not "reimplement SABR." The libraries above already speak the session. The hard part is staying honest about that, minting tokens that YouTube will actually accept, and presenting the result as something a normal player can seek in.</p>
<p>Source: <a href="https://github.com/biolend83/tubehaus">https://github.com/biolend83/tubehaus</a>
Release: <a href="https://github.com/biolend83/tubehaus/releases/tag/app-v1.7.0">https://github.com/biolend83/tubehaus/releases/tag/app-v1.7.0</a>
Helper: <a href="https://github.com/biolend83/tubehaus/blob/main/server/sabr/sabr.mjs">https://github.com/biolend83/tubehaus/blob/main/server/sabr/sabr.mjs</a></p>
]]></content:encoded></item></channel></rss>