konstruct's longform posts

Opening youtube videos in mpv with good UX

Hello! This tutorial will describe how to redirect youtube videos, twitch streams, and other media to mpv, using yt-dlp, on linux, from your browser, in a single middle-click or ctrl-click!

Instructions

step 1/4

Copy the following script to your userscripts manager of choice. Mine is violentmonkey, on librewolf, and most browsers have a userscript manager extension available.

// ==UserScript==
// @name        mpv ytdlp redirect
// @namespace   Violentmonkey Scripts
// @version     1.3.0
//
// @match       https://www.youtube.com/watch?*
// @match       https://youtu.be/*
// @match       https://www.youtube.com/shorts/*
// @match       https://www.youtube.com/embed/*
// @match       https://www.youtube-nocookie.com/*
// @match       https://www.dailymotion.com/video/*
// @match       https://www.instagram.com/reel/*
// @include     /https:\/\/www\.twitch\.tv\/(?!directory|search\?term=|user\/|p).+/
// @grant       window.close
// @license     BSD-2 license
// @run-at      document-start
// ==/UserScript==
window.stop(); // don't load useless site
location.href = location.href.replace(/^https/, 'ytdl') // redirect to whatever handles "ytdl//" protocol
const PreviouslyRanCookie = document.cookie.indexOf('previously_ran=');
if (PreviouslyRanCookie != null) {                   // if there is a cookie
      window.close();                                // close the tab
}
document.cookie = "previously_ran=1"; // once ran, create (or overwrite, if previously ran) the cookie

step 2/4

Go to ~/.local/share/applications and create a new file called 'mpvredirect.desktop'. Paste this inside :

[Desktop Entry]
Type=Application
Name=mpvredirect
Exec=mpv --player-operation-mode=pseudo-gui --quiet -- %U
MimeType=x-scheme-handler/ytdl
NoDisplay=true

This file creates a new app entry to select when picking a default for opening something with.

step 3/4

run sudo xdg-mime default mpvredirect.desktop x-scheme-handler/ytdl in your terminal. If you edit the desktop file, refresh the database with sudo update-desktop-database ~/.local/share/applications/

This makes our mpvredirect.desktop file the default handler for ytdl:// links. It will invoke mpv, which will invoke yt-dlp to download the media for mpv to play it.

step 4/4

open whatever site you want to redirect videos from, middle-click (or control-click) a video to open it in a new tab. Then, in the browser pop-up dialog, check the always allow box, and click allow to let this site open the ytdl link. Next time the script redirects you, it will automatically close the tab.

There, all done!

compatibility

This script sets cookie per site, after its first execution, so that it doesn't close the tab when your browser would ask you to authorize the site to open in mpv. If your browser forgets cookies (librewolf does that by default) it'll simply not close the first tab every time you re-open your browser, which is still fine because the script prevents the redirect tab from loading anything. It'll just be blank.

Context

I've been trying to trim down the youtube website of unecessary things. Superfluous network requests, telemetry (spyware), useless files, a bunch of corpo crap.

I was sick of wrestling with the site itself, so I revisited the idea of using MPV/yt-dlp. I had tried before, but it was convoluted. Most guides on the internet tell you to use random extensions, that need multiple clicks for a simple action : right-click to open the context menu, then click "open in mpv". It wasn't really better than outright copy-pasting the URL in a terminal like mpv https://www.youtube.com/watch?v=fBXY_AyA0bo.

Then it struck me : mpv supports ytdl:// URIs! If we simply redirect https://youtube.com/watch.* to ytdl://youtube.com/watch.*, bob's our uncle. In fact, this could work with any site supported by yt-dlp! Twitch, soundcloud, the list goes on! The only requirement is to have good regex so the script only loads on pages which serve video/audio yt-dlp can grab, not home pages, galleries, etc.

One big UX hurdle I faced was : how do you know when to automatically close the tab? You don't want to close it when the "allow this site?" dialog is opened, but there's no way to detect that. So, I made the script store a single cookie per-site : has the script previously run? If yes, the "previously_ran" equals "1". That way, the first time you redirect a website, and your browser asks you to allow the redirection, it will actually let you click instead of closing on you.