Audio Streaming with mpv + youtube-dl

schwarzeralptraum:

TL;DR: I made a no frills script for streaming audio efficiently. Works great for art streams and music at work. Look down below in the instructions section for using it.

mpv is a media player utility that allows you to play audio and video files locally or online. You can also use it with youtube-dl to play YouTube videos. It’s great because that means you can play YouTube videos without requiring a browser or loading questionable scripts, ads or unnecessary resources from YouTube’s website everytime you just want to view a video, and mpv provides way more playback options. If you’re one of the people that don’t like reading YouTube comments, that’s even better; mpv with youtube-dl won’t load that stuff.


I tend to find I listen to a lot of soundtracks that come off of Youtube if I don’t already have them on disk because I am uncomfortable with signing up and paying for and using a Spotify account to stream music on-demand for privacy reasons and because the selection of music that Spotify offers, ironically enough, is completely inadequate. (Good luck trying to find any number of officially orchestrated/arranged or original obscure classic video game soundtracks.) If I could use mpv to listen to music off of Youtube, it would take a lot of work off of my browser and my computer system, so I wrote this script that streams audio from YouTube. Though potentially, it could even play music streams from other websites as well.

Unfortunately, mpv and youtube-dl by themselves aren’t enough to by-pass GEMA’s censorship, and you would have to use a proxy or Tor on a non-German exit node to do that. Normally, playing the videos from Tor’s browser bundle (TBB) is good enough for that (it’s not as slow as you think it is; Tor has gotten better with that, and now it all just depends on the Tor relay you are running on. They’ve even made it easier to switch relays once you find out it’s not loading your YouTube videos fast enough). However, TBB is still a browser and will still load all those unnecessary scripts, ads and other third party junk (and free spying…no thanks). Fortunately, you can now use another utility called ProxyChains to run mpv through a proxy or Tor.

What my script essentially does is read a list of URLs (mostly from YouTube) and play from them with mpv randomly or one after the other. It will take playlists from YouTube as well as single videos. You can still pause the playback or skip tracks or anything else you can do with the keyboard shortcuts mpv provides. I found the $RANDOM function supplied by bash tends not to be random enough for my tastes (even with a sufficiently large list), so I’ve decided to use the random.org algorithm instead.

If you were in my Picarto stream during the YaYuCo event this year, you may have noticed I ran an earlier version of this script during the stream.

Here are some benefits for using mpv, youtube-dl, Tor and ProxyChains with this script:

  • Free (as in speech, not beer). The amount of freedom also depends on which services you choose to rely on (eg, audio/video hosted on YouTube or your own server, etc.). But that’s probably not too different from picking “free” Linux distros and choosing which software you want to install on them (which may well not be free). That’s entirely up to you.
  • More audio choices. You can find more soundtracks on YouTube or other streaming services than you can on Spotify alone. Whatever youtube-dl and mpv allow you to use.
  • Not restricted by regional censorship (eg, GEMA) since you can run this streaming service over Tor or a proxy of your choice. Also protects your privacy.
  • Great performance and very minimal CPU usage owing to mpv and minimal GUI resources required. mpv by itself takes up usually a measly 1.6% for me and even less for the Tor daemon. Also great for art streams where minimal GUI doesn’t distract from the stream and doesn’t take away too many resources needed for the stream itself.
  • No accounts required. Stream anonymously.
  • Rather than playing YouTube videos as audio playlists directly in your browser, and having to load every single script, ad and every other third-party garbage that Google and other suspicious domains throw your way, you can by-pass all those unnecessary resources and load just the audio file you need. Not to mention browsers can hog up a lot of CPU and resources, especially if you have many tabs open.


Using this script does require a bit of effort though:

  • You have to create the list of URLs yourself. They’re curated so that it plays only what you want it to play, but that curation has to be done by you (or anyone else who wants to create such lists). That’s what happens when you want freedom and choice.
  • Playlists are not easy to maintain because lists are stored as URLs with no easy way of telling what is what in the list.
  • Music currently being streamed does not display the title. You kind of have to just know what songs are being played.


Which means the best use case for this script is for people who want to stream audio in a random order from a curated list that they themselves have spent the time to create who don’t really care what song from that list is currently being played and have enough computer skills to use command line or aren’t afraid to try it. And probably don’t need to do too much heavy maintenance on those lists.

Unfortunately, like with plain vanilla flavored mpv, this script isn’t written with usability in mind, so although I’ve tried to make it as easy as possible to use, you will have to touch the command line to use it. (It’s not that horrible. Really.) The script also isn’t perfect. If you run it with Tor, there are occasional Tor relays that don’t work well with YouTube and may return 429 errors. I’ve tried to get my script to detect this and restart the Tor daemon to get a new relay. It also just sometimes stops with too many errors for reasons I haven’t discovered. It’s also sometimes a bit slow with loading the next URL, but that might just be because of using proxy/Tor, despite Tor being better at streaming YouTube videos than previously. It doesn’t really happen frequently enough for it to be a problem anyways. This script is also written for Unix like systems, and while I haven’t tried it on Linux and wrote it for OSX, it probably will work on Linux anyways with very little editing. It is probably possible to make a batch script version of this that runs on Windows, but it will probably require more time and effort to adapt it than just testing it on Linux would. If you are not averse to trying it or using it, feel free to read on about how to set this all up.


Instructions

The audio streaming script can be downloaded here.

You will need to install mpv, youtube-dl, tor and proxychains-ng.* You can get all of them off of Homebrew on OSX and most Linux distros have package managers that provide them. If you install mpv, youtube-dl might come installed with it as a dependency. Just check to be sure anyways. If you can use youtube-dl in command line, then you have it.

*If you’re not using Tor or proxy, you can skip installing either of them or the steps required to configure them properly. It should just work without them if you tell the script not to use them.


Configuring Tor/TBB

If you already have the Tor Browser Bundle, you won’t need to install tor, as the Tor daemon comes with it. To have it set up properly so that the script can find it, you will need to symlink it because on OSX, the TBB doesn’t do this. You can probably find it under [Tor app location]/Contents/MacOS/Tor/tor.real:

sudo ln -s [Tor app location]/Contents/MacOS/Tor/tor.real /usr/local/bin/tor

If you are using Tor to get past GEMA, you will also have to provide a torrc file that this Tor daemon will use so that it will know to avoid any German exit nodes. You can usually find the usual one used by the TBB under ~/Library/Application Support/TorBrowser-Data/Tor/torrc.
You can reuse it, but copy it to your home directory under the name .torrc so that the Tor daemon can find it. To filter out the German exit nodes, add this line (or add {de} if ExcludeExitNodes is already there. The list is comma separated):

ExcludeExitNodes {de}

If there are other things you would like this Tor daemon to do when you use it with the audio streaming script, feel free to modify them in this torrc file. You can find out more details about this on Tor Stackexchange or the Tor website itself (look under the Configuration File Format and General Options sections). If you copied it from the TorBrowser-Data/Tor folder, also remove the DataDirectory line or comment it out.

You can test that this all works by running the tor command in a terminal window. This would also be useful for finding out what port the Tor daemon is running on, since you will need it to configure ProxyChains correctly. You can find the port in a line that looks like this:


[notice] Opening Socks listener on 127.0.0.1:9050


For the other utilities:

brew install mpv youtube-dl proxychains-ng tor (only include tor if you don’t already have it and need it)
(or apt-get install or pacman -S or choco; whatever your OS uses)

Depending on your Linux distro and the way in which you’ve configured it, you may or may not have curl. If you don’t, install it as well or switch to using wget in the script itself for generating the random index.

After you have installed all these utilities, we need to configure ProxyChains.


Configuring ProxyChains

The ProxyChains configuration file is under /usr/local/Cellar/proxychains-ng/[version]/etc/proxychains.conf if you used Homebrew. By default, ProxyChains will look for Tor under some port number like 9150. To make sure it finds the right one, open the .conf file and go all the way to the end where there is an uncommented line like socks5 127.0.0.1 9150. Change the port number at the end to the one your Tor daemon uses. You can easily test if ProxyChains works by typing something like proxychains4 curl ifconfig.co (Also be sure that your Tor daemon is running for this). If you get back an IP address that isn’t from your computer, then it works. You can also use a different proxy service if you don’t want to use Tor. Change the socks line to the address your proxy uses. I don’t have access to a consistent proxy server, so you’re on your own for that one. But there should be plenty of help available online for how to use ProxyChains with your proxy.

After you have this all set up, you will need a list of URLs for the script to read from. Just go to YouTube and copy and paste the URLs of your favorite music into a plaintext file, and put each URL on a separate line. By default, the script will look for a list called “list” if you don’t specify which file to use for the list, so put in the URLs of the music you will be using the most into that list first. Put this list file in the same directory as the script.

Then try to run the script (from the script directory):

./run.sh

Here’s some other other options you can use with this script:

–no-tor              Stream without tor
-p, –proxy           Stream with proxy (without Tor. You need to set up ProxyChains for this.)
-l, –list            Read list of URLs from a file. By default, the list is a file called “list” in the same directory as this script.
-s, –sequence        Stream the list of URLs in order rather than randomly.

You can use them by adding the flags after the run command:

./run.sh –no-tor -p
stream music with ProxyChains configured to run with another proxy, but without starting the Tor daemon

./run.sh -s -l castlevania
stream music in the order the URLs appear in the list and use the file called “castlevania” for the list

Minor update: Now uses torsocks. Proxychains is okay, but torsocks is better for privacy because it additionally blocks certain requests that proxychains doesn’t. Torsocks will only be used with Tor, so if you aren’t using Tor, this won’t really affect you. If you want to continue using proxychains with Tor, turn on the proxy option without disabling the Tor option.

You still need to get Torsocks though, which you should also be able to find with your regular package manager or Homebrew on OSX. If you want to build it from source, here it is.

Here’s the updated script.