McSinyx

Best Ways to Watch YouTube Videos

In today's episode of guides nobody asked for and likely having been covered by someone more qualified, lemme show you the correct ways to view videos hosted on YouTube and other hostile, tracker-riddled hellscapes. Whilst I despise Google's mass surveillance practices, it stores a large proportion of culturally significant videos and clips that would be difficult to mirror to user-respecting services due to copyright. Hell, even YouTube doesn't have the right to distribute many of them in the first place.

Because of YouTube's circumvention of advertisement blockers, the ad-blocking arm race finally caught mainstream media attention and tis kool to talk about that now. Hence I'm happy to jump on the bandwagon, albeit a wee bit late, but this ain't just that. Since I feed you poison—over 4% of the pages linked from my site are on YouTube—the least I can do is sell you my cures.

Using a Proper Media Player

The most popular solutions are either to use for a good blocker on a browser with (supposedly) long-term support for Manifest V2 like uBlock Origin on Firefox, or use alternative front-ends such as Invidious or Piped. Although uBlock Origin is essential for a pleasant experience on the modern interwebs and alternative frontends offers the best UX for browsing videos, in-browser and service-specific media players are inferior anyway when compared to programs properly designed for a decent playback experience.

My favorite has been mpv for as long as I can remember, as it makes it easy to adjust video brightness/contrast/etc., playback speed, subtitle size and placements, and to overamplify quiet audios. Out of the box, it integrates with yt-dlp, a time shifter with support for most online media services. Just drop the URL into an mpv window and boom, it werks!

Either drag-and-drop or invoking mpv $url is quite convenient, but not that close to following an anchor, is it? You'd need to first open mpv or a program launcher[1], then drag the URL there, or perhaps copy and paste it for the latter cases. What if you gotta go fast, aye? As a hedgehog-maxxer meself, of course I can do better, and here's how.

With a Browser Add-on

While drafting this article, I noticed that the ff2mpv extension I was using had technically been non-free for a while. Albeit I understand and respect the author's noble intention against violence, I believe discrimination never ends up helping those oppressed due to the power imbalance for the exclusion false-positives to be worth it.

For this reason, I switched to Open in mpv and recommend it instead. The usage is practically the same: open context menu at the video URL and select Open this link in mpv. The internal mechanism is a bit different though, and because it influences the installation process, I will try to briefly explain how it works.

The way Open in mpv works is a bit convoluted. First, it wraps the specified URL in a mpv scheme. The new URL starts with mpv:// is then passed back to Firefox, which must have been configured to open it in the native program open-in-mpv. This program parses the URL into the equivalent mpv command and execute it. If you are not on NixOS, see the extension's README to set it up yourself.

Otherwise, it can be declared in configuration.nix(5) as follows. The declarations should be self-explanatory after referencing Firefox's documentation for policies.json. If you have trouble finding an extension's ID and download URL, search for it in Mozzarella.

{ pkgs, ... }:
{
  programs.firefox = {
    enable = true;
    policies = {
      ExtensionSettings."{d66c8515-1e0d-408f-82ee-2682f2362726}" = {
        default_area = "menupanel";
        installation_mode = "normal_installed";
        install_url =
          "https://addons.mozilla.org/firefox"
          + "/downloads/latest/iina-open-in-mpv/latest.xpi";
      };
      Handlers.scheme.mpv = {
        action = "useHelperApp";
        ask = false;
        handlers = [ {
          name = "open-in-mpv";
          path = "${pkgs.open-in-mpv}/bin/open-in-mpv";
        } ];
      };
    };
  };
}

Even though Mozzarella is supposed to only show libre add-ons, be aware that the metadata it crawls from addons.mozzila.org might not always be correct. Ideally, browser extensions should be packaged in the distribution's repository, but packaging discipline is not exactly NixOS's strong suit. I will probably post an update on how to declare policies.json in Guix once I figure that out.

From a Feed Reader

Now we can properly watch videos while browsing the web, but subscribing to YouTube channels on its web interface would require creating an account and subjecting one's self to more surveillance. Fortunately, at the time of writing, YouTube still provide Atom feeds for syndication. Funny enough, they are advertised on the channel pages as RSS:

<link rel="alternate"
      type="application/rss+xml"
      title="RSS"
      href="https://www.youtube.com/feeds/videos.xml?channel_id=…">

The referenced feed employ Media RSS to communicate the video URL. This extension is widely supported by feed readers, as well as the previously mentioned feed-discovery mechanism. I use Liferea, which allows me to directly paste the YouTube channel's URL[2], and displays each video's description, thumbnail and enclosed media, e.g.

Liferea in action

For each MIME type to, enclosures can be configured to be opened by a user-preferred program. In this case, I set mpv --ytdl-format=b for application/x-shockwave-flash (a reminiscence of a time when browsers needed Flash to play videos and animations) for the second best quality to save some bandwidth. YouTube encodes the highest resolution video separate from the audio, so the best combined format b is one level lower than yt-dlp's default best video and best audio together.

Via Clipboard Integration

People also share videos with me via instant messaging. I find it cumbersome to open the URL in the browser then redirect it to the media player, so the clipboard is used as the bridge instead. To do this, I simply create a key binding to the command below.[3]

mpv --ytdl-format=b "$(xclip -out -selection clipboard)"

Musing

There, I shared how I do it so you can too! If they seem needlessly complex, you share my disappointment on the UX evolution of the mainstream web. I dream of a more semantic web, not necessarily web 3.0, perhaps just more explicitly typed, where e.g. a YouTube URL for embedding would be a video/webm instead of a text/html.

If mailto URIs can launch our email client, and social media pages can bug us to open the post in their own app, why can't we have interoperable media handling? Maybe we should, but I'm not sure if we can. Greed stands in our way. Providers force us to use their proprietary malware to consume their service. DRM has become the foundation of media distribution. Grassroots movements like Framasoft might never reach mainstream status.

I don't mean to tell you to give up though, just to direct your energy to where it matters. Spend less on developing alternative front-ends than on ethical replacements, bridges and inviting people over. We need more videos, more music, more podcasts, more knowledge, better instant messaging, better search engines, better translations, better home automation, and whatnot. Against all odds, maybe things will finally start to improve even for those outside of our bubble. Perchance.

[1] Or a terminal emulator
[2] Something starting with https://www.youtube.com/@
[3] On Wayland, replace xclip with something equivalent

Tags: fun recipe net nix clipboard Nguyễn Gia Phong, 2024-01-17

Comments

I didn't know that you can configure plugins with nix!

as for the wayland equivalent, we have wl-clipboard and the command is simply wl-paste

Ngô Ngọc Đức Huy, 2024-01-17

Follow the anchor in an author's name to reply. Please read the rules before commenting.