como-convertir-imagenes-a-avif

How to Convert Images to AVIF Format Using avif-cli

  • 3 min

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.

ParameterDescriptionDefault Value
--inputInput filename(s)*.{jpg,jpeg,tif,tiff,webp,png,gif,svg}
--outputOutput directory, defaults to the same directory as the input""
--qualityQuality vs file size, from 1 (minimum) to 100 (maximum)50
--effortCPU effort vs file size, from 0 (faster/larger) to 9 (slower/smaller)4
--losslessUse lossless compressionfalse
--chroma-subsamplingChroma subsampling, options: ‘4:2:0’ (subsampling) or ‘4:4:4’ (no subsampling)4:4:4
--keep-metadataKeep all metadatafalse
--overwriteAllow overwriting existing output filesfalse
--append-extAdd .avif to the filename instead of replacing the current extensionfalse
--verboseWrite progress to stdoutfalse
-h, --helpShow helpfalse
--versionShow version numberfalse

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%.