Improve video playback performance including YouTube (ytfzf)

Video playback using mpv

Also available as a simple SaltStack recipe with didactic dom0 step-by-step at: ( brunoschroeder/qubes-salt-video-playback - Codeberg.org ).

This guide will show you how to install the mpv player and use it with maximum performance (in own tests I saw video performance improvements of up to 600% !). It will assume that you are using debian-12 as a template (other templates should also work, however) and that you play videos in DVMs; I recommend looking into this solution for caching disposables to decrease startup time (or this much more elaborate take on it, verify cryptographically via this repo). I also recommend looking into one of these solutions to have files, including video files, always start in DVMs if you “open” them in your file manager.

Since not everyone is interested in YouTube and the YouTube solution presented here is admittedly a bit unpolished due to bugs in mpv as well as insufficiently up-to-date packages in debian stable, I will present separately the necessary steps to set up mpv for local video playback only and mpv with ytfzf, which basically lets you search for YouTube videos using the Invidious API and then stream them without having to actually launch a browser; it’s also compatible with Peertube and Odysee.

In your base template

In your debian-12 template run:

sudo apt install mpv

For (additional) YouTube functionality run instead:

sudo apt install mpv fzf make
sudo apt remove yt-dlp

yt-dlp is probably going to get installed together with mpv, but if you’re using Debian it will be a very old version that won’t work, so we’ll have to install a newer one by ourselves (see further down).

In both cases (YouTube or not) we proceed to set mpv to use a certain configuration, which will drastically improve playback performance:

sudo vi /etc/mpv/mpv.conf

Add the following to the file (press i to edit):

vo=x11
profile=sw-fast

Save your changes (press Esc and enter :x and press Enter) and shut down the template. This completes the guide for those who don’t care about YouTube. Enjoy much improved video playback performance!

In your DVM template (YouTube)

First download the latest version of yt-dlp from here in some Internet-connected DVM or AppVM (choose the yt-dlp_linux file and verify the checksum after downloading just in case; you can also verify the pgp signature on it if you want to be maximally secure; in that case you may want to create a separate dvm template to do the following in); then copy this file to your debian-12-dvm using qvm-copy.

In your debian-12-dvm disposable template run (we’ll assume that you copied from myAppVm):

chmod +x QubesIncoming/myAppVm/yt-dlp_linux
sudo mv QubesIncoming/myAppVm/yt-dlp_linux /usr/local/bin/yt-dlp
rmdir -p QubesIncoming/myAppVm/

Then create a new file:

vi update-yt-dlp-and-ytfzf.sh

Into this file copy the following (copy this code into that VM’s clipboard and then into Vim, e.g. by using your terminal’s paste functionality, e.g. Edit - Paste in the menu):

#!/bin/bash

echo -e "GET http://github.com HTTP/1.0\n\n" | nc github.com 80 > /dev/null 2>&1

if [ $? -eq 0 ]; then 
	sudo yt-dlp -U &&
	cd ~/Downloads &&
	rm -rf ytfzf &&
	git clone https://github.com/pystardust/ytfzf &&
	cd ytfzf &&
	sudo make install doc
else
	echo "No internet connection or cannot reach github.com!"
fi

Make the script executable and run it once:

chmod +x update-yt-dlp-and-ytfzf.sh
./update-yt-dlp-and-ytfzf.sh

The above script will install the latest version of ytfzf as well as update yt-dlp. You should periodically run this script to update both yt-dlp and ytfzf. Run in the dvm-template for persistence or in the DVM itself on every start of that DVM.

Now shut down your dvm-template and don’t forget to set its netvm to None if that was the prior setting (e.g. if you only use web-connected named DVMs based on it).

Congratulations! You have completed the guide and can now watch both local videos as well as YouTube videos with probably the best possible performance in QubesOS despite lacking GPU acceleration!

Addendum: a short ytfzf tutorial

You can use ytfzf simply like this: ytfzf videos I like to watch (use a maximized terminal). Then select one of the titles with the up/down arrow keys and press Enter to play it. Launching with the -l option is useful as ytfzf will return to the list of videos found after playing a video. Useful keyboard shortcuts:

  • Alt+D downloads the video instead of playing it (you can then play it with mpv video_file_name.webm)
  • Alt+F lists available formats (e.g. 1080p) for that video (you can then select a format and play it with Enter)
  • Alt+E launches the video in mpv as a detached process, which works best with the -l option as it retains the selection screen while the video is playing
  • Ctrl+P loads the next page of videos (the next 20 video titles by default)
  • Ctrl+C exits the program

See the man page (man ytfzf) for more controls and options, but note that good looking thumbnails (option -t) either need additional non-standard-repo packages to be installed so they can be displayed in the terminal itself or will be displayed in a separate window: the only way I’ve gotten them to work acceptably on my machine (and without installing additional packages) is by using: -lt -T mpv --preview-side=down as options and then usually I have to Ctrl+P once to fix misalignment of the infos (this bug has been fixed for the next version). One can automatically apply these options by setting an alias in the .bashrc file of the dvm template:

alias ytfzf='ytfzf -lt -T mpv --preview-side=down'

The next version (after 2.6.1) will allow getting rid of the preview panel altogether by using the following alias instead of the one above (that setting can also be put into ~/.config/ytfzf/conf.sh):

alias ytfzf='preview_window_width=0% ytfzf -lt -T mpv'

Bonus

To launch the ytfzf program even more quickly with better configured thumbnails, use a cached, named, networked DVM; find out the size the terminal window should have such that the separate thumbnail still fully fits on screen (e.g. by launching ytfzf -t -T mpv video and positioning the thumbnail window next to the terminal window while resizing the terminal window and noting the final size down); you may also have to go to the Window Manager Tweaks app of your QubesOS installation and in the tab “Placement” move the slider further left until the window manager automatically places the thumbnail window in the “free space” of the workspace when created.

Then in your DVM’s dvm template run:

sudo mkdir -p /usr/local/share/applications
sudo vi /usr/local/share/applications/ytfzf.desktop

In this file put the following (replace the geometry values with your recorded values and either remove the --ytdl-pref option or customize it for your needs; the one you see there basically tries to pick the best video and audio combination available that does not exceed 1080p and that is not HDR when you launch without Alt+F, e.g. with Enter or Alt+E):

[Desktop Entry]
Type=Application
Name=YouTube FzF
Comment=Watch YouTube videos without a browser
Icon=mpv
Exec=gnome-terminal --title=YTFzF --geometry=211x29 -- ytfzf -lts -T mpv --preview-side=down --ytdl-pref='ba+bv[height<=?1080][dynamic_range=?SDR]/b[height<=?1080][dynamic_range=?SDR]' %U
Terminal=true
Categories=AudioVideo;Audio;Video;Player;TV;
Keywords=youtube;ytfzf;yt-dlp;

Save and exit (:x) and shut down the dvm template. Now open your Qubes Manager and edit the settings of your named DVM based on that template:

  1. Go to the “Applications” tab
  2. Press “Refresh Applications” and wait for the process to complete
  3. Find YouTube FzF in the left list at the bottom and push it over to the right list using the > button
  4. Press OK

Done! From now on you can instantly launch a YT search by pressing Alt+F3 and typing youtube or ytf and hitting Enter; it will pop up the correctly sized terminal of your cached DVM with a search prompt ready; the thumbnail window will automatically be placed in a useful way and the videos you stream will have the format restrictions listed in the desktop file.

Credits: @rustybird @tanky0u @balko for making me aware of mpv and ytfzf!

11 Likes

I also recommend smplayer. Because it has GUI including buttons for average users and more functions for advanced users. Also FLOSS and available in fedora fusion repos (after adding fusion repo).

It will automatically install and use mpv as a backend, so performance is the same.

Run in template:

sudo dnf install https://mirrors.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm
sudo dnf install rpmfusion-free-release-tainted

sudo dnf upgrade --refresh

sudo apt install smplayer
1 Like

Just an additional note (not particularly related to performance):

If you care about privacy, don’t use YouTube. Use Invidious. There are multiple public instances and it works perfectly without JavaScript.

For downloading videos, there is this fairly old and abandoned project avideo (a fork of youtube-dl, I think). Unlike the others, it does not execute proprietary JavaScript (if you care about that). AFAIK, this prevents it from working with today’s YouTube, however it works fine when downloading from Invidious.

3 Likes

To be blunt: smplayer almost gave me eye cancer when I looked at the GUI, but maybe others like it. So far I haven’t needed the extra functionality it gives and I actually like the minimalistic UI of mvp itself (which also has “buttons”, they just auto-hide most of the time), but maybe in the future I’ll give smplayer another try.

Re: Invidious I’ll add that ytfzf actually uses those public Invidious instances (also added that info to the OP now).

It has themes out of the box and can look differently. In your case the reason is probably that you use gnome or xfce template without proper themes and Qt settings.
Also, how can you blame other applications for looking bad when you use XFCE and xscreensaver?

I do not think it looks bad at all:

1 Like

Here are more examples of how smplayer can look. I personally prefer to hide the top panel completely (F5 toggles it).
I do not know which one gives you cancer, sorry in advance.



mini_gui2

2 Likes

I said almost :wink: :smile:

As stated, I’m perfectly happy with mpv, which has all the functions and controls I need right now and appeals to me with its minimalist UI. For me it’s just unnecessary hassle to install smplayer, as I’d have to

  1. change the mime types, since upon installation when I tested it, it did not actually become the default (mpv was still the mime default)
  2. find a theme that I can tolerate, while there probably isn’t a comparable one to the mpv default look, where all of the UI autohides (and if there is that’s yet more time spent on finding it).
  3. deal with the settings to make sure everything works as desired, which for mpv is already the case now

And of course it’s yet more packages installed in the template, while the benefit, for now, seems to be exactly zero…so excuse me, but I’ll stick with mpv only.

If smplayer is Qt based like VLC, then you can tell it to use the native GTK theme.

Add these as a global environment variable, or set the variable before the program runs.

export QT_QPA_PLATFORMTHEME="gtk2"
export QT_STYLE_OVERRIDE="gtk2"

Source: Uniform look for Qt and GTK applications - ArchWiki

2 Likes

Just a side note about smplayer:

Testing on a very very old 1-core laptop running GNU/Linux (not Qubes OS) , I had noticed that smplayer doesn’t give the same performance as “naked” mpv or mplayer. So, it makes me think that smplayer’s UI may be adding certain burden to the CPU.

I have not tested this in Qubes OS because I am happy with mpv itself. Just wanted to share this, in case someone is interested in making any actual detailed benchmarks.

1 Like

Thanks for information, burt I am using KDE-based template and smplayer has no appearance issues for me, it looks similar to screenshots I provided above.

I personally try to avoid GTK and Gnome software as I consider it to be not great for advanced users.

I also had numerous trials and erros in order to provide smooth playback of videos, only to get the best results with VLC under whonix-ws.

But, the true answer came later: in order to get best performance all I needed was to buy new, stock, recent generation machine (not older than 3 years). SInce then no problem with any distro, Windows, qube, HVM, standalone, player…

1 Like

Sure, but as someone running Qubes on a pretty new machine where videos will run under any template with a variety of players, it’s still nice to reduce CPU load from 50% in a VM with 8 cores to 10% when watching a full HD video, especially on laptops (battery life, fan noise, heat), but also on desktop computers I imagine since it means you have more resources for other computationally intensive stuff you may want to do while watching your vids and electricity is not as cheap as it used to be anyway (lower load should also reduce wear on some HW components).

@everyone:

I created a patched version of ytfzf that fixes the two issues I mention in my OP:
files.log (34.1 KB)

Note that this is a zip archive (I uploaded as *.log because of the upload restrictions…if that’s not ok then mods please advise how to handle it otherwise); the archive includes an updated script (from the one in the Bonus section of my OP) and ytfzf executable; copy the file to your dvm template’s home folder and run (type y if it asks you to replace existing files):

unzip files.log
rm files.log
chmod +x update-yt-dlp-and-ytfzf.sh ytfzf_fixed_copy
./update-yt-dlp-and-ytfzf.sh

The updated script will, after trying to update yt-dlp and ytfzf as the original script in the OP does, check the ytfzf version; if the version hasn’t changed from the current 2.6.1 then it will replace the existing executable with the patched one; the patched executable fixes the annoying misalignment bug and adds the feature to hide the info panel completely; for the latter to work you need to put preview_window_width=0% into ~/.config/ytfzf/conf.sh, which will enable the feature so that more of the screen real estate is used for the more useful and much denser info that is the title list.

1 Like

These tools are great.

I have just created an MIT License repo fro the community with the SaltStack files to configure this automatically and a setp-by-setp of how to use it.

Please update in the initial message.

My motivation is help those whostruggle with SaltStack, and have more Salt code and less visual/bash step-by-steps in the forum.

1 Like

I don’t have time to test your solution right now, but if you’ve tested it and it works, feel free to edit the OP to include it (it’s a wiki post, so any forum member can edit it). Thank you for your contribution!

1 Like

Please rewrite your bash scripts following shellcheck guidance.

Currently they have major issues, like not-quoting variables, that can lead to huge problems in case paths have spaces, start with - and etc.

Bash is a dangerous animal.

2 Likes

Thank you @balko, this is done now.

1 Like

Script prints error for me:
No internet connection or cannot reach github.com!
despite taking the correct steps.

It also ask to download jq.

Unfortunately, I get much better performance via browser.
And still can’t play smoothly 4k youtube videos in fullscreen mode.

Thank for the guide.

Can you print the output of which nc for me, please?

It’s possible that nc is not installed in the template of the VM you’re running the script in…otherwise there may actually be a connection problem. You could try installing nc in the template or just remove the check in the script (i.e. delete all lines beginning with echo, else, if and fi).

1 Like

I doubt there is any user PC that will be able to play even 1080p video in Qubes OS smoothly.

You can try to watch this video in 1080p or 1440p to find out that it is played a bit jumpy during panning.

2 Likes

I have reasonably modern Intel CPU, and even 360p, even in a small frame inside the forum webpage is jumpy, e.g. very noticeable after 0m20s.
360p is not smooth!

I think it is related to how Xen is poorly rending media using Xorg or something.