3.0.3 • Published 11 months ago

leather v3.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Leather

Licence npm Issues Build Status

Have you ever wished for the ability to retrieve image or video file attributes such as width, height, size, and mime type in Node without having to resort to external libraries such as ffprobe?

Yeah, me too! This is why leather was created. At the moment, the only package that does something similar is image-size and while it does work well, it does not handle video formats.

How

leather uses streams to read image and video files in byte-sized chunks. As soon attributes have been extracted, the stream will be closed. Some file formats have a well-defined fixed position in which these attributes can be found, in those cases, leather skips any bytes before that position and reads only the bytes needed to extract attributes directly.

However, sometimes the byte offset of these attributes may vary, in these scenarios leather makes a best-effort attempt to read as few bytes as possible to get to the good stuff!

Why

Before leather, if you wanted to do something like this in Node you would need to install image-size to handle images and either use ffprobe directly or using some kind of wrapper package to handle video files.

While I love ffprobe for its capabilities, setting up a cross-platform package requires some configuration. This package aims to solve that by steering clear of any command-line tools which makes it more portable.

Node support

Stable Node versions from 14.18 and up are tested, Node 12.20 and up is supported.

Installation

Install the package locally using npm:

npm install leather --save

Or using yarn

yarn add leather

Usage

After installing the package, it can be imported using commonjs:

const {readMediaAttributes} = require('leather');

Or using ES modules:

import {readMediaAttributes} from 'leather';

Then, it can be called on supported image and video formats:

console.log(readMediaAttributes('cat.jpg'));

// => {width: 200, height: 200, size: 40000, mime: 'image/jpeg'}

Buffer support

Starting from version 2.1.0, all readMediaAttributes methods also accept Buffer instances in addition to file paths:

const buffer = fs.readFileSync('cat.png');

console.log(readMediaAttributes(buffer));

// => {width: 200, height: 200, size: 40000, mime: 'image/jpeg'}

The width and height are pixel based. The size is the same as fs.stat. If the width or height could not be extracted, they will default to 0. The mime type is also returned if found, otherwise undefined.

Using specific extractors

If you are only using one or a few of the extractors, you can opt to require only the extractors you need, e.g. for jpg/jpeg using commonjs:

const {readFileSync} = require('fs');
const {readMediaAttributes} = require('leather/extractors/jpg');

console.log(readMediaAttributes('cat.jpg'));
console.log(readMediaAttributes(readFileSync('cat.jpg')));

// => {width: 200, height: 200, size: 40000, mime: 'image/jpeg'}

Or using ES modules:

import {readFileSync} from 'fs';
import {readMediaAttributes} from 'leather/extractors/jpg';

console.log(readMediaAttributes('cat.jpg'));
console.log(readMediaAttributes(readFileSync('cat.jpg')));

// => {width: 200, height: 200, size: 40000, mime: 'image/jpeg'}

Supported formats

All supported image and video formats can be found in the table below:

formatextractormime type
bmpbmpimage/bmp
ddsddsimage/vnd.ms-dds
gifgifimage/gif
icnsicnsimage/x-icns
icoicoimage/vnd.microsoft.icon
curicoimage/vnd.microsoft.icon
j2cj2cimage/jp2
jp2j2cimage/jp2
jpgjpgimage/jpeg
ktxktximage/ktx
pngpngimage/png
apngpngimage/apng
pfmpnmapplication/x-font-type1
pampnmimage/x-portable-arbitrarymap
pbmpnmimage/x-portable-bitmap
pgmpnmimage/x-portable-graymap
ppmpnmimage/x-portable-pixmap
psdpsdimage/vnd.adobe.photoshop
svgsvgimage/svg+xml
tifftiffimage/tiff
webpwebpimage/webp
xpmxpmimage/x-xpixmap
xbmxbmimage/x-xbitmap
fitfitimage/fits
celcelapplication/octet-stream
hdrhdrimage/vnd.radiance
aviavivideo/x-msvideo
fliflivideo/x-fli
flcflivideo/x-fli
flvflvvideo/x-flv
mngpngvideo/x-mng
mp4mp4video/mp4
m4vmp4video/x-m4v
movmp4video/quicktime
ogvogvvideo/ogg
mkvwebmvideo/x-matroska
webmwebmvideo/webm
wmvwmvvideo/x-ms-wmv

Changelog

View releases.

Credits and other resources that saved my soul

3.0.3

11 months ago

3.0.2

11 months ago

3.0.1

11 months ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.5

1 year ago

2.1.0

1 year ago

3.0.0

1 year ago

2.0.0

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.2.0

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago