-
Notifications
You must be signed in to change notification settings - Fork 1
/
songrec-rename.sh
executable file
·102 lines (79 loc) · 2.63 KB
/
songrec-rename.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
# songrec-rename - automatic naming of music files using songrec.
# by Dan MacDonald
# An internet connection is required to run this script successfully.
# If run with no options, it will rename music files in the current dir.
# If you add the -r option it will rename all music files in all folders
# including and within the current one.
# Use the -i option to add id3 tags to mp3 files if you have id3v2 installed.
if ! command -v songrec &> /dev/null
then
echo "You must install songrec before you can run songrec-rename."
exit
fi
# Don't add id3tags by default.
id3=0
# Don't recurse directories by default.
recurse=0
while getopts ":ri" opt; do
case $opt in
i ) id3=1 ;;
r ) recurse=1 ;;
\? ) echo -e "Usage: songrec-rename [-i] [-r]\n-i Add ID3 tags\n-r Recurse subdirectories"; exit 1;;
esac
done
if [[ "${id3}" -eq 1 && ! $(command -v id3v2) ]] ; then
echo "You must install id3v2 to add id3tags to mp3 files."
exit
fi
if ! command -v jq &> /dev/null ; then
echo "You must install jq to use songrec-rename."
exit
fi
srr() {
for t in *; do
if [ -f "$t" ]; then
# Try to identify current file.
songrec audio-file-to-recognized-song "$t" > srr.json
# subtitle is the artist name.
subtitle=$(jq -r '.track.subtitle' srr.json | sed 's/&/and/g' | sed 's/*//g' | sed 's/"//g' | sed 's/\\//g')
# title is the name of the track.
title=$(jq -r '.track.title' srr.json | sed 's/&/and/g' | sed 's/*//g' | sed 's/"//g' | sed 's/\\//g')
# Store the file extension.
extension=$(echo "$t" | sed 's/.*\.//')
if [ "$id3" -eq 1 ]; then
album=$(jq -r 'if .track.sections[0].metadata[0].text == null then "" else .track.sections[0].metadata[0].text end' srr.json)
year=$(jq -r 'if .track.sections[0].metadata[2].text == null then "" else .track.sections[0].metadata[2].text end' srr.json)
fi
if [[ "$title" != "null" ]]; then
if [[ "$extension" == "mp3" && "$id3" -eq 1 ]]; then
echo "Adding id3 tags to $subtitle-$title."
id3v2 -t "$title" -a "$subtitle" -A "$album" -y "$year" "$t"
fi
echo "Renaming $t to $subtitle-$title.$extension"
mv -n "$t" "$subtitle-$title.$extension"
else
echo "ERROR: $t is unrecognized by songrec."
fi
rm srr.json
while [ -f srr.json ];
do
sleep 1
done
sleep 1
year=""
album=""
title=""
subtitle=""
fi
done
}
if [[ "${recurse}" -eq 1 ]] ; then
BASEDIR=$(pwd)
find . -type d | sort | while read -r DIR; do
cd "$BASEDIR/$DIR" || exit 1
srr
done
else
srr
fi