Skip to content

Instantly share code, notes, and snippets.

@bonelifer
Forked from jladan/my-mpc
Last active January 26, 2025 04:52
Show Gist options
  • Save bonelifer/daec6d717c558963f71f45e7ab92e8ce to your computer and use it in GitHub Desktop.
Save bonelifer/daec6d717c558963f71f45e7ab92e8ce to your computer and use it in GitHub Desktop.
Bash script for easy MPD hotkeys
#!/usr/bin/env bash
# Script to interact with the music queue and manage song ratings and statuses using "stickers"
# The stickers feature is used for marking songs with specific labels (e.g., "broken" or "rating").
# This script supports rating songs, flagging songs as "bad" (broken), removing songs from the queue,
# listing songs flagged as bad, and jumping to a random song in the queue.
# Ratings are compatible with clients such as Cantata, mpedv, and myMPD.
# Function to display usage instructions
usage () {
# Display the command usage with details of available options
echo "$0 <command> <args>"
echo -e "\nCommands:"
echo -e " remove remove current song from queue"
echo -e " random jump to a random song in queue"
echo -e " flag_bad flags current song as bad"
echo -e " list_bad list all songs marked as bad"
echo -e " rate <0-10> rate the current song"
echo -e " ratings <dir> list the ratings for all songs in <dir>"
}
# Function to check if mpc (Music Player Client) is installed
check_mpc_installed () {
# If mpc is not installed, exit the script with an error message
if ! command -v mpc &> /dev/null; then
echo "mpc command not found. Please install it."
exit 1
fi
}
# Function to remove the current song from the queue
remove () {
check_mpc_installed # Ensure mpc is installed before proceeding
mpc del 0 # Remove the song at position 0 in the queue (the current song)
}
# Function to flag the current song as "bad" (broken)
flag_bad () {
check_mpc_installed # Ensure mpc is installed before proceeding
# Get the file path of the current song and set the "broken" sticker to 1 (true)
mpc sticker "$(mpc -f '%file%' current)" set broken 1
}
# Function to list all songs that are marked as "bad" (broken)
list_bad () {
check_mpc_installed # Ensure mpc is installed before proceeding
# Search for all songs with the "broken" sticker set
mpc sticker "" find broken
}
# Function to jump to a random song in the queue
random () {
check_mpc_installed # Ensure mpc is installed before proceeding
mpc random on # Enable random play mode
mpc next # Skip to the next song, which will be random due to random play being enabled
mpc random off # Disable random play mode after jumping to the song
}
# Function to rate the current song (from 0 to 10)
rate () {
check_mpc_installed # Ensure mpc is installed before proceeding
# Validate that the rating is between 0 and 10
if [[ $1 -ge 0 && $1 -le 10 ]]; then
# Apply the rating to the current song
mpc sticker "$(mpc -f '%file%' current)" set rating $1
else
# Print an error message and exit if the rating is not valid
echo "Invalid rating. Please provide a rating between 0 and 10."
exit 1
fi
}
# Function to list all ratings for songs in a specific directory
ratings () {
check_mpc_installed # Ensure mpc is installed before proceeding
# Search for all songs in the specified directory and list their ratings
mpc sticker "$1" find rating
}
# Main script logic: Handle different commands passed as arguments
case $1 in
"remove") remove ;; # Remove the current song from the queue
"flag_bad") flag_bad ;; # Flag the current song as "bad"
"random") random ;; # Jump to a random song in the queue
"list_bad") list_bad ;; # List all songs marked as "bad"
"rate") # Handle song rating
# Ensure the rating argument is provided
if [[ -z "$2" ]]; then
echo "Please provide a rating between 0 and 10."
exit 1
fi
rate $2 ;; # Call the rate function with the provided rating
"ratings") ratings $2 ;; # List ratings for all songs in the specified directory
*) usage ;; # Display usage information if an invalid command is provided
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment