-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_song.sh
119 lines (99 loc) · 2.79 KB
/
get_song.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
# Created by Jason Hardman on 20211021
# Configurable arguments at the start of main function
function usage() {
# Prints usage
if [ "$@" ] ; then
echo "ERROR: ${@}"
fi
cat <<EOF
Usage:
-a "<artist>" Artist (required)
-s "<song>" Song (required)
* Prints this helpful output
EOF
}
function getOptions() {
local OPTIND OPTION a s h
while getopts "a:s:h" OPTION; do
case "$OPTION" in
'a') # Artist String
THE_ARTIST="$OPTARG"
has_artist=1
;;
's') # Song string
THE_SONG="$OPTARG"
has_song=1
;;
'h')
usage
exit 1
;;
?)
usage
exit 1
;;
esac
done
if [[ "$has_artist" -ne 1 ]]; then
usage "No Artist Provided"
exit 1
elif [[ "$has_song" -ne 1 ]]; then
usage "No Song Provided"
exit 1
fi
}
function to_mp3() {
# Changes name to "xx - Artist - Song.mp3"
# Adds album set as third arg
the_track="${1}"
m4a_file="${2}"
the_album="${3}"
mp3_file="${the_track} - ${THE_ARTIST} - ${THE_SONG}.mp3"
/usr/bin/ffmpeg -i "$m4a_file" \
-map_metadata 0 \
-metadata title="$THE_SONG" \
-metadata artist="$THE_ARTIST" \
-metadata album="$the_album" \
-metadata track=$the_track \
-metadata disc=1 \
-metadata album_artist="$THE_ALBUM_ARTIST" \
"$mp3_file" >/dev/null 2>&1
echo " - Converted to \"$mp3_file\""
}
function archive_m4a() {
# Move the downloaded m4a file to archive m4a_dir
the_file="${1}"
the_dir="${2}"
mv -v "$the_file" "$the_dir"
echo " - Archived $the_file to $the_dir"
}
function main() {
## ARGS
THE_ARTIST=""; THE_SONG=""; getOptions "${@}"
THE_ALBUM_ARTIST="Various Artists"
target_dir="/home/jason/music/Mixed/ElectroString/"
m4a_dir="/home/jason/music/m4aFiles/"
album="Electrostring"
next_track="$(ls -1 $target_dir | awk 'END{printf "%02i\n", ($1+1)}')"
url="$(/home/jason/bin/youtubeSearch.sh "${THE_ARTIST} ${THE_SONG}")"
##
# Go to the target directory, download the video from YouTube, convert it to
# MP3 format with metadata, and archive the M4A file
cd "$target_dir"
echo " - Changed to $target_dir"
echo " - \"${THE_SONG}\" by ${THE_ARTIST} found at ${url} "
echo -n " - Download started..."
/usr/local/bin/youtube-dl -f 'bestaudio[ext=m4a]' \
--restrict-filenames "$url" \
>/dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo -e "\b\b\b\b\b\b\b\b\b\bcomplete "
new_file="$(ls | grep -E 'm4a$')"
to_mp3 "$next_track" "$new_file" "$album"
archive_m4a "$new_file" "$m4a_dir"
else
echo -e "\b\b\b\b\b\b\b\b\b\bfailed "
fi
}
main "${@}"