commit ebf5027a28924031d31197f100cb97e6f96ba4b9
parent dde8cce761996722139bac8a18aebd7e9c754f4b
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Fri, 29 May 2020 09:16:49 +0200
Improve control flow and add options for auto-tagging and adding to mpd queue
Diffstat:
1 file changed, 150 insertions(+), 88 deletions(-)
diff --git a/.local/bin/youtube-dl-music b/.local/bin/youtube-dl-music
@@ -1,98 +1,160 @@
#!/bin/sh
-set -ue
+# requirements: youtube-dl, id3tag
-if [ $# -ne 1 ]; then
- printf 'usage: %s <url>\n' "${0##*/}"
- exit 1
-fi
+set -ue
musicroot="/home/music/"
-printf "use torsocks? [Y/n] "
-read -r REPLY
-case "$REPLY" in
- N|n) prefix="";;
- *) prefix="torsocks";;
-esac
-
-oldpwd="$PWD"
-outputdir="$(mktemp -d)"
-mkdir -p "$outputdir"
-cd "$outputdir"
-
-album="$("$prefix" youtube-dl \
- --format bestaudio \
- --extract-audio \
- --audio-quality 0 \
- --audio-format mp3 \
- --output \'%\(playlist_index\)s - %\(title\)s.%\(ext\)s\' \
- "$1" | tee /dev/tty | grep 'Finished downloading' | sed 's/.*: //')"
-
-printf "add metadata? [Y/n] "
-read -r REPLY
-case "$REPLY" in
- N|n)
- mv ./*.mp3 "$oldpwd/"
- cd -
- exit 0;;
-esac
-
-artist="Unknown Artist"
-album="${album:-Unknown Album}"
-track=1
-
-# Loop over files with spaces
-SAVEIFS=$IFS
-IFS=$(printf '\n\b')
-for f in *.mp3; do
-
- [ "$track" = 1 ] && \
- artist="$(printf '%s' "$f" | sed 's/^[0-9]\+ - //' | sed 's/ - .*//' | \
- sed 's/^ //')"
-
- printf '\033[0;31m%s\033[0m\n' "$f"
-
- song=$(printf '%s' "$f" | sed 's/.* - //' | sed 's/^ //' | sed 's/\.mp3//')
- printf 'song [%s]: ' "$song"
- read -r input
- song="${input:-$song}"
-
- printf 'track [%s]: ' "$track"
- read -r input
- track="${input:-$track}"
-
- printf 'album [%s]: ' "$album"
- read -r input
- album="${input:-$album}"
-
- printf 'artist [%s]: ' "$artist"
- read -r input
- artist="${input:-$artist}"
-
- id3tag --artist="$artist" --album="$album" \
- --song="$song" --track="$track" \
- "$f"
- track=$(( track + 1 ))
-done
-
-IFS=$SAVEIFS
-
-printf 'move to music folder (%s)? [Y/n] ' "$musicroot"
-read -r REPLY
-case "$REPLY" in
- N|n)
- echo "leaving mp3's in $oldpwd"
- mv ./*.mp3 "$oldpwd/"
- cd -
- exit 0;;
- *)
+die() {
+ printf '%s: error: %s\n' "${0##*/}" "$1" >&2
+ exit 1
+}
+
+show_help() {
+ printf 'usage: %s [OPTIONS] URL\n' "${0##*/}"
+ echo "downloads music from the web and tags it."
+ echo "Playlists are supported."
+ echo
+ echo "OPTIONS are one or more of the following:"
+ echo " -a add metadata without interaction"
+ echo " -h show this message"
+ echo " -m move output music file to $musicroot"
+ echo " -q add to mpd queue (requires -m)"
+ echo " -t download through torsocks(1)"
+ echo " -- do not consider any following arguments as options"
+}
+
+
+handle_url() {
+ oldpwd="$PWD"
+ outputdir="$(mktemp -d)"
+ mkdir -p "$outputdir"
+ cd "$outputdir"
+
+ album="$($prefix youtube-dl \
+ --format bestaudio \
+ --extract-audio \
+ --audio-quality 0 \
+ --audio-format mp3 \
+ --output \'%\(playlist_index\)s - %\(title\)s.%\(ext\)s\' \
+ "$1" | tee /dev/tty | grep 'Finished downloading' | sed 's/.*: //')"
+
+ if [ $? -ne 0 ]; then
+ die "youtube-dl error"
+ fi
+
+ if [ "$auto" = 0 ]; then
+ printf "add metadata? [Y/n] "
+ read
+ else
+ REPLY=y
+ fi
+
+ case "$REPLY" in
+ N|n)
+ mv ./*.mp3 "$oldpwd/"
+ cd -
+ exit 0;;
+ esac
+
+ artist="Unknown Artist"
+ album="${album:-Unknown Album}"
+ track=1
+
+ # Loop over files with spaces
+ SAVEIFS=$IFS
+ IFS=$(printf '\n\b')
+ for f in *.mp3; do
+
+ [ "$track" = 1 ] && \
+ artist="$(printf '%s' "$f" | \
+ awk -F'-' '{gsub(/^ +/, "", $2); gsub(/ +$/, "", $2); print $2}')"
+
+ printf 'file: %s\n' "$f"
+
+ song=$(printf '%s' "$f" | sed 's/.* - //' | sed 's/^ //' | sed 's/\.mp3//')
+
+ if [ "$auto" = 0 ]; then
+ printf 'song [%s]: ' "$song"
+ read
+ song="${REPLY:-$song}"
+
+ printf 'track [%s]: ' "$track"
+ read
+ track="${REPLY:-$track}"
+
+ printf 'album [%s]: ' "$album"
+ read
+ album="${REPLY:-$album}"
+
+ printf 'artist [%s]: ' "$artist"
+ read
+ artist="${REPLY:-$artist}"
+ fi
+
+ id3tag --artist="$artist" --album="$album" \
+ --song="$song" --track="$track" \
+ "$f"
+
+ track=$(( track + 1 ))
+ done
+
+ IFS=$SAVEIFS
+
+ if [ "$move_music" = 1 ]; then
outdir="$musicroot/$artist/$album"
mkdir -p "$outdir"
mv ./*.mp3 "$outdir"
+ if type -v mpc >/dev/null 2>&1; then
+ mpc update --wait >/dev/null
+ if [ "$queue" = 1 ]; then
+ mpc findadd \
+ artist "$artist" \
+ album "$album" \
+ title "$song"
+ fi
+ fi
+ else
+ mv ./*.mp3 "$oldpwd/"
+ fi
+
+ rmdir "$outputdir"
+ cd - >/dev/null
+}
+
+prefix=""
+auto=0
+move_music=0
+queue=0
+while :; do
+ case "$1" in
+ -a)
+ auto=1;;
+ -h)
+ show_help
+ exit 0;;
+ -m)
+ move_music=1;;
+ -q)
+ queue=1;;
+ -t)
+ prefix="torsocks";;
+ --)
+ shift
+ break;;
+ -?*)
+ die "unknown option specified: $1";;
+ *)
+ break
+ esac
+ shift
+done
- echo "updating mpd"
- mpc update > /dev/null
+if [ $# -lt 1 ]; then
+ show_help
+ exit 1
+fi
- rmdir "$outputdir"
- cd -;;
-esac
+for u in "$@"; do
+ handle_url "$u"
+done