Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active March 11, 2025 13:41
Show Gist options
  • Save ZiTAL/3dce824e4020daa8190aadac8d23ea77 to your computer and use it in GitHub Desktop.
Save ZiTAL/3dce824e4020daa8190aadac8d23ea77 to your computer and use it in GitHub Desktop.
liquidsoap / icecast2: switch from stream, playlist and silent via telnet without stopping the stream
{
"server_host": "0.0.0.0",
"server_port": 1234,
"icecast":
[
{
"host": "localhost",
"port": 8000,
"password": "passwd",
"type": "mp3",
"mount": "/stream.mp3"
},
{
"host": "localhost",
"port": 8000,
"password": "passwd",
"type": "ogg",
"mount": "/stream.ogg"
}
]
}
#!/usr/bin/env liquidsoap
settings.log.file.set(false)
let json.parse (config : {
server_host: string,
server_port: int,
icecast: [{
host: string,
port: int,
password: string,
type: string,
mount: string
}]
}) = file.contents("radio.json")
settings.server.telnet.set(true)
settings.server.telnet.port.set(config.server_port)
settings.server.telnet.bind_addr.set("0.0.0.0")
pause_playlist = interactive.bool("pause_playlist", true)
s = input.http("", timeout=5.)
p = playlist("silence.m3u", reload_mode="manual")
b = blank()
radio = switch(track_sensitive=false, [
({true}, s),
({pause_playlist() == false}, p),
({true}, b)
])
radio = normalize(radio)
def setPlaylist(uri) =
# echo "set_playlist /path/to/playlist" | nc localhost 1234
p.reload(uri=uri)
"Playlist set to #{uri}"
end
def startPlaylist(_)
pause_playlist := false
"Playlist started"
end
def stopPlaylist(_)
pause_playlist := true
"Playlist stopped"
end
def stopStream(_)
s.stop()
"Stream stopped"
end
def setStream(uri) =
# echo "set_stream http://stream" | nc localhost 1234
s.stop()
s.set_url(uri)
s.start()
"Stream set to #{uri}"
end
def getEncoder(type) =
if type == 'mp3' then
%mp3.cbr(bitrate=128, samplerate=44100, stereo=true)
elsif type == 'ogg' then
%vorbis.cbr(bitrate=128, samplerate=44100, channels=2)
else
%mp3.cbr(bitrate=128, samplerate=44100, stereo=true)
end
end
server.register("set_playlist", setPlaylist)
server.register("start_playlist", startPlaylist)
server.register("stop_playlist", stopPlaylist)
server.register("set_stream", setStream)
server.register("stop_stream", stopStream)
let icecast_list = config.icecast
list.iter(fun (ic) ->
output.icecast(
getEncoder(ic.type),
host=ic.host,
port=ic.port,
password=ic.password,
mount=ic.mount,
name="radio name",
description="radio desc",
genre="Various",
url="http://radio.localhost",
public=true,
radio
)
, icecast_list)
#!/bin/bash
# set playlist
echo "set_playlist /path/to/playlist" | nc -w 1 localhost 1234
# start playlist
echo "start_playlist" | nc -w 1 localhost 1234
# stop playlist
echo "stop_playlist" | nc -w 1 localhost 1234
# set stream
echo "set_stream http://stream" | nc -w 1 localhost 1234
# stop stream
echo "stop_stream" | nc -w 1 localhost 1234
@ZiTAL
Copy link
Author

ZiTAL commented Mar 7, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment