Low-level FFmpeg bindings for Bare.
npm i bare-ffmpeg
The IOContext
API provides functionality to create input/output contexts for media files with support for streaming and custom I/O operations.
const io = new ffmpeg.IOContext(buffer[, options])
Parameters:
buffer
(Buffer
|number
): The media data buffer or buffer size for streamingoptions
(object
, optional): Configuration optionsonread
(function
): A function for refilling the buffer.onwrite
(function
): A function for writing the buffer contents.onseek
(function
): A function for seeking to specified byte position.
Returns: A new IOContext
instance
Callback function called when FFmpeg needs to read data. For streaming scenarios where data is not available in a single buffer.
Parameters:
buffer
(Buffer
): Buffer to fill with datarequestedLen
(number
): Number of bytes requested
Returns: number
- Number of bytes actually read, or 0 for EOF
Callback function called when FFmpeg needs to write data. For streaming output scenarios.
Parameters:
buffer
(Buffer
): Buffer containing data to write
Returns: number
- Number of bytes written
Callback function called when FFmpeg needs to seek within the data source.
Parameters:
offset
(number
): Offset to seek towhence
(number
): Seek mode (seeffmpeg.constants.seek
)
Returns: number
- New position or file size for AVSEEK_SIZE
Basic usage with buffer:
const image = require('./fixtures/image/sample.jpeg', {
with: { type: 'binary' }
})
const io = new ffmpeg.IOContext(image)
io.destroy()
Streaming with custom read callback:
const io = new ffmpeg.IOContext(4096, {
onread: (buffer) => {
const bytesToRead = Math.min(buffer.length, data.length - offset)
if (bytesToRead === 0) return 0
const chunk = data.subarray(offset, offset + bytesToRead)
buffer.set(chunk)
offset += bytesToRead
return bytesToRead
}
})
Streaming with seek support:
const io = new ffmpeg.IOContext(4096, {
onread: (buffer) => {
// ... read implementation
},
onseek: (offset, whence) => {
switch (whence) {
case ffmpeg.constants.seek.SIZE:
return data.length
case ffmpeg.constants.seek.SET:
offset = offset
return offset
default:
return -1
}
}
})
Destroys the IOContext
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
The Dictionary
API provides functionality to store and retrieve key-value pairs, commonly used for passing options to various FFmpeg components.
const dict = new ffmpeg.Dictionary()
Returns: A new Dictionary
instance
Example:
const dict = new ffmpeg.Dictionary()
dict.set('video_codec', 'h264')
dict.set('audio_codec', 'aac')
dict.set('bitrate', '1000k')
Sets a key-value pair in the dictionary. Non-string values are automatically converted to strings.
Parameters:
key
(string
): The dictionary keyvalue
(string
|number
): The value to store
Returns: void
Retrieves a value from the dictionary by key.
Parameters:
key
(string
): The dictionary key
Returns: string
value or null
if the key doesn't exist
Retrieves all keys and values.
Returns: Array<[string, string]>
value
Destroys the Dictionary
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
The Dictionary
class implements the iterator protocol, allowing you to iterate over all key-value pairs:
const dict = new ffmpeg.Dictionary()
dict.set('foo', 'bar')
dict.set('baz', 'qux')
for (const [key, value] of dict) {
console.log(`${key}: ${value}`)
}
A helper to create a Dictionary
instance from an object.
const dict = ffmpeg.Dictionary.from({
foo: 'bar',
baz: 'qux'
})
Returns: A new Dictionary
instance
The FormatContext
API provides the base functionality for reading and writing media files.
This is the base class that
InputFormatContext
andOutputFormatContext
extend.
Gets the IO context associated with this format context.
Returns: IOContext
instance or null
Gets the array of media streams.
Returns: Array of Stream
instances
Reads the next frame from the media file into a packet.
Parameters:
packet
(Packet
): The packet to store the frame data
Returns: boolean
indicating if a frame was read
Gets the best stream of the specified media type.
Parameters:
type
(number
): The media type fromffmpeg.constants.mediaTypes
Returns: Stream
instance or null
if not found
Destroys the FormatContext
and frees all associated resources including streams. Automatically called when the object is managed by a using
declaration.
Returns: void
The InputFormatContext
API extends FormatContext
to provide functionality for reading media files.
const format = new ffmpeg.InputFormatContext(io, options[, url])
Parameters:
io
(IOContext
|InputFormat
): The IO context or input format. The ownership ofio
is transferred.options
(Dictionary
): Format options. Required when usingInputFormat
, ignored when usingIOContext
. The ownership ofoptions
is transferred.url
(string
, optional): Media source URL. Defaults to a platform-specific value
Returns: A new InputFormatContext
instance
Destroys the InputFormatContext
and closes the input format. Automatically called when the object is managed by a using
declaration.
Returns: void
The OutputFormatContext
API extends FormatContext
to provide functionality for writing media files.
const format = new ffmpeg.OutputFormatContext(formatName, io)
Parameters:
formatName
(string
): The output format name (e.g.,'mp4'
,'avi'
)io
(IOContext
): The IO context for writing. The ownership ofio
is transferred.
Returns: A new OutputFormatContext
instance
Creates a new stream in the output format.
Parameters:
codec
(Codec
): The codec to use for the stream
Returns: A new Stream
instance
Destroys the OutputFormatContext
and closes the output format. Automatically called when the object is managed by a using
declaration.
Returns: void
The Codec
API provides access to FFmpeg codecs for encoding and decoding.
H.264 video codec.
Returns: Codec
instance
Motion JPEG video codec.
Returns: Codec
instance
AAC audio codec.
Returns: Codec
instance
AV1 video codec.
Returns: Codec
instance
Gets the codec ID.
Returns: number
Gets the encoder for this codec.
Returns: Encoder
instance
Gets the decoder for this codec.
Returns: Decoder
instance
Gets a codec by ID.
Parameters:
id
(number
): The codec ID
Returns: Codec
instance
The CodecContext
API provides functionality to encode or decode media frames.
const codecCtx = new ffmpeg.CodecContext(codec)
Parameters:
codec
(Codec): The codec to use (e.g.,ffmpeg.Codec.H264.encoder
)
Returns: A new CodecContext
instance
Gets or sets the time base for the codec context.
Returns: Rational
instance
Gets or sets the pixel format for video codecs.
Returns: number
(pixel format constant)
Gets or sets the frame width for video codecs.
Returns: number
Gets or sets the frame height for video codecs.
Returns: number
Opens the codec context for encoding/decoding.
Parameters:
options
(Dictionary
, optional): Codec-specific options
Returns: CodecContext
instance (for chaining)
Sends a frame to the encoder.
Parameters:
frame
(Frame
): The frame to encode
Returns: boolean
indicating if the frame was sent
Receives a decoded frame from the decoder.
Parameters:
frame
(Frame
): The frame to store the decoded data
Returns: boolean
indicating if a frame was received
Sends a packet to the decoder.
Parameters:
packet
(Packet
): The packet to decode
Returns: boolean
indicating if the packet was sent
Receives an encoded packet from the encoder.
Parameters:
packet
(Packet
): The packet to store the encoded data
Returns: boolean
indicating if a packet was received
Destroys the CodecContext
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
The CodecParameters
API provides functionality to access codec parameters from streams.
const params = stream.codecParameters // Get from stream
General type of the encoded data.
Returns: number
Specific type of the encoded data (the codec used).
Returns: number
Additional information about the codec (corresponds to the AVI FOURCC).
Returns: number
Gets the bit rate.
Returns: number
Gets the bits per coded sample.
Returns: number
Gets the bits per raw sample.
Returns: number
Gets the sample rate for audio codecs.
Returns: number
Gets the frame rate for video codecs.
Returns: Rational
Out-of-band global headers that may be used by some codecs.
Returns: Buffer
Codec-specific bitstream restrictions that the stream conforms to.
Returns: number
Codec-specific bitstream restrictions that the stream conforms to.
Returns: number
Video: the pixel format, the value corresponds to AVPixelFormat. Audio: the sample format, the value corresponds to AVSampleFormat.
Returns: number
Number of channels in the layout.
Returns: number
Gets or sets the channel layout, see ffmpeg.constants.channelLayouts
Returns: ChannelLayout
Audio only. The number of bytes per coded audio frame, required by some formats.
Corresponds to nBlockAlign
in WAVEFORMATEX
.
Returns: number
Audio only. The amount of padding (in samples) inserted by the encoder at the beginning of the audio. I.e. this number of leading decoded samples must be discarded by the caller to get the original audio without leading padding.
Returns: number
Audio only. The amount of padding (in samples) appended by the encoder to the end of the audio. I.e. this number of decoded samples must be discarded by the caller from the end of the stream to get the original audio without any trailing padding.
Audio only. Number of samples to skip after a discontinuity.
Returns: number
Video only. The aspect ratio (width / height) which a single pixel should have when displayed.
When the aspect ratio is unknown / undefined, the numerator should be set to 0 (the denominator may have any value).
Returns: number
Video only. Number of delayed frames.
Returns: number
Copies parameters from a codec context.
Parameters:
context
(CodecContext
): The codec context
Returns: void
Copies parameters to a codec context.
Parameters:
context
(CodecContext
): The codec context
Returns: void
The InputFormat
API provides functionality to specify input format for media sources.
const format = new ffmpeg.InputFormat([name])
Parameters:
name
(string
, optional): The input format name. Defaults to a platform-specific value:darwin
,ios
: ``'avfoundation'`linux
:'v4l2'
win32
:'dshow'
Returns: A new InputFormat
instance
Destroys the InputFormat
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
The OutputFormat
API provides functionality to specify output format for media files.
const format = new ffmpeg.OutputFormat(name)
Parameters:
name
(string
): The output format name (e.g.,'mp4'
,'avi'
,'mov'
)
Returns: A new OutputFormat
instance
Destroys the OutputFormat
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
This structure describes decoded (raw) audio or video data.
const frame = new ffmpeg.Frame()
Returns: A new Frame
instance
Gets or sets the frame width.
Returns: number
Gets or sets the frame height.
Returns: number
Gets or sets the format of the frame, -1
if unknown or unset.
Returns: number
(sample format constant)
Gets or sets the channel layout for audio frames.
Returns: number
(channel layout constant)
Gets or sets the number of audio samples.
Returns: number
Allocates memory for the frame data.
Returns: void
Destroys the Frame
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
This structure stores compressed data. It is typically exported by demuxers and then passed as input to decoders, or received as output from encoders and then passed to muxers.
const packet = new ffmpeg.Packet([buffer])
Parameters:
buffer
(Buffer
, optional): Initial packet data
Returns: A new Packet
instance
Gets the packet data buffer.
Returns: Buffer
Gets the stream index this packet belongs to.
Returns: number
Decrements the reference count and unreferences the packet.
Returns: void
Destroys the Packet
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
The Image
API provides functionality to create and manage image buffers.
const image = new ffmpeg.Image(pixelFormat, width, height[, align])
Parameters:
pixelFormat
(number
|string
): The pixel formatwidth
(number
): The image width in pixelsheight
(number
): The image height in pixelsalign
(number
, optional): Memory alignment. Defaults to 1
Returns: A new Image
instance
Gets the pixel format.
Returns: number
Gets the image width.
Returns: number
Gets the image height.
Returns: number
Gets the memory alignment.
Returns: number
Gets the image data buffer.
Returns: Buffer
Fills a frame with the image data.
Parameters:
frame
(Frame
): The frame to fill
Returns: void
Reads image data from a frame into the image buffer.
Parameters:
frame
(Frame
): The frame to read from
Returns: void
Gets the line size for a specific plane.
Parameters:
plane
(number
, optional): Plane index. Defaults to 0
Returns: number
Static method to get line size for a pixel format.
Parameters:
pixelFormat
(number
|string
): The pixel formatwidth
(number
): The image widthplane
(number
, optional): Plane index. Defaults to 0
Returns: number
The Rational
API provides functionality to represent rational numbers (fractions).
const rational = new ffmpeg.Rational(numerator, denominator)
Parameters:
numerator
(number
): The numeratordenominator
(number
): The denominator
Returns: A new Rational
instance
Gets the numerator.
Returns: number
Gets the denominator.
Returns: number
Returns if true if rational describes a non-zero & non-negative quantity.
Returns: number
Returns if true when is not set.
Returns: number
see av_q2d()
Returns: number
see av_d2q()
Returns: Rational
see av_rescale_q()
Returns: number
The Stream
API provides functionality to access media stream information and create decoders/encoders.
const stream = new ffmpeg.Stream(handle)
Parameters:
handle
(ArrayBuffer
): Internal stream handle
Returns: A new Stream
instance
Streams are typically obtained from format contexts:
const stream = format.streams[0]
Gets or sets the stream ID.
Returns: number
Gets the stream index.
Returns: number
Gets the codec for this stream.
Returns: Codec
instance
Gets the codec parameters for this stream.
Returns: CodecParameters
instance
Gets or sets the time base for the stream.
Returns: Rational
instance
Gets or sets the average framerate for video streams.
Returns: Rational
instance
Example:
const fps = stream.avgFramerate.toNumber()
Creates and opens a decoder for this stream.
Returns: CodecContext
instance
Creates and opens an encoder for this stream.
Returns: CodecContext
instance
The Resampler
API provides functionality to convert audio between different sample rates, channel layouts, and sample formats.
const resampler = new ffmpeg.Resampler(
inputSampleRate,
inputChannelLayout,
inputSampleFormat,
outputSampleRate,
outputChannelLayout,
outputSampleFormat
)
Parameters:
inputSampleRate
(number
): Input sample rate in HzinputChannelLayout
(number
): Input channel layout constantinputSampleFormat
(number
): Input sample format constantoutputSampleRate
(number
): Output sample rate in HzoutputChannelLayout
(number
): Output channel layout constantoutputSampleFormat
(number
): Output sample format constant
Returns: A new Resampler
instance
Gets the input sample rate.
Returns: number
Gets the output sample rate.
Returns: number
Gets the resampler delay in samples.
Returns: number
Converts audio data from input frame to output frame.
Parameters:
inputFrame
(Frame
): The input audio frameoutputFrame
(Frame
): The output audio frame
Returns: number
of samples converted
Flushes any remaining samples in the resampler.
Parameters:
outputFrame
(Frame
): The output audio frame
Returns: number
of samples flushed
Destroys the Resampler
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
The Scaler
API provides functionality to scale and convert video frames between different pixel formats and resolutions.
const scaler = new ffmpeg.Scaler(
sourcePixelFormat,
sourceWidth,
sourceHeight,
targetPixelFormat,
targetWidth,
targetHeight
)
Parameters:
sourcePixelFormat
(number
|string
): Source pixel formatsourceWidth
(number
): Source width in pixelssourceHeight
(number
): Source height in pixelstargetPixelFormat
(number
|string
): Target pixel formattargetWidth
(number
): Target width in pixelstargetHeight
(number
): Target height in pixels
Returns: A new Scaler
instance
Scales a source frame to a target frame.
Parameters:
source
(Frame
): The source frametarget
(Frame
): The target frame
Returns: boolean
indicating success
Scales a portion of a source frame to a target frame.
Parameters:
source
(Frame
): The source framey
(number
): Starting Y coordinateheight
(number
): Height to scaletarget
(Frame
): The target frame
Returns: boolean
indicating success
Destroys the Scaler
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
The AudioFIFO
API provides a first in first out buffer for audio samples. This is useful for buffering audio data between different processing stages.
const fifo = new ffmpeg.AudioFIFO(sampleFormat, channels, nbSamples)
Parameters:
sampleFormat
(number
|string
): The audio sample formatchannels
(number
): Number of audio channelsnbSamples
(number
): Initial buffer size in samples
Returns: A new AudioFIFO
instance
Example:
const fifo = new ffmpeg.AudioFIFO(ffmpeg.constants.sampleFormats.S16, 2, 1024)
Gets the number of samples currently in the FIFO.
Returns: number
Gets the number of samples that can be written to the FIFO.
Returns: number
Writes samples from a frame to the FIFO. The FIFO will automatically grow if needed.
Parameters:
frame
(Frame
): The audio frame containing samples to write
Returns: number
of samples written
Reads samples from the FIFO into a frame.
Parameters:
frame
(Frame
): The frame to read samples intonbSamples
(number
): Number of samples to read
Returns: number
of samples actually read
Reads samples from the FIFO without removing them.
Parameters:
frame
(Frame
): The frame to read samples intonbSamples
(number
): Number of samples to peek
Returns: number
of samples peeked
Removes samples from the FIFO without reading them.
Parameters:
nbSamples
(number
): Number of samples to drain
Returns: void
Resets the FIFO to empty state.
Returns: void
Destroys the AudioFIFO
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
Apache-2.0