Documentation Index
Fetch the complete documentation index at: https://mintlify.com/faraasaaay/innertube-v2/llms.txt
Use this file to discover all available pages before exploring further.
YouTubeExtractor is a Kotlin object that resolves YouTube’s stream URL protection. It fetches YouTube’s base.js player script, extracts the signature decipher and n-parameter transform functions, and caches them to disk so subsequent app launches require no network call.
YouTubeExtractor uses Mozilla Rhino — a pure JVM JavaScript engine — to execute the deobfuscation functions. No WebView is required. Scripts execute entirely in JVM memory in milliseconds.Overview
YouTube protects stream URLs in two ways:- Signature cipher — Some format entries have
signatureCipherinstead of a directurl. The signature must be decrypted before the URL becomes valid. nparameter throttling — All stream URLs include ann=query parameter that intentionally limits CDN download speed unless transformed using a JavaScript function from the player.
YouTubeExtractor handles both automatically. The typical call path is:
YouTubeExtractor.cacheDir: File?
context.cacheDir.
- If
null, deobfuscation scripts are held in memory only and must be re-fetched on every cold start. - Scripts are cached as plain text files (
yt_sig_js.txt,yt_n_js.txt, etc.) alongside a timestamp file. - The cache is considered fresh for 24 hours. After that, the next call automatically re-fetches
base.js.
YouTubeExtractor.isReady: Boolean
true if both the signature decipher code and the n-transform code are loaded in memory (either from a prior network fetch or from disk cache). Use this to check initialization state without triggering a network call.
YouTubeExtractor.ensureInitialized()
Application.onCreate() on a background thread so the first song play has zero initialization latency.
What it does:
- Checks for a fresh disk cache — if found, loads scripts and skips the network entirely.
- On cache miss: fetches
https://www.youtube.com/iframe_apito discover the current player JS URL, then downloadsbase.js. - Extracts the signature decipher function and its helper object from the player JS.
- Extracts the
n-parameter transform function. - Compiles both into persistent Rhino
Functionobjects (reused on every call — no re-parsing overhead). - Saves the extracted snippets to disk for the next 24 hours.
initLock. Concurrent calls block and return immediately once the first caller completes — no duplicate network work.
YouTubeExtractor.decryptSignature(s: String): String
s parameter extracted from a signatureCipher query string.
In most cases you will not call this directly — use decryptUrl() instead, which handles full signatureCipher parsing automatically.
Returns the original s string unchanged if decryption fails.
YouTubeExtractor.deobfuscateThrottling(n: String): String
n query parameter value into the deobfuscated version that removes CDN throttling.
In most cases you will not call this directly — use deobfuscateUrlNParam() instead, which extracts and replaces the n parameter within a full URL.
Returns the original n string unchanged if deobfuscation fails.
YouTubeExtractor.decryptUrl(signatureCipher: String): String
signatureCipher (or cipher) query string from a PlayerResponse.StreamingData.Format, decrypts the signature, appends it to the base URL, and then deobfuscates the n parameter.
Parameters:
signatureCipher— The raw value offormat.signatureCipherorformat.cipher.
url, sp (signature parameter name, defaults to "signature"), and s (obfuscated signature).
YouTubeExtractor.deobfuscateUrlNParam(url: String): String
n= parameter in a stream URL (via regex [?&]n=([^&]+)) and replaces it with its deobfuscated value.
- If no
nparameter is present, the URL is returned unchanged. - If deobfuscation fails, the original URL is returned unchanged (no-op on error).
format.url values that do not require signature decryption.
YouTubeExtractor.parseQueryParams(query: String): Map<String, String>
Map<String, String>. Keys and values are URL-decoded.
Caching Behavior
| Scenario | Behavior |
|---|---|
| First cold start (no cache) | Fetches iframe_api → resolves player JS URL → downloads base.js → extracts + saves snippets |
| Cache hit (< 24 hours old) | Loads snippets from disk files — no network call at all |
| Cache stale (≥ 24 hours) | Re-fetches base.js and updates the cache files |
| Player JS URL changed | Automatically re-fetches — detected by comparing the cached URL to the resolved URL |
cacheDir is null | No disk caching; scripts are re-fetched on every cold start |
cacheDir:
| File | Contents |
|---|---|
yt_player_url.txt | Resolved player JS URL |
yt_player_cache_time.txt | Unix timestamp of last cache save (milliseconds) |
yt_sig_js.txt | Signature decipher JavaScript code |
yt_sig_func.txt | Name of the signature decipher function |
yt_n_js.txt | n-parameter transform JavaScript code |
yt_n_func.txt | Name of the n-transform function |