Skip to content

Instantly share code, notes, and snippets.

@bonnebulle
Last active August 11, 2025 20:29
Show Gist options
  • Select an option

  • Save bonnebulle/8a245ecbc4acee7ef49108e64cedba66 to your computer and use it in GitHub Desktop.

Select an option

Save bonnebulle/8a245ecbc4acee7ef49108e64cedba66 to your computer and use it in GitHub Desktop.
Help find Album (with artist+title in filename) using MusicBrainz API
#!/bin/bash
# GIST
# https://gist.github.com/bonnebulle/8a245ecbc4acee7ef49108e64cedba66
# USED BY (part of)
# https://gist.github.com/bonnebulle/749b879f693a689dab10b836c2cb9745
# DEP yq
# https://github.com/mikefarah/yq/releases/tag/v4.47.1
### TODO autre piste (sans json/YQ) ==> https://github.com/emacsmirror/musicbrainz
# Script pour trouver le nom d'un album à partir du titre et de l'artiste
# Utilise l'API MusicBrainz
# DEBUG_PLS="yes" # FORCE SEARCH+ADD ALBUM from MusicBrainz
# Configuration
API_BASE="https://musicbrainz.org/ws/2"
USER_AGENT="find-album-script/1.0 ([email protected])"
DELAY=1 # Délai entre les requêtes (1 seconde minimum requis par MusicBrainz)
# Couleurs pour l'affichage
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Fonction d'aide
show_help() {
echo -e "${BLUE}Usage:${NC}"
echo " $0 \"Titre de la chanson\" \"Nom de l'artiste\""
echo " $0 -f fichier.mp3 # Extrait automatiquement les métadonnées"
echo ""
echo -e "${BLUE}Options:${NC}"
echo " -h, --help Afficher cette aide"
echo " -f, --file Utiliser un fichier MP3 (requiert ffprobe)"
echo " -v, --verbose Affichage détaillé"
echo ""
echo -e "${BLUE}Exemples:${NC}"
echo " $0 \"Bohemian Rhapsody\" \"Queen\""
echo " $0 -f \"ma_chanson.mp3\""
}
# Fonction pour encoder les URL
url_encode() {
python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$1"
}
# Fonction pour extraire les métadonnées d'un fichier MP3
extract_metadata() {
local file="$1"
if ! command -v ffprobe &> /dev/null; then
echo -e "${RED}Erreur: ffprobe n'est pas installé. Installez ffmpeg.${NC}" >&2
return 1
fi
if [[ ! -f "$file" ]]; then
echo -e "${RED}Erreur: Le fichier '$file' n'existe pas.${NC}" >&2
return 1
fi
echo -e "${YELLOW}Extraction des métadonnées de: $file${NC}"
# Extraire le titre et l'artiste avec ffprobe
title_raw=$(ffprobe -v quiet -show_entries format_tags=title -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null)
title=$(echo "$title_raw" | sed 's/ (.*)//') # Supprime tout ce qui est entre parenthèses
artist=$(ffprobe -v quiet -show_entries format_tags=artist -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null)
if [[ -z "$title" || -z "$artist" ]]; then
echo -e "${RED}Erreur: Impossible d'extraire le titre et/ou l'artiste du fichier.${NC}" >&2
return 1
fi
echo -e "${GREEN}Titre trouvé: $title${NC}"
echo -e "${GREEN}Artiste trouvé: $artist${NC}"
echo ""
}
# Fonction pour rechercher un enregistrement
search_recording() {
local title="$1"
local artist="$2"
echo -e "${YELLOW}Recherche de l'enregistrement...${NC}"
# Construire la requête de recherche
local query="recording:\"$title\" AND artist:\"$artist\""
local encoded_query=$(url_encode "$query")
local search_url="$API_BASE/recording?query=$encoded_query&fmt=json&limit=5"
if [[ "$VERBOSE" == "true" ]]; then
echo -e "${BLUE}URL de recherche: $search_url${NC} OK"
fi
# Effectuer la recherche
local response=$(curl -s -H "User-Agent: $USER_AGENT" "$search_url")
# echo "$search_url"
# sleep 10000
# Extraire et afficher les albums avec yq
if ! echo "$response" | yq eval '.releases[] | .title' -; then
echo -e "${RED}Erreur lors du traitement de la réponse avec yq${NC}" >&2
return 1
fi
if [[ $? -ne 0 ]]; then
echo -e "${RED}Erreur: Impossible de contacter l'API MusicBrainz${NC}" >&2
return 1
fi
echo -e "${YELLOW}Réponse de l'API:${NC}"
echo
echo "$response" | jq . # Formate la réponse JSON pour une meilleure lisibilité
echo
echo "---------"
# echo "$response" | yq eval '.recordings[].releases[].release-group[]'
echo "$response" | yq eval '.recordings[].releases[].["release-group"].title' -
ALBUM_NAME_ALL_LINES=$(echo "$response" | yq eval '.recordings[].releases[].["release-group"].title' -)
# Supposons que ALBUM_NAME_ALL_LINES contient plusieurs lignes
echo "$ALBUM_NAME_ALL_LINES" | while IFS= read -r line; do
if [[ "$line" != "True" ]]; then
ALBUM_NAME="$line"
echo "ooooooo"
echo $ALBUM_NAME
echo "ooooooo"
if [[ $ALBUM_NAME != "" ]];then
ALBUM=$(
### GO
eyeD3 "$MP3_FILE" | grep -i 'album:' | sed 's/album: //'
)
# if [[ "$ALBUM" == "" ]];then
if [[ ("$ALBUM" == "favorites_306446955") || ("$ALBUM" == "") || ($DEBUG_PLS == "yes") ]]; then
echo "============================="
echo "ALBUM_NAME == $ALBUM_NAME"
echo "============================="
echo
eyeD3 --album "$ALBUM_NAME" "$MP3_FILE"
echo $MP3_FILE
echo "============================="
echo
echo "============================="
echo "ALBUM_NAME == $ALBUM_NAME"
echo "============================="
sleep 2
elif [[ "$ALBUM" == "" ]];then
echo
eyeD3 "$file"
echo "============================="
echo "$ALBUM ---- DEJA PRESENT !!!!"
echo "============================="
elif [[ "$ALBUM_NAME" == "" ]];then
echo
echo "============================="
echo "!!!!!!! NO ALBUM FOUND !!!!"
echo "============================="
sleep 3
fi
else
echo -e "${RED}Erreur: album_is vide${NC}" >&2
fi
echo "---------"
echo
return;
fi
done
}
# Variables globales
VERBOSE=false
# Traitement des arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-f|--file)
if [[ -z "$2" ]]; then
echo -e "${RED}Erreur: L'option -f requiert un nom de fichier${NC}" >&2
exit 1
fi
FILE_MODE=true
MP3_FILE="$2"
shift 2
;;
-v|--verbose)
VERBOSE=true
shift
;;
-*)
echo -e "${RED}Option inconnue: $1${NC}" >&2
show_help
exit 1
;;
*)
if [[ -z "$TITLE" ]]; then
TITLE="$1"
elif [[ -z "$ARTIST" ]]; then
ARTIST="$1"
else
echo -e "${RED}Trop d'arguments${NC}" >&2
show_help
exit 1
fi
shift
;;
esac
done
# Afficher l'état du mode verbose si activé
if [[ "$VERBOSE" == "true" ]]; then
echo -e "${BLUE}Mode verbose activé${NC}"
fi
# Vérification des dépendances
if ! command -v curl &> /dev/null; then
echo -e "${RED}Erreur: curl n'est pas installé${NC}" >&2
exit 1
fi
if ! command -v python3 &> /dev/null; then
echo -e "${RED}Erreur: python3 n'est pas installé${NC}" >&2
exit 1
fi
# Mode fichier MP3
if [[ "$FILE_MODE" == "true" ]]; then
if ! extract_metadata "$MP3_FILE"; then
exit 1
fi
TITLE="$title"
ARTIST="$artist"
fi
# Vérification des paramètres requis
if [[ -z "$TITLE" || -z "$ARTIST" ]]; then
echo -e "${RED}Erreur: Le titre et l'artiste sont requis${NC}" >&2
show_help
exit 1
fi
echo -e "${BLUE}=== Recherche d'album ===${NC}"
echo -e "${GREEN}Titre: $TITLE${NC}"
echo -e "${GREEN}Artiste: $ARTIST${NC}"
echo ""
# Rechercher l'enregistrement
if ! mbid=$(search_recording "$TITLE" "$ARTIST"); then
echo -e "${RED}Erreur: échec de la recherche d'enregistrement${NC}"
exit 1
fi
echo -e "${GREEN}ID de l'enregistrement: $mbid${NC}"
echo ""
# Récupérer les albums
# get_albums "$mbid"
echo -e "${BLUE}Recherche terminée.${NC}"
echo
sleep 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment