2021-07-02 14:00:50 +02:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# Sortie à la moindre erreur
|
2021-07-02 14:00:50 +02:00
|
|
|
|
set -e
|
|
|
|
|
|
|
|
|
|
shopt -s globstar
|
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# Initialisation de la mise en forme
|
2021-07-02 14:00:50 +02:00
|
|
|
|
blue=$(tput setaf 4)
|
|
|
|
|
green=$(tput setaf 2)
|
2022-07-23 02:26:09 +02:00
|
|
|
|
red=$(tput setaf 1)
|
2021-07-02 14:00:50 +02:00
|
|
|
|
reset=$(tput sgr0)
|
|
|
|
|
bold=$(tput bold)
|
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# Répertoire source
|
2021-07-02 14:00:50 +02:00
|
|
|
|
rootdir="${HOME}/Musique/"
|
|
|
|
|
|
|
|
|
|
cd "$rootdir"
|
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# Pour tous les sous-répertoires, à tous les niveaux
|
2021-07-02 14:32:14 +02:00
|
|
|
|
for dir in **/ .
|
2021-07-02 14:00:50 +02:00
|
|
|
|
do
|
2022-10-18 01:34:52 +02:00
|
|
|
|
# Ignorer les liens symboliques
|
2022-11-10 00:55:04 +01:00
|
|
|
|
if [[ -h "${rootdir}/${dir::-1}" && "${dir}" != "Vidéos/" ]]
|
2022-10-18 01:34:52 +02:00
|
|
|
|
then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# Affichage du répertoire en cours de traitement
|
2021-07-02 14:00:50 +02:00
|
|
|
|
echo "$bold$dir$reset"
|
2021-12-11 17:25:35 +01:00
|
|
|
|
|
|
|
|
|
# Définition du répertoire de destination
|
2021-07-02 14:00:50 +02:00
|
|
|
|
dstdir="${HOME}/Musique.opus/${dir}"
|
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# Création de la destination
|
2021-07-02 14:00:50 +02:00
|
|
|
|
mkdir -p "${dstdir}"
|
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# On entre dans le répertoire à traiter
|
2021-07-02 14:00:50 +02:00
|
|
|
|
cd "${rootdir}/${dir}"
|
2022-08-10 18:37:33 +02:00
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# Pour chaque fichier
|
2021-07-02 14:00:50 +02:00
|
|
|
|
for file in *
|
|
|
|
|
do
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# On ignore les dossiers
|
2021-07-02 14:00:50 +02:00
|
|
|
|
if [[ -d "${file}" ]]
|
|
|
|
|
then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# S’il s’agit de FLAC ou d’une vidéo
|
2021-07-02 14:26:57 +02:00
|
|
|
|
if [[ "${file}" =~ .flac$ ]] || [[ "${dir}" == "Vidéos/" && "$(file --mime-type -b -e ascii -e compress -e tar -e cdf "$file")" =~ video/ ]]
|
2021-07-02 14:00:50 +02:00
|
|
|
|
then
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# Définition du fichier de destination
|
2021-07-02 14:00:50 +02:00
|
|
|
|
dstfile="$(basename -s .flac "$file").opus"
|
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# S’il a déjà été traité on passe, pour gagner du
|
|
|
|
|
# temps.
|
2021-07-02 14:00:50 +02:00
|
|
|
|
if [[ -f "${dstdir}${dstfile}" ]]
|
|
|
|
|
then
|
|
|
|
|
echo "[${blue}SKIP${reset}] $file"
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# Parrallélisation des conversions
|
2021-07-02 14:00:50 +02:00
|
|
|
|
declare -a running
|
|
|
|
|
|
2022-11-10 00:55:04 +01:00
|
|
|
|
mapfile -t running < <(jobs -p)
|
|
|
|
|
if [[ "${#running[@]}" -ge 12 ]]
|
2021-07-02 14:00:50 +02:00
|
|
|
|
then
|
|
|
|
|
wait -n
|
|
|
|
|
fi
|
|
|
|
|
|
2022-08-10 18:37:33 +02:00
|
|
|
|
togain="true"
|
|
|
|
|
|
2021-07-02 14:00:50 +02:00
|
|
|
|
(
|
|
|
|
|
echo " $file"
|
2021-09-09 01:46:43 +02:00
|
|
|
|
ffmpeg -n -loglevel error -i "file:$file" -c:a libopus -b:a 256K "file:${dstdir}${dstfile}"
|
2021-07-02 14:00:50 +02:00
|
|
|
|
echo "[${green} OK ${reset}] $file"
|
|
|
|
|
) &
|
|
|
|
|
else
|
2021-12-11 17:25:35 +01:00
|
|
|
|
# Pour les autres fichiers, les copier tel quels
|
2021-07-02 14:00:50 +02:00
|
|
|
|
echo "[${green}COPY${reset}] $file"
|
|
|
|
|
cp -n --reflink=auto "${file}" "${dstdir}/"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
wait
|
2022-08-10 18:37:33 +02:00
|
|
|
|
|
|
|
|
|
# Si le moindre fichier a été converti, on refait les tags replay gain
|
|
|
|
|
|
|
|
|
|
if ${togain} && [[ "${dir}" != "Vidéos/" ]]
|
|
|
|
|
then
|
|
|
|
|
(
|
|
|
|
|
cd "${dstdir}"
|
|
|
|
|
shopt -s nullglob
|
|
|
|
|
echo "[${blue}GAIN${reset}] $dir"
|
2022-11-10 00:55:04 +01:00
|
|
|
|
loudgain -S -a -k -q -s e -- *.opus &>/dev/null && \
|
2022-08-10 18:37:33 +02:00
|
|
|
|
echo "[${green}GAIN${reset}] $dir" || \
|
|
|
|
|
echo "[${red}GAIN${reset}] $dir"
|
|
|
|
|
shopt -u nullglob
|
|
|
|
|
) & disown $!
|
|
|
|
|
|
|
|
|
|
# Réinitialise le tag togain
|
|
|
|
|
togain="false"
|
|
|
|
|
fi
|
2021-07-02 14:00:50 +02:00
|
|
|
|
done
|
|
|
|
|
wait
|