Bobbie Smulders Freelance Software Developer

Turning an old Apple TV into a Spotify player

Picture of the first generation Apple TV

In 2006, Apple unveiled the first-generation Apple TV. It was a set-top box for media consumption with hardware based on the Mac Mini. The small aluminium casing housed an Intel Pentium M, Nvidia Geforce Go, 40 or 160 GB internal HDD, integrated power supply, and a built-in Wi-Fi card. For connectivity, there was USB, Ethernet HDMI, composite, optical out and analog audio out through a high-quality DAC. Because of the tight coupling with iTunes and lack of software support the device isn’t very useful anymore and can be had for as little as €10 instead of the original €299.

For that low price point, it is amazing what you get from a hardware point of view. It’s a compact desktop computer with built-in Wi-Fi and high-quality audio components. And while the device is useless with the original software, thanks to the OSMC project it is fairly simple to turn it into a Linux box. Running a desktop environment on it is not advised due to the limited hardware, but it is perfectly fine for command-line applications.

Picture of the first generation Apple TV

So why not turn it into a standalone Spotify player? All the hardware we need is already present on the Apple TV, we just need to configure the software. By installing Linux and using the open-source spotifyd and spotify-tui we can turn the Apple TV into a standalone Spotify receiver and player.

1. Install Linux

The easiest method for installing Linux is to use OSMC. OSMC will install Debian 8 and Kodi Media Center on either a USB drive or the internal HDD. I prefer USB, so the original Apple TV software remains intact and usable. Download OSMC on a USB drive using the device installer provided on the website. Plug it in the Apple TV, hold down Menu and Volume Down on the remote while booting and let OSMC install itself. This is an unattended installation that may take a while. Afterwards, use the wizard to set up networking and make sure to enable SSH.

Screenshot of OSMC installation

Disable Kodi Media Center

OSMC starts with Kodi Media Center by default. As we will not be using it, we can disable it. Use SSH to log on to the Apple TV remotely using the hostname, username and password osmc and run:

sudo systemctl disable mediacenter

To make sure that we have a terminal after logging in, enable a getty with:

sudo systemctl enable [email protected]

Reboot the Apple TV to verify that the media center does not start and that you can log on to the terminal from the Apple TV itself.

Install updates

Update OSMC to get all the latest updates. Both OSMC for the Apple TV and Debian 8 are no longer supported but there are still some packages that can be upgraded after installation. Upgrading can take over an hour due to the limited hardware of the Apple TV.

sudo apt-get update
sudo apt-get dist-upgrade

The upgrade process will give a warning regarding the Nvidia driver but as we will not be using X that shouldn’t be a problem. Reboot after the update to ensure that everything is still operational.

You should now have a fully functional Debian 8 installation with a functioning terminal on the device and the ability to logon remotely through SSH.

Screenshot of the OSMC terminal

2. Install Spotifyd

Spotifyd is an open-source daemon that can stream music from Spotify clients such as the Android, iOS, Windows and macOS apps. It shows up in Spotify as a Spotify Connect device and allows you to stream music to it. It works standalone and will continue playing the current playlist even after you close the Spotify app.

Install Rust

Unfortunately, because we run Debian 8, it is not possible to use modern package managers. We need to compile spotifyd ourselves. To prepare the system for this, we need the build-essential package and rustup:

sudo apt-get install build-essential
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh 

Be sure to check the installation instructions for Rust as the command above might be outdated.

Build spotifyd

After installing Rust, we can compile spotifyd and all necessary packages with:

sudo apt-get install libasound2-dev libssl-dev
cargo install spotifyd

This can take up to four hours to finish.

Configure spotifyd

Create a config file on ~/.config/spotifyd/spotifyd.conf:

[global]

# Credentials
username = "username"
password = "password"

# Visibility in Spotify (as set-top box)
device_name = "AppleTV"
device_type = "s_t_b"

# Use ALSA for playing audio. hw:0,1 refers to the RCA audio jacks, use hw:0,3 for HDMI
backend = "alsa"
device = "hw:0,1"

# Use ALSA for volume controller, so we can control the main system volume through Spotify
volume_controller = "alsa"
initial_volume = "60"

# Bitrate, 160 or 320
bitrate = 320

I created a config file that should work in most cases but feel free to consult the configuration instructions for all options.

Start spotifyd

After compiling the spotifyd binary and putting the configuration in place, we can start the application with:

spotifyd --no-daemon

Verify that the Spotify app on your phone or computer now shows “AppleTV” available as a device for audio streaming. Connect to the device to verify playback. It should play audio through the red and white RCA jacks.

Exit the application with CTRL+C.

Screenshot of Spotify Connect device list in Spotify

Create a self-starting spotifyd service

Now that spotifyd is configured, we can wrap it into a service that boots automatically as the device starts. Start by creating a symlink to spotifyd:

sudo ln -s ~/.cargo/bin/spotifyd /usr/bin/spotifyd

Continue by creating a file on ~/.config/systemd/user/spotifyd.service:

[Unit]
Description=A spotify playing daemon
Documentation=https://github.com/Spotifyd/spotifyd
Wants=sound.target
After=sound.target
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/bin/spotifyd --no-daemon
Restart=always
RestartSec=12

[Install]
WantedBy=default.target

Enable the service and verify in Spotify that “AppleTV” is available for streaming:

systemctl --user enable spotifyd.service

As this is a user service, the service will only start after logging in. We can fix that by enabling lingering for the OSMC user:

sudo loginctl enable-linger osmc

This will start a user manager for this user on boot. Reboot to make sure that the spotifyd service starts on boot.

Your old and (formerly) useless Apple TV is now a Spotify receiver!

Put it in a nice place, attach speakers to the RCA jacks and enjoy.

Picture of the first generation Apple TV connected to a TV

But wait, there is more! We can now stream audio from the official Spotify apps on our phones and computers, but to be a truly standalone device, we need to install a Spotify client on the Apple TV. As running a full desktop and using the Spotify desktop client is too heavy for the limited hardware, we are going to use the terminal instead.

3. Install spotify-tui

Spotify TUI is a Spotify client for the terminal. We can either control it directly on the Apple TV or use SSH to control it remotely which makes it perfect for this use case.

Screenshot of spotify-tui

Build spotify-tui

As we already prepared the system for compiling Rust applications, we can easily compile it with:

sudo apt-get install libssl-dev
cargo install spotify-tui

This can take up to three hours to finish.

Configure spotify-tui

To configure spotify-tui, it is best to read the manual on connecting to the Spotify API. A small problem is that spotify-tui uses the browser to authenticate the client with Spotify. Copy the URL that is shown in the configuration wizard and paste it in a browser. After logging in it will redirect to a callback URL on localhost:8888 that is not reachable. Open a second console on the Apple TV (Ctrl+Alt+F2) or open a second SSH session. Run:

curl http://localhost:8888/callback?code=...

Spotify-tui should now be configured and running. Spotify-tui is only a client and can not play audio. Press D on the keyboard and select the spotifyd client to output audio to the daemon we set up earlier. Press ? on the keyboard to enter the help menu to familiarise yourself with the client.

Start spotify-tui

To run spotify-tui on either the Apple TV directly or through an SSH session:

spt

TV displaying spotify-tui

Wrapping it up

The formerly useless Apple TV can now be used as a Spotify streamer and player. By installing Linux, spotifyd and spotify-tui we were able to give meaning to what was essentially a paperweight. And with the low purchase price, built-in power supply, aluminium casing, high-quality DAC and optical audio output it easily tops a solution based on a Raspberry Pi or similar.

Sources

Pages that helped in the creation of this article:

OSMC

Spotifyd

Spotify TUI