This repository has been archived by the owner on Aug 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaylist-pls-to-oga.sh
318 lines (303 loc) · 11.7 KB
/
Playlist-pls-to-oga.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
#Script to copy lossy files (and convert .flac files to .oga) from playlist (.pls file)
#.flac filed need to be in 1.3.1 revision and playlist in .pls
#It takes first image (bmp,png,jpg,jpeg) from the current file directory (and convert it as 300 pixels max) as cover, copy it as folder.jpg on destination.
#Lang test
lang=en
if [ $(echo $LANG | grep fr) ];then
lang=fr
fi
#Check if pre-requisites are good
if [ ! -f /usr/bin/mediainfo ];then
if [ "$lang" == "fr" ];then
echo "Il manque le paquet mediainfo. Merci de l'installer."
elif [ "$lang" == "en" ];then
echo "mediainfo package needed. Please install it."
fi
fi
if [ ! -f /usr/bin/oggenc ];then
if [ "$lang" == "fr" ];then
echo "Il manque le paquet oggenc. Merci de l'installer (paquet = vorbis-tools)."
elif [ "$lang" == "en" ];then
echo "oggenc package needed. Please install it (vorbis-tools)."
fi
fi
if [ ! -f /usr/bin/convert ];then
if [ "$lang" == "fr" ];then
echo "Il manque le paquet convert. Merci de l'installer (imagemagick)."
elif [ "$lang" == "en" ];then
echo "convert package needed. Please install it (imagemagick)."
fi
fi
#No args ? Let's explain
if [ $# -eq 0 ];then
if [ "$lang" == "fr" ];then
echo "Script pour copier (et convertir les .flac en .oga) le contenu d'une playlist .pls
Nécessite que les fichiers .flac soient en version 1.3.1 et que la playlist soit en .pls
INFO : Le script peut être lancé plusieurs fois (un par terminal) avec la même playliste, même destination, tout se fera en parallèle et les CPU seront plus sollicités (ainsi que le stockage de destination)
==== Usage ====
1er argument = chemin vers la playliste (.pls) à convertir.
2ème argument = chemin du dossier de destination."
elif [ "$lang" == "en" ];then
echo "Script to copy (and convert .flac files to .oga files) content of a playlist .pls
.flac files need to be in 1.3.1 revision and playlist in .pls
INFO : This script can be executed several times (one per console) with same playslit and same destination, all will be parallelized and CPUs will be more active (destination device too)
==== Usage ====
1st argument = full path to playliste (.pls) file containing files to convert.
2ème argument = full path to destination device."
fi
exit
fi
#Arguments
pls="$1"
dest="$2"
#Arguments tests
if [ ! -d "$dest" ];then
if [ "$lang" == "fr" ];then
echo "Erreur : le dossier de destination n'existe pas"
elif [ "$lang" == "en" ];then
echo "Error : folder's destination doesn't exists"
fi
exit 1
fi
if [ ! "$(echo "$pls" | grep ".pls$")" ];then
if [ "$lang" == "fr" ];then
echo "Erreur : la playlist n'est pas une playlist .pls"
elif [ "$lang" == "en" ];then
echo "Error : playlist file is not .pls file"
fi
exit 2
fi
#Question to know which folders level should be cut. As .pls could contain several path (if files are not stored on same folder tree)
nbexclu=0
chem=0
while [ "$chem" != "" ];do
if [ "$lang" == "fr" ];then
echo "Indiquer les dossiers à ne pas recréer sur la destination, par exemple :
Les fichiers (listés dans le fichier .pls) se trouvent tous dans '/home/login/Ma Musique/...' et on veut garder la structure à l'identique sur la destination de tout ce qu'il y a sous 'Ma Musique' (N.B. : ce dernier dossier est également exclu) -> indiquer dans ce cas '/home/login/Ma Musique/'
/!\ NE PAS indiquer de caractères d'échappement (ici le '\'), ni de chemin relatif (~/ ou ./) :
PAS BIEN : /home/login/Ma\ Musique/
BIEN : /home/login/Ma Musique/
PAS BIEN : ~/Ma Musique
/!\
ATTENTION : il ne faut pas de caractères de ce type dans les noms des fichiers ou dossiers :
- Point d'interrogation '?' et '~'
- Il NE faut PAS non plus de double espace
Entrer le chemin n°$(let "nbexclu += 1";echo $nbexclu) à exclure ? (taper juste ENTREE sans rien écrire quand terminé)"
elif [ "$lang" == "en" ];then
echo "Give folders that you don't want to be recreated on destination folder, example :
All files (in .pls file) are into /home/login/My music/... and we want to keep (on the destination folder) the folder tree but only subfolders of 'My music' not the upper folders ('My music, login and home')
N.B. : last folder written is excluded too) -> in this case, type '/home/login/My music', all those three folders will not be recreated on destination
/!\ DO NOT type escape characters (example the backslash '\'), nor relative path (~/ or ./) :
NOT GOOD : /home/login/My\ music/
GOOD : /home/login/My music/
NOT GOOD : ~/My music
/!\
WARNING :
- files or folders name containing interrogative mark '?' or '~' will generate an error, and will not be copied/converted !
- NO double space in folders, neither in files name !
Enter folder path #$(let "nbexclu += 1";echo $nbexclu) to exclude : (just type ENTER when empty to finish)"
fi
read chem
if [ ! -d "$chem" ];then
if [ ! -z $chem ];then
if [ "$lang" == "fr" ];then
echo "Chemin non valide !"
elif [ "$lang" == "en" ];then
echo "Invalid path !"
fi
fi
else
let "nbexclu += 1"
suffix=$nbexclu
eval "field$suffix=$chem"
fi
done
#BEGIN
if [ "$lang" == "fr" ];then
echo -e "\e[1;36mINFO\033[0m : Playliste = $pls, dossier de sortie = $dest"
echo -e "\e[1;36mDébut dans 10 sec\033[0m, tapez CTRL+C pour annuler"
elif [ "$lang" == "en" ];then
echo -e "\e[1;36mINFO\033[0m : Playlist = $pls, destination folder = $dest"
echo -e "\e[1;36mStarting in 10 sec\033[0m, type CTRL+C to abort"
fi
sleep 10
pid=$BASHPID
#.pls file reformatting for using by this script
sed '/^File/!d' "$pls" > /tmp/`basename "$pls"`
cat /tmp/`basename "$pls"` | cut -d "=" -f2 > /tmp/cut-$pid`basename "$pls"`
#In the case of this script is executed several times in parallel (multitasking), randomize .pls lines
sort --random-sort /tmp/cut-$pid`basename "$pls"` > /tmp/$pid`basename "$pls"`
#Counter files, for information
count=0
while read line; do
let "count += 1"
traitfic=0
odir=""
#Keep original folder location of the current file
dir="$(dirname "$line")"
#Delete folders that was indicated not to be recreated on destination, several possible, stop when match
i=1
while [ $i -le $nbexclu ];do
suffix=$i
odir=$(echo $dir | sed "s:$(eval echo \$field$suffix)::")
let "i += 1"
if [ "$dir" != "$odir" ];then
break
fi
done
#Test if current file already exists on destination
if [ ! -r "$dest/$odir/`basename "${line%.*}.oga"`" -a ! -r "$dest/$odir/`basename "$line"`" ];then
echo -e "\e[1;36m$count\033[0m:$line"
#Test if destination folder exists, create it if needed
if [ ! -d "$dest/$odir" ];then
mkdir -p "$dest/$odir"
if [ $? -eq 0 ];then
if [ "$lang" == "fr" ];then
echo -ne " Création dossier \e[32m[OK]\033[0m"
elif [ "$lang" == "en" ];then
echo -ne " Folder creation \e[32m[OK]\033[0m"
fi
else
if [ "$lang" == "fr" ];then
echo -ne " Création dossier \033[31m[ERREUR]\033[0m"
elif [ "$lang" == "en" ];then
echo -ne " Folder creation \033[31m[ERROR]\033[0m"
fi
fi
else
if [ "$lang" == "fr" ];then
echo -ne " Dossier \e[32m[OK]\033[0m"
elif [ "$lang" == "en" ];then
echo -ne " Folder \e[32m[OK]\033[0m"
fi
fi
#ENCODE (.flac source file) / COPY (lossy source file)
if [ "$lang" == "fr" ];then
echo -n " | Encodage"
elif [ "$lang" == "en" ];then
echo -n " | Encoding"
fi
#Test if source = .flac
if [ "$(echo "$line" | grep -i ".flac$")" ];then
#Test if it's 44.1KHz or 48KHz samples, if not resampling in 48KHz
if [ "$(mediainfo "$line" | grep "Sampling rate" | cut -d ":" -f2 | tr -d " " | cut -d "." -f1 | grep -E '44|48')" ];then
oggenc "$line" -o "$dest/$odir/`basename "${line%.*}.oga"`" -Q
traitfic=1
else
if [ "$lang" == "fr" ];then
echo -n " (conversion 48KHz)"
elif [ "$lang" == "en" ];then
echo -n " (resampling to 48KHz)"
fi
oggenc "$line" --resample=48000 -o "$dest/$odir/`basename "${line%.*}.oga"`" -Q
traitfic=1
fi
echo -en " \e[32m[OK]\033[0m"
#If current file is not .flac, tests if it's lossy, then no need to encode, just direct copy
elif [ "$(mediainfo "$line" | grep "Lossy")" ];then
if [ "$lang" == "fr" ];then
echo -n " Compressé = copie directe"
elif [ "$lang" == "en" ];then
echo -n " Lossy = direct copy"
fi
cp "$line" "$dest/$odir/"
traitfic=1
echo -en " \e[32m[OK]\033[0m"
fi
#COVER
#Test if cover already existants on destination
traitcov=0
if [ -r "$dest/$odir/folder.jpg" ];then
if [ "$lang" == "fr" ];then
echo -ne " | Cover existante \e[32m[OK]\033[0m"
elif [ "$lang" == "en" ];then
echo -ne " | Cover exists \e[32m[OK]\033[0m"
fi
traitcov=1
else
#Cover don't exists and current file encoded or copied = let's go for the cover
if [ -r "$(echo "$(dirname "$line")/$(ls "$(dirname "$line")" | grep -i -m 1 -e ".jpg$\|.jpeg$\|.png$\|.bmp$")")" -a -d "$dest/$odir" -a $traitfic -eq 1 ];then
#Test if dimension (W or H) is greater than 300 pixels, resize to 300 pixels
if [ "$(mediainfo echo "$(dirname "$line")/$(ls "$(dirname "$line")" | grep -i -m 1 -e ".jpg$\|.jpeg$\|.png$\|.bmp$")" | grep Width | cut -d ":" -f2 | tr -d [a-z] | tr -d " ")" -gt 300 -o "$(mediainfo echo "$(dirname "$line")/$(ls "$(dirname "$line")" | grep -i -m 1 -e ".jpg$\|.jpeg$\|.png$\|.bmp$")" | grep Height | cut -d ":" -f2 | tr -d [a-z] | tr -d " ")" -gt 300 ];then
convert -resize 300 "$(dirname "$line")/$(ls "$(dirname "$line")" | grep -i -m 1 -e ".jpg$\|.jpeg$\|.png$\|.bmp$")" "$dest/$odir/folder.jpg"
if [ $? -eq 0 ];then
if [ "$lang" == "fr" ];then
echo -ne " | Redimensionnement cover \e[32m[OK]\033[0m"
elif [ "$lang" == "en" ];then
echo -ne " | Cover resize \e[32m[OK]\033[0m"
fi
traitcov=1
else
if [ "$lang" == "fr" ];then
echo -ne " | Redimensionnement cover \033[31m[ERREUR]\033[0m"
elif [ "$lang" == "en" ];then
echo -ne " | Cover resize \033[31m[ERROR]\033[0m"
fi
fi
else
#Cover already OK, direct copy
cp "$(dirname "$line")/$(ls "$(dirname "$line")" | grep -i -m 1 -e ".jpg$\|.jpeg$\|.png$\|.bmp$")" "$dest/$odir/folder.jpg"
if [ $? -eq 0 ];then
if [ "$lang" == "fr" ];then
echo -ne " | Copie cover \e[32m[OK]\033[0m"
elif [ "$lang" == "en" ];then
echo -ne " | Cover copy \e[32m[OK]\033[0m"
fi
traitcov=1
else
if [ "$lang" == "fr" ];then
echo -ne " | Copie cover \033[31m[ERREUR]\033[0m"
elif [ "$lang" == "en" ];then
echo -ne " | Cover copy \033[31m[ERROR]\033[0m"
fi
fi
fi
fi
#Flag traitcov = 0 : no cover for this file
if [ $traitcov -eq 0 ];then
if [ "$lang" == "fr" ];then
echo -ne " | Pas de cover \033[1;40m\033[1;33m[ATTENTION]\033[0m"
elif [ "$lang" == "en" ];then
echo -ne " | No cover \033[1;40m\033[1;33m[WARNING]\033[0m"
fi
fi
fi
fi
#traitfic = 1 then current file OK, let's write a separator
if [ $traitfic -eq 1 ];then
echo ""
echo "-----------------"
fi
done < /tmp/$pid`basename "$pls"`
#End of playlist, let's check and give files that don't are present in destination
if [ "$lang" == "fr" ];then
echo -n "Vérifications des fichiers..."
elif [ "$lang" == "en" ];then
echo -n "Checking files..."
fi
while read line; do
if [ ! "$(find "$dest" -name "`basename "${line%.*}"`*")" ];then
if [ "$lang" == "fr" ];then
echo -e "\033[31mABSENT\033[0m : $line"
elif [ "$lang" == "en" ];then
echo -e "\033[31mMISSING\033[0m : $line"
fi
fi
done < /tmp/$pid`basename "$pls"`
if [ "$lang" == "fr" ];then
echo -en "\e[32m[OK] Terminé\033[0m"
elif [ "$lang" == "en" ];then
echo -en "\e[32m[OK] Finished\033[0m"
fi
echo -e
#Remove temp files
if [ "$lang" == "fr" ];then
echo "Nettoyage des fichiers temporaires du script..."
elif [ "$lang" == "en" ];then
echo "Removing script's temp files..."
fi
rm -v "/tmp/$pid`basename "$pls"`"
rm -v "/tmp/cut-$pid`basename "$pls"`"
#END
exit 0