playerctl: get currently playing music

Date

Tags
#short #nuggets #selfhosting

TLDR

To get information about music currently playing on the computer, run:

playerctl metadata --all-players --format '{{ status }}: {{ artist }} - {{ title }}'

A wide variety of media players are MPRIS-enabled and can be queried using the above command, including media players running in the browser such as Airsonic.

playerctl on man.archlinux.org

Explanation

For a long time, I've been using Airsonic as my media player, handy if you are into selfhosted services and despise streaming platforms with a passion.

Since I also do live streaming and like to play ambient music in the background, I attempted to build a little "Now playing" widget for on stream.

Sadly, no luck: Airsonic doesn't have an API.

After a lot of trying out different clients and hosting additional services, I settled on syncing my music collection from the NAS to my computer and then playing music through mopidy, as I could then query the currently playing music using MPD:

# Snippet will be added soon, currently away from computer

Not my favorite solution but it works.

In comes this guy DeavidSedice and he tells me I can get that same information from most existing media players with a single line of bash:

# The command as he sent it to me
playerctl metadata --all-players --format '{{ status }}: {{ artist }} - {{ title }}               ' | grep Playing | tr '\n' ' '

Why did I never hear about this before? A life changer! And it even works with Airsonic running in the browser.

This tool makes it quite trivial to write a bash script that loops the command, writes the output to a text file and have OBS display it on stream.