summarylogtreecommitdiffstats
path: root/exiftool.patch
blob: 0bfa8947c254ad9755a337259b0bb675e49a30d7 (plain)
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
--- kunst/kunst	2021-02-02 12:40:50.127158706 +0100
+++ /usr/bin/kunst	2021-02-02 12:33:26.573815292 +0100
@@ -9,10 +9,12 @@
 
 VERSION=1.3.2
 COVER=/tmp/kunst.jpg
-MUSIC_DIR=~/Music/
-SIZE=250x250
-POSITION="+0+0"
 ONLINE_ALBUM_ART=false
+MUSIC_DIR=${KUNST_MUSIC_DIR:-"~/Music/"}
+SIZE=${KUNST_SIZE:-"250x250"}
+POSITION=${KUNST_POSITION:-"+0+0"}
+COVER_NAMES=${KUNST_COVER_NAMES:-"folder|cover|front"}
+COVER_EXT=${KUNST_COVER_EXT:-"jpg|png"}
 
 show_help() {
 printf "%s" "\
@@ -138,33 +140,52 @@
 
 	# Extract the album art from the mp3 file and dont show the messsy
 	# output of ffmpeg
-	ffmpeg -i "$MUSIC_DIR$(mpc current -f %file%)" "$COVER" -y &> /dev/null
+		if [ "$(type -p exiftool &>/dev/null)" ]; then
+			SONG="$MUSIC_DIR/$(mpc --format "%file%" current)"
+			PICTURE_TAG="-Picture"
+
+			if [ "$SONG" = *".m4a" ]; then
+				PICTURE_TAG="-CoverArt"
+			fi
+			# Extract album cover using perl-image-exiftool
+			exiftool -b "$PICTURE_TAG" "$SONG"  > "$COVER"
+			# Check if image is valid
+			img_data=$(identify "$COVER" 2>&1)
+			if [ "$img_data" = *"insufficient"* ]; then
+				return 1
+			fi
+		else
+			ffmpeg -i "$MUSIC_DIR/$(mpc current -f %file%)" "$COVER" -y &> /dev/null
+		fi
 
-	# Get the status of the previous command
-	STATUS=$?
+		STATUS=$?
 
 	# Check if the file has a embbeded album art
 	if [ "$STATUS" -eq 0 ];then
-        [ ! "$SILENT" ] && echo "kunst: extracted album art"
+		[ ! "$SILENT" ] && echo "kunst: extracted album art"
 		ARTLESS=false
 	else
-        DIR="$MUSIC_DIR$(dirname "$(mpc current -f %file%)")"
-        [ ! "$SILENT" ] && echo "kunst: inspecting $DIR"
+		DIR="$MUSIC_DIR/$(dirname "$(mpc current -f %file%)")"
+
+		# prevent iterating the entire $MUSIC_DIR if mpc experiences an error
+		[ "$DIR" == "$MUSIC_DIR"/. ] && ARTLESS=true && return 1
+
+		[ ! "$SILENT" ] && echo "kunst: inspecting $DIR"
 
 		# Check if there is an album cover/art in the folder.
-		# Look at issue #9 for more details
-        for CANDIDATE in "$DIR/cover."{png,jpg}; do
-            if [ -f "$CANDIDATE" ]; then
-                STATUS=0
-                ARTLESS=false
-                convert "$CANDIDATE" $COVER &> /dev/null
-                [ ! "$SILENT" ] && echo "kunst: found cover.png"
-            fi
-        done
+		# Look at issues #9 and #45 for more details
+		while IFS= read -r CANDIDATE; do
+		if [ -f "$CANDIDATE" ]; then
+			STATUS=0
+			ARTLESS=false
+			convert "$CANDIDATE" $COVER &> /dev/null
+			[ ! "$SILENT" ] && echo "kunst: found $(basename "$CANDIDATE")"
+		    fi
+		done  < <(find "$DIR" -type f | grep -i -E -- "($COVER_NAMES).($COVER_EXT)")
     fi
 
 	if [ "$STATUS" -ne 0 ];then
-        [ ! "$SILENT" ] && echo "error: file does not have an album art"
+		[ ! "$SILENT" ] && echo "error: file does not have an album art"
 		get_cover_online
 	fi
 }
@@ -200,10 +221,6 @@
         }
     done
 
-    [ "$KUNST_MUSIC_DIR" != "" ] && MUSIC_DIR="$KUNST_MUSIC_DIR"
-    [ "$KUNST_SIZE" != "" ] && SIZE="$KUNST_SIZE"
-    [ "$KUNST_POSITION" != "" ] && POSITION="$KUNST_POSITION"
-
 	# Flag to run some commands only once in the loop
 	FIRST_RUN=true
 
@@ -211,12 +228,9 @@
 		update_cover
 
 		if [ "$ARTLESS" == true ];then
-			# Dhange the path to COVER because the music note
-			# image is a png not jpg
-			COVER=/tmp/kunst.png
 
 			# Decode the base64 encoded image and save it
-			# to /tmp/kunst.png
+			# to /tmp/kunst.jpg
 			echo "$MUSIC_NOTE" | base64 --decode > "$COVER"
 		fi
 
@@ -229,8 +243,9 @@
 			FIRST_RUN=false
 
 			# Display the album art using sxiv
-			sxiv -g "$SIZE$POSITION" -b "$COVER" -N "Kunst" &
-
+			# If sxiv is closed, kill the script as well with the trap at the end of
+			# the script (see issue #48).
+			(sxiv -g "$SIZE$POSITION" -b "$COVER" -N "Kunst"; kill -USR1 0) &
 			# Save the process ID so that we can kill
 			# sxiv when the user exits the script
 			echo $! >/tmp/kunst.pid
@@ -251,4 +266,6 @@
 trap "" SIGTSTP
 
 trap pre_exit EXIT
+trap 'exit 1' TERM HUP INT
+trap 'exit 0' USR1
 main