#!/bin/sh #{{{ printhelp printhelp () { printf "amg: Auto Music Grabber; script to download, format, and tag music.\n" printf " -h) prints this help message.\n" printf " -u X) uses X for the URL to download from.\n" printf " -s) processes the url as a single track as a single song.\n" printf " -l) processes the url as a single track as an album.\n" printf " -p) processes the yrk as a playlist as an album.\n" printf " -a X) sets the artist name to X.\n" printf " -A X) sets the album name to X.\n" printf " -t X) sets the track name to X (only for a single song).\n" printf " -T X) sets the track number to X (only for a single song).\n" printf " -y X) sets the date to X.\n" exit } #}}} #{{{ getopts unset ALBUM ARTIST MODE TITLE TRACKNUM YEAR while getopts "hslpu:a:A:t:T:y:" arg; do case "${arg}" in s) MODE='single' ;; l) MODE='split' ;; p) MODE='album' ;; u) URL="$OPTARG" ;; a) ARTIST="$OPTARG" ;; A) ALBUM="$OPTARG" ;; t) TITLE="$OPTARG" ;; T) TRACKNUM="$OPTARG" ;; y) YEAR="$OPTARG" ;; *) printhelp ;; esac done #}}} #{{{ print given args printf "Received Tags: Artist: %s Album: %s Song: %s Number: %s Year: %s\n\n" "$ARTIST" "$ALBUM" "$TITLE" "$TRACKNUM" "$YEAR" #}}} #{{{ fill in missing variables # prompt to determine later tagging if [ -z "$MODE" ]; then printf "Will the link be for a [p]laylist, a[l]bum video, or [s]ong video? " read -r RESPONSE case "$RESPONSE" in p) MODE='album' ;; l) MODE='split' ;; s) MODE='single' ;; *) printhelp ;; esac printf "\n" fi # prompt to get the URL if [ -z "$URL" ]; then printf "Enter the link: " read -r URL printf "\n" fi #}}} #{{{ base logic for certain modes # if song, don't download playlist case "$MODE" in s) NO_PLAYLIST="yes" ;; l) NO_PLAYLIST="yes" unset TITLE TRACKNUM ;; p) unset TITLE TRACKNUM ;; esac #}}} #{{{ download the video(s) printf "Starting download...\n\n" if [ "$(find . | wc -l)" -gt 0 ]; then printf "Already downloaded\n" else yt-dlp ${NO_PLAYLIST:+"--no-playlist"} --format bestaudio --restrict-filenames "$URL" fi printf "Download stopped.\n\n" #}}} #{{{ while loop to remove files from list printf "\n\nDo you want to remove any tracks?\n\n" while true; do find . | nl printf "Enter the line number of the file to remove:\n" read -r WHILE_RESPONSE [ -z "$WHILE_RESPONSE" ] && break rm -rf "$(find . | head -n "$WHILE_RESPONSE" | tail -1)" done #}}} #{{{ convert the downloaded files to mp3 printf "\n\nStarting to convert files..." for i in *.m4a *.webm; do imp3=$(echo "$i" | sed 's/\.m4a$/.mp3/;s/\.webm$/.mp3/') [ ! -f "$imp3" ] && ffmpeg -i "$i" -acodec mp3 "$imp3" || echo "File not converted" done printf "Finished converting files.\n\n" #}}} #{{{ tag files printf "\n\n" for file in *.mp3; do printf "%s\n" "$file" if [ -z "$ARTIST" ]; then printf "\nWho is the artist? " read -r ARTIST fi if [ -z "$ALBUM" ]; then printf "\nWhat is the album? " read -r ALBUM fi if [ -z "$TITLE" ]; then printf "\nWhat is the title? " read -r TITLE fi if [ -z "$TRACKNUM" ]; then printf "\nWhat is the track number? " read -r TRACKNUM_ TRACKNUM="$(printf "%02d" "$TRACKNUM_")" unset TRACKNUM_ fi if [ -z "$YEAR" ]; then printf "\nWhat is the release date? " read -r YEAR fi case "$TRACKNUM" in [!0-9]*) unset TRACKNUM printf "\n%s doesn't look like a track number!\n" "$TRACKNUM" 1>&2 ;; esac FMT_ARTIST="$(echo "$ARTIST" | sed "s/[^[:alnum:].-]/_/g")" FMT_ALBUM="$(echo "$ALBUM" | sed "s/[^[:alnum:].-]/_/g")" FMT_TITLE="$(echo "$TITLE" | sed "s/[^[:alnum:].-]/_/g")" [ -n "$ARTIST" ] && kid3-cli -c "set \"Artist\" \"$ARTIST\"" "$file" [ -n "$ALBUM" ] && kid3-cli -c "set \"Album\" \"$ALBUM\"" "$file" [ -n "$TITLE" ] && kid3-cli -c "set \"Title\" \"$TITLE\"" "$file" [ -n "$TRACKNUM" ] && kid3-cli -c "set \"Track Number\" \"$TRACKNUM\"" "$file" [ -n "$YEAR" ] && kid3-cli -c "set \"Date\" \"$YEAR\"" "$file" printf "\n\n%s\n" "$file" kid3-cli -c get "$file" NEWFILENAME="${FMT_ARTIST}_-_${FMT_ALBUM}_-_${TRACKNUM:-00}_${FMT_TITLE}.mp3" #NEWFILENAME="$(printf "%s" "$NEWFILENAME" | sed 's/ /_/g;s/[!@#$%^&*()-_=+{}\|;:,.<>/?`~'"\[\]\'\""']//g')" [ -n "${ARTIST}" ] && [ -n "${ALBUM}" ] && [ -n "${TITLE}" ] && mv "$file" "$NEWFILENAME" unset TITLE TRACKNUM printf "%s\n\n\n" "$NEWFILENAME" done printf "\n\nDone.\n" #}}}