youtube-unthrottle
A fun personal project from the past week: youtube-unthrottle
Usage
youtube-unthrottle is essentially a tiny subset of the functionality provided by more sophisticated tools like yt-dlp, rusty_ytdl. and the OG youtube-dl.
Specifically, youtube-unthrottle extracts the video and audio stream URLs from a YouTube link passed via argv[1] and then prints the results to stdout. This output can be combined with an external video player like mpv:
uri="$(xclip -o)"
x="/tmp/streams.txt"
youtube-unthrottle "$uri" > "$x"
audio="$(head -1 $x)"
video="$(tail -1 $x)"
mpv --title="$uri" --audio-file="$audio" "$video"
Motivation
The main challenge here is that YouTube stream URLs contain parameters that must be deobfuscated using JavaScript fragments supplied elsewhere in the YouTube payload. This is why solving this puzzle requires the use of an embedded JavaScript engine (in this case, Duktape).
That being said, why does this project exist, when tools like yt-dlp already solve this problem (and many others) so completely? The reasons are personal, rather than purely technical:
- gain experience embedding a scripting language within another program
- use the C APIs of libcurl and pcre2 directly, rather than indirectly through a higher-level language’s wrapper
- test-drive the -fanalyzer compiler option
- learn how ASan, LSan, and UBSan behave in the context of a greenfield codebase, especially one where error-handling codepaths are exercised by evolving external inputs on an ongoing basis
Additional day-to-day reasons:
- watch YouTube without crashing my desktop PC due to CPU/GPU thermals
- watch YouTube without triggering the OOM killer
- watch YouTube without jank, tearing, and other visual artifacts
I also like to avoid python3 dependencies on my desktop PC whenever possible, more for fun than anything else.
Platforms
I’ve only tested this on 64-bit Arch Linux so far.
Dependencies
curl
duktape
pcre2
I developed with the following versions of these libaries (though I am currently assuming that many other versions would work equally well):
$ pacman -Q curl duktape pcre2
curl 8.8.0-1
duktape 2.7.0-6
pcre2 10.44-1
Build
make
./build/youtube-unthrottle --help