YouTube mpv hook
A userscript and protocol definition for watching YouTube videos in mpv.
It has been more and more trendy to hate on YouTube’s approach to advertising. Sure, no one has ever liked ads, but with their recent firmer push of Premium, ad blocker blocking, and in-stream adverts, it’s been more popular than ever to not like ads. Additionally, their frontend is slow, and has only been getting slower. Whether it’s crap programming or just an “oops” (thread), it’s often unpleasant to wait for the piece of shit to load. For this reason, for some time now, I’ve had a hook to open YT links in mpv. mpv is a somewhat minimal media player, but its style does not take away from its functionality. Most importantly, if yt-dlp is installed, it can directly open YouTube links.
The first part of this setup is a userscript which hooks clicks in YT links.
This one has been written by Flash, whom I stole this setup from.
It captures link clicks and, if the link points to YouTube, redirects the event
to a new link with mpv:// prepended. This will outsource the request from
the browser to a locally installed application holding a handler for the (made
up) mpv protocol.
// ==UserScript==
// @name mpv friend
// @version 1
// @grant none
// @match *://youtube.com/*
// @match *://www.youtube.com/*
// @match *://*
// ==/UserScript==
/** mpvfriend.reg
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\mpv]
@="URL:mpv"
"URL Protocol"=""
"DefaultIcon"="\"C:\\Users\\flash\\Software\\mpv\\mpv.exe\",1"
[HKEY_CLASSES_ROOT\mpv\shell]
[HKEY_CLASSES_ROOT\mpv\shell\open]
[HKEY_CLASSES_ROOT\mpv\shell\open\command]
@="\"C:\\Users\\flash\\Software\\mpv\\open.bat\" \"%1\""
**/
/** open.bat
@echo off
set URL=%1
mpv %URL:~0,1%%URL:~5,255%
**/
window.addEventListener('click', function(ev) {
var target = ev.target;
while(!(target instanceof HTMLAnchorElement)) {
target = target.parentNode;
if(!target)
return;
}
if(!target.href)
return;
var url = new URL(target.href);
if(url.protocol === 'mpv:')
return;
if((url.origin === 'https://www.youtube.com' && url.pathname === '/watch') || (url.origin === "https://youtu.be")) {
ev.preventDefault();
ev.stopPropagation();
var open = document.createElement('a');
open.href = 'mpv://' + url.href
open.target = '_blank';
document.body.appendChild(open);
open.click();
document.body.removeChild(open);
return false;
}
}, true);
If you use Windows, Flash has helpfully provided the necessary registry tweaks
and batch file to set this up yourself, though modifications to paths will
likely be necessary. For a Linux setup, I created something analogous. First,
a protocol handler; this is the contents of a file placed at
~/.local/share/applications/youtube-mpv.desktop. By existing, it provides
that protocol handler for mpv:// links. Remember to modify the path in Exec.
[Desktop Entry]
Type=Application
Name=open link in mpv
Exec=/home/szymek/bin/openmpvurl.sh %u
StartupNotify=false
MimeType=x-scheme-handler/mpv
Next is the script that is started by the handler, and will pass the link to
mpv, after cropping out the appended earlier prefix. This is
~/bin/openmpvurl.sh, as defined in the .desktop file.
It is equivalent to the open.bat file from Flash.
#!/bin/bash
ref=${1#mpv://}
mpv --pause "$ref"
At this point, clicking a YouTube link in your browser should open mpv.
Thanks to Flash for the original implementation and to the yt-dlp team for daring to fight Google and making this possible.
Addendum - configuring mpv for comfortably viewing Internet video
You might want to configure mpv to limit the maximum resolution used.
I have a 1080p screen, and don’t need 4K video, therefore I have a line in
~/.config/mpv/mpv.conf saying
ytdl-format=bestvideo[height<=?1080]+bestaudio/best
Adjust according to your preferences, but this should work fine for you.
For further messing with quality, install the youtube-quality.lua
script. You will be able to use Ctrl+F to pick a quality and codec you prefer,
on the fly.
You might want to install a script for integrating SponsorBlock. I personally use the Minimal one, but that is up to you.
Tangentially related, did you know that YouTube didn’t kill their RSS feeds yet? You don’t need an account to subscribe to creators, just add the feeds to your RSS reader. I use SeaMonkey, because I like how it looks and I already use it for my mail.
RSS is not dead!