dotfiles/.local/bin/music-video-convert

89 lines
2 KiB
Bash
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Sortie à la moindre erreur
set -e
if ! loudgain
then
echo "loudgain semble non fonctionnel, peut-être besoin dune recompilation de loudgain et/ou ffmpeg6.1?"
exit 1
fi
shopt -s globstar
# Initialisation de la mise en forme
blue=$(tput setaf 4)
green=$(tput setaf 2)
red=$(tput setaf 1)
reset=$(tput sgr0)
bold=$(tput bold)
# Répertoire source
rootdir="${HOME}/Vidéos/Musique"
cd "$rootdir"
# Pour tous les sous-répertoires, à tous les niveaux
for dir in **/ ./
do
# Ignorer les liens symboliques
if [[ -h "${rootdir}/${dir::-1}" ]]
then
continue
fi
# Répertoire de destination
dstdir="${HOME}/Musique/Vidéos/${dir}"
# Création du répertoire de destination
mkdir -p "${dstdir}"
# On entre dans le répertoire à traiter
cd "${rootdir}/${dir}"
# Pour chaque fichier
for file in *
do
# On ignore les dossiers
if [[ -d "${file}" ]]
then
continue
fi
# Définition du fichier de destination
dstfile="${file%.*}.opus"
# Passe les fichiers déjà traités
if [[ "${dstdir}${dstfile}" -nt "${file}" || "${dstdir}${file}" -nt "${file}" ]]
then
echo "[${blue}SKIP${reset}] ${dir#./}$file"
continue
fi
# Converti uniquement les vidéos
if [[ "$(file --mime-type -b -e ascii -e compress -e tar -e cdf "$file")" =~ video/ ]]
then
# Parallélisation des conversions
declare -a running
mapfile -t running < <(jobs -p)
if [[ "${#running[@]}" -ge 12 ]]
then
wait -n
fi
(
echo " ${dir#./}$file"
ffmpeg -n -loglevel error -i "file:$file" -c:a libopus -b:a 128K "file:${dstdir}${dstfile}" && \
loudgain -S -r -k -q -s e "${dstdir}${dstfile}" &>/dev/null && \
echo "[${green} OK ${reset}] ${dir#./}$file" || \
echo "[${red}FAIL${reset}] ${dir#./}$file"
) &
else
# Copie les autres types de fichiers
echo "[${green}COPY${reset}] ${dir#./}$file"
cp -n --reflink=auto "${file}" "${dstdir}/"
fi
done
done
wait