@adonis-agora/media
Version:
Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a
172 lines • 8.93 kB
TypeScript
import type { Transformer, TransformerContext, TransformResult } from '../transformer.js';
/** A streaming format a {@link VideoTransformer} can write manifests for. */
export type VideoFormat = 'hls' | 'dash';
/**
* The container of the segments.
*
* - `'cmaf'` — fragmented MP4 (`.m4s`), the format HLS and DASH share: one set of segments, a
* manifest per format. The default.
* - `'ts'` — MPEG-TS (`.ts`), the original HLS segment format. HLS only (dash.js cannot play it);
* for players that predate fMP4 HLS (iOS 9 and older, some set-top boxes and smart TVs).
*/
export type VideoSegmentFormat = 'cmaf' | 'ts';
/**
* Lazily supplies a WebCodecs implementation (`{ VideoEncoder, VideoDecoder, AudioEncoder, … }`).
* Node has no built-in WebCodecs, which is why the mediabunny engine is remux-only by default —
* but NAPI-based implementations exist (e.g. `@napi-rs/webcodecs`), and injecting one unlocks the
* re-encode fallback for sources whose codecs cannot be stream-copied:
*
* ```ts
* transformers.video({ webcodecs: () => import('@napi-rs/webcodecs') })
* ```
*
* The engine installs the provided classes onto `globalThis` (only the ones missing — a native
* implementation is never overwritten), because that is where the media engine looks them up.
*/
export type WebCodecsProvider = () => Promise<Record<string, unknown>>;
/** How a runtime can satisfy the WebCodecs requirement. See {@link resolveWebCodecsSupport}. */
export type WebCodecsSupport = 'native' | 'injected' | 'absent';
/**
* Detect how (whether) WebCodecs is available: `'native'` when the runtime already exposes
* `VideoEncoder`, `'injected'` when a {@link WebCodecsProvider} supplies one, `'absent'` otherwise.
*/
export declare function resolveWebCodecsSupport(provider?: WebCodecsProvider): Promise<WebCodecsSupport>;
/**
* Timeline thumbnails — the preview a player shows over the progress bar while scrubbing. Frames
* are taken every `interval` seconds, scaled to `width` px wide (height follows the aspect ratio),
* and tiled `columns` × `rows` per JPEG sprite sheet; a WebVTT ({@link VIDEO_STORYBOARD}) maps each
* time range to its tile (`storyboard-001.jpg#xywh=x,y,w,h`), the format Video.js, Vidstack,
* JW Player and others read from a `<track kind="metadata" label="thumbnails">`.
*/
export interface VideoStoryboardOptions {
/** Seconds between thumbnails. Default 5. */
interval?: number;
/** Thumbnail width in px. Default 160. */
width?: number;
/** Thumbnails per sheet row. Default 10. */
columns?: number;
/** Thumbnail rows per sheet. Default 10. */
rows?: number;
}
/** The storyboard an engine is asked for: every option resolved, plus the WebVTT file name. */
export interface VideoStoryboardRequest extends Required<VideoStoryboardOptions> {
vtt: string;
}
/** What {@link VideoPackagingEngine.package} receives. */
export interface VideoPackageRequest {
/** Local path of the source file. */
sourcePath: string;
/** Local directory the engine writes the whole package into. */
outputDir: string;
/** Segment container. `'ts'` only ever comes with HLS alone. */
segments: VideoSegmentFormat;
/** File name of the HLS master playlist to write, or `undefined` when HLS is not wanted. */
hlsEntry?: string | undefined;
/** File name of the DASH manifest to write, or `undefined` when DASH is not wanted. */
dashManifest?: string | undefined;
/** Target (max) segment duration in seconds. */
targetDuration: number;
/** Optional WebCodecs implementation enabling the mediabunny re-encode fallback. */
webcodecs?: WebCodecsProvider | undefined;
/**
* Timeline thumbnails to write next to the package (sprite sheets plus `storyboard.vtt`), or
* `undefined` when not wanted. An engine that cannot decode frames must reject the request.
*/
storyboard?: VideoStoryboardRequest | undefined;
}
/** What the engine learned about the source — persisted into the conversion `meta`. */
export interface VideoPackageSummary {
durationSeconds?: number;
width?: number;
height?: number;
videoCodec?: string | null;
audioCodec?: string | null;
}
/**
* The engine seam of {@link VideoTransformer}: a local file in, a streaming package in a local
* directory out — segments plus every manifest the request names, at the root of `outputDir`.
* The default is mediabunny (pure JS, remux); `engines.ffmpeg()` transcodes with the system
* ffmpeg. Tests inject a fake that writes files.
*/
export interface VideoPackagingEngine {
/**
* Whether the engine can write a storyboard (it decodes frames). `transformers.video({
* storyboard })` refuses, at config time, an engine that does not say so.
*/
readonly storyboard?: boolean;
package(request: VideoPackageRequest): Promise<VideoPackageSummary>;
}
export interface VideoTransformerOptions<Name extends string = 'video'> {
/** Conversion name on the record. Default `'video'`. */
name?: Name;
/**
* Which manifests to write. Default `['hls', 'dash']`: with CMAF segments both describe the
* same files, so the second format costs a manifest, not a second copy of the video.
*/
formats?: readonly VideoFormat[];
/** Segment container. Default `'cmaf'`; `'ts'` requires `formats: ['hls']`. */
segments?: VideoSegmentFormat;
/** Target (max) segment duration in seconds. Default 4. */
targetDuration?: number;
/**
* Run inside attach. Default `false`, and think twice: packaging takes real time, and an eager
* failure rolls the attach back. Run `media.library.transform(id, 'video')` from a job.
*/
eager?: boolean;
/**
* The engine. Default: mediabunny (pure JS — no ffmpeg on the host — remux only). Use
* `engines.ffmpeg({ ladder })` to transcode an adaptive-bitrate ladder.
*/
engine?: VideoPackagingEngine;
/** WebCodecs for the mediabunny engine's re-encode fallback (see {@link WebCodecsProvider}). */
webcodecs?: WebCodecsProvider;
/**
* Timeline thumbnails (see {@link VideoStoryboardOptions}): `true` for the defaults, an object
* to tune them. Default off. Needs an engine that decodes frames — `engines.ffmpeg()`; the
* default mediabunny engine only remuxes in Node.
*/
storyboard?: boolean | VideoStoryboardOptions;
}
/** File name of the HLS master playlist a {@link VideoTransformer} writes. */
export declare const VIDEO_HLS_ENTRY = "index.m3u8";
/** File name of the storyboard WebVTT a {@link VideoTransformer} writes with `storyboard`. */
export declare const VIDEO_STORYBOARD = "storyboard.vtt";
/** Defaults of {@link VideoStoryboardOptions}: a thumbnail every 5 s, 160 px wide, 10×10 a sheet. */
export declare const VIDEO_STORYBOARD_DEFAULTS: Required<VideoStoryboardOptions>;
/** File name of the DASH manifest a {@link VideoTransformer} writes. */
export declare const VIDEO_DASH_MANIFEST = "manifest.mpd";
/**
* The built-in video {@link Transformer}: packages a stored video for adaptive streaming —
* segments plus an HLS master playlist and/or a DASH manifest — persisted as
* `record.conversions['video']`.
*
* - **CMAF by default.** Fragmented-MP4 segments are the one format both HLS and DASH play, so
* `formats: ['hls', 'dash']` (the default) writes the video once and describes it twice.
* - **TS when you need it.** `segments: 'ts'` writes MPEG-TS segments, HLS only, for players
* that predate fMP4 HLS.
* - **No ffmpeg needed.** The default engine (mediabunny) remuxes in pure JS; `engines.ffmpeg()`
* transcodes, ladder included, where the host has ffmpeg.
*
* Serve it with `media.access.streamRoute()` and play it with `useMediaStream`.
*/
export declare class VideoTransformer<Name extends string = 'video'> implements Transformer {
readonly name: Name;
readonly eager: boolean;
readonly formats: readonly VideoFormat[];
readonly segments: VideoSegmentFormat;
private readonly targetDuration;
private readonly engine;
private readonly webcodecs;
private readonly storyboard;
constructor(options?: VideoTransformerOptions<Name>);
transform(context: TransformerContext): Promise<TransformResult>;
}
/**
* Run `task` over `items`, at most `limit` at a time. On the first failure no further item starts,
* and the error is rethrown only once every task already running has settled — so a caller
* cleaning up after the failure never races an upload still in flight.
*/
export declare function forEachBounded<T>(items: readonly T[], limit: number, task: (item: T, index: number) => Promise<void>): Promise<void>;
/** Drop `undefined` values so the persisted meta round-trips identically through JSON stores. */
export declare function compactMeta(meta: Record<string, unknown>): Record<string, unknown>;
//# sourceMappingURL=video.d.ts.map