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:
- Use
youtube-dl
to get the list of URLs on my watch later playlist. - Shuffle it with
shuf
. - Make
mpv
run each video.
However…
- MPV has a playlist feature. Instead of using
shuf
and callingmpv
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. - 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. Then, replace${cookies_file}
with the path for that file. -
The last argument is the playlist URL; in this case the watch later playlist.
If you already have your cookies file set on your youtube-dl 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 youtube-dl 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:
- My window manager swallows the terminal.
- 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 currently watch some other playlists regularly. Copying and pasting the URLs for each every time would be tedious. Instead, I wrote a dmenu script.
Pressing a keybind on my computer, dmenu shows up with the playlists 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.
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 them 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 (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. 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=${cookies_file}" %s
As I have a keybinding on MPV 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.
Additionally, this entry will allow you to watch individual videos, if you select it on a video page.
Happy watching!