Watching YouTube playlists on shuffle with MPV

Right now, I have a bunch of videos in my YouTube watch later playlist. After a certain number, browsing through them becomes a pain. So I thought of watching them randomly, and skip whatever didn’t interested me at the moment.

YouTube does have a shuffle button, but it never worked well when I tried it. Also, I wanted to use MPV, which gives me better performance on Linux and a nice experience.

A first approach

Initially, I thought of a very nixy solution, combining some utilities to get the job done:

  1. Use yt-dlp to get the list of URLs on my watch later playlist.
  2. Shuffle it with shuf.
  3. Make mpv run each video.

However…

  1. MPV has a playlist feature. Instead of using shuf and calling mpv for each video, we could let it handle the whole playlist. This would give us more possibilities such as easily toggling shuffle on and off and going to previous videos.
  2. MPV has a yt-dlp hook. If we pass a playlist URL, it will automatically retrieve the list of videos. This could simplify our code further.

Solution

So my solution came to this:

$ mpv --shuffle --ytdl-raw-options="cookies-from-browser=<browser>" "https://youtube.com/playlist?list=WL"

Let’s take a look at the arguments:

  • The first one, --shuffle, is self-explanatory. We want the videos to play randomly.

  • The second argument passes your browser cookies to yt-dlp. This is necessary in the case of a private playlist, which your “watch later” probably is. Replace <browser> with your browser’s name. For example, if you use the Chrome browser, use --ytdl-raw-options="cookies-from-browser=chrome". Check yt-dlp documentation for the supported browsers.

    As an alternative, you can pass a cookies file to yt-dlp. You can easily obtain it using an extension for your browser. Then,use with --ytdl-raw-options="cookies=path/to/cookies.txt", replacing the path to cookies.txt.

  • The last argument is the playlist URL; in this case the watch later playlist.

If you already have your cookies configured on your yt-dlp configuration, or if the playlist is public, you won’t need the second argument, and the command will be as simple as:

$ mpv --shuffle "https://youtube.com/playlist?list=WL"

Just be sure to have MPV and yt-dlp installed.

Usage

Running this, MPV will pop with a random video from your watch later playlist.

To go to the next (random) video, the default MPV keybinds are > and Enter. To go the previous, <. Close MPV with q. You also have the current and previous videos URLs printed in the terminal, so you can easily visit their pages if you need to.

Open the video page with a keybinding

While watching my playlists on MPV, I often want to open the current video in the browser.

MPV makes it easy by printing the URL of the current video in the terminal. However, I can’t make use of that, because:

  1. My window manager swallows the terminal.
  2. If I invoke it via “Open with” or dmenu, I won’t get the terminal output by default.

Besides, it would be nice to have a keybinding for that, instead of clicking the URL.

Implementing this is as simple as dropping a line at MPV’s input.conf or using this plugin.

Invocation with dmenu

Besides “watch later”, I sometimes watch other playlists. Copying and pasting their URLs every time would be tedious. Instead, I wrote a dmenu script.

Pressing a keybind on my computer, dmenu shows all the playlists from my logged account and let me pick one to watch. After that, MPV starts the playback on shuffle mode. This is very convenient and much better than browsing YouTube.

My playlists on dmenu.

If you don’t use dmenu, you can adapt this idea. You just need some kind of menu from which to choose the playlist and call the correct MPV command.

Invocation from browser

Another convenient way to watch different playlists is to invoke MPV directly from the browser.

I do that with a browser extension called Open With. This extension allows you to send the URL of your current tab or a right-clicked link to a program of your choice. Btw, I wrote a whole article about how I use that extension.

Download the extension and add an entry for the command we crafted, replacing the playlist URL with %s:

$ mpv --shuffle --ytdl-raw-options="cookies-from=<browser>" %s

As I have a keybinding on MPV for shuffling and the cookies are taken care by my yt-dlp configuration, I simply use:

$ mpv %s

Now, when you visit a playlist and click the entry, MPV will pop with that playlist on shuffle.

Additionally, this entry will allow you to watch individual videos, if you select it on a video page.

Happy watching!


Updates

  • : Use new `--cookies-from-browser` option; replace youtube-dl with yt-dlp.

  • All posts · Show comments