Kodi setup on Rasperry Pi (Raspbian buster)

My DVD and Blu-ray collection is stored on a 2 TB drive. When I buy a new Blu-ray, I rip it with MakeMKV and encode it with Handbrake, then store it on the drive. DVDs can be read directly by Handbrake. Then, I make this collection available on my LAN with NFS.

I have two TVs at home. One of them has a Fire TV Stick and a Linux PC running Debian connected to it. The other one has a Rasperry Pi 3B running Raspbian. I use the Linux PC and the Rasperry Pi to access my movie collection by running Kodi. Kodi works a lot better than it used to for decoding H264-encoded video on a Raspberry Pi. These days, it takes advantage of the hardware decoding that the Pi offers via the MMAL library [1].

I'm writing this post to provide notes for myself in case I need to set up the Raspberry Pi again, but also in case these steps are useful to someone else.

Raspian base setup

Create the SD card

First, I downloaded "Raspberry Pi OS Lite," since for this use case an image with a desktop environment installed would get in the way (we want the system to boot up to Kodi, not a desktop). The latest release at the time of this writing was Buster 2021-05-07. I unzipped the download and wrote it to an SD card (this works from Linux):

sudo dd bs=4M if=2021-05-07-raspios-buster-armhf-lite.img of=/dev/sdX

I replaced /dev/sdX with the device name of the unmounted SD card. I'm sure it goes without saying, but be careful! They don't call dd "disk destroyer" for nothing.

Before removing the SD card from my computer, I mounted the newly-imaged filesystems. One of them is the "boot" partition (probably the first one), formatted as fat32. I created an empty file called ssh in this partition, which enables the SSH server by default.

Initial Raspbian configuration

I booted the new system with an Ethernet conection, but I eventually used the raspi-config utility to configure my WLAN. From raspi-config, I

  1. Change the default password for the pi user.
  2. Set the password for the root user: sudo passwd.
  3. Set the keyboard layout: Generic 104-key, then I had to click "Other" to get the US layout.
  4. Set the locale (I used en-US.UTF-8) and timezone (I used US/Mountain).
  5. Set the hostname (I used tvpi-bedroom)
  6. Set video memory to 256 MB (this is necessary for hardware decoding of high-resolution video).

Network configuration

My router runs a DHCP server, and I run a bind9 DNS server on a different Linux machine. I configured a static DHCP lease for the Pi's wireless interface, and I added an entry to my local DNS zone for tvpi-bedroom.

Configuring Kodi to start automatically

I wrote a systemd service for Kodi, based sort of on a service I found on Github. It looks like this:

Description = Kodi Media Center
After = remote-fs.target network-online.target
Wants = network-online.target
Conflicts = getty@tty1.service

User = pi
Group = pi
Type = simple
ExecStart = /usr/bin/kodi-standalone
Restart = on-abort
RestartSec = 5

WantedBy = multi-user.target

I enabled it like this:

sudo systemctl enable kodi
sudo reboot

NFS client configuration

My NFS server is named "abra," so I added an entry like this to the Pi's /etc/fstab:

abra:/home/mnt/storage3-media /abra-media nfs4 defaults,noatime 0       0

Kodi configuration

I added the media folder, /abra-media/Movies, to my Movies library, then I let Kodi index away. Apparenty Kodi instances can share a database, but I'm not confident I can keep my amd64 PC and my Pi at the same version of Kodi, so I'll let them maintain their own databases for now.

Then, under Settings → Services, I enabled remote control of Kodi over HTTP, and I set a username and password. Then I installed the Kodi remote on my phone and entered the authentication details.

[1]Docs on MMAL are a little light, but the headers can be found on the Pi under /opt/vc/include, if the libraspberrypi-dev package is installed. Doxygen can be built <http://www.jvcref.com/files/PI/documentation/html/> from the headers. If you're curious about the underlying hardware, you can read about VideoCore <https://en.wikipedia.org/wiki/VideoCore>