summaryrefslogtreecommitdiff
path: root/songgrab
blob: 20423f024292be762a6d28f6507114a34016bb7d (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/sh

# check flags
for i; do
  case "$i" in
    "-"*) unset GETARTIST GETALBUM GETTITLE
  esac
  [ -n "$GETURL" ] && URL="$i" && unset GETURL
  [ -n "$GETARTIST" ] && ARTIST="$ARTIST $i"
  [ -n "$GETALBUM" ] && ALBUM="$ALBUM $i"
  [ -n "$GETTITLE" ] && TITLE="$TITLE $i"
  [ -n "$GETTRACKNUM" ] && TRACKNUM="$i" && unset GETTRACKNUM
  [ -n "$GETYEAR" ] && YEAR="$i" && unset GETYEAR
  case "$i" in
    "-h")
      printf "Grabs music (either individual songs or albums) and formats it.\n"
      printf "\n"
      printf "  -s |  downloads and formats a single song.\n"
      printf "  -l |  downloads and formats a whole album.\n"
      printf "  -p |  downloads and formats an album (via a playlist).\n"
      printf "  -u |  the url to download.\n"
      printf "  -a |  the artist name (to tag every song with).\n"
      printf "  -A |  the album name (to tag every song with).\n"
      printf "  -t |  the title of the song (if downloading a single song).\n"
      printf "  -T |  the number of the track in its album (if downloading a single song).\n"
      printf "  -y |  the year of release.\n"
      exit 0 ;;
    "-s")
      SPLITTER_SELECTION="s"
      ;;
    "-l")
      SPLITTER_SELECTION="a"
      ;;
    "-p")
      SPLITTER_SELECTION="p"
      ;;
    "-u")
      GETURL="y"
      ;;
    "-a")
      GETARTIST="y"
      ;;
    "-A")
      GETALBUM="y"
      ;;
    "-t")
      GETTITLE="y"
      ;;
    "-T")
      GETTRACKNUM="y"
      ;;
    "-y")
      GETYEAR="y"
      ;;
  esac
done

ARTIST="$(echo "$ARTIST" | sed 's/^ //')"
ALBUM="$(echo "$ALBUM" | sed 's/^ //')"
TITLE="$(echo "$TITLE" | sed 's/^ //')"

printf "Received Tags:
Artist: %s
Album: %s
Song: %s
Number: %s
Year: %s\n\n" "$ARTIST" "$ALBUM" "$TITLE" "$TRACKNUM" "$YEAR"

notify-send "songgrab started" "Received Tags:
Album: $ALBUM
Song: $TITLE
Number: $TRACKNUM
Year: $YEAR\n\n"

printf "\n"
# prompt to determine later tagging
if [ -z "$SPLITTER_SELECTION" ]; then
  printf "Will the link be for a [p]laylist, a[l]bum video, or [s]ong video? "
  read -r SPLITTER_SELECTION
  printf "\n"
fi

# prompt to get the URL
if [ -z "$URL" ]; then
  printf "Enter the link: "
  read -r URL
  printf "\n"
fi

# if song, don't download playlist
if [ "$SPLITTER_SELECTION" = "s" ]; then
  NO_PLAYLIST="yes"
fi

# download the video(s)
notify-send "songgrab" "Starting download..."
printf "Starting download..."
MATCHSTRING="$(echo "$URL" | cut -d'=' -f2 | cut -d'&' -f1)"
for file in ./*; do
  case "$file" in
    *${MATCHSTRING}*) true ;;
    *) yt-dlp ${NO_PLAYLIST:+"--no-playlist"} --format bestaudio --restrict-filenames "$URL" ;;
  esac
done
notify-send "songgrab" "Download stopped."
printf "Download stopped."

# while loop to remove files from list
printf "Do you want to remove any tracks?\n\n"
while true; do
  find ./* | nl
  printf "Enter the line number: "
  read -r WHILE_RESPONSE
  [ -z "$WHILE_RESPONSE" ] && break
  rm -rf "$(find . | head -n "$WHILE_RESPONSE" | tail -1)"
done

# convert the downloaded files to mp3
printf "Starting to convert files..."
notify-send "songgrab" "Starting to convert files..."
for i in *.m4a *.webm; do
  imp3=$(echo "$i" | sed 's/\.m4a$/.mp3/;s/\.webm$/.mp3/')
  notify-send "songgrab" "Converting $i to $imp3"
  [ ! -f "$imp3" ] && ffmpeg -i "$i" -acodec mp3 "$imp3" || echo "File not converted"
done
printf "Finished converting files.\n\n"
notify-send "songgrab" "Finished converting files."

# fix names for suggestions
vimv

# tag songs with tagmp3
notify-send "songgrab" "Tagging songs..."
printf "Tagging songs..."
for i in *.mp3; do
  tagmp3 ${ARTIST:+"-a"} ${ARTIST:+"$ARTIST"} ${ALBUM:+"-A"} ${ALBUM:+"$ALBUM"} \
    ${TITLE:+"-t"} ${TITLE:+"$TITLE"} ${TRACKNUM:+"-T"} ${TRACKNUM:+"$TRACKNUM"} \
    ${YEAR:+"-y"} ${YEAR:+"$YEAR"} "$i"
done
notify-send "songgrab" "Done."
printf "Done.\n\n"