Skip to main content

@twick/media-utils / Exports

@twick/media-utils

Table of contents

Type Aliases

Functions

Type Aliases

Dimensions

Ƭ Dimensions: Object

Type declaration

NameType
heightnumber
widthnumber

Defined in

types.ts:1


Position

Ƭ Position: Object

Type declaration

NameType
xnumber
ynumber

Defined in

types.ts:3


VideoMeta

Ƭ VideoMeta: Dimensions & { duration: number }

Defined in

types.ts:5

Functions

blobUrlToFile

blobUrlToFile(blobUrl, fileName): Promise<File>

Converts a Blob URL to a File object.

Parameters

NameTypeDescription
blobUrlstringThe Blob URL to convert.
fileNamestringThe name to assign to the resulting File.

Returns

Promise<File>

A Promise that resolves to a File object.

Defined in

file-helper.ts:8


detectMediaTypeFromUrl

detectMediaTypeFromUrl(url): Promise<"image" | "video" | "audio" | null>

Detects the media type (image, video, or audio) of a given URL by sending a HEAD request.

Parameters

NameTypeDescription
urlstringThe URL of the media file.

Returns

Promise<"image" | "video" | "audio" | null>

A promise that resolves to 'image', 'video', or 'audio' based on the Content-Type header, or null if the type couldn't be determined or the request fails.

Defined in

url-helper.ts:8


downloadFile

downloadFile(url, filename): Promise<void>

Downloads a file from a given URL and triggers a browser download.

Parameters

NameTypeDescription
urlstringThe URL of the file to download.
filenamestringThe name of the file to be saved.

Returns

Promise<void>

A Promise that resolves when the download is initiated or rejects if there is an error.

Defined in

file-helper.ts:41


getAudioDuration

getAudioDuration(audioSrc): Promise<number>

Retrieves the duration (in seconds) of an audio file from a given source URL. Uses a cache to avoid reloading the same audio multiple times.

Parameters

NameTypeDescription
audioSrcstringThe source URL of the audio file.

Returns

Promise<number>

A Promise that resolves to the duration of the audio in seconds.

Defined in

get-audio-duration.ts:10


getImageDimensions

getImageDimensions(url): Promise<Dimensions>

Gets the dimensions (width and height) of an image from the given URL. Uses a cache to avoid reloading the image if already fetched. Also uses a concurrency limiter to control resource usage.

Parameters

NameTypeDescription
urlstringThe URL of the image.

Returns

Promise<Dimensions>

A Promise that resolves to an object containing width and height.

Defined in

get-image-dimensions.ts:35


getObjectFitSize

getObjectFitSize(objectFit, elementSize, containerSize): Dimensions

Calculates the resized dimensions of an element to fit inside a container based on the specified object-fit strategy ("contain", "cover", "fill", or default).

Parameters

NameTypeDescription
objectFitstringThe object-fit behavior ('contain', 'cover', 'fill', or default/fallback).
elementSizeDimensionsThe original size of the element (width and height).
containerSizeDimensionsThe size of the container (width and height).

Returns

Dimensions

An object containing the calculated width and height for the element.

Defined in

dimension-handler.ts:63


getScaledDimensions

getScaledDimensions(width, height, maxWidth, maxHeight): Dimensions

Calculates the scaled dimensions of an element to fit inside a container based on the specified max dimensions.

Parameters

NameTypeDescription
widthnumberThe original width of the element.
heightnumberThe original height of the element.
maxWidthnumberThe maximum width of the container.
maxHeightnumberThe maximum height of the container.

Returns

Dimensions

An object containing the calculated width and height for the element.

Defined in

dimension-handler.ts:13


getThumbnail

getThumbnail(videoUrl, seekTime?, playbackRate?): Promise<string>

Extracts a thumbnail from a video at a specific seek time and playback rate.

This function creates a hidden <video> element in the browser, seeks to the specified time, and captures the frame into a canvas, which is then exported as a JPEG data URL or Blob URL.

Parameters

NameTypeDefault valueDescription
videoUrlstringundefinedThe URL of the video to extract the thumbnail from.
seekTimenumber0.1The time in seconds at which to capture the frame. Default is 0.1s.
playbackRatenumber1Playback speed for the video. Default is 1.

Returns

Promise<string>

A Promise that resolves to a thumbnail image URL (either a base64 data URL or blob URL).

Defined in

get-thumbnail.ts:13


getVideoMeta

getVideoMeta(videoSrc): Promise<VideoMeta>

Fetches metadata (width, height, duration) for a given video source. If metadata has already been fetched and cached, it returns the cached data.

Parameters

NameTypeDescription
videoSrcstringThe URL or path to the video file.

Returns

Promise<VideoMeta>

A Promise that resolves to an object containing video metadata.

Defined in

get-video-metadata.ts:11


limit

limit<T>(fn): Promise<T>

Wraps an async function to enforce concurrency limits. If concurrency limit is reached, the function is queued and executed later.

Type parameters

Name
T

Parameters

NameTypeDescription
fn() => Promise<T>Async function returning a Promise

Returns

Promise<T>

Promise that resolves/rejects with fn's result

Defined in

limit.ts:33


saveAsFile

saveAsFile(content, type, name): void

Triggers a download of a file from a string or Blob.

Parameters

NameTypeDescription
contentstring | BlobThe content to save, either a string or a Blob.
typestringThe MIME type of the content.
namestringThe name of the file to be saved.

Returns

void

Defined in

file-helper.ts:21