From 938bd641a286ac0591c09bc63e214f553ddd1da3 Mon Sep 17 00:00:00 2001 From: Pieds-Nus Date: Thu, 27 Feb 2025 21:40:47 +0000 Subject: [PATCH] Minor update, some new scripts, some updated sss: save some space, picture compressor pdfdiff : perform a visual diff on simila pdfs to highlight --- pdfdiff | 38 ++++++++++++++++++++++++++++++++++++++ sss | 54 ++++++++++++++++++++++++++++++------------------------ xmute | 4 ++++ xpause | 4 ++-- 4 files changed, 74 insertions(+), 26 deletions(-) create mode 100644 pdfdiff create mode 100644 xmute diff --git a/pdfdiff b/pdfdiff new file mode 100644 index 0000000..761e4ff --- /dev/null +++ b/pdfdiff @@ -0,0 +1,38 @@ +#!/bin/bash + + +# pdfdiff : perform a graphical diff on two similar pdf files, and highlight the differences +# Usage : pdfdiff SOURCE1 SOURCE2 [OUTPUT] +# +# Dependencies: mutool (https://mupdf.com) ; ImageMagikc (imagemagick.org) + +tmpdir=$(mktemp -d) + +cp "${1}" "${tmpdir}/a.pdf" +cp "${2}" "${tmpdir}/b.pdf" + +cd "${tmpdir}" + +n=$(mutool info a.pdf | grep -im1 "Pages: " | cut -d " " -f 2) +m=$(mutool info b.pdf | grep -im1 "Pages: " | cut -d " " -f 2) + +if test "${n}" != "${m}" ; then + read -p "Warning: Source PDFs do not have the same number of pages. Continue ? [Y/n]" + test "${REPLY}" = "n" || test "${REPLY}" = "N" && exit 0 + test "${n}" -gt "${m}" && n="${m}" +fi + +mutool draw -F png -A 0 -N -o a-%d.png a.pdf +mutool draw -F png -A 0 -N -o b-%d.png b.pdf + +for i in $(seq 1 "${n}") ; do + compare "a-${i}.png" "b-${i}.png" -compose src "diff-${i}.png" + montage "a-${i}.png" "diff-${i}.png" "b-${i}.png" -tile 3x1 -geometry +0+0 "out-${i}.png" +done + +convert $(ls -v out*.png) out.pdf + +cd - +mv "${tmpdir}/out.pdf" "${3:-./diff.pdf}" + +rm -rf "${tmpdir}" diff --git a/sss b/sss index d841235..cdb4c1b 100644 --- a/sss +++ b/sss @@ -1,7 +1,9 @@ #!/bin/bash +# TODO: Read the doc about optipng for further compression ? + usage="\ -Usage: $0 [-p PREFIX] [-s SUFFIX] [-e EXTENSION] [-g \"GEOMETRY\"] [-q QUALITY] [-v] image1 image2 image3 ...\n +Usage: $0 [-p PREFIX] [-s SUFFIX] [-e EXTENSION] [-g \"GEOMETRY\"] [-q QUALITY] [-vntP] image1 image2 image3 ...\n Use ImageMagick's convert tool to reduce the size of a selection of image files.\n \n Options:\n @@ -30,6 +32,7 @@ Use ImageMagick's convert tool to reduce the size of a selection of image files. \t\tBest compression, most loss of quality. Default off\n -v\t\tMake verbose\n -P\t\tPrint progress (for use with zenity --progress)\n + -n\t\tNo changes. Simulate only, print the command lines but do not run them. -h\t\tPrint this help and exit.\n" if [[ $# == 0 ]] ; then echo -e $usage ; exit 0 ; fi @@ -40,10 +43,11 @@ geom="1920x1080>" qual="85" ext="jpg" verbose=false +nochange="" sampling="-sampling-factor 4:2:0" median="2x2-1-1" -while getopts p:s:g:q:M:e:xGmhSvPt option ; do +while getopts p:s:g:q:M:e:xGmhSvPnt option ; do case "${option}" in p) ((nopt+=2)) ; @@ -79,6 +83,9 @@ while getopts p:s:g:q:M:e:xGmhSvPt option ; do G) ((nopt+=1)) ; gaussian=true; ;; + n) ((nopt+=1)) ; + nochange=true; + ;; m) ((nopt+=1)) ; unset median ;; @@ -106,38 +113,37 @@ test $verbose == true && echo -e "Old file \t Old size \t Old res \t New res \t for f in "${@:$nopt}" ; do - test "$verbose" == true && status="$f \t $(ls -lh $f | cut -d " " -f 5) \t $(identify $f | rev | cut -d " " -f 7 | rev) \t " ; size=$(ls -l "$f" | cut -d " " -f 5) + test "$verbose" == true && status="$f \t $(ls -lh $f | cut -d " " -f 5) \t $(identify "${f}" | rev | cut -d " " -f 7 | rev) \t " ; size=$(ls -l "${f}" | cut -d " " -f 5) name="$(echo $f | rev | cut -d '.' -f 2- | rev)" ext="${EXT:-$(echo $f | rev | cut -d '.' -f 1 | rev)}" - test "$time" && exif -m -t "DateTime" "$f" > /dev/null && name="$(exif -m -t "DateTime" $f | tr ": " ".-")" || echo "$f : no DateTime found in exif data. Using filename instead." + if test "$time" ; then + tmp="$(exif -m -t "DateTime" "${f}")" && name="$(echo ${tmp} | tr ": " "-_" | head -n 1)" || echo "$f : no DateTime found in exif data. Using filename instead." + tmp="$(echo "${f}" | grep -E "([[:digit:]]{4}).?([[:digit:]]{2}).?([[:digit:]]{2}).?([[:digit:]]{2}).?([[:digit:]]{2}).?([[:digit:]]{2})" | sed -e "s/[^0-9]//g" | sed -E "s/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/\1-\2-\3_\4-\5-\6/")" + test "${tmp}" && name="${tmp}" + fi + + nf="${prefix}${name}${suffix}.${ext}" - nf="$prefix""$name""$suffix"."$ext" - - while test -e $nf ; do nf="$prefix""$name""$suffix"_$(((++i)))."$ext" ; done + while test -e $nf ; do nf="${prefix}${name}${suffix}"_$(((++i)))."${ext}" ; done unset i - ((insize+=$(ls -l "$f" | cut -d " " -f 5))) + ((insize+=$(ls -l "${f}" | cut -d " " -f 5))) - convert \ - -auto-orient \ - -define jpeg:dct-method=float \ - $sampling \ - ${median:+\-median $median} \ - -interlace Plane \ - ${gaussian:+-gaussian-blur 0.1} \ - -colorspace RGB \ - -resize "$geom" \ - -quality "$qual" "$f" "$nf"; + cmd="convert -auto-orient -define jpeg:dct-method=float $sampling ${median:+-median $median} -interlace Plane ${gaussian:+-gaussian-blur 0.1} -colorspace RGB -resize ${geom} -quality ${qual} ${f} ${nf}" + test "$exif" = true && cmd="$cmd ; exif --remove --no-fixup -o ${nf} ${nf} > /dev/null" + + if test "${nochange}" ; then + echo "${cmd}" + else + $cmd - test "$exif" = true && exif --remove --no-fixup -o $nf $nf > /dev/null - - ((outsize+=$(ls -l "$nf" | cut -d " " -f 5))) + ((outsize+=$(ls -l "${nf}" | cut -d " " -f 5))) + test "$verbose" == true && echo -e $status"$(identify "$nf" | rev | cut -d " " -f 7 | rev) \t $(ls -lh "$nf" | cut -d " " -f 5) \t "$nf" \t $((100*$(ls -l "$nf" | cut -d " " -f 5)/$size))% " + fi - test $verbose == true && echo -e $status"$(identify "$nf" | rev | cut -d " " -f 7 | rev) \t $(ls -lh "$nf" | cut -d " " -f 5) \t "$nf" \t $((100*$(ls -l "$nf" | cut -d " " -f 5)/$size))% " - - test $progress && echo $(((++progress * 100 / $#))) + test "$progress" && echo "$(((++progress * 100 / $#)))" done test $verbose == true && echo -e "Average compression : $((100*outsize/insize))%" diff --git a/xmute b/xmute new file mode 100644 index 0000000..c912aae --- /dev/null +++ b/xmute @@ -0,0 +1,4 @@ +#!/bin/bash +pid=$(xprop | grep -i pid | cut -d "=" -f 2 | tr -dc "0-9") +index=$(pacmd list-sink-inputs | grep -B30 "application.process.id = \"$pid\"" | grep -m1 "index:" | cut -d ":" -f 2 | tr -dc "0-9") +pacmd list-sink-inputs | grep -A30 "index: $index" | grep -q "muted: no" && pacmd set-sink-input-mute $index true || pacmd set-sink-input-mute $index false diff --git a/xpause b/xpause index 0cfae83..7fe28c8 100644 --- a/xpause +++ b/xpause @@ -30,13 +30,13 @@ if test -s /tmp/xpause ; then echo "unpaused $pid" exit 0 else - echo "xpause FAILED (kill)" + echo "xunpause FAILED (kill -SIGCONT \"$pid\")" exit 1 fi fi fi echo $pid >> /tmp/xpause -kill -s "SIGSTOP" $pid || ( echo "xpause FAILED (kill)" ; exit 1 ) +kill -s "SIGSTOP" $pid || ( echo "xpause FAILED (kill - SIGSTOP $pid)" ; exit 1 ) echo "xpaused $pid" exit 0