Post

Controlling Squeezbox From The Terminal

Using the Lyrion Music Server API from Bash with Curl

Squeezebox Touch

This is a tutorial on controlling a Squeezebox music player from the command line via Lyrion Music Server (LMS) JSON-RPC API. There are lots of ways to control a Squeezbox: from the device itself, LMS web interface, LMS CLI, IR remote control, and various mobile apps. However, I’m in a Linux terminal all day and just need a few commands to control my music without intalling the CLI or opening a web page. I just want the following one-letter shell commands:

  • s - show current playing track
  • m - play a random shuffled mix
  • n - skip to the next track
  • p - pause/resume

What is a Squeezebox and what is Lyrion Music Server?

Squeezebox is a network music player (streamer). It began its life as the SliMP3, introduced in 2001 by Slim Devices. It had an ethernet interface and played MP3 music files from a media server. In 2006, Slim Devices was acquired by Logitech, and they sold many new versions of the Squeezebox that included features like support for more media formats, WiFi, digitial output, touch-screen interfaces, etc.

The hardware was discontinued in 2012, but there are still many ways to use a Squeezebox (besides buying an old one). Several companies manufacture compatible streamers and amps , and many people build their own compatible players using a Raspberry Pi or similar cheap single-board computers or mini-PCs that run a software based player.

To use a Squeezebox, you need to connect it to a cpmpatible media server. Over the years, the official server has been renamed many times: SlimServer, Squeezebox Server, SqueezeCenter, Logitech Media Server, and now Lyrion Music Server. It has many plugins to connect with online streaming services, and allows you to index and play your own music collection. It runs on several different platforms, and is really easy to setup. I run it via Docker on a QNAP NAS.

Thankfully, the original server code and most of the associated utilites were released under an open source license (mostly GPLv2), and it lives on today! The server is written in Perl 5 and still has pretty active development on GitHub with regular releases.

Using the JSON-RPC API

To control it from the terminal, I use simple command-line tools:

These should all be available or easily installed on any Linux distro, macOS, Windows (via MSYS2, Git-Bash, or WSL2), or almost any other platform.

To contol the player, you need to know 2 things:

  • Lyrion Music Server IP address and port (default port is 9000)
  • Squeezebox MAC address

Then you can control the player by sending HTTP POST requests with a JSON payload to the http://IP:PORT/jsonrpc.js endpoint.

Example

My LMS server and Squeezebox player are both connected to my local network. The IP address of my server is 10.0.0.100, and the MAC address of my Squeezebox is 00:04:20:23:82:6f. With this curl command, I can pause my player:

1
2
3
curl --request POST \
    --data '{"id":1,"method":"slim.request","params":["00:04:20:23:82:6f",["pause"]]}' \
    http://10.0.0.100:9000/jsonrpc.js

Code

To have the commands I wanted, I created a file with some Bash functions and gave them really short aliases. I save this as ~/.bashrc_squeezebox and in my ~/.bashrc, I add source ~/.bashrc_squeezebox.

Now, I can just type a one-letter command:

  • s <enter> (show current playing track)
  • m <enter> (play a random shuffled mix)
  • n <enter> (skip to the next track)
  • p <enter> (pause/resume)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# ~/.bashrc_squeezebox
# =============================================================
# Bash functions and aliases for controlling Squeezebox via LMS
# - requires: curl, jq
# =============================================================


LMS_URL="http://10.0.0.100:9000"
SQUEEZEBOX_MAC="00:04:20:23:82:6f"


# send request to Squeezebox player API on local nextwork
send-squeezebox-cmd () {
    local command="$1"
    local payload='
        {
            "id":1,
            "method":"slim.request",
            "params":[
                "'"${SQUEEZEBOX_MAC}"'",
                '"${command}"'
            ]
        }'
    lms_result=$(curl -4 --fail --silent --request POST \
        --max-time 1.0 --data "${payload}" "${LMS_URL}/jsonrpc.js")
    if [ -z "${lms_result}" ]; then
        echo "can't reach LMS or Squeezebox"
    fi
}


# show currently playing track on Squeezebox player
squeezebox-show () {
    send-squeezebox-cmd '["status", "-", "1", "tags:a"]'
    if [ -n "${lms_result}" ]; then
        jq -r '.result.playlist_loop | .[0] | "\(.artist) - \(.title)"' \
            <<< "${lms_result}"
    fi
}
alias s=squeezebox-show


# skip to next track in playlist on Squeezebox player
squeezebox-next () {
    send-squeezebox-cmd '["button","jump_fwd"]'
    squeezebox-show
}
alias n=squeezebox-next


# play random song mix on Squeezebox player
squeezebox-mix () {
    send-squeezebox-cmd '["randomplay","tracks"]'
    squeezebox-show
}
alias m="squeezebox-mix"


# pause/resume audio on Squeezebox player
squeezebox-pause () {
    send-squeezebox-cmd '["pause"]'
}
alias p="squeezebox-pause"
This post is licensed under CC BY 4.0 by the author.