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.
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.
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
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.