Tagging, 755 mode

This commit is contained in:
DarkCat09 2023-02-03 12:06:41 +04:00
parent ddb619f1c0
commit 902b6414a5
4 changed files with 104 additions and 0 deletions

0
.convert_helper.sh Normal file → Executable file
View file

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
files/
convert/
tagged/
.vscode/

81
.id3tag_helper.sh Executable file
View file

@ -0,0 +1,81 @@
#!/usr/bin/env bash
# Should not be called manually
conv_title () {
echo "$1" | \
# remove directory name
sed -E 's#\./(convert|files)/##' | \
# remove video ID and ext
sed 's/---[A-Za-z0-9_-]*\.[A-Za-z0-9]*//' | \
# underscore -> space
sed 's/_/ /g' | \
# remove "(Official Audio)"
sed -E 's/\(.*\)//' | \
# trim spaces
xargs
}
rm_quotes () {
cat | sed 's/^"//' | sed 's/"$//'
}
title=$(conv_title "$2")
title_prev="$title"
echo 'Correct the song title if needed:'
read -e -r -i "$title" title
title=${title:-$title_prev}
echo 'Searching on Genius'
link="https://genius.com/api/search/multi?q=${title// /%20}"
echo "URL: $link"
song=$(curl -sL "$link" | jq '.response.sections[1].hits[0].result')
title=$(echo "$song" | jq '.title' | rm_quotes)
artist=$(echo "$song" | jq '.primary_artist.name' | rm_quotes)
year=$(echo "$song" | jq '.release_date_components.year')
page_url=$(echo "$song" | jq '.url' | rm_quotes)
echo "Title: $title"
echo "Artist: $artist"
echo "Lyrics: $page_url"
echo 'Parsing lyrics page'
page=$(curl -sL "$page_url")
album=$(echo "$page" | pup -p 'a[class^="PrimaryAlbum__Title"] text{}' | sed -E 's#\([0-9]+\)$##' | xargs)
tracknum=$(echo "$page" | pup -p 'div[class^="HeaderTracklist__AlbumWrapper"]' | grep -oE 'Track [0-9]+' | grep -oE '[0-9]+')
trackall=$(echo "$page" | pup -p 'ol[class^="AlbumTracklist__Container"] > li:last-child div[class^="AlbumTracklist__TrackNumber"] text{}' | grep -oE '[0-9]+')
lyrics=$(echo "$page" | pup -p 'div[data-lyrics-container="true"] text{}' | sed 's#^\[#\n[#')
# remove first blank line
if [[ $(echo "$lyrics" | sed -n '1p') == "" ]]; then
lyrics=$(echo "$lyrics" | sed '1d')
fi
echo "Album: $album"
if [[ $1 == 1 ]]; then
newdir="./tagged/${artist}/${album}"
mkdir -p "$newdir"
newpath="${newdir}/${tracknum}. ${title}.mp3"
echo "Copying to $newpath"
cp -f "$2" "$newpath"
else
newpath="$2"
fi
echo
echo "$lyrics"
echo
echo 'Adding ID3v2 tags'
mid3v2 \
--artist "$artist" \
--album "$album" \
--song "$title" \
--year "$year" \
--track "${tracknum}/${trackall}" \
--USLT "$lyrics" \
"$newpath"

22
id3tag.sh Executable file
View file

@ -0,0 +1,22 @@
#!/usr/bin/env bash
ask () {
read -r -p "[Y/n] " answer
if [[ $answer =~ [NnНн] ]]; then
echo 0
else
echo 1
fi
}
echo "Enter directory where your music is located:"
read -e -r -i "./convert" directory
directory="${directory:-./convert}"
echo
echo "Copy files into ./tagged/artist/album directory?"
copy_arg=$(ask)
echo
find "$directory" -type f -name "*.mp3" -exec \
bash ./.id3tag_helper.sh "$copy_arg" {} \;