-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalbumart-ueber.bash
executable file
·139 lines (120 loc) · 3.22 KB
/
albumart-ueber.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# A Script to Display Album Art in a TMUX pane
# These variables will be read from the command line for testing
[[ -z "$verbose" ]] && verbose=false
# This is the music directory scanned by mpd, edit this if you have this set differently
[[ -z "$music_dir" ]] && music_dir="$HOME/Music"
#default dimensions
width=32
height=32
x=0
y=0
# size down for smaller screens
typeset -i hsize
# we only need the horizontal size here
hsize=$(xdpyinfo | awk '/dimensions/{print $2}' | cut -f1 -dx)
if [[ hsize -lt 1920 ]]; then
width=22
height=22
fi
main() {
tput civis # hide the cursor
trap finish 2
# Main Loop
while true; do
tmux_client_check
mpd_check
update_art
if [[ "$verbose" == 'false' ]] || [[ "$verbose" == 0 ]];then
clear
fi
ueber_art
done
}
rm_tmpimgfiles() {
# Delete image files used when extracting artwork from music files
find /tmp -name "tmp.*.jpg" -delete
}
finish() {
# Cleanup steps
ueber_clear
tput cnorm # make cursor visible
rm_tmpimgfiles
exit 0
}
tmux_client_check() {
# If in a tmux session, make sure there is a client before moving on
if [[ "$TERM" == "tmux-256color" ]]; then
until tmux list-clients -t "$(tmux display -p '#{session_name}')" | grep -q pts; do
sleep 1
done
fi
}
mpd_check() {
# wait until mpd is running
while true; do
if mpc -q; then
break
fi
sleep 1
done
}
update_art() {
# update $old_filename and then (re)-fetch the filename for the current album cover
old_filename=$filename
art_filename
}
art_filename() {
# use mpc to find the path of the currently playing album's artwork
local current_file
local current_dir
local tmpimgfile
current_file=$(mpc current -f "%file%")
current_dir=$(dirname "$current_file")
if [[ -n "$current_file" ]]; then
# Try finding artwork in the album directory
filename=$(find "$music_dir"/"$current_dir" -iregex '^.*\(front\|cover\|folder\)\.\(jpg\|png\|bmp\)$' | head -1)
# Otherwise, try to extract artwork from the music file
if [[ -z "$filename" ]]; then
tmpimgfile=$(mktemp --suffix=.jpg)
ffmpeg -i "$music_dir"/"$current_file" "$tmpimgfile" -y
if ! cmp -s "$filename" "$tmpimgfile"; then
filename=$tmpimgfile
fi
find /tmp -name "tmp.*.jpg" | grep -v "$filename"| xargs -r rm
fi
# Finally, if no art can be found then fallback to a placeholder image
if [[ -z "$filename" ]] || [[ ! -s "$filename" ]]; then
filename="$HOME/Pictures/no_art.png"
fi
fi
}
ueber_art() {
# Declare the image details, time it with the check_old function, pipe it to ImageLayer
{
# shellcheck disable=2154
declare -Ap add_command=([action]="add" [identifier]="album_art" [x]="$x" [y]="$y" [width]="$width" [height]="$height" [path]="$filename")
check_old
} | ueberzug layer --parser bash
}
check_old() {
# check to see if the file contents has changed
while true; do
mpc idle player update
update_art
if ! cmp -s "$old_filename" "$filename"; then
break && old_filename=$filename
fi
done
}
ueber_clear() {
{
# shellcheck disable=2154
declare -Ap remove_command=([action]="remove" [identifier]="album_art")
} | ueberzug layer --parser bash
}
if [[ "$verbose" == 'false' ]] || [[ "$verbose" == 0 ]];then
main 2> /dev/null
elif [[ "$verbose" == 'true' ]] || [[ "$verbose" == 1 ]];then
main
fi