又一个使用 Go 编写的 开源 Spotify 客户端。
go-librespot
Yet another open-source Spotify Connect compatible client, written in Go.
go-librespot gives you the freedom to have a Spotify Connect device wherever you want.
Features • Getting Started • Configuration • Development
To get started you can download prebuilt binaries for the latest release.
Development prebuilt binaries are also available as GitHub Actions artifacts.
A lightweight Docker image for go-librespot is available on the GitHub Container Registry.
An example Docker Compose configuration for PulseAudio is available here.
You can also install go-librespot using Brew on macOS and Linux (thanks @kriive):
brew install go-librespotTo build from source the following prerequisites are necessary:
libogg, libvorbis, flac, mpg123 (plus libasound2 on Linux)To install Go, download it from the Go website.
To install the required libraries on Debian-based systems (Debian, Ubuntu, Raspbian), use:
sudo apt-get install libogg-dev libvorbis-dev libflac-dev libmpg123-dev libasound2-devOn Windows the default backend is WASAPI (default playback device). Install the decode libraries as MinGW packages (MSVC .lib files will not link with CGO), for example with MSYS2:
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-pkg-config mingw-w64-x86_64-libogg mingw-w64-x86_64-libvorbis mingw-w64-x86_64-flac mingw-w64-x86_64-mpg123Cross-compiling a static windows/amd64 binary with vcpkg is described in CROSS_COMPILE.md.
Once prerequisites are installed you can clone the repository and run the daemon with:
go run ./cmd/daemonDetails about cross-compiling go-librespot are described here (thank you @felixstorm).
The default directory for configuration files is ~/.config/go-librespot. On macOS this is
~/Library/Application Support/go-librespot. On Windows it is %APPDATA%\go-librespot. You can change this directory with the
-config_dir flag. The configuration directory contains:
config.yml: The main configuration (does not exist by default)state.json: The player state and credentialslockfile: A lockfile to prevent running multiple instances on the same configurationThe full configuration schema is available here, only the main options are detailed below.
Zeroconf mode enables mDNS auto discovery, allowing Spotify clients inside the same network to connect to go-librespot. This is also known as Spotify Connect.
Backend selection:
go-librespot supports two different backends for mDNS service registration:
You can configure which backend to use via the zeroconf_backend setting in your configuration file:
zeroconf_backend: avahi # Options: "builtin" (default), "avahi"Or via the command line:
go-librespot -c zeroconf_backend=avahisystemd).zeroconf_enabled: true # Whether to keep the device discoverable at all times, even if authenticated via other means
zeroconf_port: 0 # The port to use for Zeroconf, 0 for random
zeroconf_backend: avahi
credentials:
type: zeroconf
zeroconf:
persist_credentials: false # Whether to persist zeroconf user credentials even after disconnectingIf persist_credentials is true, after connecting to the device for the first time credentials will be stored locally
and you can switch to interactive mode without having to authenticate manually.
If zeroconf_interfaces_to_advertise is provided, you can limit interfaces that will be advertised. For example, if you
have Docker installed on your host, you may want to disable advertising to its bridge interface, or you may want to
disable interfaces that will not be reachable.
This mode allows you to associate your account with the device and make it discoverable even outside the network. It requires some manual steps to complete the authentication:
Configure interactive mode
zeroconf_enabled: false # Whether to keep the device discoverable at all times
credentials:
type: interactiveStart the daemon to begin the authentication flow
Open the authorization link in your browser and wait to be redirected
If go-librespot is running on the same device as your browser, authentication should complete successfully
(optional) If go-librespot is running on another device, you'll need to copy the URL from your browser and call it from the device, for example:
curl http://127.0.0.1:36842/login?code=xxxxxxxxThis mode associates your account with the device without a browser on the device itself: Spotify issues a short code which you enter at spotify.com/pair from a phone or computer. Nothing has to listen on a port, so unlike interactive mode there is no redirect URL to copy around when go-librespot runs headless.
Configure device authorization mode
zeroconf_enabled: false # Whether to keep the device discoverable at all times
credentials:
type: device_authStart the daemon to begin the authentication flow
Open the link it logs, or go to spotify.com/pair and enter the code it prints
Approve the request; the daemon picks it up automatically and stores the credentials
With the API server enabled, the same link and code are also served at GET /auth/code for as long as the daemon is
waiting, so a frontend can show them instead of asking the user to read the logs.
Optionally, an API server can be started to control and monitor the player. To enable this feature, add the following to your configuration:
server:
enabled: true
address: localhost # Which address to bind to
port: 3678 # The server port
allow_origin: '' # Value for the Access-Control-Allow-Origin header
cert_file: '' # Path to certificate file for TLS
key_file: '' # Path to key file for TLS
image_size: 'default' # Album art image size (default, small, large, xlarge)For detailed API documentation see here.
Downloaded audio files can be cached on disk so that replaying a track does not download it again from the CDN. Only the raw, still-encrypted files are stored: a cached file is useless without a valid Spotify account, since the audio key is retrieved again on every playback. The cache is disabled by default; once enabled it applies a 1 GB limit, after which the least-recently-used files are evicted.
cache:
enabled: false # Whether to cache downloaded audio files (default: false)
dir: '' # Directory for cached files (default: the XDG cache directory, e.g. $XDG_CACHE_HOME/go-librespot or $HOME/.cache/go-librespot)
size_limit: '1GB' # Maximum total cache size before evicting least-recently-used files ('0' for unlimited)Optionally, the daemon can cache track metadata (name, artists, cover art) in memory and fetch it — via the same
internal API playback uses, not the rate-limited public Web API — for the tracks around the playback position. This
enables a next_track field in GET /status and a GET /context/tracks?uri=... endpoint that lists any playable
context (playlist, album, artist) in order with metadata, so a client can render a browsable song list and start any
entry via POST /player/play with skip_to_uri. Everything is opt-in and disabled by default: a headless speaker has
no use for metadata beyond the playing track and should not pay network requests for it.
metadata:
enabled: false # Cache + fetch metadata around the playback position; enables next_track and /context/tracks
context_sweep: false # Also resolve metadata for the whole context when one starts playing (requires enabled)
max_tracks: 800 # Maximum number of tracks of a context to enumerate and sweepVarious configurations for volume control are available:
go-librespot implements audio normalization according to Spotify's standards, which targets -14 dB LUFS (Loudness Units relative to Full Scale) based on the ITU-R BS.1770 standard. Source
Normalization can be configured with the following options:
normalisation_disabled: false # Whether to disable normalization (default: false)
normalisation_use_album_gain: false # Whether to use album gain instead of track gain (default: false)
normalisation_pregain: 0 # Pregain in dB to apply before normalization (default: 0)暂无开放 Issues,或尚未同步最近议题。