Avif-cli is a command-line tool that allows us to convert images to AVIF format quickly and easily.
The AVIF image format (AV1 Image File Format) is becoming increasingly popular due to its high compression capacity and superior quality compared to other formats like JPEG and PNG, and even WebP.
Avif-cli is based on sharp
, an image processing library capable of handling large images and batch conversions.
Although AVIF is gaining support, it is still not compatible with all browsers and devices. Make sure to provide alternatives like JPEG or PNG or even WEBP on your website.
Installing avif-cli
To use Avif-cli, you first need to install it globally using NPM (the Node.js package manager):
npm install -g avif-cli
This command will install Avif-cli and make it accessible from anywhere.
Using Avif-cli
Basic Conversion
To convert a single image, use the following command:
avif input.jpg -o output.avif
Here, input.jpg
is the image you want to convert, and output.avif
is the name of the converted file.
Advanced Options
Avif-cli offers a wide range of options that allow you to control specific aspects of the conversion.
Parameter | Description | Default Value |
---|---|---|
--input | Input file name(s) | *.{jpg,jpeg,tif,tiff,webp,png,gif,svg} |
--output | Output directory, defaults to the same directory as input | "" |
--quality | Quality vs file size, from 1 (lowest) to 100 (highest) | 50 |
--effort | CPU effort vs file size, from 0 (faster/larger) to 9 (slower/smaller) | 4 |
--lossless | Use lossless compression | false |
--chroma-subsampling | Chroma subsampling, options: ‘4:2:0’ (subsampling) or ‘4:4:4’ (no subsampling) | 4:4:4 |
--keep-metadata | Keep all metadata | false |
--overwrite | Allow overwriting existing output files | false |
--append-ext | Add .avif to the file name instead of replacing the current extension | false |
--verbose | Write progress to stdout | false |
-h, --help | Show help | false |
--version | Show version number | false |
Converting a Complete Directory
To convert all images in a directory, you can use the following command:
avif /path/to/directory/* -o /path/to/output/
This will convert all images in the specified directory to AVIF format and save them in the indicated output folder.
Batch Convert Multiple Images
If you have several images you want to convert, you can do so in batch using the following command:
avif *.jpg -o output/
This command converts all images with the .jpg
extension in the current directory to AVIF format and saves the converted files in a directory called output
.
Adjusting Compression Quality
You can control the quality of the resulting AVIF image by adjusting the quality parameter. The quality value can range from 0 (worst quality and highest compression) to 100 (best quality and lowest compression).
avif input.jpg -o output.avif -q 40
In this example, the image quality has been set to 40%.
Avif-cli is Open Source, and all the code and documentation is available in the project repository on GitHub - lovell/avif-cli