Skip to content

Instantly share code, notes, and snippets.

@MineRobber9000
Created July 26, 2020 03:23
Show Gist options
  • Save MineRobber9000/0a942c406ac4bdf273c17804f5fd3eb3 to your computer and use it in GitHub Desktop.
Save MineRobber9000/0a942c406ac4bdf273c17804f5fd3eb3 to your computer and use it in GitHub Desktop.
Broadcast from liquidsoap with custom metadata
#!/usr/bin/liquidsoap
set("log.file", false)
# Allow a UNIX domain socket server. (You can make a telnet server, but the UNIX domain socket is easier for me to grok.)
set("server.socket",true)
set("server.socket.path","/home/khuxkm/RadioMineRobber/socket")
# This is your input source. In this example, it's just the tracks from `tracks.pls`, but it could be soundcard input or line-in
pl = mksafe(playlist("tracks.pls"))
# This is where the magic happens.
# This allows you to connect to the server and change metadata like artist name, title, etc.
pl = server.insert_metadata(id="meta",pl)
# replace this info with yours, or use your own output block (use `pl` as your source)
output.icecast(%mp3,host="radio.tildeverse.org",port=8005,mount="/",user="khuxkm",password="NiCeTrYhAcKeRs",pl,on_stop=shutdown)
#!/usr/bin/python3
"""
A simple script to change metadata from the command line.
---
usage: ./change_metadata [-h] metadata [metadata ...]
Change metadata on a running instance of broadcast_with_metadata.liq.
positional arguments:
metadata The metadata to change. First the field, then the value. Errors
if a non-even number of arguments are given.
optional arguments:
-h, --help show this help message and exit
Example: ./change_metadata title 'Title of the song' artist 'Artist'
"""
import socket, argparse, time
parser = argparse.ArgumentParser(description="Change metadata on a running instance of broadcast_with_metadata.liq.",epilog="Example: ./change_metadata title 'Title of the song' artist 'Artist'")
parser.add_argument("metadata",nargs="+",help="The metadata to change. First the field, then the value. Errors if a non-even number of arguments are given.")
args = parser.parse_args()
assert (len(args.metadata)%2)==0,"Non-even amount of arguments given."
assert not any([(i%2)==0 and len(c.split())>1 for i,c in enumerate(args.metadata)]),"Field names may not contain a space!"
out = []
for i in range(0,len(args.metadata),2):
val = args.metadata[i+1].replace('"','\\"')
out.append(f"{args.metadata[i]}=\"{val}\"")
command = "meta.insert "+",".join(out)+"\n"
print(command.strip())
s = socket.socket(socket.AF_UNIX)
s.connect("/home/khuxkm/RadioMineRobber/socket")
s.send(command.encode("utf-8"))
time.sleep(2)
print(s.recv(2**16).decode("utf-8"))
s.close()
#!/usr/bin/python3
"""
A simple script to change metadata from a file. Modify "metadata.txt", and the program will update metadata from it.
Metadata should be given as CSV, for instance:
```
title,This is the title of the song
artist,This is the artist who wrote the song
```
"""
import socket, time, csv, threading
from inotify.adapters import Inotify
def change_metadata(**kwargs):
out = []
for k,v in kwargs.items():
val = v.replace('"','\\"')
out.append(f"{k}=\"{val}\"")
command = "meta.insert "+",".join(out)+"\n"
s = socket.socket(socket.AF_UNIX)
s.connect("/home/khuxkm/RadioMineRobber/socket")
s.send(command.encode("utf-8"))
time.sleep(2)
s.recv(2**16)
s.recv(2**16)
s.shutdown(socket.SHUT_RDWR)
s.close()
del s
i = Inotify()
i.add_watch("/home/khuxkm/RadioMineRobber")
while True:
changed = False
for _, event_types, path, filename in i.event_gen(yield_nones=False,timeout_s=10):
# print(time.time(),path,filename,event_types)
if filename=="metadata.txt" and "IN_CLOSE_WRITE" in event_types:
changed = True
if not changed: continue
try:
with open("metadata.txt","r") as f:
metadata = {x[0]: x[1] for x in csv.reader(f)}
t = threading.Thread(target=change_metadata,kwargs=metadata)
t.start()
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment