UNPKG

@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

167 lines • 7.23 kB
import type { MediaProbeEngine } from './probe.js'; import type { VideoPackagingEngine, VideoStoryboardRequest } from './video.js'; /** * Engines backed by the system `ffmpeg` / `ffprobe` binaries — the alternative to the default * mediabunny engines for hosts that have ffmpeg installed. What ffmpeg buys over mediabunny is * **encoding**: mediabunny can only stream-copy in Node (no WebCodecs), while ffmpeg can transcode * any source it decodes into an adaptive-bitrate ladder. * * Plug them into the existing transformers through their `engine` seam: * * ```ts * import { engines, transformers } from '@adonis-agora/media' * * transformers: [ * transformers.video({ engine: engines.ffmpeg({ ladder: engines.ladders.standard }) }), * transformers.probe({ engine: engines.ffprobe() }), * ] * ``` * * No npm dependency: the binaries are spawned, never bundled. A missing binary surfaces as * {@link TransformerRuntimeMissingError}; a failing run as {@link FfmpegFailedError} carrying the * tail of ffmpeg's stderr. */ /** One rung of an adaptive-bitrate ladder. */ export interface FfmpegRendition { /** Output height in px; the width follows the source aspect ratio (rounded to even). */ height: number; /** Target video bitrate, ffmpeg syntax (`'2800k'`, `'5M'`). Caps peak rate at 1.5× this. */ videoBitrate: string; /** Audio bitrate, ffmpeg syntax. Default `'128k'`. */ audioBitrate?: string; } export interface FfmpegBinaryOptions { /** Path or name of the `ffmpeg` binary. Default `'ffmpeg'` (resolved on `PATH`). */ ffmpegPath?: string; /** Path or name of the `ffprobe` binary. Default `'ffprobe'` (resolved on `PATH`). */ ffprobePath?: string; } export interface FfmpegVideoEngineOptions extends FfmpegBinaryOptions { /** * Renditions to encode. Omit to **remux** (stream copy, no re-encode — fast; TS segments need an * h264/aac source, CMAF carries h264, hevc, vp9 or av1 alike). With a ladder, every rung is * transcoded to h264/aac and listed in the manifests. Rungs taller than the source are dropped * (never upscale); if every rung is taller, the smallest one is kept at the source height. */ ladder?: FfmpegRendition[]; /** * AAC bitrate of the shared audio track of a CMAF ladder (TS renditions each carry their rung's * `audioBitrate`). Default: the ladder's highest, else `'128k'`. */ audioBitrate?: string; /** x264 preset when transcoding. Default `'veryfast'`. */ preset?: string; /** Extra ffmpeg arguments, inserted right before the muxer options. */ extraArgs?: string[]; } /** Ready-made ladders. */ export declare const ladders: { /** 360p / 720p / 1080p — a sane default for course videos. */ standard: ({ height: number; videoBitrate: string; audioBitrate: string; } | { audioBitrate?: never; height: number; videoBitrate: string; })[]; /** 360p / 720p — lighter on storage and encode time. */ compact: ({ height: number; videoBitrate: string; audioBitrate: string; } | { audioBitrate?: never; height: number; videoBitrate: string; })[]; }; /** The rungs actually encoded for a source of `sourceHeight` px: never upscale, never empty. */ export declare function effectiveLadder(ladder: FfmpegRendition[], sourceHeight: number | undefined): FfmpegRendition[]; /** * Build the ffmpeg argument list for one HLS run with TS segments. Exported for tests and for anyone wrapping the * engine; `renditions` empty means remux. */ export declare function buildHlsArgs(input: { sourcePath: string; outputDir: string; entryName: string; targetDuration: number; hasAudio: boolean; renditions: FfmpegRendition[]; preset: string; extraArgs: string[]; }): string[]; /** * Validate a ladder while the config is in hand — a bad rung would otherwise fail deep inside * libx264 with a cryptic error on the first transcode. */ /** Check a ladder at config time: even positive heights, parseable bitrates. Exported for the other engines. */ export declare function validateLadder(ladder: FfmpegRendition[] | undefined): void; /** `'2800k'` → 2_800_000. Plain numbers pass through. */ /** `'800k'` → `800000`, `'5M'` → `5000000`. Exported for the other engines. */ export declare function parseBitrate(value: string): number; /** Thumbnail height for `width` px wide, keeping the source aspect ratio, rounded to even. */ export declare function storyboardTileHeight(width: number, source: { width?: number | undefined; height?: number | undefined; }): number; /** * Build the ffmpeg argument list that writes a storyboard's sprite sheets: one frame every * `interval` seconds, scaled to `width`×`height`, tiled `columns`×`rows` per JPEG. Exported for * tests. */ export declare function buildStoryboardArgs(input: { sourcePath: string; outputDir: string; storyboard: VideoStoryboardRequest; height: number; }): string[]; /** * The storyboard WebVTT: one cue per thumbnail, `[i·interval, (i+1)·interval)` (the last one ends * at `durationSeconds`), pointing at its tile with a media fragment * (`storyboard-002.jpg#xywh=320,90,160,90`). Thumbnails are capped at what the written sheets * hold, so a cue never points past the last tile. Exported for tests. */ export declare function buildStoryboardVtt(input: { storyboard: VideoStoryboardRequest; height: number; sheets: number; durationSeconds?: number | undefined; }): string; /** * Build the ffmpeg argument list for one DASH (CMAF) run — one manifest, fragmented-MP4 segments * and, with `hlsEntryName`, an HLS master playlist over the same segments. Exported for tests and * for anyone wrapping the engine; `renditions` empty means remux. */ export declare function buildDashArgs(input: { sourcePath: string; outputDir: string; manifestName: string; hlsEntryName?: string | undefined; targetDuration: number; hasAudio: boolean; renditions: FfmpegRendition[]; audioBitrate: string; preset: string; extraArgs: string[]; }): string[]; /** * {@link VideoPackagingEngine} backed by the system ffmpeg: remux by default, or an * adaptive-bitrate ladder when `ladder` is set. * * - CMAF segments go through ffmpeg's DASH muxer, which writes the MPD and (for HLS) a master * playlist over the same fragmented-MP4 segments — one audio track shared by every rung. * - TS segments go through the HLS muxer, every rendition carrying its own audio. * * Needs ffmpeg ≥ 4.3 (`-hls_master_name`). */ export declare function ffmpegVideoEngine(options?: FfmpegVideoEngineOptions): VideoPackagingEngine; /** * {@link MediaProbeEngine} backed by the system ffprobe. Differences from the mediabunny probe: * `format` is ffprobe's `format_name` (e.g. `'mov,mp4,m4a,3gp,3g2,mj2'`), and `mimeType` is not * reported — ffprobe has no MIME/codecs-string notion; the record's own `mimeType` still applies. */ export declare function ffprobeEngine(options?: Pick<FfmpegBinaryOptions, 'ffprobePath'>): MediaProbeEngine; //# sourceMappingURL=ffmpeg.d.ts.map