Convert Figma logo to code with AI

librespot-org logolibrespot

Open Source Spotify client library

4,702
573
4,702
65

Top Related Projects

A Spotify Connect client that mostly Just Works™

A spotify daemon

Open Source Spotify client library

Adblocker for Spotify

Quick Overview

Librespot is an open-source Spotify client library written in Rust. It allows developers to create their own Spotify Connect players and integrate Spotify playback functionality into their applications. Librespot implements the Spotify Connect protocol, enabling seamless streaming of Spotify content on various devices.

Pros

  • Written in Rust, providing memory safety and performance benefits
  • Supports multiple audio backends, including ALSA, PulseAudio, and PortAudio
  • Actively maintained with regular updates and improvements
  • Enables creation of custom Spotify Connect devices without official Spotify SDK

Cons

  • Requires Spotify Premium account for full functionality
  • May occasionally lag behind official Spotify client in terms of feature support
  • Limited documentation compared to official Spotify SDKs
  • Potential legal concerns due to reverse-engineered protocol implementation

Code Examples

  1. Creating a Spotify session:
use librespot::core::session::Session;
use librespot::core::config::SessionConfig;

let session_config = SessionConfig::default();
let session = Session::connect(session_config, credentials).await?;
  1. Playing a track:
use librespot::playback::player::Player;
use librespot::core::spotify_id::SpotifyId;

let player = Player::new(config, session.clone(), None, move || {});
let track_id = SpotifyId::from_base62("spotify:track:0tgVpDi06FyKpA1z0VMD4v").unwrap();
player.load(track_id, true, 0);
  1. Handling events:
use librespot::core::EventSender;

let (event_sender, event_receiver) = tokio::sync::mpsc::unbounded_channel();
let event_sender = EventSender::new(event_sender);

tokio::spawn(async move {
    while let Some(event) = event_receiver.recv().await {
        println!("Received event: {:?}", event);
    }
});

Getting Started

To use librespot in your Rust project, add the following to your Cargo.toml:

[dependencies]
librespot = "0.4"
tokio = { version = "1", features = ["full"] }

Then, in your Rust code:

use librespot::core::config::SessionConfig;
use librespot::core::session::Session;
use librespot::playback::config::PlayerConfig;
use librespot::playback::player::Player;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let session_config = SessionConfig::default();
    let player_config = PlayerConfig::default();
    let session = Session::connect(session_config, credentials).await?;
    let player = Player::new(player_config, session.clone(), None, move || {});
    
    // Your Spotify playback logic here
    
    Ok(())
}

Competitor Comparisons

A Spotify Connect client that mostly Just Works™

Pros of Raspotify

  • Easier setup and installation process, especially for Raspberry Pi users
  • Includes systemd service files for automatic startup
  • Provides a convenient wrapper script for librespot with additional features

Cons of Raspotify

  • Less frequently updated compared to Librespot
  • Limited to Raspberry Pi and Debian-based systems
  • May not include the latest features and improvements from Librespot

Code Comparison

Raspotify (wrapper script):

#!/bin/bash
LIBRESPOT_BINARY=/usr/bin/librespot
CACHE_ARGS="--cache /var/cache/raspotify"
VOLUME_ARGS="--initial-volume 100 --volume-range 24"

Librespot (Rust code):

pub struct SpotifyId {
    pub id: u128,
    pub uri: String,
    pub audio_type: AudioType,
    pub country: Option<Country>,
}

Both projects utilize Librespot as the core library, but Raspotify provides a more user-friendly approach for Raspberry Pi users. Librespot offers greater flexibility and is more frequently updated, while Raspotify simplifies the setup process and includes additional convenience features. The code comparison shows Raspotify's focus on configuration and ease of use, while Librespot's code demonstrates its core functionality as a Spotify client library.

A spotify daemon

Pros of Spotifyd

  • Lightweight and efficient, designed specifically as a Spotify daemon
  • Easy to install and configure, with pre-built binaries available
  • Supports a wide range of audio backends (ALSA, PulseAudio, PortAudio, etc.)

Cons of Spotifyd

  • Limited feature set compared to Librespot
  • Less active development and smaller community
  • Fewer customization options for advanced users

Code Comparison

Spotifyd (Rust):

let (mut stream, creds) = Session::connect(config, credentials, cache, player.clone()).await?;

Librespot (Rust):

let session = Session::connect(config, credentials, cache, device_id).await?;

Both projects use similar connection methods, but Librespot's API is more extensive and flexible.

Summary

Spotifyd is a lightweight and easy-to-use Spotify client, ideal for simple setups and resource-constrained devices. Librespot, on the other hand, offers a more comprehensive feature set and greater flexibility, making it suitable for more complex applications and integrations. While Spotifyd excels in simplicity and efficiency, Librespot provides a more robust foundation for building advanced Spotify-related projects.

Open Source Spotify client library

Pros of librespot

  • Original implementation by the creator of the Spotify protocol reverse engineering
  • More historical context and initial development insights
  • May contain some unique approaches or experimental features

Cons of librespot

  • Less actively maintained and updated
  • Fewer contributors and community involvement
  • Potentially outdated codebase and dependencies

Code Comparison

librespot:

pub struct Session {
    country: String,
    device_id: String,
    username: String,
    // ...
}

librespot-org:

pub struct Session {
    config: SessionConfig,
    device_id: String,
    proxy: Option<Proxy>,
    // ...
}

Summary

librespot is the original implementation by Paul Lietar, offering historical insights into the Spotify protocol. However, it's less actively maintained compared to librespot-org. The latter has more community involvement, regular updates, and a potentially more robust codebase.

librespot-org builds upon the foundation laid by librespot, incorporating improvements and new features over time. It's generally recommended to use librespot-org for current projects due to its active development and broader community support.

The code comparison shows some structural differences in the Session struct, with librespot-org adopting a more modular approach using SessionConfig and additional features like proxy support.

Adblocker for Spotify

Pros of spotify-adblock

  • Specifically designed to block ads in the Spotify desktop client
  • Lightweight and focused on a single purpose
  • Easy to install and use for non-technical users

Cons of spotify-adblock

  • Limited functionality compared to librespot's full-featured approach
  • Only works with the official Spotify desktop client
  • May break with Spotify client updates, requiring frequent maintenance

Code Comparison

spotify-adblock:

static const char *blacklist[] = {
    "https://spclient.wg.spotify.com/ads/",
    "https://spclient.wg.spotify.com/ad-logic/",
    "https://spclient.wg.spotify.com/gabo-receiver-service/",
};

librespot:

pub struct SpotifyId {
    pub id: u128,
    pub id_type: IdType,
}

impl SpotifyId {
    pub fn from_uri(uri: &str) -> Result<SpotifyId, ()> {
        // Implementation details
    }
}

The spotify-adblock code shows a simple blacklist approach to blocking ads, while librespot's code demonstrates a more complex structure for handling Spotify IDs, indicating its broader functionality as a full Spotify client implementation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Build Status Gitter chat Crates.io

Current maintainers are listed on GitHub.

librespot

librespot is an open source client library for Spotify. It enables applications to use Spotify's service to control and play music via various backends, and to act as a Spotify Connect receiver. It is an alternative to the official and now deprecated closed-source libspotify. Additionally, it will provide extra features which are not available in the official library.

Note: librespot only works with Spotify Premium. This will remain the case. We will not support any features to make librespot compatible with free accounts, such as limited skips and adverts.

Quick start

We're available on crates.io as the librespot package. Simply run cargo install librespot to install librespot on your system. Check the wiki for more info and possible usage options.

After installation, you can run librespot from the CLI using a command such as librespot -n "Librespot Speaker" -b 160 to create a speaker called Librespot Speaker serving 160 kbps audio.

This fork

As the origin by plietar is no longer actively maintained, this organisation and repository have been set up so that the project may be maintained and upgraded in the future.

Documentation

Documentation is currently a work in progress, contributions are welcome!

There is some brief documentation on how the protocol works in the docs folder.

COMPILING.md contains detailed instructions on setting up a development environment, and compiling librespot. More general usage and compilation information is available on the wiki. CONTRIBUTING.md also contains our contributing guidelines.

If you wish to learn more about how librespot works overall, the best way is to simply read the code, and ask any questions you have in our Gitter Room.

Issues & Discussions

We have recently started using Github discussions for general questions and feature requests, as they are a more natural medium for such cases, and allow for upvoting to prioritize feature development. Check them out here. Bugs and issues with the underlying library should still be reported as issues.

If you run into a bug when using librespot, please search the existing issues before opening a new one. Chances are, we've encountered it before, and have provided a resolution. If not, please open a new one, and where possible, include the backtrace librespot generates on crashing, along with anything we can use to reproduce the issue, e.g. the Spotify URI of the song that caused the crash.

Building

A quick walkthrough of the build process is outlined below, while a detailed compilation guide can be found here.

Additional Dependencies

We recently switched to using Rodio for audio playback by default, hence for macOS and Windows, you should just be able to clone and build librespot (with the command below). For Linux, you will need to run the additional commands below, depending on your distro.

On Debian/Ubuntu, the following command will install these dependencies:

sudo apt-get install build-essential libasound2-dev

On Fedora systems, the following command will install these dependencies:

sudo dnf install alsa-lib-devel make gcc

librespot currently offers the following selection of audio backends:

Rodio (default)
ALSA
GStreamer
PortAudio
PulseAudio
JACK
JACK over Rodio
SDL
Pipe
Subprocess

Please check the corresponding Compiling entry on the wiki for backend specific dependencies.

Once you've installed the dependencies and cloned this repository you can build librespot with the default backend using Cargo.

cargo build --release

Packages

librespot is also available via official package system on various operating systems such as Linux, FreeBSD, NetBSD. Repology offers a good overview.

Packaging status

Usage

A sample program implementing a headless Spotify Connect receiver is provided. Once you've built librespot, run it using :

target/release/librespot --name DEVICENAME

The above is a minimal example. Here is a more fully fledged one:

target/release/librespot -n "Librespot" -b 320 -c ./cache --enable-volume-normalisation --initial-volume 75 --device-type avr

The above command will create a receiver named Librespot, with bitrate set to 320 kbps, initial volume at 75%, with volume normalisation enabled, and the device displayed in the app as an Audio/Video Receiver. A folder named cache will be created/used in the current directory, and be used to cache audio data and credentials.

A full list of runtime options is available here.

Please Note: When using the cache feature, an authentication blob is stored for your account in the cache directory. For security purposes, we recommend that you set directory permissions on the cache directory to 700.

Contact

Come and hang out on gitter if you need help or want to offer some: https://gitter.im/librespot-org/spotify-connect-resources

Disclaimer

Using this code to connect to Spotify's API is probably forbidden by them. Use at your own risk.

License

Everything in this repository is licensed under the MIT license.

Related Projects

This is a non exhaustive list of projects that either use or have modified librespot. If you'd like to include yours, submit a PR.

  • librespot-golang - A golang port of librespot.
  • plugin.audio.spotify - A Kodi plugin for Spotify.
  • raspotify - A Spotify Connect client that mostly Just Works™
  • Spotifyd - A stripped down librespot UNIX daemon.
  • rpi-audio-receiver - easy Raspbian install scripts for Spotifyd, Bluetooth, Shairport and other audio receivers
  • Spotcontrol - A golang implementation of a Spotify Connect controller. No playback functionality.
  • librespot-java - A Java port of librespot.
  • ncspot - Cross-platform ncurses Spotify client.
  • ansible-role-librespot - Ansible role that will build, install and configure Librespot.
  • Spot - Gtk/Rust native Spotify client for the GNOME desktop.
  • Snapcast - synchronised multi-room audio player that uses librespot as its source for Spotify content