An Utterly Absurd (but very efficient) Way to Watch YouTube Videos without a Browser

Since I use LibreWolf with additional restrictions on JavaScript and XHS, my browser does not play any YouTube videos. This is intentional, because my philosophy is that the web browser is not a media player, but a tool to display hypertext and, occasionally, an image or two. I miss the internet as it was in the 1990s. Also, I don’t like Goolag leaving their dirty cookie crumbs all over my computer.

Additionally, I absolutely hate the YouTube website and their online media player. I also hate when the video pauses to fill the buffer. I hate when I can’t instantly jump 20 seconds ahead or back. And the thing I hate the most, with burning hatred, is that spinning icon that indicates the video is loading. :rofl:

The entire concept of streaming is crap, in my opinion, and I’d like to avoid it.

So how does a paranoid, extremely opinionated person like me watch YouTube videos?

Well, I have come up with an elaborate, but very efficient way, and here, I’m sharing the entire thing, maybe there is some other lunatic like me who will find it useful.

The basic premise is simple: I download all videos and save them locally before watching them, and I play them in my preferred media player: smplayer. Since one always needs to download the complete video in order to watch it (streaming is also downloading), my reasoning is, one may as well save it to a file, and decide later whether to watch it again, delete it, or keep it. On my connection this works reasonably well for videos that are under 5 GiB in size (so no 10-hour videos of a spinning seal), they take less than 5 minutes to download. Shorter videos download almost instantly.

Do download videos, I use yt-dlp, of course. However, since Goolag hates yt-dlp, they change their APIs ever so often, to break it. Luckily the yt-dlp dev team is always on top of that, however, the version in the Arch repos is simply not bleeding edge enough. It sometimes takes 2-3 days to get updated. So I clone the official yt-dlp repo locally (into some directory deep into my home directory):

git clone https://github.com/yt-dlp/yt-dlp.git

Then I manually symlink yt-dlp.sh to ~/.local/bin/yt-dlp

I find this is better than installing it from the repos. When it breaks, I update it by running git pull inside the directory where it is cloned.


It gets tedious to paste the URL to the shell every time so I have this handy script:

ytp:

#!/bin/bash

# Directory for downloaded videos:
if [ -z "$DL_DIR" ]; then
  DL_DIR="$HOME/Videos/temp"
fi

# Video player:
PLAYER="xdg-open"

# Downloader and options:
YTDL="$HOME/bin/yt-dlp"
YTDL_OPTS=(--no-playlist --throttled-rate=100000 -N 10)
YTDL_DIR_OPTS=(-P "$DL_DIR")

set -e

CMD() {
  printf "[CMD]: "
  printf "\"%s\" " "$@"
  printf "\n"
  "$@"
}

# Get URL from clipboard if run without argument:
if [ -z "$1" ]; then
  URL="$(xclip -o -sel c)"
  CLEAR_CLIPBOARD=1
else
  URL="$1"
fi

# Get video filename
printf "[URL]: %s\n" "$URL"
FILENAME=$("$YTDL" --get-filename "${YTDL_OPTS[@]}" "${YTDL_DIR_OPTS[@]}" "$URL")
printf "[FILE]: %s\n" "$FILENAME"

# Download video
CMD "$YTDL" "${YTDL_OPTS[@]}" "${YTDL_DIR_OPTS[@]}" "$URL"

# Play video
echo "[CMD]:" "setsid" "$PLAYER" "$FILENAME" ">/dev/null 2>&1" # ugly hack to print the output regardless of >/dev/null redirection!
CMD "setsid" "$PLAYER" "$FILENAME" >/dev/null 2>&1

# OPTIONAL clear clipboard (KDE only)
if (($CLEAR_CLIPBOARD)); then
  CMD "qdbus" "org.kde.klipper" "/klipper" "org.kde.klipper.klipper.clearClipboardHistory"
fi

I saved it to ~/.local/bin

So, I can just copy the URL of the video into clipboard, and run ytp in the shell and the script downloads the video from the URL in the clipboard into a temporary directory (in my case ~/Videos/temp/).

Additionally, by setting the DL_DIR variable, I can change the directory where the file is downloaded, so I have another script, which I run when I want to download and play a video to the current working directory:

ytc:

#!/bin/bash
DL_DIR="$PWD" ~/bin/ytp

It gets tedious to switch to a terminal window every time I want to watch a video, it would be cool to just hit a keybinding, like Meta+V and play the video whose URL is in the clipboard automatically. In KDE, I could set up a command shortcut to run the ytp script above, but I wouldn’t be able to see the download progress and any potential errors as the script would run in the background. So I made yet another script:

_keybinding_ytp.sh:

#!/bin/bash

CLIPBOARD="$(xclip -o -sel c)"
[[ $CLIPBOARD = http* ]] || exit 1

TERMINAL_CLASS="konsole"
TERMINAL_NAME="zsh"
TERMINAL_EXEC="/usr/bin/konsole"

readarray -t WINDOW_LIST < <(xdotool search --classname "$TERMINAL_CLASS" search --name "$TERMINAL_NAME")
for WINDOW in "${WINDOW_LIST[@]}"; do
  OUTPUT=$(xdotool "windowactivate" "$WINDOW" 2>&1)
  if [ -z $OUTPUT ]; then
    if [[ $(xset -q) =~ (Caps Lock: *on) ]]; then
      CAPS=1
      xdotool key Caps_Lock
    fi

    xdotool key --clearmodifiers --window "$WINDOW" ctrl+a ctrl+k space y t p Return

    if (( CAPS )); then
      xdotool key Caps_Lock
    fi

    # unstick keys if stuck
    sleep 0.5 && xdotool keyup Meta_L Meta_R Alt_L Alt_R Super_L Super_R
    exit 0
  fi
done

#otherwise
$TERMINAL_EXEC -e "$HOME/bin/ytp"

This script uses xdotool to look for any open terminal window where Zsh is running (my preferred shell), by searching the window name, then it focuses on it, and sends the following sequence of keystrokes to it:

  • Ctrl+A (to move the cursor to the beginning of the line),
  • Ctrl+K (to clear everything in the line)
  •    Space     (to prevent the command being added to Zsh history)
  • and then it types: Y, T, P
  • and finally Enter.

In other words, it autotypes ytp to run the “download and play” script. It also takes care of Caps Lock and any stuck modifier keys. If there are no open terminal windows, it launches a new instance of the terminal emulator and runs the ytp script there.

This autotyping script is then keybound in Plasma:

This works flawlessly on X. :man_cook: :ok_hand:

I just copy the video in the URL (Ctrl+C) and press Meta+V, which finds an open terminal window and runs the download script in it, a few seconds later, smplayer launches playing that video, buttery smooth, without any buffering and with instant forward and backward navigation, since it is playing a local file. Once or twice a month I clean the temporary directory with all the videos that I watched.


I never tried it on Wayland, but there is no chance that it will work there (I don’t mind that, Wayland sucks anyway). If someone can rewrite the scripts above to work with Wayland, that would be impressive, but I’d still use X, myself :eyes:

If you’ve made it this far, congratulations, you’re a freak. Hope you learnt something useful from it, or at least got a laugh.

6 Likes

I did, it’s first time you’ve admitted it’s utterly absurd :rofl:
But it makes sense.

It’s an utterly absurd solution to an utterly absurd problem of the utterly absurd modern world we’re living in. :clown_face: :earth_africa:

I really like to watch videos in a media player of my choice, and I hate streaming and buffering. So this is a complicated, but in practice, rather flawless solution, at least the way I’ve been using it for couple of years now…

1 Like

image

4 Likes

Real nerds watch YouTube videos directly in the terminal :grin:

6 Likes

I will say its kinda “Utterly Absurd” because you can just use mpv directly with the url.

For example, what I have is a .desktop/script file with /usr/bin/mpv "$@" "$(wl-paste)" and then just copy the url of the video and press Meta+m (after setting KDE to run that desktop file/script).

Not much win on firefox wayland (which plays videos very well), but a must for chrome browsers.

Its even easier in qutebrowser:

2 Likes

I’m similar to you and I also download (not only) YT vids first with yt-dlp to watch them. Of course, all “on foot” and cumbersome. So I will try to use your tips and scripts in the future. Many thanks for your work and for sharing.

btw… very nice to read you in this forum too :frog: :+1:

2 Likes

Only in this forum :wink:


Yes, smplayer will open URLs directly, too. But this does not solve issue of buffering.

And I specifically want to save the video in a file, so I can decide what to do with it:

My approach is not for everyone. But saying “mpv opens URLs” is completely missing the point of it.

Maybe I did miss the point :slight_smile: ( I … er… kinda… like … you know… maybe… there is the possibility … that… I might… have read your post … kinda… too fast :slight_smile: Sorry! )
Not to discard yt-dlp, I use it lots of times and (as mpv uses, and also a gui for it, media-downloader).

I forgot, I have a workaround for this on my mpv.conf:

cache=yes
demuxer-max-bytes=512M
demuxer-max-back-bytes=128M

I think its those settings. I generally have around ~30 mins to 1 hour buffering :slight_smile:

1 Like

Very thorough @Kresimir, thank you.

When I was running Fedora their repos were always at least one version behind on yt-dlp so I ran the official binary. To keep yt-dlp up-to-date I used two systemd units. The first is a service that runs “yt-dlp -U”, the second is just a timer unit to run the update service once a day.

yt-dlp.service

[Unit]
Description=yt-dlp update service

[Service]
Type=oneshot
ExecStartPre=/usr/bin/sleep 5
ExecStart=/usr/local/bin/yt-dlp -U

[Install]
WantedBy=network-online.target

yt-dlp.timer

[Unit]
Description=yt-dlp update timer
Requires=yt-dlp.service

[Timer]
OnCalendar=*-*-* 8:00:00
Unit=yt-dlp.service
RandomizedDelaySec=5m
Persistent=true

[Install]
WantedBy=network-online.target
5 Likes

I prefer to run the development version of yt-dlp. When stuff breaks (thanks to Goolag), that’s the version that gets the fixes quickest.

1 Like

Yeah that only grabs latest release. Maybe somebody else has a use for it, but probably not around here since Arch repo keeps up. =)

I remember last year, for example, YouTube created some changes in how channel names are resolved (all channels now begin with @) and that broke yt-dlp. The version in the Arch repos took almost a week to update, while the master branch on GitHub had the fix within 5-6 hours after the issue was discovered.

I got a lot of house-work done that week, lol.

2 Likes

btw, have you tried yt-dlp-git in chaotic-aur?

2 Likes

No, can’t say I have. It might be a good option for people who use the Chaotic AUR, though. My guess would be that it is probably the same thing that I’m using, only packaged nicely so that it updates with system updates.

In any case, from my experience, one often needs a version of yt-dlp that updates more quickly than the version from the Arch repos, because of rotten tactics that Goolag uses.

Yeah, yt-dlp-git is also in usual AUR, it’s just a master git version.

1 Like

To be honest, I don’t remember why I’m not using that. It’s been a while since I’ve decided to manually clone the repo and use git pull to update it. There probably was a reason, but what it was, escapes me at the moment.

there was also the kde app plasmatube though might not be what your looking for after you went through all that

I usually use Freetube. Admittedly I’m too lazy for this, and until Freetube stops working, it’s good enough for me.

1 Like