Low-level FFmpeg bindings for Bare.
npm i bare-ffmpeg
The IOContext
API provides functionality to create input/output contexts for media files.
const io = new ffmpeg.IOContext(buffer)
Parameters:
buffer
(Buffer
): The media data buffer
Returns: A new IOContext
instance
Example:
const image = require('./fixtures/image/sample.jpeg', {
with: { type: 'binary' }
})
const io = new ffmpeg.IOContext(image)
io.destroy()
Destroys the IOContext
and frees all associated resources. Automatically called when the object is managed by a using
declaration.
Returns: void
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
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
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 pixel format.
Returns: number
(pixel format constant)
Gets or sets the sample format for audio frames.
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
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 the codec parameters for this stream.
Returns: CodecParameters
instance
Gets the codec for this stream
Returns: Codec
instance
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