PNG/JPG → WebP
Assuming you're adding this to a Makefile (which requires all $ to be $$), and the PNG/JPG files are in a directory called ./remote-assets, you can use the following to produce a WebP version of the JPG/PNG file.
# PNG
find ./remote-assets -type f -name '*.png' | xargs -P $$(nproc) -I {} bash -c 'if [ ! -f "$${1%.png}.webp" ]; then cwebp -mt -q 75 $$1 -o "$${1%.png}.webp"; fi;' _ {} \;
# JPG
find ./remote-assets -type f -name '*.jpg' | xargs -P $$(nproc) -I {} bash -c 'if [ ! -f "$${1%.jpg}.webp" ]; then cwebp -mt -q 75 $$1 -o "$${1%.jpg}.webp"; fi;' _ {} \;
This will:
- Find all
.png/.jpgfiles in the directory (recursively) - Pass the list of matches to
xargs - Determine the number of CPU cores you have
- Spin up that many threads in order to parallelize the work
- Only run if the
.webpversion does not already exist - Use Bash to call
cwebp - Output the file with the same name, but with a
.webpfile extension
Written by Ryan Parman
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#