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 capability 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 not yet 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 (Node.js package manager):
npm install -g avif
This command will install Avif-cli and make it accessible from anywhere.
Or you can also run it directly with NPX:
npx avif
Using Avif-cli
Basic Conversion
To convert a single image, use the following command:
npx 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 filename(s) | *.{jpg,jpeg,tif,tiff,webp,png,gif,svg} |
--output | Output directory, defaults to the same directory as the input | "" |
--quality | Quality vs file size, from 1 (minimum) to 100 (maximum) | 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 filename instead of replacing the current extension | false |
--verbose | Write progress to stdout | false |
-h, --help | Show help | false |
--version | Show version number | false |
Converting an entire directory
To convert all images in a directory, you can use the following command:
npx 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.
Convert multiple images in batch
If you have several images you want to convert, you can do it in batch using the following command:
npx 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 higher compression) to 100 (best quality and lower compression).
npx avif input.jpg -o output.avif -quality 40
In this example, the image quality has been adjusted to 40%.
Avif-cli is Open Source, and all the code and documentation is available in the project repository on GitHub - lovell/avif-cli

