npm stats
  • Search
  • About
  • Repo
  • Sponsor
  • more
    • Search
    • About
    • Repo
    • Sponsor

Made by Antonio Ramirez

bare-media

2.10.0

@GitHub Actions

npmHomeRepoSnykSocket
Downloads:4705
$ npm install bare-media
DailyWeeklyMonthlyYearly

bare-media

A set of media APIs for Bare

Install

npm i bare-media

Usage

Image:

import { image } from 'bare-media'

const preview = await image(path)
  .decode({ maxFrames })
  .resize({ maxWidth, maxHeight })
  .encode({ mimetype: 'image/webp' })

Video:

import { video } from 'bare-media'

const frames = video(path).extractFrames({ frameIndex })

Each method can also be used independently:

const rgba = await image.decode(buffer, { maxFrames })

Image API

decode()

Decode an image to RGBA

ParameterTypeDescription
bufferobjectThe encoded image
opts.maxFramesnumberMax number of frames to decode in case of animated images

encode()

Encodes an image to a specific format

ParameterTypeDescription
bufferobjectThe rgba image
opts.mimetypestringThe mimetype of the output image
opts.maxBytesnumberMax bytes for the encoded image (reduces quality or fps in case of animated images)
opts...anyAdditional encoder-specific options

crop()

Crop an image

ParameterTypeDescription
bufferobjectThe rgba image
opts.leftnumberOffset from left edge
opts.topnumberOffset from top edge
opts.widthnumberWidth of the region to crop
opts.heightnumberHeight of the region to crop

resize()

Resize an image

ParameterTypeDescription
bufferobjectThe rgba image
opts.maxWidthnumberMax width of the output rgba
opts.maxHeightnumberMax height of the output rgba

slice()

Limits an animated image to a subset of frames. If the image is not animated, it returns the same rgba.

ParameterTypeDescription
bufferobjectThe rgba image
opts.startnumberFrame index at which to start extraction. Default 0.
opts.endnumberFrame index at which to end extraction. Defaults to end of the animation

orientate()

Apply EXIF orientation.

ParameterTypeDescription
bufferobjectThe rgba image
opts.fileBufferOptional. Input image used to extract EXIF metadata.
opts.exifnumberOptional. EXIF orientation value.
opts.transformobjectOptional. Transform object with rotate, flipH, and flipV.

At least one of opts.file, opts.exif, or opts.transform is required. In a pipeline, orientate() can be called without options:

const rgba = await image(path).decode().orientate()

rotate()

Rotate an image in 90-degree multiples.

ParameterTypeDescription
bufferobjectThe rgba image
opts.degnumberRotation in degrees. Allowed values: [0, 90, 180, 270]

flip()

Flip an image horizontally or vertically.

ParameterTypeDescription
bufferobjectThe rgba image
opts.hbooleanOptional. Flip horizontally. Default true
opts.vbooleanOptional. Flip vertically

read()

Read an image from a file path, URL, or buffer.

ParameterTypeDescription
inputobjectFile path, http(s) URL, or raw image buffer

save()

Write an encoded image buffer to a file.

ParameterTypeDescription
filenamestringDestination file path
bufferobjectEncoded image buffer
optsobjectOptions passed through to fs.writeFile

metadata()

[!IMPORTANT] This feature is experimental. The API is subject to change and may break at any time.

Read image metadata.

const data = await image(path).metadata()
const orientation = await image(path).metadata({ tag: 'orientation' })
ParameterTypeDescription
opts.tagstringOptional. Read a single tag by name

Supported formats: jpeg, tiff.

metadata.strip()

[!IMPORTANT] This feature is experimental. The API is subject to change and may break at any time.

Strip metadata from an image returning a new image buffer.

await image(path).metadata.strip().save(outPath)
ParameterTypeDescription
opts.keepColorbooleanKeep color-transform metadata to avoid color shifts. Default true
opts.keepOrientationbooleanKeep orientation to avoid image rotations. Default false

Check with isStripMetadataSupported() for supported types, only jpeg at the moment.

Video API

extractFrames()

Extracts frames from a video in RGBA

ParameterTypeDescription
opts.frameIndexnumberNumber of the frame to extract

metadata()

Basic metadata for the primary video stream

Returns:

FieldTypeDescription
widthnumberWidth of the primary video stream
heightnumberHeight of the primary video stream
codec.idnumberFFmpeg codec id of the primary video stream
codec.namestringFFmpeg codec name of the primary video stream
durationnumberStream duration in seconds
avgFramerate.numeratornumberAverage frame rate numerator
avgFramerate.denominatornumberAverage frame rate denominator
displayRotationnumberRotation stored in display metadata
rotationnumberCorrective rotation to display the video upright
flipHbooleanWhether the display metadata applies a horizontal flip
flipVbooleanWhether the display metadata applies a vertical flip

Throws if the input has no video stream.

transcode()

[!IMPORTANT] This feature is experimental. The API is subject to change and may break at any time.

Transcode a media file to a different format

ParameterTypeDescription
opts.formatstringOutput format name (e.g., mp4, webm, matroska). Default mp4
opts.widthnumberWidth of the output video
opts.heightnumberHeight of the output video

Supported formats: mp4 (VP9+Opus), webm (VP8+Opus), matroska/mkv (VP9+Opus)

Example

import { video } from 'bare-media'

for await (const chunk of video('input.mkv').transcode({
  format: 'mp4',
  width: 1280,
  height: 720
})) {
  console.log('Received chunk:', chunk.buffer.length)
}

CLI

bare-media [flags] [command]

Flags:
  --version|-v   Print version
  --help|-h      Show help

Commands:
  metadata       Print image or video metadata (experimental)
  convert        Convert an image
  transcode      Transcode a video
  types          List supported MIME types

Supported Types

Helpers to check supported media types are exposed in bare-media/types:

  • supportedImageMimetypes: list of supported image mimetypes.
  • supportedVideoMimetypes: list of supported video mimetypes.
  • isImageSupported(mimetype): returns true if the mimetype is a supported image format.
  • isVideoSupported(mimetype): returns true if the mimetype is a supported video format.
  • isMediaSupported(mimetype): returns true if the mimetype is either a supported image or video format.
  • isStripMetadataSupported(mimetype): returns true if metadata.strip() supports the mimetype.

Detect the MIME type of a buffer:

import { detectMimeType } from 'bare-media'

const mimetype = detectMimeType(buffer)

This may return non-media MIME types, use isMediaSupported() to validate support.

License

Apache-2.0