summaryrefslogtreecommitdiff
path: root/dotfiles/system/.local/bin/mkplaylist
blob: 66b6e9c3844cc23353a339a7bbc160f2ab1db341 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env bash
# Craig Jennings <c@cjennings.net>
# Basically just a bash wrapper around a find/grep/awk command pipe
# to generate m3u playlists from video or audio files in a directory.

# One m3u playlist will be placed in the MUSIC_DIR, and another
# will be placed inside each playlist directory.
# It also converts .opus and .ogg files to .m4a for Android playback.

# Note:
# This script requires the following utilities to be on the path:
# mid3v2 (aur package: python-mutagen)
# tageditor (aur package: tageditor)
# metaflac (aur package: flac)

set -e

MUSIC_DIR="$HOME/music"
# REQUIRED_TOOLS=("mid3v2" "tageditor")
REQUIRED_TOOLS=("mid3v2" "metaflac" "tageditor")

#  ---------------------------- Functions ----------------------------

usage () {
    printf "\nUsage: mkplaylist <playlist_name>\n\n"
    printf "mkplaylist - creates an m3u playlist in the $MUSIC_DIR directory\n"
    printf "based the music and video files in directory which m3uplaylist is called.\n\n"
    printf " - this script should be run in the directory containing the music or video files\n"
    printf " - <playlist_name> is mandatory and shouldn't end with '.m3u' extension\n"
    printf " - change the destination ($MUSIC_DIR) by editing this script\n\n"
}

tag_music_file() {
  while IFS= read -r file; do
    filename=$(basename "$file")
    extension="${filename##*.}"
    artist=$(basename "$file" | cut -d '-' -f 1)
    title=$(basename "$file" | cut -d '-' -f 2- | cut -d '.' -f 1)
    outputfile="$(dirname "$file")/$title.flac"

    # If file is not already flac, convert it
    if [ "$extension" != "flac" ]; then

      # Delete all tags using mid3v2
      mid3v2 --delete-all "$file"
      ffmpeg -i "$file" -vn -c:a flac "$outputfile"
      file="$outputfile"  # Now we're working with the new FLAC file

    fi

    # Set artist and song title tags using metaflac
    metaflac --set-tag="ARTIST=$artist" --set-tag="TITLE=$title" "$file"

   done
}

# tag_music_file() {
#     while IFS= read -r file; do
#         filename=$(basename "$file")
#         extension="${filename##*.}"
#         artist=$(basename "$file" | cut -d '-' -f 1)
#         title=$(basename "$file" | cut -d '-' -f 2- | cut -d '.' -f 1)
#         outputfile="$(dirname "$file")/$title.flac"

#         # Delete all tags using mid3v2
#         mid3v2 --delete-all "$file"

#         # If file is not already flac, convert it
#         if [ "$extension" != "flac" ]; then
#             ffmpeg -i "$file" -vn -c:a flac "$outputfile"
#             file="$outputfile"  # Now we're working with the new FLAC file
#         fi

#         # Set artist and song title tags using metaflac
#         metaflac --set-tag="ARTIST=$artist" --set-tag="TITLE=$title" "$file"
#     done
# }

# tag_music_file() {
#     while IFS= read -r file; do
#         # Extract artist and song title from filename
#         artist=$(basename "$file" | cut -d '-' -f 1)
#         title=$(basename "$file" | cut -d '-' -f 2- | cut -d '.' -f 1)
#         outputfile="$(dirname "$file")/$title.flac"

#         # Delete all tags using mid3v2
#         mid3v2 --delete-all "$file"

#         # Convert to flac and save to new file
#         ffmpeg -i "$file" -vn -c:a flac "$outputfile"

#         # Set artist and song title tags using metaflac
#         metaflac --set-tag="ARTIST=$artist" --set-tag="TITLE=$title" "$outputfile"
#     done
# }

# tag_music_file() {
#     while IFS= read -r file; do
#         # Extract artist and song title from filename
#         artist=$(basename "$file" | cut -d '-' -f 1)
#         title=$(basename "$file" | cut -d '-' -f 2 | cut -d '.' -f 1)
#         outputfile="$(dirname "$file")/$title.flac"

#         # Delete all tags using mid3v2
#         mid3v2 --delete-all "$file"

#         # Set artist and song title tags using mid3v2
#         mid3v2 --artist="$artist" --song="$title" "$file"
#         # Convert to flac and save to new file
#         ffmpeg -i "$file" -vn -c:a flac "$outputfile"
#     done
# }

# tag_music_file() {
#     while IFS= read -r file; do
#         # Extract artist and song title from filename
#         artist=$(basename "$file" | cut -d '-' -f 1)
#         title=$(basename "$file" | cut -d '-' -f 2 | cut -d '.' -f 1)

#         # Delete all tags using mid3v2
#         mid3v2 --delete-all "$file"

#         # Set artist and song title tags using mid3v2
#         mid3v2 --artist="$artist" --song="$title" "$file"
#     done
# }

 generate_music_m3u() {
     printf "retagging music files....\n"
     find "$(pwd)" -print | file -if - | grep -E '(audio)' | awk -F: '{print $1}' | tag_music_file

     printf "generating playlist.'%s'...\n" "$LOCAL_PLAYLIST"
     find "$(pwd)" -print | file -if - | grep -E '(video|audio)' |
         awk -F: '{print $1}' | while read -r line; do basename "$line"; done > "$LOCAL_PLAYLIST"
     printf "generating playlist '%s'....\n" "$MUSIC_PLAYLIST"
     find "$(pwd)" -print | file -if - | grep -E '(video|audio)' | awk -F: '{print $1}' > "$MUSIC_PLAYLIST"
     printf "Done.\n\n"
 }

 #  ----------------------------- Script ----------------------------

 # display usage if user specifically requests it
 TYPE=$(tr '[a-z]' '[A-Z]' <<< "$@");
 [ "$TYPE" = "HELP" ] && usage && exit 1
 [ "$TYPE" = "-H" ] &&  usage&& exit 1

 # check that all necessary tools are installed
 for tool in ${REQUIRED_TOOLS[@]}; do
     if ! type "$tool" >/dev/null 2>&1; then
         printf "ERROR: The script requires '%s' but it is not installed or not in PATH.\n" "$tool"
         exit 1
     fi
 done

 # use directory name for playlist name when parameter doesn't exist
 if [ $# -eq 0 ]
 then
     set -- "$(basename "$PWD")"
     echo "no playlist name entered, so using directory name: '$(basename "$PWD")'"
 fi

 # ask to overwrite if the playlist already exists
 MUSIC_PLAYLIST="$MUSIC_DIR/$@.m3u"
 LOCAL_PLAYLIST="./$@.m3u"

 if [ -f "$MUSIC_PLAYLIST" ]; then
     read -p "$MUSIC_PLAYLIST exists. Overwrite (y/n) " yn
     if [ "$yn" != "y" ] && [ "$yn" != "Y" ]; then
         exit 0
     fi
 fi

 generate_music_m3u