Skip to content

Instantly share code, notes, and snippets.

@gbrks
Created July 28, 2026 07:17
Show Gist options
  • Select an option

  • Save gbrks/c89a3b3b54c9ce9e55ff7b5cf58ffa99 to your computer and use it in GitHub Desktop.

Select an option

Save gbrks/c89a3b3b54c9ce9e55ff7b5cf58ffa99 to your computer and use it in GitHub Desktop.
fix-vinyl.sh
#!/usr/bin/env bash
#
# fix-check.sh — list vinyl imports, let the user narrow to a single album
# with a beets query, validate the tracklist, then reimport with the correct
# MusicBrainz release ID.
set -euo pipefail
# Field separator that won't appear in artist/album/media text (unlike space,
# which breaks on values like `12" Vinyl`).
SEP=$'\t'
# ---------------------------------------------------------------------------
# 1. List all albums imported as vinyl
# ---------------------------------------------------------------------------
echo "=== Vinyl imports ==="
mapfile -t vinyl_rows < <(
beet list -af "\$albumartist${SEP}\$album${SEP}\$media${SEP}https://musicbrainz.org/release/\$mb_albumid" media:vinyl
)
if [ "${#vinyl_rows[@]}" -eq 0 ]; then
echo "No albums tagged as vinyl found."
exit 0
fi
# Print a human-readable version (tabs render fine in a terminal, links stay clickable)
printf '%s\n' "${vinyl_rows[@]}"
echo
# ---------------------------------------------------------------------------
# 2. Ask the user for a query to pick a single album, then let them validate
# the resulting tracklist — looping until they confirm it's correct.
# ---------------------------------------------------------------------------
# Split the first row on the tab separator (safe even if fields contain
# spaces or double quotes, e.g. media = 12" Vinyl).
IFS="$SEP" read -r default_artist default_album default_media _ <<< "${vinyl_rows[0]}"
# Escape any double quotes so they don't break the beets query syntax, and
# strip them from the display default to keep things clean.
escape_for_query() {
printf '%s' "$1" | sed 's/"/\\"/g'
}
esc_artist=$(escape_for_query "$default_artist")
esc_album=$(escape_for_query "$default_album")
default_query="$esc_album"
QUERY=""
while true; do
read -rp "Enter a QUERY to select a single album [default: $default_query]: " user_query
QUERY="${user_query:-$default_query}"
echo
echo "Using query: $QUERY"
echo
echo "=== Matching tracks ==="
beet list -f '($album) disc:$disc track:$track - $artist - $title' "$QUERY"
echo
read -rp "Are these the correct tracks? [y/n]: " confirm
case "$confirm" in
[Yy]*) break ;;
*)
echo "Let's try a different query."
echo
;;
esac
done
# ---------------------------------------------------------------------------
# 3. Ask for the correct MusicBrainz release ID and reimport
# ---------------------------------------------------------------------------
read -rp "Enter the correct MusicBrainz release ID: " MB_ID
if [ -z "$MB_ID" ]; then
echo "No MusicBrainz ID entered, aborting reimport."
exit 1
fi
echo
echo "Reimporting with MusicBrainz ID $MB_ID for query: $QUERY"
beet import --search-id "$MB_ID" -L "$QUERY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment