mpv, youtube-dl, and Livestreamer

This guide was originally about Livestreamer but now focuses on mpv.

Guides for both remain.

Livestreamer

This is how you watch a stream with Livestreamer.

Livestreamer is particularly useful when the Twitch website is acting up, or when you want to conserve resources on watching a stream, given that Twitch streams are major hogs.

Installation

Livestreamer requires

You can install Livestreamer in one of two ways

If you already have Python installed, the simplest way to install it is

Then type

pip install -U livestreamer

If this doesn’t work, try

easy_install livestreamer

macOS comes with Python pre-installed, but Windows users will have to either install

Usage

Assuming the channel is cygnatus and quality is source, open your terminal or command prompt and type:

livestreamer twitch.tv/cygnatus best

VLC will now open and show the (live) stream of Cygnatus1 with Source quality.

Update

This setup no longer works without authentication. To set up authentication, type:

livestreamer --twitch-oauth-authenticate

This opens a browser page that isn’t working, but what you need is not on the page; it’s in the url: access_token={your OAuth token}.

Copy the code to your clipboard and create the file:

Open the file and write:

twitch-oauth-token={your OAuth token}

You should no longer receive an error when trying to use livestreamer.

Another option is to use http-header=Client-ID, but I prefer the privacy of an OAuth token.

Here are two other settings I use in livestreamerrc on Windows:

default-stream=best
player="C:\Program File (x86)\MPC-HC\mpc-hc.exe"

Chat

You might also want to read and participate in the stream chat, as you watch it through Livestreamer. I do this by going to this link in Chrome:

twitch.tv/cygnatus/chat?popout=

This opens the chat for the Cygnatus stream.

Then click on the settings hamburger, go to “More tools” and click on “Add to desktop…”, and then “Add”.

This creates a shortcut that opens an independent chat window that I can rescale to fit next to my Livestreamer video. Since most streams are 720p, you should have 360 pixels left on your 1080p monitor to fit the chat.

You can now watch your favourite stream and be active in the chat—just as on the website, but much better.

Bonus tip: If you have BetterTTV installed, you can type /localsub to only show comments from channel subscribers, if you want to tune out the filthy casuals. The filter only applies to the channel and is disabled as soon as you close the browser window.

Update: You can always use the new tc to bring up chats with support for tabs for multiple chats at the same time. You lose out on extensions like BTTV or FrankerFaceZ, however.

mpv

Livestreamer has not been updated for a year, so you might want to look into an alternative—or backup—on the non-zero chance it breaks one day. The most obvious one is mpv.

mpv automatically streams whatever you throw at it which makes it an excellent combination of Livestreamer and youtube-dl. In fact, mpv piggybacks on youtube-dl a lot.

Installation

mpv is one of those programs that don’t come with a neat installer, so we’ll have to do things the old-fashioned way.

Download the mpv Windows build that isn’t the one from SourceForge. This takes you to another list of builds. Get the most recent 64-bit version.

This will download a .7z file. You will need something like 7zip to open the file. Just install the 64-bit .exe 7zip installer at the top of the page.

Open the .7z file and unpack (or select all and copy) the files inside to a new folder like C:\mpv. Put it wherever you want, perhaps in its own folder. I usually just put these things in the root of C:\. If you want to put it in “Program Files”, remember to put it in C:\Program Files\ and not C:\Program Files (x86)\, since x86 refers to 32-bit, and you downloaded the 64-bit version.

You can now run mpv at C:\mpv\mpv.exe, but you won’t be able to use it in your terminal (command prompt) yet.

To do that, you have to add it as an environment variable to your PATH. In English, this means you will teach Windows what mpv refers to: C:\mpv\mpv.exe.

Just open the Start menu and search for “environment variable”. Two hits will show up, one for your system and one for your user. Both basically lead to the same screen, except you’ll have to click the Environment Variables… button on the system one.

I’d show you a screenshot of this, but I’d have to redact too much information.

Now, go to the row with Path and click Edit. If you’re on Windows 10, click New and enter C:\mpv. Windows will automatically figure out the rest.

Some programs require you to use \bin inside the folder instead of the root.

If you’re on an older version of Windows, you’ll have to append the path to one sorry line using ; as a separator like so:

allkindsofpaths;C:\mpv

Don’t forget to hit OK.

You will now be able to type mpv in your terminal. Try copying pasting the link a YouTube video and paste it after like this:

mpv https://www.youtube.com/watch?v=woJrjb91pW4

Pretty dope, right?

No, you’re getting an error? Check out the next part.

Right now, it doesn’t look like mpv supports Twitch’s required Client-Id token like Livestreamer does. It does seem to play Twitch streams just fine anyway, however, when you hand it the full stream URLs:

mpv https://twitch.tv/cygnatus
More installation?

It’s possible that mpv also requires youtube-dl and ffmpeg to be installed separately to work; I’ll admit I haven’t bothered to check.

If you followed the Livestreamer part of this guide, you will already have Python installed. This means you’ll install youtube-dl as easily as this:

pip install -U youtube-dl

Installing ffmpeg has to be done the manual way like we installed mpv.

Head to the ffmpeg website, pick Windows, and click Windows Builds. All the right options should be selected for you in the first palce, but if not, pick the top version with the long name, 64-bit, Static and hit Download.

Unpack and move it to something like C:\ffmpeg so you will have README.txt at C:\ffmpeg\README.txt.

Set up an environment variable for C:\ffmpeg.

You should definitely be ready to go after this. So let’s hear it just one more time; it’s a classic, you can manage:

mpv https://www.youtube.com/watch?v=woJrjb91pW4

ffmpeg and youtube-dl are invaluable software so it’s no bother to install them anyway. Try converting a video to another format by typing:

ffmpeg -i input.mp4 output.mp3

Streaming YouTube and search results

It’s easier to start you on a list of important mpv options. As is directing you to the mpv.conf file to store your configuration in.

Another one important option is ytdl://:

By default, the youtube-dl hook script (enabled by default for mpv CLI) only looks at http URLs. Prefixing an URL with ytdl:// forces it to be always processed by the script. This can also be used to invoke special youtube-dl functionality like playing a video by ID or invoking search.

Keep in mind that you can’t pass youtube-dl command line options by this, and you have to use --ytdl-raw-options instead.

youtube-dl has a ytsearch feature, and this is the way you can access it. If you want to stream audio or video from a YouTube search instead of a URL, you use ytdl://ytsearch:"guren no yumiya" to get your Jaeger on.

I’ve also seen this run with ytdl://ytsearch10 which looks like it plays all of the first ten hits.

You might also have noticed that there is no thumbnail cover image when you stream audio. I don’t think there’s anything you can do about this, as you probably only use an audio stream. When downloading audio with youtube-dl, you can use --embed-thumbnail, but mpv --ytdl-raw-options=embed-thumbnail= didn’t do the trick. --ytdl-raw-options is how you pass youtube-dl options to mpv, and the = at the end is for an option that takes no value like embed-thumbnails.

So no music covers for now.

Configure once and never again

This syntax is obviously annoying to type frequently so you’ll want to write a function to make this simpler. I’ll write one another time and include it here.

This is a lot to process, so let’s apply all this:

mpv --force-window --no-video --ytdl-format=bestaudio ytdl://ytsearch:"guren no yumiya"

This will

This is a mouthful and no one in their right mind would want to type this out more than once a week.

To address this, we’re going to put as much of this in a configuration file.

Open your text editor and save a blank mpv.conf to %APPDATA%\mpv\mpv.conf.

Paste the following:

force-window=yes
ytdl-format=bestvideo+bestaudio/best

If you plan on mostly using this for audio streaming, add

no-video

If you want mpv to load the video, override the config with --vid=auto.

If your settings are very different between situations, you can create a config profile in mpv.conf like so:

[twitch]
vid=auto

Instead of

mpv --vid=auto https://twitch.tv/cygnatus

you can type

mpv --config=twitch https://twitch.tv/cygnatus

You can load multiple profiles at once.

Here is my current Twitch profile:

[twitch]
no-taskbar-progress
vid=auto
ytdl-format=900p60__source__,720p60__source__,900p60,720p60,1080p60__source__,1080p60,best,900,720,1080p

You can also extend profiles within profiles, so if you copy-paste my Twitch profile to your mpv.conf, add profile=twitch to another profile.

List all available formats with youtube-dl -F https://twitch.tv/cygnatus.

mpv also automatically loads extension- and protocol-based configs, but you can check that out in the docs yourself.

Save your config and now run the new and slimmer version of our command:

mpv ytdl://ytsearch:"guren no yumiya"

Much better, right?

This is as simple as gets using just mpv and its config file.

Typing this can seem annoying at first, but you have to compare it to the alternative of

  1. Opening a browser (tab)
  2. Going to YouTube
  3. Typing in the search query
  4. Hitting return
  5. Picking the video you want
  6. Fidgeting with the volume and quality setting

On top of time, you also save resources from running a browser and YouTube video in the background of whatever else you’re doing.

Suddenly, this seems like the simple approach.

To learn more, I recommend the archlinux wiki on mpv. It also mentions a lot of other useful configuration options.

Oh, and

Keyboard shortcuts

Fun with audio devices

If you read my audio guide, you might have some virtual audio cables set up. You may even have set one of them up so you can play music through your microphone input in videogames.

With mpv, you can choose your input device with --audio-device.

But why?

The audio guide also showed you how virtual audio cables gave you a more granular control of audio streams with a virtual audio mixer like VoiceMeeter Banana. Let me refresh your memory a little bit:

VoiceMeeter interface with settings. A lot to take in, I know.

A big problem with controlling audio is that “everything else” always ends up under “Desktop audio”, both in VoiceMeeter and OBS. In many cases, this audio comes from your browser playing YouTube. Because it ends up in “Desktop audio”, we can’t control it granularly as we control our music in under “Music” in our audio mixer.

With mpv --audio-device, however, you can stream your audio in your mpv player while sending the audio to CABLE-A, the virtual audio cable we’ve assigned our music audio stream to. Now you can control the volume as well as to whom and where the music goes. And you can do the same if you want to stream video, of course.

Another advantage for streamers is that you can disable the audio stream for music in your OBS recording settings so your stream can hear the music while the music is stricken from your recordings. Why is this important? Because you won’t run afoul of YouTube’s aggressive Content ID that automatically detects, demonetizes, and sometimes takes down your entire video.

I haven’t played around with this feature myself, but you can see a list of your active audio devices with mpv --audio-device=help.

It’s also possible that you can just specify your microphone as the audio device to play music directly through the audio stream. Just remember to hold push-to-talk if the mic is not open by default in your desired application.

Config all the things

As always, we’ll usually end up saving our future selves a lot of times by using an mpv profile in our mpv.conf for this:

[playlist]
audio-device=whichevervirtualaudiocableyouareusing
keep-open=yes
loop-playlist=no # Other args: integer, `inf`, and `force`
no-video
# playlist-shuffle # Uncomment if you want it to shuffle
resume-playback
save-position-on-quit # Save playback status
# vid=auto # Loads video (and thumbnail vs black window)
ytdl-format=bestaudio

We are dead and live in hell: mpv and playlists

Now you can play your YouTube playlist in mpv while directing the audio to your dedicated virtual audio cable with minimum overhead just by typing:

mpv --profile=playlist https://www.youtube.com/watch?v=OOvcmavsjko&list=PLF7D1E34C52E9BECD

Actually, no, you can’t. If you type that without quotes around the URL, you’ll get this error:

'list' is not recognized as an internal or external command, operable program or batch file.

If you wrap your URL in quotes, the playlist will start! But it will stop by the end of the first song.

There’s actually a decent reason for this. Google who own YouTube want you to play as many videos as possible, just as Facebook do with their godawful autoplaying videos. This racks up the play count which the companies can use to convince advertisers that they will get their money’s worth of exposure by buying ads to show on their platform. get a lot of views on their platform.

As a result, linking to playlists instead of videos is encouraged, which among other things has the annoying consequence of adding a &list= query string to a lot of URL.

The people who made the youtube-dl hook for mpv, ytdl_hook, decided that the default option should be to disable playlists with --no-playlist.

Yes it sucks, but you can hardly blame them.

Big whoop, we just use --yes-playlist, which is a thing that actually exists in youtube-dl. No bueno. Sigh. Deeper sigh. Take a minute to scream out the window or punch a pillow.

First of all, --yes-playlist is a youtube-dl command, not an mpv command. ytdl_hook is what makes mpv talk to youtube-dl, and that’s what passes on everything and lets youtube-dl take it from there. We would have to use ytdl-raw-options=yes-playlist= (remember the =) to have mpv tell youtube-dl in no uncertain terms that we want to play our goddamn playlist. But then youtube-dl takes charge instead of mpv, and we run into other issues that deprive of the granular control we so love.

Fortunately, there’s another way to tell youtube-dl in no uncertain terms how we do things: remove the video query string in the URL so you only hand mpv and youtube-dl a playlist ID. This way, *there is literally no other option than to play our motherfenacking playlist.

You do this by removing everything between the ? and list:

Before and after:

https://www.youtube.com/watch?v=OOvcmavsjko&list=PLF7D1E34C52E9BECD
https://www.youtube.com/watch?list=PLF7D1E34C52E9BECD

This oughta do it:

mpv --profile=playlist https://www.youtube.com/watch?list=PLF7D1E34C52E9BECD

Thank. Fucking. God. It works.

I tried to find a way to play a URL in a config profile by default, but I can’t find the setting in the documentation.

mpv does support --playlist=<filename>, which uses a local, unsafe playlist file. It’s only unsafe if you play a file whose contents you don’t know, and it might fit your workflow better. mpv’s maintainers don’t seem to fond of using it. If you’re unsure and want to use playlists, at least make sure not to open your terminal as admin to let your terminal run roughshod with unchecked power.

Interface decryption

We’re finally out of the woods, but I want to take a second to save you minutes if not hours of trying to skip to the next track in your playlist.

As I was writing this playlist section and gradually losing my mind, I also noticed that I couldn’t for the life of me skip to the next track in my playlist; the skip buttons were all greyed out!

I’ll spare you the harrowing piece of non-fiction to give you the answer that you are probably seeking; turns out you’re not supposed to use those buttons, but these fuckers instead:

Use the arrow buttons in the second row to skip to the next playlist track.

You’re welcome.

(Also just use Enter to skip to the next track.)

Taking a .bat to mpv

Using mpv config profiles has made us lazy, so typing out stuff like this for playlists sucks:

mpv --profile=playlist https://www.youtube.com/watch?list=PLF7D1E34C52E9BECD

One solution that circumvents mpv and the terminal is to open your default playlist or whatever with a batch file. You just just save the following to playlist.bat (or whatever.bat):

mpv --profile=playlist https://www.youtube.com/watch?list=PLF7D1E34C52E9BECD

:: Don't close window when job is done
pause >nul

Batch files can be run just by double-clicking them. Add them to your Start menu or the Taskbar or somewhere else. Your playlist will literally just be one click away.

If you want to be able to load different playlist with the same scripts, we can prompt you to enter the playlist ID (and just the playlist ID) instead:

SET /p PLAYLISTID= "Enter playlist ID, and just the ID: "

mpv https://www.youtube.com/watch?list=%PLAYLISTID%

:: Don't close window when job is done
pause >nul

Copy the playlist ID to your clipboard and paste it with right-click, and voila!


If you tend to only watch a few streamers on Twitch, you can write a .bat file to open the stream’s video in mpv AND the chat:

REM You may or may not need to start the browser first.
REM You can do this here if you want to.

:: Open the chat first so you see the info updates for mpv last
start "Chrome" chrome --new-window https://twitch.tv/cygnatus/chat?popout
:: start microsoft-edge:https://twitch.tv/cygnatus/chat?popout
:: REM Make sure to change this if Firefox is in a different location.
:: start "Firefox" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -new-window https://twitch.tv/cygnatus/chat?popout

mpv --profile=twitch https://twitch.tv/cygnatus
pause >nul

This opens the chat in a new Chrome window by default. start chrome /newwindow also seems to work. I’m sure it can be tweaked a billion ways to your own liking. I’ve also left in the option to open it in Microsoft Edge and Firefox3. Firefox also supports all kinds of options like defining the width and height of the window. Chromium’s options are absurdly extensive.

If Chrome doesn’t work, try installing Chrome Canary and let it do the job. This way, you can also customize the browser so it won’t load with all your crappy extensions. Or you can specify your chrome executable’s destination; you can always find this stuff by

Then you try replacing the unquoted chrome with the executable path, quotes intact, and see what happens.

I am not going to explore every single quirk or edge case. Just hit me up on Twitter if you find something.

Alright, now let’s change it so you can choose the streamer each time:

SET /p CHANNEL= "Enter channel name: "

:: Open the chat first so you see the info updates for mpv last
start "Chrome" chrome --new-window https://twitch.tv/%CHANNEL%/chat?popout
:: start microsoft-edge:https://twitch.tv/%CHANNEL%/chat?popout
:: REM Make sure to change this if Firefox is in a different location.
:: start "Firefox" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -new-window https://twitch.tv/%CHANNEL%/chat?popout

mpv --profile=twitch https://twitch.tv/%CHANNEL%
pause >nul

Now we can save this to twitch.bat.

We’ve reached the point where you load a stream with tailor-made settings and open its chat in a separate browser window just by double-clicking a .bat file.

We have reached peak lazy and peak nerd, so I think I’d better sign off here.

I also know that Streamlink and Streamlink-GUI exist, but I couldn’t get it to work, so I’m not going to vouch for something like that.

But you’re free to give it a shot yourself, of course.

Settings

As always, you can check out my own settings in the GitHub repo for my settings.

  1. That’s me! ↩︎

  2. “Technically, mpv doesn’t have an official GUI, it’s an On-screen controller, blah, blah, blah.” ↩︎

  3. I am so dedicated to this guide and helping you that I downloaded and installed Firefox just to test it. ↩︎