Using MPRIS D-Bus to access track metadata

Posted by Felipe Arturo López Bonilla on

Introduction

A while back, I came across a cool project called Sunamu. As their website says, "It's a fancy music controller". This means you can use it to control other media players like Spotify and VLC. Just keep in mind that Sunamu currently only works on Linux systems.

I became curious about how Sunamu controlled players like Spotify. After digging into the code, I discovered it used MPRIS D-Bus. Curious to learn more, I continued researching MPRIS D-Bus. Thanks to the information provided by D-Bus, I was able to develop some useful examples and projects.

MPRIS D-bus

According to the documentation, the MPRIS bus [1] is a standard D-bus [2] that provides an API for controlling media players like Spotify and VLC. It also allows you to retrieve information about each item in the player's tracklist.

Each media player registers a unique name on the D-Bus that starts with org.mpris.MediaPlayer2. For instance, the Spotify desktop application uses the name org.mpris.MediaPlayer2.spotify.

Media players use signals to notify clients, like the Sunamu desktop application, about changes in their state (playing, paused, stopped, etc.). This allows other applications to constantly monitor and receive these signals.

One major advantage of MPRIS D-Bus is that it publishes metadata about the currently playing track. This metadata includes information like artist, title, album, etc. We can parse this information, which will be useful for the projects I'll describe later.

Project examples

The following projects demonstrate how to receive metadata about the current track playing on Spotify. However, it's important to note that these projects are currently only functional on Linux systems.

Python scripts

I've developed some Python examples to demonstrate MPRIS functionality. You can find them on GitHub in the mpris-example repository. These programs monitor the metadata of the current track playing on Spotify by utilizing the mpris2 Python library to interact with the MPRIS D-Bus.

Spotify track info on LED matrix display

After building some Python programs to retrieve track information, I got the idea to display this data on a physical interface. Currently, I'm using an LED matrix, but the next step is to try this out on different screens, like those character LCDs and TFT displays. Ultimately, my goal is to create a system that not only displays track information but also controls Spotify playback through an external device.

This project aims to create a system that displays track information on an external device. A Python script retrieves the current track's metadata and publishes it to an MQTT broker (in my case, a docker container). A separate device, a WeMos D1 Mini Pro, subscribes to this information and displays it on an LED matrix display. The following diagram illustrates the interaction between these two devices:

MQTT network diagram

To get started, download and run the Python program spotify-mqtt-publisher. This program will publish the track's metadata to the MQTT broker. Next, download the firmware led-matrix-track-info and flash it onto your WeMos D1 mini. This firmware will allow the device to subscribe to the relevant topics and display the track information on the LED matrix display.

So, with all this info, you're ready to play around with the MPRIS D-Bus and make your own cool projects!

References