import { EventEmitter, Transform, TransformOptions, Readable, ReadableOptions, Writable, WritableOptions, TransformCallback as TransformCallback$1 } from 'node:stream'; import ffmpeg from '@mmomtchev/ffmpeg'; import { TransformCallback } from 'stream'; declare const StreamTypes: { readonly Audio: "Audio"; readonly Video: "Video"; readonly Subtitle: "Subtitle"; readonly Data: "Data"; }; type StreamType = keyof typeof StreamTypes; interface MediaStreamDefinition { type: StreamType; bitRate: number; codec: ffmpeg.AVCodecID | ffmpeg.Codec; timeBase?: ffmpeg.Rational; codecOptions?: Record; } interface VideoStreamDefinition extends MediaStreamDefinition { type: 'Video'; width: number; height: number; frameRate: ffmpeg.Rational; pixelFormat: ffmpeg.PixelFormat; flags?: number; } interface AudioStreamDefinition extends MediaStreamDefinition { type: 'Audio'; channelLayout: ffmpeg.ChannelLayout; sampleFormat: ffmpeg.SampleFormat; sampleRate: number; frameSize?: number; } interface MediaTransformOptions extends TransformOptions { objectMode?: never; } /** * A generic user-definable MediaTransform, uses object mode. */ declare class MediaTransform extends Transform { constructor(options?: MediaTransformOptions); } /** * A generic raw MediaStream, has a definition and it is an EventEmitter. */ interface MediaStream extends EventEmitter { ready: boolean; definition(): MediaStreamDefinition; codec(): ffmpeg.Codec; } /** * A generic encoding MediaStream */ interface MediaEncoder extends MediaStream { } /** * A generic decoding MediaStream */ interface MediaDecoder extends MediaStream { } interface EncodedMediaReadableOptions extends ReadableOptions { stream: ffmpeg.Stream; } /** * A generic encoded media stream */ declare class EncodedMediaReadable extends Readable { stream_: ffmpeg.Stream; type: 'Audio' | 'Video'; constructor(options: EncodedMediaReadableOptions); get ready(): boolean; get stream(): ffmpeg.Stream; codec(): ffmpeg.Codec; codecParameters(): ffmpeg.CodecParametersView; context(): ffmpeg.AudioEncoderContext | ffmpeg.VideoEncoderContext | null; isAudio(): boolean; isVideo(): boolean; } /** * Encoded audio stream */ interface EncodedAudioReadable extends EncodedMediaReadable { type: 'Audio'; push(chunk: ffmpeg.Packet, encoding?: BufferEncoding): boolean; } /** * Encoded video stream */ interface EncodedVideoReadable extends EncodedMediaReadable { type: 'Video'; push(chunk: ffmpeg.Packet, encoding?: BufferEncoding): boolean; } /** * A generic encoded media stream */ interface EncodedMediaWritable extends Writable { _write(chunk: ffmpeg.Packet, encoding: BufferEncoding, callback?: (error: Error | null | undefined) => void): void; } /** * A raw media stream */ interface MediaWritable extends Writable { _write(chunk: ffmpeg.VideoFrame | ffmpeg.AudioSamples, encoding: BufferEncoding, callback?: (error: Error | null | undefined) => void): void; } /** * A video media stream */ interface VideoWritable extends MediaWritable { _write(chunk: ffmpeg.VideoFrame, encoding: BufferEncoding, callback?: (error: Error | null | undefined) => void): void; } /** * An audio media stream */ interface AudioWritable extends MediaWritable { _write(chunk: ffmpeg.AudioSamples, encoding: BufferEncoding, callback?: (error: Error | null | undefined) => void): void; } /** * A raw media stream */ interface MediaReadable extends Readable { push(chunk: ffmpeg.VideoFrame | ffmpeg.AudioSamples, encoding?: BufferEncoding): boolean; } /** * A video media stream */ interface VideoReadable extends MediaReadable { push(chunk: ffmpeg.VideoFrame, encoding?: BufferEncoding): boolean; } /** * An audio media stream */ interface AudioReadable extends MediaReadable { push(chunk: ffmpeg.AudioSamples, encoding?: BufferEncoding): boolean; } interface MuxerOptions extends WritableOptions { /** * The name of the output file, null for exposing a ReadStream */ outputFile?: string; /** * Amount of data to buffer, only when writing to a WriteStream, @default 64Kb */ highWaterMark?: number; /** * Output format to use, may be deduced from the filename */ outputFormat?: string; /** * An array of MediaEncoder streams to be multiplexed */ streams: EncodedMediaReadable[]; /** * Output format options */ outputFormatOptions?: Record; /** * Open options */ openOptions?: Record; } /** * A Muxer is an object that creates a number of Writables * that can accept data from encoders. * The encoders must be created before creating the Muxer as * their parameters must be known beforehand. * Can write either to a file using the built-in ffmpeg I/O * (which is generally faster as it allows seeking) or in can * expose a Readable that can be piped into a WriteStream when * `outputFile` is undefined. * * Emits 'finish' on close. * * @example * const muxer = new Muxer({ outputFile: tempFile, streams: [videoOutput, audioOutput] }); * * @example * const muxer = new Muxer({ highWaterMark: 16 * 1024, outputFormat: 'mp4', streams: [videoOutput, audioOutput] }); * output.output.pipe(writable); */ declare class Muxer extends EventEmitter { protected outputFile: string; protected highWaterMark: number; protected outputFormatName: string; protected outputFormatOptions: Record; protected outputFormat: ffmpeg.OutputFormat; protected openOptions: Record; protected formatContext: ffmpeg.FormatContext; protected rawStreams: EncodedMediaReadable[]; protected writing: boolean; protected primed: boolean; protected ended: number; protected writingQueue: { idx: number; packet: ffmpeg.Packet; callback: (error?: Error | null | undefined) => void; }[]; protected ready: Promise[]; protected delayedDestroy: Error | null; streams: EncodedMediaWritable[]; video: EncodedMediaWritable[]; audio: EncodedMediaWritable[]; output?: Readable; destroyed: boolean; constructor(options: MuxerOptions); protected destroy(e: Error): Promise; protected prime(): Promise; protected write(idx: number, packet: ffmpeg.Packet, callback: (error?: Error | null | undefined) => void): void; } interface DemuxerOptions extends ReadableOptions { /** * The name of the input file, null for reading from a ReadStream */ inputFile?: string; /** * Amount of data to buffer, only when reading from a ReadStream, @default 64Kb */ highWaterMark?: number; /** * Open options */ openOptions?: Record; } /** * A Demuxer is an object that exposes a number of Readables. * It emits 'ready' when its outputs have been created. * It can use either ffmpeg's built-in I/O (which is generally faster) * when `inputFile` is specified or it can expose a Writable into * which a ReadStream can be piped. Not all files can streamed - they * must have a special streaming structure with all the necessary * data written in the header. * * @example * // Reading directly from the filesystem * const input = new Demuxer({ inputFile: 'input.mp4') }); * input.on('ready', () => { * const audioInput = new AudioDecoder(input.audio[0]); * const videoInput = new VideoDecoder(input.video[0]); * }); * * @example * // Reading from a ReadStream * const demuxer = new Demuxer(); * const instream = fs.createReadStream('input.mp4'); * input.on('ready', () => { * const audioInput = new AudioDecoder(input.audio[0]); * const videoInput = new VideoDecoder(input.video[0]); * }); * instream.pipe(demuxer.input); */ declare class Demuxer extends EventEmitter { protected inputFile: string | undefined; protected highWaterMark: number; protected formatContext: ffmpeg.FormatContext | undefined; protected rawStreams: ffmpeg.Stream[]; protected openOptions: Record; streams: EncodedMediaReadable[]; video: EncodedMediaReadable[]; audio: EncodedMediaReadable[]; input?: Writable; reading: boolean; constructor(options?: DemuxerOptions); protected prime(): Promise; /** * All demuxed streams share the same read function. * When it is called for one of those streams, it will read and * push data to all of them - until the one that requested data * has enough */ protected read(idx: number, size: number): Promise; } /** * A VideoEncoder is Transform stream that can read raw video frames * and write encoded video data to a Muxer. * Its parameters must be explicitly configured. */ declare class VideoEncoder extends MediaTransform implements MediaEncoder, EncodedVideoReadable, VideoWritable { protected def: VideoStreamDefinition; protected encoder: ffmpeg.VideoEncoderContext; protected codec_: ffmpeg.Codec; protected busy: boolean; stream_: ffmpeg.Stream; type: "Video"; ready: boolean; constructor(def: VideoStreamDefinition); _construct(callback: (error?: Error | null | undefined) => void): void; _transform(frame: ffmpeg.Packet, encoding: BufferEncoding, callback: TransformCallback): void; _flush(callback: TransformCallback): void; get stream(): ffmpeg.Stream; codec(): ffmpeg.Codec; codecParameters(): ffmpeg.CodecParametersView; definition(): VideoStreamDefinition; context(): ffmpeg.VideoEncoderContext; isAudio(): boolean; isVideo(): boolean; } /** * A VideoDecoder is Transform stream that can read raw encoded video data * from a Demuxer and write decoded video frames. * Its parameters are inherited from the Demuxer. */ declare class VideoDecoder extends MediaTransform implements MediaDecoder, EncodedMediaWritable, VideoReadable { protected decoder: ffmpeg.VideoDecoderContext; protected busy: boolean; protected stream: ffmpeg.Stream; ready: boolean; constructor(options: { stream: ffmpeg.Stream; }); _construct(callback: (error?: Error | null | undefined) => void): void; _transform(packet: ffmpeg.Packet, encoding: BufferEncoding, callback: TransformCallback): void; codec(): ffmpeg.Codec; definition(): VideoStreamDefinition; } interface VideoTransformOptions extends MediaTransformOptions { input: VideoStreamDefinition; output: VideoStreamDefinition; interpolation: number; } /** * A stream Transform that uses VideoRescaler to rescale/resample the raw video. * Must receive input from a VideoDecoder and must output to a VideoEncoder */ declare class VideoTransform extends MediaTransform implements VideoWritable, VideoReadable { protected rescaler: ffmpeg.VideoRescaler; constructor(options: VideoTransformOptions); _transform(chunk: ffmpeg.VideoFrame, encoding: BufferEncoding, callback: TransformCallback$1): void; } /** * An AudioDecoder is Transform stream that can read raw encoded audio data * from a Demuxer and write decoded audio samples * Its parameters are inherited from the Demuxer. */ declare class AudioDecoder extends MediaTransform implements MediaDecoder, EncodedMediaWritable, AudioReadable { protected decoder: ffmpeg.AudioDecoderContext; protected busy: boolean; ready: boolean; constructor(options: { stream?: ffmpeg.Stream; }); _construct(callback: (error?: Error | null | undefined) => void): void; _transform(packet: ffmpeg.Packet, encoding: BufferEncoding, callback: TransformCallback): void; context(): ffmpeg.AudioDecoderContext; codec(): ffmpeg.Codec; definition(): AudioStreamDefinition; } /** * An AudioEncoder is Transform stream that can read raw audio samples * and write encoded audio data to a Muxer. * Its parameters must be explicitly configured. */ declare class AudioEncoder extends MediaTransform implements MediaEncoder, EncodedAudioReadable, AudioWritable { protected def: AudioStreamDefinition; protected encoder: ffmpeg.AudioEncoderContext; protected codec_: ffmpeg.Codec; stream_: ffmpeg.Stream; protected busy: boolean; type: "Audio"; ready: boolean; constructor(def: AudioStreamDefinition); _construct(callback: (error?: Error | null | undefined) => void): void; _transform(samples: ffmpeg.AudioSamples, encoding: BufferEncoding, callback: TransformCallback): void; _flush(callback: TransformCallback): void; get stream(): ffmpeg.Stream; codec(): ffmpeg.Codec; codecParameters(): ffmpeg.CodecParametersView; definition(): AudioStreamDefinition; context(): ffmpeg.AudioEncoderContext; isAudio(): boolean; isVideo(): boolean; } interface AudioTransformOptions extends MediaTransformOptions { input: AudioStreamDefinition; output: AudioStreamDefinition; } /** * A stream Transform that uses AudioResampler to rescale/resample the raw Audio. * Must receive input from a AudioDecoder and must output to a AudioEncoder */ declare class AudioTransform extends MediaTransform implements AudioReadable, AudioWritable { protected resampler: ffmpeg.AudioResampler; protected frameSize: number | undefined; constructor(options: AudioTransformOptions); _transform(chunk: ffmpeg.AudioSamples, encoding: BufferEncoding, callback: TransformCallback$1): void; _flush(callback: TransformCallback$1): void; } interface FilterOptions { inputs: Record; outputs: Record; graph: string; timeBase: ffmpeg.Rational; } /** * A Transform stream that uses avfilter to transform a number of MediaStream. * Must receive raw decoded input and sends raw decoded output. */ declare class Filter extends EventEmitter { protected filterGraph: ffmpeg.FilterGraph; protected filterGraphOp: Promise | false; protected bufferSrc: Record; protected bufferSink: Record; protected timeBase: ffmpeg.Rational; protected stillStreamingSources: number; protected destroyed: boolean; src: Record; sink: Record; constructor(options: FilterOptions); protected destroy(error: Error): void; protected write(id: string, frame: ffmpeg.VideoFrame | ffmpeg.AudioSamples, callback: (error?: Error | null | undefined) => void): Promise; protected read(id: string, size: number): Promise; } /** * A stream used for discarding data. */ declare class Discarder extends Writable { constructor(options?: WritableOptions & { objectMode?: never; }); _write(chunk: ffmpeg.Packet | ffmpeg.AudioSamples, encoding: BufferEncoding, callback: (error?: Error | null | undefined) => void): void; } export { AudioDecoder, AudioEncoder, AudioTransform, Demuxer, Discarder, Filter, MediaTransform, Muxer, VideoDecoder, VideoEncoder, VideoTransform }; export type { AudioStreamDefinition, MediaStream, MediaStreamDefinition, VideoStreamDefinition };