summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzachir <zachir@librem.one>2023-08-16 13:21:21 -0500
committerzachir <zachir@librem.one>2023-08-16 13:21:21 -0500
commitb2c69c3e087608ffd81eb08dbde530422bb4032f (patch)
tree05e72616816594db20436f46cdf7223af8714e72
parentcf8b3ea27cb1a5eb14a1c1a932f908e165653d88 (diff)
amg will replace songgrab
-rwxr-xr-xamg161
1 files changed, 161 insertions, 0 deletions
diff --git a/amg b/amg
new file mode 100755
index 0000000..9162e6b
--- /dev/null
+++ b/amg
@@ -0,0 +1,161 @@
+#!/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: $ARTIST
+Album: $ALBUM
+Song: $TITLE
+Number: $TRACKNUM
+Year: $YEAR\n\n"
+#}}}
+
+#{{{ 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 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 URL
+ printf "\n"
+fi
+#}}}
+
+#{{{ base logic for certain modes
+# if song, don't download playlist
+case "$MODE" in
+ s)
+ SPLITTER_FLAGS="--no-playlist"
+ ;;
+ l)
+ SPLITTER_FLAGS="--no-playlist"
+ unset TITLE TRACKNUM
+ ;;
+ p)
+ unset TITLE TRACKNUM
+ ;;
+esac
+#}}}
+
+#{{{ download the video(s)
+printf "Starting download...\n\n"
+if [ "$(ls | wc -l)" -gt 0 ]; then
+ printf "Already downloaded\n"
+else
+ yt-dlp $SPLITTER_FLAGS --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
+ ls | nl
+ echo -n "Enter the line number of the file to remove:\n"
+ read WHILE_RESPONSE
+ [ -z "$WHILE_RESPONSE" ] && break
+ rm -rf `ls | head -n "$WHILE_RESPONSE" | tail -1`
+done
+#}}}
+
+#{{{ convert the downloaded files to mp3
+printf "\n\nStarting to convert files..."
+for i in `ls *.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 `ls *.mp3`; do
+ printf "$file\n"
+ if [ -z "$ARTIST" ]; then
+ printf "\nWho is the artist? "
+ read ARTIST
+ fi
+ if [ -z "$ALBUM" ]; then
+ printf "\nWhat is the album? "
+ read ALBUM
+ fi
+ if [ -z "$TITLE" ]; then
+ printf "\nWhat is the title? "
+ read TITLE
+ fi
+ if [ -z "$TRACKNUM" ]; then
+ printf "\nWhat is the track number? "
+ read TRACKNUM_
+ TRACKNUM="$(printf "%02d" "$TRACKNUM_")"
+ unset TRACKNUM_
+ fi
+ if [ -z "$YEAR" ]; then
+ printf "\nWhat is the release date? "
+ read YEAR
+ fi
+ case "$TRACKNUM" in
+ [!0-9]*)
+ unset TRACKNUM
+ printf "\n$TRACKNUM doesn't look like a track number!\n" 1>&2
+ ;;
+ esac
+ [ -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$file\n"
+ kid3-cli -c get "$file"
+ printf "\n"
+ NEWFILENAME="${ARTIST}_-_${ALBUM}_-_${TRACKNUM:-00}_${TITLE}.mp3"
+ NEWFILENAME="$(printf "%s" "$NEWFILENAME" | sed 's/ /_/g;s/[!@#$%^&*()-_=+{}\|;:,.<>/?`~'"\[\]\'\""']//g')"
+ [ -n "${ARTIST}" -a -n "${ALBUM}" -a -n "${TITLE}" ] && mv "$file" "$NEWFILENAME"
+ unset TITLE TRACKNUM
+ printf "\n$NEWFILENAME\n"
+done
+
+printf "\n\nDone.\n"
+#}}}