YouTube-DL playlist web proxy

Run youtube-dl on “watch” URL requests and give playlists with resolved “videoplayback” audio URLs, suitable for most players with streaming support.

The YouTube-DL Web Proxy allows to play YouTube “watch” URLs with any music player that is able to stream (“videoplayback”) audio URLs. In the background, resolving YouTube links into playable files gets provided by youtube-dl. This tool is merely a webserver that will respond with a corresponding M3U playlist such that for example simply http://localhost:8984/watch?v=dQw4w9WgXcQ can be added to any remote playlist/streaming capable player, e.g., MPD or VLC. Links to actual playlists containing multiple videos are supported as well.

Usage

As a single python script, youtube-dl-d starts a webserver and runs youtube-dl on the requests, returning a playlist with audio URLs. Request filenames are ignored, query parameters are appended to https://www.youtube.com/watch?. Only audio URLs will be resolved, all results are returned as a single M3U playlist (which is basically only a text file with one URL per line).

usage: youtube-dl-d.py [-h] [--bind-all] [--port PORTNUM] [--cache-ttl SECONDS] [--preferred-ext EXT]

Start a webserver and run youtube-dl on the requests, returning a playlist with audio URLs.
Request filenames are ignored, query parameters are appended to 'https://www.youtube.com/watch?'.
Only audio URLs will be resolved by youtube-dl, all results are returned as a single M3U playlist.

optional arguments:
  -h, --help           show this help message and exit
  --bind-all           bind to all interfaces instead of localhost only (default: False)
  --port PORTNUM       port to bind to (default: 8984)
  --cache-ttl SECONDS  cache lookups up to this duration in seconds, 0 to disable (default: 3600)
  --preferred-ext EXT  preferred audio file extension, for example m4a (default: None)

For example, when running on port 8984, getting a usable URL for players is simply done by exchanging https://www.youtube.com/ with http://localhost:8984/:

./youtube-dl-d.py --preferred-ext m4a &
wget -q -O - 'http://localhost:8984/watch?v=dQw4w9WgXcQ'
https://rr4---sn-4g5e6nzl.googlevideo.com/videoplayback?expire=128083&ei=QwjcYv2JOgAes0yA&…

Any media player that is able to open remote playlists can thus simply be given http://localhost:8984/watch?v=dQw4w9WgXcQ to play the corresponding YouTube video or playlist audio. Note that the filename (watch) gets completely ignored and can be left out or replaced arbitrarily, for example with something like playlist.m3u.

Installation

Apart from a default python3 environment and youtube-dl, no additional dependencies are needed. If youtube-dl is already installed, the self-contained script can thus be directly executed:

./youtube-dl-d.py -h

If not already done so, the very most recent version of youtube-dl can be installed directly from the GitHub repository via pip – the official PyPI package is quite outdated and the yt-dl.org download page seems to be seized and blocked.

sudo pip install --upgrade --no-deps --force-reinstall git+https://github.com/ytdl-org/youtube-dl@master
sudo install -v -T youtube-dl-d.py /usr/local/bin/youtube-dl-d

The last command moves the script to /usr/local/bin/ such that it can be more easily executed. If you’re using mpd and systemd, you can automatically start it with something like the following /etc/systemd/system/youtube-dl-d.service:

[Unit]
Description=local youtube-dl webserver daemon
StopWhenUnneeded=true
After=mpd.service
BindsTo=mpd.service

[Service]
Type=exec
User=mpd
KillMode=process
Environment=PYTHONUNBUFFERED=TRUE
ExecStart=/usr/local/bin/youtube-dl-d --port 8984

[Install]
WantedBy=mpd.service

Code & Download