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.
So I opened VIM and started scripting a solution. In just a few minutes and a few lines of Bash script, there was it.
Yes, I am actually taking more time (and lines) to write this article than the whole script. And that’s exactly the reason why I’m typing this: to show the power and elegance of the Unix philosophy in action. A few utilities doing small tasks piped into each other, solving somewhat complex problems.
Run it and MPV will pop with a random video from your playlist. Don’t like it? Close MPV (for example hitting Q) and another video will begin to play. Also, whenever a video finishes, another random one will play.
Tired of videos? Just press ctrl-C and you are good to go. Simple as that.
The dependencies are also quite simple:
- youtube-dl: to get the URLs from the playlist.
- jq: to read the JSON file generated by youtube-dl.
- mpv: to play the videos.
You only need to configure two parameters, at the top of the script:
The first one is the playlist ID. By default it is set to
WL, which is your watch later playlist. You can leave it like that or pick another one.
The second is the path to your
cookies.txt. This is only needed if you want to play a private playlist. You can easily obtain this file using an extension on your browser. For example: for Chrome, for Firefox.