How to Convert Images to WebP Format in Linux

One of the numerous best practices you will hear of, for optimizing your web-site performance is using compressed images. In this article, we will share with you a new image format called webp for creating compressed and quality images for the web.

WebP is a relatively new, open source image format that offers exceptional lossless and lossy compression for images on the web, designed by Google. To use it, you need to download pre-compiled utilities for Linux, Windows and Mac OS X.

With this modern image format, webmasters and web developers can create smaller, richer images that make the web faster.

How to Install WebP Tool in Linux

Thankfully, the webp package is present in the Ubuntu official repositories, you can install it using the APT package manager as shown.

$ sudo apt install webp 

On other Linux distributions, start by downloading the webp package from Googles repository using the wget command as follows.

$ wget -c https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-0.6.1-linux-x86-32.tar.gz

Now extract the archive file and move into the extracted package directory as follows.

$ tar -xvf libwebp-0.6.1-linux-x86-32.tar.gz 
$ cd libwebp-0.6.1-linux-x86-32/
$ cd bin/
$ ls

Webp Packages

As you can see from the above screen shot, the package contains a precompiled library (libwebp) for adding webp encoding or decoding to your programs and various webp utilities listed below.

  • anim_diff – tool to display the difference between animation images.
  • anim_dump – tool to dump the difference between animation images.
  • cwebp – webp encoder tool.
  • dwebp – webp decoder tool.
  • gif2webp – tool for converting GIF images to webp.
  • img2webp – tools for converting a sequence of images into an animated webp file.
  • vwebp – webp file viewer.
  • webpinfo – used to view info about a webp image file.
  • webpmux – webp muxing tool.

To convert an image to webp, you can use the cwebp tool, where the -q switch defines the output quality and -o specifies the output file.

$ cwebp -q 60 Cute-Baby-Girl.png -o Cute-Baby-Girl.webp
OR
$ ./cwebp -q 60 Cute-Baby-Girl.png -o Cute-Baby-Girl.webp

Covert Image to WebP Format

You can view the converted webp image using the vwebp tool.

$ ./vwebp Cute-Baby-Girl.webp

View WebP Format Image

You can see all options for any of the tools above by running them without any arguments or using the -longhelp flag, for example.

$ ./cwebp -longhelp

Last but not least, if you want to run the above programs without typing their absolute paths, add the directory ~/libwebp-0.6.1-linux-x86-32/bin to your PATH environmental variable in your ~/.bashrc file.

$ vi ~/.bashrc

Add the line below towards the end of the file.

export PATH=$PATH:~/libwebp-0.6.1-linux-x86-32/bin

Save the file and exit. Then open a new terminal window and you should be able to run all webp programs like any other system commands.

WebP Project Homepage: https://developers.google.com/speed/webp/

 

Convert audio between MP3, FLAC, WAV — including hi-fi and lo-fi and short clips

Using SoxLAMEFLAC, and madplay — I put together this “dictionary” of shell commands that can be run to convert audio from one format to other, including dialup-modem-streamable lofi mono MP3 files, and including shorter clips of music.

Above each command-line and the INPUT and OUTPUT audio formats. (represented by $in and $out in the command line.)

For short audio clips, you need $start — what second to start the clip at, and $duration — how long the clip should go. All clips have a 1-second fade-in, and 2-second fade-out.

flac wav
flac -sd $in -o $out

flac wav clip
flac -sdc $in | sox -t wav — -t raw -s -w -c 2 — trim $start $duration | sox -t raw -r 44100 -s -w -c 2 — $out fade h 1 $duration 2

flac wav-lofi
flac -sdc $in | sox -t wav — -s -w -c 1 -r 22050 $out rate

flac mp3-hifi
flac -sdc $in | lame — $out

flac mp3-hifi clip
flac -sdc $in | sox -t wav — -t raw -s -w -c 2 — trim $start $duration | sox -t raw -r 44100 -s -w -c 2 — -t wav — fade h 1 $duration 2 | lame — $out

flac mp3-lofi
flac -sdc $in | sox -t wav — -t wav -s -w -c 1 -r 22050 — rate | lame -b 80 — $out

flac mp3-lofi clip
flac -sdc $in | sox -t wav — -t raw -s -w -c 1 -r 22050 — rate | sox -t raw -r 22050 -s -w -c 1 — -t raw -r 22050 -s -w -c 1 — trim $start $duration | sox -t raw -r 22050 -s -w -c 1 — -t wav — fade h 1 $duration 2 | lame -b 80 — $out

wav flac
flac -s $in -o $out

wav wav clip
sox -t wav $in -t raw -s -w -c 2 — trim $start $duration | sox -t raw -r 44100 -s -w -c 2 — -t wav $out fade h 1 $duration 2

wav wav lofi
sox -t wav $in -s -w -c 1 -r 22050 $out rate

wav mp3-hifi
lame $in $out

wav mp3-hifi clip
sox -t wav $in -t raw -s -w -c 2 — trim $start $duration | sox -t raw -r 44100 -s -w -c 2 — -t wav — fade h 1 $duration 2 | lame — $out

wav mp3-lofi
sox -t wav $in -t wav -s -w -c 1 -r 22050 — rate | lame -b 80 — $out

wav mp3-lofi clip
sox -t wav $in -t raw -s -w -c 1 -r 22050 — rate | sox -t raw -r 22050 -s -w -c 1 — -t raw -r 22050 -s -w -c 1 — trim $start $duration | sox -t raw -r 22050 -s -w -c 1 — -t wav — fade h 1 $duration 2 | lame -b 80 — $out

mp3 flac
madplay -q -o wave:- $in | flac -s — -o $out

mp3 wav clip
madplay -q -o wave:- $in | sox -t wav — -t raw -s -w -c 2 — trim $start $duration | sox -t raw -r 44100 -s -w -c 2 — $out fade h 1 $duration 2

mp3 wav lofi
madplay -q -o wave:$out -m -R 22050 $in

mp3 mp3-hifi
cp $in $out

mp3 mp3-hifi clip
madplay -q -o wave:- $in | sox -t wav — -t raw -s -w -c 2 — trim $start $duration | sox -t raw -r 44100 -s -w -c 2 — -t wav — fade h 1 $duration 2 | lame — $out

mp3 mp3-lofi
madplay -q -o wave:- -m -R 22050 $in | lame -b 80 — $out

mp3 mp3-lofi clip
madplay -q -o wave:- -m -R 22050 $in | sox -t raw -r 22050 -s -w -c 1 — -t raw -r 22050 -s -w -c 1 — trim $start $duration | sox -t raw -r 22050 -s -w -c 1 — -t wav — fade h 1 $duration 2 | lame -b 80 — $out

Got any more single-line open-source audio-converting commands to add?

—————————————————

Try this one 🙂

ls *.flac |while read a; do n=$(basename «$a» .flac); flac -sdc «$a» |lame -V 1 — «${n}.mp3» ; done

————————————————

just save this into a file
such as convert.sh and make it executable

#!/bin/bash
FILES=$(ls *.flac | cut -d ‘.’ -f1)
for i in $FILES; do
echo converting: $i.flac
flac -sdc $i.flac | lame — $i.mp3
done

—————————————-