dotfiles/.local/bin/music-convert

84 lines
1.8 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-06-17 11:56:49 +02:00
rootdir="${HOME}/Vidéos/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
dstdir="${HOME}/Musique/Vidéos/${dir}"
# 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
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
if [[ "${dstdir}${dstfile}" -nt "${file}" ]]
then
echo "[${blue}SKIP${reset}] ${dir#./}$file"
continue
fi
2023-06-17 11:56:49 +02:00
# Converti uniquement les vidéos
if [[ "$(file --mime-type -b -e ascii -e compress -e tar -e cdf "$file")" =~ video/ ]]
2021-07-02 14:00:50 +02:00
then
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)
if [[ "${#running[@]}" -ge 12 ]]
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
loudgain -S -r -k -q -s e "${dstdir}${dstfile}" &>/dev/null && \
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
done
wait