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 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 youtube-dl 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 youtube-dl 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=${cookies_file}" "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 a cookies.txt file to youtube-dl. This is necessary in the case of a private playlist, which your “watch later” probably is. You can easily obtain that file using an extension for your browser - for example for Chrome and for Firefox. Then, replace ${cookies_file} with the path for that file.

If you already have this set on your youtube-dl configuration, skip this argument.

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

Usage

Running this command, 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.

Just be sure to have MPV and youtube-dl installed.

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”, 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 our input.conf or using this plugin.

Invocation from browser

If you want to watch different playlists, it may be convenient to invoke MPV directly from the browser, instead of typing the command and copying and pasting the URLs.

I do that with a browser extension called Open With (for Chrome and for Firefox). This extension allows you to send the URL of your current tab or a right-clicked link to a program of your choice.

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

mpv --shuffle --ytdl-raw-options="cookies=${cookies_file}" %s

As I have a keybinding for shuffling and the cookies are taken care by my youtube-dl configuration, I simply use:

mpv %s

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

Obviously, this entry will also allow you to watch individual videos, if you click it while on a video page.

Happy watching!