Skip to content

Instantly share code, notes, and snippets.

@mxschmitt
Last active August 7, 2023 23:41
Show Gist options
  • Save mxschmitt/e2ab50b9d68f983a6a48e56e0c5ba330 to your computer and use it in GitHub Desktop.
Save mxschmitt/e2ab50b9d68f983a6a48e56e0c5ba330 to your computer and use it in GitHub Desktop.
Librespot -> ffmpeg -> vlc http streaming -> Spotify connect
#!/bin/bash
# Using the Librespot client libary for streaming the Spotfiy connect sound via http
# Instruction:
# 1. Drop this script into the root directory of the librespot repository => https://github.com/plietar/librespot
# 2. Install the dependencies => "sudo apt-get install vlc ffmpeg"
# 3. Set the correct privileges => "chmod +x start.sh"
# 4. Set the librespot directory owner to a non-root user
# 5. Switch to this user
# 6. Change the variables under this instruction for your needs
# 7. Start it by executing it => "./start.sh" or "bash start.sh" whatever :)
LIBR_DEVICE_NAME="Server"
LIBR_USERNAME="yourUsername"
LIBR_CACHE_DIR="cache"
LIBR_PORT="8081"
LIBR_STREAMING_PATH="/stream.mp3"
LIBR_LIBRESPOT_TYPE="release"
#Functions
function redMessage {
echo -e "\\033[31;1m${@}\033[0m"
}
function magentaMessage {
echo -e "\\033[35;1m${@}\033[0m"
}
# Main progress
function startMainProgress {
magentaMessage "Using username ${LIBR_USERNAME} with device name ${LIBR_DEVICE_NAME}"
magentaMessage "Using 0.0.0.0:portpath :${LIBR_PORT}${LIBR_STREAMING_PATH}"
target/${LIBR_LIBRESPOT_TYPE}/librespot --cache ${LIBR_CACHE_DIR} --name ${LIBR_DEVICE_NAME} --username ${LIBR_USERNAME} --backend pipe | ffmpeg -f s16le -ar 44.1k -ac 2 -i pipe:0 -f mp3 - | cvlc - vvv input_stream --sout "#standard{access=http,mux=mp3,dst=:${LIBR_PORT}${LIBR_STREAMING_PATH}}"
}
# Make sure the script can only be run as non-root
if [[ $EUID == 0 ]]; then
redMessage "This script must be run as non-root (because of vlc)"
exit 1
fi
# Check if the credentials exits -> start normally if not let the user input the password
if [ ! -f "$LIBR_CACHE_DIR/credentials.json" ]; then
redMessage "At the first start you need to start librespot manually to enter the password."
magentaMessage "After you entered the password wait until librespot finished starting up and then exit it by using ctrl+c"
trap startMainProgress INT
target/${LIBR_LIBRESPOT_TYPE}/librespot --cache $LIBR_CACHE_DIR --name $LIBR_DEVICE_NAME --username $LIBR_USERNAME
else
startMainProgress
fi
@irgendwr
Copy link

irgendwr commented Jan 3, 2017

typo in l.36
fixed

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