From: Andrew Lorimer Date: Tue, 14 Jun 2022 05:51:16 +0000 (+1000) Subject: add webp-convert.sh, fix quotes in lastframe.sh X-Git-Url: https://git.lorimer.id.au/scripts.git/diff_plain/f77f89905e3009f185e5c5823f95fc21371a0fe1 add webp-convert.sh, fix quotes in lastframe.sh --- diff --git a/lastframe.sh b/lastframe.sh index 45dd995..611c862 100755 --- a/lastframe.sh +++ b/lastframe.sh @@ -1,9 +1,9 @@ #!/bin/bash -input_fn='input.mov' +input_fn="input.mov" image_fn='output.png' -frame_count=`ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 $input_fn` +frame_count=`ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 "$input_fn"` -ffmpeg -i $input_fn -vf "select='eq(n,$frame_count-1)'" -vframes 1 "$image_fn" 2> /dev/null +ffmpeg -i "$input_fn" -vf "select='eq(n,$frame_count-1)'" -vframes 1 "$image_fn" 2> /dev/null diff --git a/webp-convert.sh b/webp-convert.sh new file mode 100755 index 0000000..be7d1f7 --- /dev/null +++ b/webp-convert.sh @@ -0,0 +1,60 @@ +#!/bin/bash +# +# webp-convert.sh +# +# Search through HTML and PHP files in a directory, get a list of the image +# files used (jpg/png), and convert these to webp in the same directory as +# the originals. Can be used with a rewrite rule on the web server to +# serve the webp file when the original is requested. +# +# Andrew Lorimer, January 2021 +# + +docroot=$1 +if [ ! $docroot ]; then + docroot=. +fi; + +convert_count=0 +larger_count=0 + +echo "Converting files in $docroot" +convert_webp () { + # Perform the conversion. Takes input filename, output filename. + if [[ "$1" == *.png ]]; then + # PNG conversion + convert "$1" -quality 75 -define webp:lossless=true "$2" + echo "Converted $1 to $2" + else + # JPG conversion + convert "$1" -quality 75 "$2" + echo "Converted $1 to $2" + fi + ((convert_count+=1)) + + # Check if webp is actually smaller than original and remove if not + orig_size=$(stat -c%s "$1") + webp_size=$(stat -c%s "$2") + if (( webp_size > orig_size )); then + rm $2 + echo "Removed $2 as it was larger than $1 ($webp_size > $orig_size)" + ((larger_count+=1)) + return 1 + fi + return 0 +} + +while read -r srcfile; do + while IFS= read -r file; do + file_path=$(echo $file | sed "s|^.|$docroot/|") + webp_path=$(sed 's/\.[^.]*$/.webp/' <<< "$file_path") + + # Convert if not already converted + if [ ! -f "$webp_path" ]; then + convert_webp $file_path $webp_path + fi + done < <(sed -n "s:.*