You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.1 KiB
101 lines
2.1 KiB
#!/bin/bash |
|
|
|
#Создан: Ср 19 фев 2020 21:59:29 |
|
#Изменён: Чт 20 фев 2020 00:45:38 |
|
|
|
# Requirements: |
|
# - mid3v2 from media-libs/mutagen |
|
|
|
rstc="\033[00m" |
|
green="\033[1;32m" |
|
yellow="\033[1;33m" |
|
blue="\033[1;34m" |
|
|
|
REGEXP=".*/(.*)/(.*) \[(.*)\]/(.*)/(.*?) - (.*)/(.*?) - (.*)\.([[:alnum:]]{3})$" |
|
|
|
usage() { |
|
echo "$(basename "$0") - simple mp3 tagger." |
|
echo |
|
echo "USAGE: $(basename "$0") [file] [options]" |
|
echo |
|
echo "The filenames should be in the format \"[...]/GENRE/ARTIST [COUNTRY]/TYPE/YEAR - ALBUM/TRACK - TITLE.FORMAT\"" |
|
echo |
|
echo "Command line arguments:" |
|
echo " -h --help - show this help" |
|
echo " -v --verbose - show updating tags" |
|
echo " -d --dry - run without tags saving" |
|
} |
|
|
|
tag_file() { |
|
file="$1" |
|
|
|
[[ $file =~ $REGEXP ]] |
|
|
|
printf "${blue}%s[%02d/%02d] ${yellow}Tagging the file «%s»${rstc}\n" "$INDENT" "$((++CURRENT_TRACK))" "$TRACKS" "$file" |
|
|
|
GENRE="${BASH_REMATCH[1]}" |
|
ARTIST="${BASH_REMATCH[2]}" |
|
YEAR="${BASH_REMATCH[5]}" |
|
ALBUM="${BASH_REMATCH[6]}" |
|
TRACK="${BASH_REMATCH[7]}" |
|
TITLE="${BASH_REMATCH[8]}" |
|
|
|
if [ -n "$VERBOSE" ]; then |
|
printf "%s\t Genre: %s | Artist: %s |" "$INDENT" "$GENRE" "$ARTIST" |
|
printf "Year: %s | Album: %s |" "$YEAR" "$ALBUM" |
|
printf "Track: %s | Title: %s\n" "$TRACK" "$TITLE" |
|
fi |
|
|
|
if [ -z "$DRY_RUN" ]; then |
|
mid3v2 --genre="$GENRE" --artist="$ARTIST" --year="$YEAR" \ |
|
--album="$ALBUM" --track="$TRACK" --song="$TITLE" \ |
|
"$file" |
|
fi |
|
} |
|
|
|
tag_directory() { |
|
TRACKS=$(find "$1" -name '*.mp3' -print | wc -l) |
|
|
|
printf "${green}> %s/${rstc}\n" "$1" |
|
INDENT=" " |
|
|
|
|
|
find "$1" -name '*.mp3' | while read -r file; do tag_file "$file"; done |
|
} |
|
|
|
tag() { |
|
if [[ -d "$1" ]]; then |
|
tag_directory "$1" |
|
elif [[ "$1" =~ \.mp3 ]]; then |
|
tag_file "$1" |
|
fi |
|
} |
|
|
|
if (( $# == 0 )); then |
|
usage |
|
exit 1 |
|
fi |
|
|
|
QUEUE=() |
|
|
|
while [[ $# -gt 0 ]]; do |
|
key="$1" |
|
|
|
case $key in |
|
-h|--help) usage; exit 0; shift;; |
|
-v|--verbose) VERBOSE=1; shift;; |
|
-d|--dry) DRY_RUN=1; VERBOSE=1; shift;; |
|
*) QUEUE+=("$1"); shift;; |
|
esac |
|
done |
|
|
|
set -- "${QUEUE[@]}" |
|
|
|
INDENT= |
|
TRACKS="$#" |
|
CURRENT_TRACK=0 |
|
|
|
for item in "${QUEUE[@]}"; do |
|
tag "$(realpath "$item")" |
|
done |
|
|
|
|