Changement du script i3blocks/mpd

This commit is contained in:
Breizh 2022-08-10 18:37:33 +02:00
parent cc6f15e243
commit 8ce08fa228
4 changed files with 149 additions and 117 deletions

View file

@ -41,7 +41,7 @@ interval=once
# Supported players are: spotify, vlc, audacious, xmms2, mplayer, and others. # Supported players are: spotify, vlc, audacious, xmms2, mplayer, and others.
[mpd] [mpd]
#instance=spotify #instance=spotify
interval=1 interval=persist
signal=13 signal=13
#markup=none #markup=none

View file

@ -1,27 +1,24 @@
#!/bin/bash #!/bin/bash
runtime="${XDG_RUNTIME_DIR}/i3blocks" # Fonction de réinitialisation en cas de perte de la connexion
[[ ! -d "$runtime" ]] && mkdir -p "$runtime" self-reset() {
echo "<span foreground=\"#515151\">[--:-- --:-- --:--]</span>"
sleep 5
exec "$0"
}
if ! systemctl --user is-active mpd &>/dev/null trap self-reset PIPE
then
origstatus='off'
else
origstatus="$(mpc status | sed -n '2p' | grep -Eo '^\[.*\]' | tr -d '[]')"
[[ -z "$origstatus" ]] && origstatus='stop'
fi
LONG=40 # Initialisation de la connexion au serveur
SHORT=20 coproc nc -U /home/breizh/.mpd/socket
IN=${COPROC[1]}
OUT=${COPROC[0]}
#echo "password xxx" >&${IN}
#sed '/^OK$/q' <&$OUT &>/dev/null
case $BLOCK_BUTTON in # Initialisation des valeurs par défaut et des fonctions
1) mpc -q prev ;; declare -i SCROLL=50 I=0
2) mpc -q toggle ;; declare -- artist song oldfile
3) mpc -q next ;;
#4) mpc -q volume +2 ;;
#5) mpc -q volume -2 ;;
#?*) notify-send "Test" "$BLOCK_BUTTON" ;;
esac
human_time() { human_time() {
local seconds="$1" local seconds="$1"
@ -43,100 +40,123 @@ human_time() {
return return
} }
# Initialisation de létat
echo "status" >&$IN
while read -t 1 -u $OUT output
do
[[ "$output" == "OK" ]] && break
o[${output%%:*}]="${output##*: }"
done
# Boucle principale
while [[ -n "$COPROC_PID" ]]
do
# Gestion du clic, fait office de délai si aucune action (une seconde).
# En cas de clic, lactualisation de laffichage sera instantanné.
read -t 1 BLOCK_BUTTON
status="<span foreground=\"#6699CC\">" case $BLOCK_BUTTON in
case $origstatus in 1|3) [[ "${o["state"]}" == stop ]] \
stop ) status="<span foreground=\"#F2777A\">" ;; && echo "play" >&${IN} \
paused ) status="<span foreground=\"#FFCC66\">" ;; && sed '/^OK$/q' <&$OUT &>/dev/null ;;&
playing ) status="<span foreground=\"#99CC99\">" ;; 2) [[ "${o["state"]}" == stop ]] \
off ) status="<span foreground=\"#515151\">" ;; && echo "play" >&${IN} \
esac || echo "pause" >&${IN} ;;&
1) echo "previous" >&${IN} ;;&
if [[ "$origstatus" != "stop" ]] && [[ "$origstatus" != "off" ]] 3) echo "next" >&${IN} ;;&
then 1|2|3) sed '/^OK$/q' <&$OUT &>/dev/null ;;
if [[ ! -f $runtime/mpd ]] esac
then
mpc -f '[[%artist%§|%name%§]%title%|[%file%]' current > ${runtime}/mpd
fi
song=$(<${runtime}/mpd)
if [[ "${#song}" -gt $LONG ]] # Réinitialisation de létat
then unset o
scrolling=long declare -A o
elif [[ "${#song}" -gt $SHORT ]]
then
scrolling=short
else
scrolling=none
fi
if [[ "$scrolling" != "none" ]]
then
scroll="$(<${runtime}/mpd-scroll)"
if [[ -z "${scroll// /}" ]] # Récupération de létat
then echo "status" >&$IN
printf "%-${LONG}s" "$(<${runtime}/mpd)" > ${runtime}/mpd-scroll while read -t 1 -u $OUT output
song="$(<${runtime}/mpd-scroll)" do
else [[ "$output" == "OK" ]] && break
song="$(<${runtime}/mpd-scroll)" o[${output%%:*}]="${output##*: }"
printf "%-${LONG}s" "${song:1}" > ${runtime}/mpd-scroll done
fi
fi
time="$(echo -e 'status\nclose' | nc -U $MPD_HOST | grep time \
| cut -d' ' -f2)"
time_elapsed=$(human_time "${time%:*}") # Récupération des informations du morceau en cours
time_total=$(human_time "${time#*:}") echo "currentsong" >&$IN
time_left=$(human_time "$(( ${time#*:} - ${time%:*} ))") while read -t 1 -u $OUT output
do
[[ "$output" == "OK" ]] && break
o[${output%%:*}]="${output##*: }"
done
longtime="[$time_elapsed $time_left ${time_total/00:00/--:--}]" # Choix des couleurs selon létat
shorttime="$time_elapsed $time_left" case "${o["state"]}" in
play ) status="<span foreground=\"#99CC99\">" ;;
case $scrolling in pause ) status="<span foreground=\"#FFCC66\">" ;;
long) stop ) status="<span foreground=\"#F2777A\">" ;;
longsong="${song}"
shortsong="${song}"
;;
short)
longsong="$(<${runtime}/mpd)"
shortsong="${song}"
;;
none)
longsong="$(<${runtime}/mpd)"
shortsong="$longsong"
;;
esac esac
longsong="${longsong:0:$LONG}"
shortsong="${shortsong:0:$SHORT}"
longsong="${longsong//&/&amp;}"
shortsong="${shortsong//&/&amp;}"
if [[ "$longsong" =~ § ]] # En cas de changement de morceau,
# on réinitialise laffichage défilant
if [[ "${o["file"]}" != "$oldfile" ]]
then then
longsong="<b>${longsong/§/</b> }" unset bloc1 bloc2 bloc3 bloc4
elif [[ "$song" =~ § ]] artist="${o["Artist"]:-${o["Name"]}}"
then song="${o["Title"]}"
longsong="<b>${longsong}</b>" [[ -z "$artist$song" ]] && song="${o["file"]}"
fi
if [[ "$shortsong" =~ § ]] if [[ "$(( ${#song} + ${#artist} + 1 ))" -gt "$SCROLL" ]]
then then
shortsong="<b>${shortsong/§/</b> }" scroll=true
elif [[ "$song" =~ § ]] song=" $song "
then else
shortsong="<b>${shortsong}</b>" scroll=false
song=" $song"
fi
bloc1="$artist"
bloc2="$song"
oldfile="${o["file"]}"
I=0
fi fi
echo "${longsong} $status$longtime</span>" # Gestion du défilement
echo "${shortsong} $status$shorttime</span>" if "$scroll"
else then
[[ -f ${runtime}/mpd ]] && rm ${runtime}/mpd unset bloc1 bloc2 bloc3 bloc4
[[ -f ${runtime}/mpd-scroll ]] && rm ${runtime}/mpd-scroll declare -- bloc1 bloc2 bloc3 bloc4
printf "$status[--:-- --:-- --:--]</span>\n" " " bloc1="${artist:$I:$SCROLL}"
printf "$status--:-- --:--</span>\n" " " if [[ "${#bloc1}" -eq 0 ]]
fi then
bloc2="${song:$(( I - ${#artist} )):$SCROLL}"
if [[ "$(( ${#bloc1} + ${#bloc2} ))" -lt "$SCROLL" ]]
then
bloc3="${artist:0:$(( SCROLL - ${#bloc2} ))}"
if [[ "$(( ${#bloc1} + ${#bloc2} + ${#bloc3} ))" -lt "$SCROLL" ]]
then
bloc4="${song:0:$(( SCROLL - ${#bloc1} - ${#bloc2} - ${#bloc3} ))}"
fi
fi
else
bloc2="${song:0:$(( SCROLL - ${#bloc1} ))}"
if [[ "$(( ${#bloc1} + ${#bloc2} ))" -lt "$SCROLL" ]]
then
bloc3="${artist:0:$(( SCROLL - ${#bloc2} - ${#bloc1} ))}"
fi
fi
I+=1
[[ "$I" -ge "$(( ${#artist} + ${#song} ))" ]] && I=0
fi
# Gestion du temps
time_elapsed=$(human_time "${o["time"]%:*}")
time_total=$(human_time "${o["time"]#*:}")
time_left=$(human_time "$(( ${o["time"]#*:} - ${o["time"]%:*} ))")
time="[${time_elapsed:---:--} ${time_left:---:--} ${time_total:---:--}]"
# DEBUG
printf '<b>%s</b>%s<b>%s</b>%s%s\n' "$bloc1" "$bloc2" "$bloc3" "$bloc4" " $status$time</span>"
done

View file

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
SINK="alsa_output.pci-0000_2b_00.3.analog-stereo"
currentport=$(pactl list sinks | grep -Po "Active Port: analog.*") currentport=$(pactl list sinks | grep -Po "Active Port: analog.*")
@ -7,10 +8,10 @@ if [[ -n "$button" ]]
then then
if [[ "$currentport" == "Active Port: analog-output-lineout" ]] if [[ "$currentport" == "Active Port: analog-output-lineout" ]]
then then
pactl set-sink-port "alsa_output.pci-0000_2b_00.3.analog-stereo.5" "analog-output-headphones" pactl set-sink-port "$SINK" "analog-output-headphones"
echo "casque" echo "casque"
else else
pactl set-sink-port "alsa_output.pci-0000_2b_00.3.analog-stereo.5" "analog-output-lineout" pactl set-sink-port "$SINK" "analog-output-lineout"
echo "front " echo "front "
fi fi
pkill -RTMIN+10 i3blocks pkill -RTMIN+10 i3blocks

View file

@ -31,7 +31,7 @@ do
# On entre dans le répertoire à traiter # On entre dans le répertoire à traiter
cd "${rootdir}/${dir}" cd "${rootdir}/${dir}"
# Pour chaque fichier # Pour chaque fichier
for file in * for file in *
do do
@ -59,11 +59,13 @@ do
declare -a running declare -a running
running=($(jobs -p)) running=($(jobs -p))
if [[ "${#running[@]}" -ge 6 ]] if [[ "${#running[@]}" -ge 10 ]]
then then
wait -n wait -n
fi fi
togain="true"
( (
echo " $file" echo " $file"
ffmpeg -n -loglevel error -i "file:$file" -c:a libopus -b:a 256K "file:${dstdir}${dstfile}" ffmpeg -n -loglevel error -i "file:$file" -c:a libopus -b:a 256K "file:${dstdir}${dstfile}"
@ -76,14 +78,23 @@ do
fi fi
done done
wait wait
(
cd "${dstdir}" # Si le moindre fichier a été converti, on refait les tags replay gain
shopt -s nullglob
echo "[${blue}GAIN${reset}] $dir" if ${togain} && [[ "${dir}" != "Vidéos/" ]]
loudgain -S -a -k -q -s e *.opus &>/dev/null && \ then
echo "[${green}GAIN${reset}] $dir" || \ (
echo "[${red}GAIN${reset}] $dir" cd "${dstdir}"
shopt -u nullglob shopt -s nullglob
) & disown $! echo "[${blue}GAIN${reset}] $dir"
loudgain -S -a -k -q -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
done done
wait wait