dotfiles/.local/bin/music-convert

104 lines
2.1 KiB
Text
Raw Normal View History

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)
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
2023-07-29 02:50:05 +02:00
rootdir="${HOME}/Musique"
2021-07-02 14:00:50 +02:00
cd "$rootdir"
2021-12-11 17:25:35 +01:00
# Pour tous les sous-répertoires, à tous les niveaux
2023-06-17 11:56:49 +02:00
for dir in **/ ./
2021-07-02 14:00:50 +02:00
do
# Ignorer les liens symboliques
2023-06-17 11:56:49 +02:00
if [[ -h "${rootdir}/${dir::-1}" ]]
then
continue
fi
2021-12-11 17:25:35 +01:00
2023-06-17 11:56:49 +02:00
# Répertoire de destination
2023-07-29 02:50:05 +02:00
dstdir="/mnt/data/breizh/Musique.opus/${dir}"
2023-06-17 11:56:49 +02:00
# Création du répertoire de destination
2021-07-02 14:00:50 +02:00
mkdir -p "${dstdir}"
2023-06-17 11:56:49 +02:00
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
2023-07-29 02:50:05 +02:00
togain=false
2021-12-11 17:25:35 +01:00
# Pour chaque fichier
2021-07-02 14:00:50 +02:00
for file in *
do
2023-06-17 11:56:49 +02:00
# On ignore les dossiers
if [[ -d "${file}" ]]
then
continue
fi
2023-07-29 02:29:42 +02:00
# Définition du fichier de destination
dstfile="${file%.*}.opus"
# Passe les fichiers déjà traités
2023-07-29 02:50:05 +02:00
if [[ "${dstdir}${dstfile}" -nt "${file}" || "${dstdir}${file}" -nt "${file}" ]]
2023-07-29 02:29:42 +02:00
then
echo "[${blue}SKIP${reset}] ${dir#./}$file"
continue
fi
2023-07-29 02:50:05 +02:00
# Converti uniquement les flacs
if [[ "$file" =~ \.flac$ ]]
2021-07-02 14:00:50 +02:00
then
2023-07-29 02:50:05 +02:00
togain=true
2023-06-17 11:56:49 +02:00
# Parallé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)
2023-07-29 02:50:05 +02:00
if [[ "${#running[@]}" -ge 6 ]]
2021-07-02 14:00:50 +02:00
then
wait -n
fi
(
2023-06-17 11:56:49 +02:00
echo " ${dir#./}$file"
ffmpeg -n -loglevel error -i "file:$file" -c:a libopus -b:a 128K "file:${dstdir}${dstfile}" && \
2023-06-17 11:56:49 +02:00
echo "[${green} OK ${reset}] ${dir#./}$file" || \
echo "[${red}FAIL${reset}] ${dir#./}$file"
2021-07-02 14:00:50 +02:00
) &
else
2023-06-17 11:56:49 +02:00
# Copie les autres types de fichiers
echo "[${green}COPY${reset}] ${dir#./}$file"
2023-04-12 02:34:24 +02:00
cp -n --reflink=auto "${file}" "${dstdir}/" || true
2021-07-02 14:00:50 +02:00
fi
done
2023-07-29 02:50:05 +02:00
wait
if ${togain}
then
(
cd "${dstdir}"
shopt -s nullglob
echo "[${blue}GAIN${reset}] $dir"
loudgain -q -S -a -k -s e -- *.opus &>/dev/null && \
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