~hnaguski/aart

ref: e4089defa610e59cc64564201c10339d8ecc7acf aart/aart -rwxr-xr-x 2.0 KiB
e4089defHenry Naguski give set_art more responsibility and remove the annoying if statement in the main loop 2 years ago
                                                                                
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
#!/bin/sh

music_dir=$(xdg-user-dir MUSIC)

extract_art() {

	current_path=$music_dir/$1

	# use ffmpeg to extract album art embedded in music files. copies the same
	# codec to (hopefully) not modify the file at all. Not sure if I have the
	# options for it set correctly, especially the format being image2 for
	# everything.
	ffmpeg -loglevel error -y -i "$current_path" -map v? -c copy -f image2 art || return 1

	art=art
	printf "found embedded art\n"
	return 0
}

find_folder_art() {
	for art in "$album_dir"/folder.*; do
		[ -f "$art" ] || continue
		printf "found folder art\n"
		return 0
	done

	printf "no folder.*\n"
	return 1
}

find_cover_art() {
	for art in "$album_dir"/cover.*; do
		[ -f "$art" ] || continue
		printf "found cover art\n"
		return 0
	done

	printf "no cover.*\n"
	return 1
}

copy_art() {
	# get diretory of currently playing song
	album_dir="$music_dir/$1"
	album_dir=${album_dir:-}
	album_dir=${album_dir%%"${album_dir##*[!/]}"}
	[ "${album_dir##*/*}" ] && album_dir=.
	album_dir=${album_dir%/*}
	album_dir=${album_dir%%"${album_dir##*[!/]}"}

	find_folder_art || find_cover_art || return 1

	printf "found external art\n"
	return 0
}

no_art() {
	printf "no art found! falling back to default\n"
	art=noart.png
}

set_art() {
	# attempt to get the album art in various ways
	printf "finding art for %s\n" "$(mpc current)"
	extract_art "$1" || copy_art "$1" || no_art

	cp -f "$art" art
}

first_start() {
	# check if mpd is running
	pgrep mpd >/dev/null || exit 1

	# check status of mpd, if it's stopped then wait for mpd to start playing a
	# song maybe only check for playing, I leave mpd paused quite often
	mpc status | grep -o "playing\|paused" > /dev/null || file=$(mpc -q current --wait -f %file%)

	# get current song's filename relative to mpd's music directory
	file=$(mpc current -f %file%)

	set_art "$file"

	# open sxiv on the art
	sxiv -b art &
}

first_start

while true;
do
	new_file=$(mpc -q current --wait -f %file%)
	
	set_art "$new_file"
done