rockyourcode: TIL: How to Watch YouTube Videos With mpv and Keyboard Shortcuts
Hello đź‘‹! Thanks for subscribing.
TIL: How to Watch YouTube Videos With mpv and Keyboard Shortcuts
Published on: 2021-01-22
tags: TIL, Unix
Distraction-free YouTube (and other videos) on your computer
The free YouTube version has ads and suggestions what to watch. Those try to keep the user on YouTube’s website.
Tons of useful videos exist. If you’re like me, you’ll soon fall into the rabbit hole and spend too much time on YouTube.
Today I learned how to use a distraction-free method of watching YouTube with mpv and (optionally) Vimium.
Necessary Tools
You’ll need:
- mpv
- youtube-dl
- (optionally) Vimium
Make sure to follow your operating system’s instructions to install those programs.
Vimium is a Google Chrome extension which allows you to navigate websites using keyboard shortcuts. The tool offers you a “mouse-less” experience with Vim key-bindings. This can be a boon because you can browse the web without your hands leaving the keyboard.
Vimium also has a Firefox version.
How to Use mpv
mpv is a free and open source media player. You can start it with the terminal.
The easiest command is this one (run in your shell):
mpv https://www.youtube.com/watch?v=FsQuGplQvrw
mpv starts streaming the video, thanks to its integration with youtube-dl.
That works, but let’s make it better.
Integrating Vimium and mpv
Create a new file called youtube-watch.sh
(or whatever you’d like to call it) in your $PATH
.
#!/bin/bash
# originally by Kris Occhipinti
# https://www.youtube.com/watch?v=FsQuGplQvrw
notify-send -t 3000 “Playing Video” “$(xclip -o)“; mpv –ytdl-format=bestvideo+bestaudio/best –fs –speed=2.5 –af=rubberband=pitch-scale=0.981818181818181 “$(xclip -o)“
Don’t forget to make it executable:
chmod u+x youtube-watch.sh
The script uses notify-send and xclip. notify-send is small utility that can send desktop notifications. xclip can use your clipboard to pass the copied link to mpv.
MacOS users might use xanthia as a drop-in replacement for notify-send. Replace xclip with MacOS’s native pbpaste.
Windows users can use vaskovsky/notify-send. Pasteboard looks like an alternative for pbcopy/pbpaste, so it should also work as an xclip stand-in in the above script.
Let’s take a look at the mpv command in the bash utility:
--ytdl-format
: selects which format to stream. You can look at youtube-dl’s documentation for more info. We use the best format available. This also works for Vimeo or other services that can interface with youtube-dl.--fs
: starts mpv in full-screen mode.--speed=2.5
: automatically starts at 2.5 speed (adjust to your liking).--af=rubberband=pitch-scale=0.981818181818181
: if you speed up a video, you want to retain the pitch. Otherwise the video begins to sound squeaky. I use therubberband
utility (comes with mpv installation) because it gave me the best quality. See this GitHub issue for more info.
You can bind your shell script to a keyboard shortcut. I use i3 and use my mod
key with y
.
Use Vimium to copy a YouTube link. Unfortunately, you’ll still have to visit YouTube’s website.
Hit yf
to open Vimium’s context menu. Select the entry you want to watch. This will copy the link into your clipboard.
Then use your key-binding for the youtube script (in my case mod+y
). VoilĂ .
Credits
My blog post uses a modified version of the steps from the below video by Kris Occhipinti:
Watching Youtube Videos with MPV Youtube dl Vimium on Linux
Thank you for reading my blog posts.