Skip to content

jonschlinkert/media-size

Repository files navigation

@jonschlinkert/media-size NPM version NPM downloads Build Status

Get the width and height of a video or image.

Install

Install with npm:

$ npm install --save @jonschlinkert/media-size

Get the width and height of image and video files.

Images are read with image-size. Videos are read with video-size, which shells out to ffprobe.

Install

npm install media-size
pnpm add media-size
yarn add media-size

Requirements

Image sizing works without extra system dependencies.

Video sizing uses video-size, which shells out to ffprobe, so FFmpeg needs to be installed and ffprobe needs to be available on your PATH.


Usage

import { mediaSize } from '@jonschlinkert/media-size';

const dimensions = await mediaSize('photo.jpg');
console.log(dimensions);
// { width: 4032, height: 3024, orientation: undefined }

const dimensions = await mediaSize('video.mp4');
console.log(dimensions);
// { width: 1280, height: 720, orientation: 'landscape' }

API

mediaSize(filepath)

Returns a promise for the dimensions of a supported image or video file.

const dimensions = await mediaSize('photo.webp');

Returns null when filepath is not a supported image or video file.

const dimensions = await mediaSize('notes.txt');

console.log(dimensions);
// null

Result

mediaSize returns an object with width, height, and orientation for supported files:

interface MediaDimensions {
  width: number;
  height: number;
  orientation: unknown;
}

Image orientation is passed through from image-size when available. Video orientation is described below.

interface VideoDimensions {
  width: number;
  height: number;
  orientation: 'landscape' | 'portrait' | null;
}

Image orientation is passed through from video-size when available. Video orientation is described below.

  • width and height are the encoded dimensions reported by ffprobe.
  • orientation is based on the display dimensions. Rotation metadata is respected, so a rotated video can be reported as portrait even when the encoded width is greater than the encoded height. Square videos return null.

Supported Image Types

Supports the following image extensions:

  • avif
  • avifs
  • bmp
  • cur
  • dds
  • dib
  • gif
  • heic
  • heics
  • heif
  • heifs
  • icb
  • icns
  • ico
  • j2c
  • j2k
  • jfi
  • jfif
  • jif
  • jp2
  • jpc
  • jpe
  • jpeg
  • jpf
  • jpg
  • jpm
  • jxl
  • ktx
  • ktx2
  • mj2
  • pam
  • pbm
  • pfm
  • pgm
  • png
  • pnm
  • ppm
  • psd
  • svg
  • svgz
  • tga
  • tif
  • tiff
  • vda
  • vst
  • webp

Supported Video Types

Supports the following video types / file extensions:

  • 3g2
  • 3gp
  • avi
  • flv
  • m2t
  • m2ts
  • m4v
  • mkv
  • mov
  • mp4
  • mpeg
  • mpg
  • mts
  • ts
  • webm

About

Related projects

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)

To generate the readme and API documentation with verb:

$ npm install -g verb verb-generate-readme && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2026, Jon Schlinkert. MIT


This file was generated by verb-generate-readme, v0.1.31, on May 16, 2026.

Packages

 
 
 

Contributors