#!/bin/bash
#
origem=/tmp/novo_mp3/
#/media/
#
destino=/tmp/novo_mp3_normalize/
#
source ./cores
#
clear
#
if [[ -d ${destino} ]]; then
    echo -e "Diretorio de Destino$cbved Ja Existe!$cbbra"$cfim
    echo
else
    echo -e "Diretorio de Destino$cver Não Existe!$cbbra Criando.: $cbved${destino}"$cfim
    mkdir -p ${destino}
    echo
fi
#
cd ${origem}
#
IFS=$(echo -en "\n\b")
#
for fp in *.mp2 *.mp3 *.aac *.ac3 *.m4a *.ogg *.wav; do
    if [[ -f ${fp} ]]; then
    tmb=80
    msg1=$cbbra"Processando o Arquivo.: $cbbra'$cbcia${fp}$cbbra'"$cfim
    tmh1=$((${#msg1} - 35)) # tamanho da msg -8 caracteres da cor mas temos + 2 ## assim -6
    if [[ $tmh1 -gt $tmb  ]]; then
       tmb=$(($tmh1 + 2))
    fi
    msi1_spaces=$(printf "%*s" $((($tmb - $tmh1)/2)) "") # espacos antes da msg
    msf1_spaces=$(printf "%*s" $(($tmb - ($tmh1 + ($tmb - $tmh1)/2)))"") # espacos depois da msg
#
    msg2=$cbbra"Verificando o Volume em ${cbved}dB${cbbra}! ${cbama}>>> AGUARDE <<<$cbbra"$cfim
    tmh2=$((${#msg2} - 43))
    msi2_spaces=$(printf "%*s" $((($tmb - $tmh2)/2)) "")
    msf2_spaces=$(printf "%*s" $(($tmb - ($tmh2 + ($tmb - $tmh2)/2)))"")
#
    repetec=$bverde"$(printf '%.0s#' $(seq $tmb))"$cfim
#
#    echo -e $repetec
#    echo -e $bverde"#$msi1_spaces$bamarelo$msg1$msf1_spaces$bverde#"$cfim
#    echo -e $repetec
#    echo -e $bverde"#$msi2_spaces$bamarelo$msg2$msf2_spaces$bverde#"$cfim
#    echo -e $repetec
    #
#    echo
#    sleep 1
    #
#   madplay --quiet -b 16 -R 44100 -S -o raw:- $f | lame --quiet --preset cbr 128 -r -s 44.1 --bitwidth 16 - -o $out > /dev/null 2>&1
#   lame --quiet $f --preset cbr 128 -r -s 44.1 --bitwidth 16 -o $out
#   ffmpeg -loglevel info -i $f -af "volume=0dB" -c:a libmp3lame -ac 2 -b:a 128k -ar 44100 $out > /dev/null 2>&1
    #
###############################
       echo -e $repetec
       echo -e $bverde"#$msi1_spaces$bamarelo$msg1$msf1_spaces$bverde#"$cfim
       echo -e $bverde"#$msi2_spaces$bamarelo$msg2$msf2_spaces$bverde#"$cfim
       echo -e $repetec
       DBLEVEL=$(ffmpeg -i ${fp} -af "volumedetect" -f null /dev/null 2>&1 | grep max_volume | awk -F': ' '{print $2}' | cut -d' ' -f1)
       if (( $(echo "${DBLEVEL} < 0" | bc -l) )); then
          if (( $(echo "${DBLEVEL} > -0.5" | bc -l) )); then
             msg=$cbama"Ja esta no Nivel $cbbra'${cbved}Aceitavel$cbbra'$cbama de -->$cbbra '${DBLEVEL}dB!'"$cfim
             tmh=$((${#msg} - 51))
             msi_spaces=$(printf "%*s" $((($tmb - $tmh)/2)) "")
             msf_spaces=$(printf "%*s" $(($tmb - ($tmh + ($tmb - $tmh)/2)))"")
             echo -e $bverde"#$msi_spaces$bamarelo$msg$msf_spaces$bverde#"$cfim
             echo -e $repetec
             echo
          else
             DB=$(echo "${DBLEVEL}" | cut -c 2-6)
             BITRATE=$(ffmpeg -i ${fp} 2>&1 | grep Audio | awk -F', ' '{print $5}' | cut -d' ' -f1)
             msg=$cbama"Corrigindo o Volume do Arquivo --> $cbbra'$cver${fp}$cbbra'"$cfim
             tmh=$((${#msg} - 35))
             if [[ $tmh -gt $tmb  ]]; then
                tmb=$(($tmh + 2))
             fi
             repetec=$bverde"$(printf '%.0s#' $(seq $tmb))"$cfim
             msi_spaces=$(printf "%*s" $((($tmb - $tmh)/2)) "")
             msf_spaces=$(printf "%*s" $(($tmb - ($tmh + ($tmb - $tmh)/2)))"")
             echo -e $repetec
             echo -e $bverde"#$msi_spaces$bamarelo$msg$msf_spaces$bverde#"$cfim
             #
             msg=$cbbra"Nivel de '${cbved}dB${cbbra}'.: $cver${DBLEVEL}"$cfim
             tmh=$((${#msg} - 35))
             msi_spaces=$(printf "%*s" $((($tmb - $tmh)/2)) "")
             msf_spaces=$(printf "%*s" $(($tmb - ($tmh + ($tmb - $tmh)/2)))"")
             echo -e $bverde"#$msi_spaces$bamarelo$msg$msf_spaces$bverde#"$cfim
             echo -e $repetec
             sleep 2
             echo "ffmpeg -i ${fp} -af "volume=${DB}dB" -c:a libmp3lame -ac 2 -b:a 128k -ar 44100 ${destino}${fp}"
             echo
          fi
       else
          msg=$cbama"Ja esta no Nivel de -->$cbbra '${DBLEVEL}dB!'"$cfim
          tmh=$((${#msg} - 19))
          msi_spaces=$(printf "%*s" $((($tmb - $tmh)/2)) "")
          msf_spaces=$(printf "%*s" $(($tmb - ($tmh + ($tmb - $tmh)/2)))"")
          echo -e $bverde"#$msi_spaces$bamarelo$msg$msf_spaces$bverde#"$cfim
          echo -e $repetec
          echo
       fi
    fi
done

