Install the official build of mpv media player on any Linux distribution
Tags: tutorial, productivity, opensource, container
Published: 2024-05-18 01:08:47

Ever wonder why we should use the software directly from the developer’s _official_ distribution channel when it's possible? Maybe, we should learn from an example in the recent Snap store incidents: 
- A fake Exodus wallet app had entered the store and [scammed 9 BTC (worth around $490K at the time)](https://popey.com/blog/2024/02/exodus-bitcoin-wallet-490k-swindle/) from an investor. And [another incident](https://popey.com/blog/2024/03/exodus-wallet-part-three/) happened _again_ shortly after the first incident. It's a mess nonetheless.
- As we can see, the sandboxing security of Snap didn’t help. This is why we should only use the official apps _when it’s possible_, as this could’ve happened to any store, Flathub, Play Store, App Store, etc. In fact, it already happened on App Store [[1]](https://news.ycombinator.com/item?id=39685272), [[2]](https://www.bleepingcomputer.com/news/security/fake-leather-wallet-app-on-apple-app-store-is-a-crypto-drainer/) and Microsoft Store [[3]](https://www.bleepingcomputer.com/news/security/fake-ledger-live-app-in-microsoft-store-steals-768-000-in-crypto/) recently.

## The level of trust
### App Developer
Sure, if we can’t trust developers, we shouldn’t use their apps at all. But if we decide to use their apps, it means we _must_ only download the apps from them, unless we prefer to get ourselves scammed. Therefore, this should be our 101 security practice.

This is not only for our security concerns, but using the _official_ build will also make the debugging a lot _less_ painful for the upstream developers.

### System
We can’t possibly use everything directly from the original developers, e.g. apps, packages, drivers, or even the Linux kernel, etc. It’s undeniably that _choosing Linux distro wisely_ is a very important part of our threat model. Therefore, your distro’s main/default repos, _excluding_ the community maintained repos like [AUR](https://aur.archlinux.org/packages) or home projects on [OBS](https://build.opensuse.org/) for example, are _probably_ safe enough to use. At the very least, they shouldn’t introduce more risk, since you already trusted the kernel from them.

### Third-party / unverified / community maintained
Unless we have no other choice, this channel should be avoided at all cost.

---

The good thing about `mpv` over other video players is that it plays HDR videos nicely on my SDR monitors and projectors. Without further ado, let’s see how to install `mpv`, my favorite video player, using [the official build (Arch package)](https://mpv.io/installation/) on any Linux distro.

## Distrobox
Yes, we can install [`mpv`](https://mpv.io/) for Arch (official package) on any Linux distro using a rootless [Distrobox](https://github.com/89luca89/distrobox) container. We’ll install `mpv` in Arch container using [the official Arch Docker image](https://hub.docker.com/_/archlinux).

I will use command lines in this walkthrough. You can use [BoxBuddy](https://github.com/Dvlv/BoxBuddyRS) if you prefer the GUI for the container’s creation process. But that’s about it. The app is pretty much bare bone currently. In the end, you’ll have to use the command lines to set up everything inside the container. Therefore, have your terminal ready!

> Note, I use openSUSE Tumbleweed. Therefore, I will use `zypper` command in this walkthrough. Please change the command according to your distro's package manager.

👉️ Table of contents:

1. Preparing the Container
2. Install `mpv`
3. Enable Video Hardware Acceleration
4. Exporting the App
5. Automatically Update the App

---

## 1. Preparing the Container

### 1.1. Install Distrobox and Podman on the Host
```
sudo zypper install distrobox podman
```

### 1.2. Configure Distrobox to use Podman
```
echo 'container_manager="podman"' > ~/.config/distrobox/distrobox.conf
```

> You can see more of the config options on [the official repo](https://github.com/89luca89/distrobox#configure-distrobox).

### 1.3. Create a New Distrobox Container for `mpv`
```
distrobox create -i docker.io/library/archlinux:latest -n mpv-dbx -H ~/distrobox/mpv-dbx --additional-packages "adwaita-cursors"
```

> `distrobox create` is used to create a Distrobox container. [See the docs](https://github.com/89luca89/distrobox/blob/main/docs/usage/distrobox-create.md) for all the usages.

### 1.4. Prepare the Packages Inside the Container

#### Update All the Packages
```
sudo pacman -Syu
```

When it asks which DBus you want to use, I recommend `dbus-broker` over `dbus-daemon`, as it’s [the new default DBus on Arch](https://archlinux.org/news/making-dbus-broker-our-default-d-bus-daemon/) now.

#### Install GPU Video Acceleration Driver for Your GPU

For example, VA-API drivers for Intel GPUs
```
sudo pacman -S intel-media-driver libva-utils
```

---

## 2. Install `mpv`
```
sudo pacman -S mpv
```

It will ask which jack you want to use, between `jack` and `pipewire-jack`. You can choose `pipewire-jack`.

---

## 3. Enable Video Hardware Acceleration

Creating a config file at `~/.config/mpv/mpv.conf` (in the container), as shown below:

```
# enable video hardware acceleration
hwdec=auto

# optional options
vo=gpu-next
tone-mapping=reinhard
ao=pipewire
sub-back-color=0.0/0.0/0.0/0.75
sub-auto=fuzzy
```

- I put `vo=gpu-next` option to use the new GPU backend, which is supposed to be a lot faster than the current default.

- I put `tone-mapping=reinhard` to enable tone mapping from HDR to SDR on my SDR projector. There are many tone mappings available, but I like this one the most.

- I put `ao=pipewire` option to use [PipeWire](https://www.pipewire.org/) audio driver.

- I put `sub-back-color=0.0/0.0/0.0/0.75` option to make some subtitles easier to see.

- I put `sub-auto=fuzzy` option to make `mpv` scan for separated subtitle files, even if they don't have exactly the same filenames as the video file. You can try to enable this option if `mpv` can't find your subtitles.

You can see all the available config options from [the official mpv manual](https://mpv.io/manual/stable/).

---

## 4. Exporting the App
```
distrobox-export -a mpv
```

By exporting the app, it can be set as your default video player. You can also open any video with the app. No one would notice that it's installed in the container. This is the system integration power of Distrobox!

---

## 5. Automatically Update the App

We can use systemd's service and timer to update/upgrade _all_ Distrobox's containers like this:

### `dbx-upgrade.service`
```
[Unit]
Description=Upgrade all rootless Distrobox containers.
RequiresMountsFor=/run/user/1000/containers

[Service]
Type=exec
ExecStart=-bash -c "distrobox-upgrade --all"
Restart=on-failure
RestartSec=60
TimeoutStopSec=5min
RemainAfterExit=yes
```

Save this file as `~/.config/systemd/user/dbx-upgrade.service`.

### `dbx-upgrade.timer`
```
[Unit]
Description=Run distrobox-upgrade --all daily.

[Timer]
OnCalendar=daily
RandomizedDelaySec=5min
Persistent=true

[Install]
WantedBy=timers.target
```

Save this file as `~/.config/systemd/user/dbx-upgrade.timer`.

### Enable the Timer
```
systemctl --user daemon-reload && systemctl --user enable dbx-upgrade.timer
```

---

Cover Photo by [Denisse Leon](https://unsplash.com/@denisseleon?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash) on [Unsplash](https://unsplash.com/photos/closeup-photo-of-audio-mixer-n4BDkIEls78?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash)