Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Small script that syncs the current now playing (MPD) with Icecast
By Melan
import time
import os
import requests
from mpd import MPDClient
UPDATE_RATE = 5 # Update rate in seconds
MPD_HOST = "localhost"
MPD_PORT = 6600
ICECAST_HOST = "localhost"
ICECAST_MNT = "/nurds"
prev_meta = ""
mpd = MPDClient()
mpd.connect(MPD_HOST, MPD_PORT)
def update_metadata(metadata, prev_meta):
if metadata == prev_meta:
url = "http://%s:%s/admin/metadata" % (ICECAST_HOST, ICECAST_PORT)
data_params = {"mount": ICECAST_MNT, "mode": "updinfo", "song": metadata} # mount=/mystream&mode=updinfo&song=ACDC+Back+In+Black
r = requests.get(url, params=data_params, auth=(ICECAST_AUTH_USR, ICECAST_AUTH_PWD))
while True:
metadata = None
mpd_status = mpd.status()
# When we are playing music
if mpd_status['state'] == "play":
current_song = mpd.currentsong()
# When the metadata contains both artist and title
if "artist" in current_song and "title" in current_song:
metadata = "%s - %s" % (current_song['artist'], current_song['title'])
# When the metadata only contains the title
elif "title" in current_song and not "artist" in current_song:
metadata = current_song['title']
# When the metadata only contains the artist
elif "artist" in current_song and not "title" in current_song:
metadata = current_song['artist']
# When it contains neither, fall back to the file name
metadata = os.path.splitext(os.path.basename(current_song['file']))[:-1]
# If not display MPD status
metadata = "MPD state: %s" % (mpd_status['state'])
if metadata:
update_metadata(metadata, prev_meta)
prev_meta = metadata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment