commit f49f736446824f4f7921e249384a48e54ea68c76 parent fc83118f2539767830276cfe9be77a47a50caf40 Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Fri, 20 Mar 2020 16:06:15 +0100 Rewrite youtube-dl-music for POSIX sh Diffstat:
M | .local/bin/youtube-dl-music | | | 163 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
1 file changed, 83 insertions(+), 80 deletions(-)
diff --git a/.local/bin/youtube-dl-music b/.local/bin/youtube-dl-music @@ -1,93 +1,96 @@ -#!/usr/bin/env bash -set -e -set -u - -read -p "use torsocks? [Y/n] " -n 1 -r -echo -if [[ $REPLY =~ ^[Nn]$ ]]; then - prefix="" -else - prefix="torsocks" -fi +#!/bin/sh +set -ue + +printf "use torsocks? [Y/n] " +read -r REPLY +case "$REPLY" in + N|n) prefix="";; + *) prefix="torsocks";; +esac -oldpwd=$PWD +oldpwd="$PWD" outputdir="$(mktemp -d)" mkdir -p "$outputdir" cd "$outputdir" -album="$($prefix youtube-dl \ +album="$("$prefix" youtube-dl \ --format bestaudio \ --extract-audio \ --audio-quality 0 \ --audio-format mp3 \ - --output "%\(playlist_index\)s - %\(title\)s.%\(ext\)s" \ + --output \'%\(playlist_index\)s.%\(ext\)s - %\(title\)s.%\(ext\)s\' \ "$1" | tee /dev/tty | grep 'Finished downloading' | sed 's/.*: //')" -read -p "add metadata? [Y/n] " -n 1 -r -echo -if [[ $REPLY =~ ^[Nn]$ ]]; then - mv ./*.mp3 "$oldpwd/" - cd - - exit 0 +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=$(echo -en "\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 +if [ "$(uname)" = "Darwin" ]; then + musicroot="$HOME/Music/iTunes/iTunes Media/Music/" else - - artist="Unknown Artist" - album="${album:-Unknown Album}" - track=1 - - # Loop over files with spaces - SAVEIFS=$IFS - IFS=$(echo -en "\n\b") - for f in *.mp3; do - - [ "$track" = 1 ] && \ - artist="$(echo "$f" | sed 's/^[0-9]\+ - //' | sed 's/ - .*//' | \ - sed 's/^ //')" - - echo -e "\033[0;31m$f\033[0m" - - song=$(echo "$f" | sed 's/.* - //' | sed 's/^ //' | sed 's/\.mp3//') - read -r -p "song [$song]: " input - song="${input:-$song}" - - read -r -p "track [$track]: " input - track="${input:-$track}" - - read -r -p "album [$album]: " input - album="${input:-$album}" - - read -r -p "artist [$artist]: " input - artist="${input:-$artist}" - - id3tag --artist="$artist" --album="$album" \ - --song="$song" --track="$track" \ - "$f" - (( track += 1 )) - done - # Revert default behavior - IFS=$SAVEIFS - if [[ "$(uname)" == 'Darwin' ]]; then - musicroot="$HOME/Music/iTunes/iTunes Media/Music/" - else - musicroot="/home/music/" - fi - - read -p "move to music folder ($musicroot)? [Y/n] " -n 1 -r - echo - if [[ $REPLY =~ ^[Nn]$ ]]; then - echo "leaving mp3's in $oldpwd" - mv ./*.mp3 "$oldpwd/" - cd - - exit 0 - else - outdir="$musicroot/$artist/$album" - mkdir -p "$outdir" - mv ./*.mp3 "$outdir" - - echo "updating mpd" - mpc update > /dev/null - - rmdir "$outputdir" - cd - - fi + musicroot="/home/music/" fi + +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;; + *) + outdir="$musicroot/$artist/$album" + mkdir -p "$outdir" + mv ./*.mp3 "$outdir" + + echo "updating mpd" + mpc update > /dev/null + + rmdir "$outputdir" + cd -;; +esac