Class: CodecContext
Defined in: src/lib/codec-context.ts:73
Codec context for encoding and decoding.
Main structure for codec operations, containing all codec parameters and state. Handles encoding raw frames to packets and decoding packets to frames. Supports both software and hardware-accelerated codecs. Must be configured and opened before use.
Direct mapping to FFmpeg's AVCodecContext.
Example
import { CodecContext, Codec, FFmpegError } from 'node-av';
import { AV_CODEC_ID_H264, AV_PIX_FMT_YUV420P } from 'node-av/constants';
// Create decoder
const decoder = new CodecContext();
const codec = Codec.findDecoder(AV_CODEC_ID_H264);
decoder.allocContext3(codec);
// Configure from stream parameters
decoder.parametersToContext(stream.codecpar);
// Open decoder
let ret = await decoder.open2(codec);
FFmpegError.throwIfError(ret, 'open2');
// Decode packets
ret = await decoder.sendPacket(packet);
if (ret >= 0) {
ret = await decoder.receiveFrame(frame);
if (ret >= 0) {
// Process decoded frame
}
}
// Cleanup
decoder.freeContext();
See
- AVCodecContext - FFmpeg Doxygen
- Codec For finding codecs
- CodecParameters For stream parameters
Extends
Implements
Disposable
NativeWrapper
<NativeCodecContext
>
Constructors
Constructor
new CodecContext():
CodecContext
Defined in: src/lib/codec-context.ts:77
Returns
CodecContext
Overrides
OptionMember<NativeCodecContext>.constructor
Properties
native
protected
native:NativeCodecContext
Defined in: src/lib/option.ts:999
Inherited from
Accessors
bitRate
Get Signature
get bitRate():
bigint
Defined in: src/lib/codec-context.ts:115
Average bitrate.
Target bitrate for encoding, detected bitrate for decoding. In bits per second.
Direct mapping to AVCodecContext->bit_rate.
Returns
bigint
Set Signature
set bitRate(
value
):void
Defined in: src/lib/codec-context.ts:119
Parameters
value
bigint
Returns
void
channelLayout
Get Signature
get channelLayout():
ChannelLayout
Defined in: src/lib/codec-context.ts:535
Audio channel layout.
Describes channel configuration.
Direct mapping to AVCodecContext->ch_layout.
Returns
Set Signature
set channelLayout(
value
):void
Defined in: src/lib/codec-context.ts:539
Parameters
value
Returns
void
channels
Get Signature
get channels():
number
Defined in: src/lib/codec-context.ts:481
Number of audio channels.
Direct mapping to AVCodecContext->channels.
Returns
number
Set Signature
set channels(
value
):void
Defined in: src/lib/codec-context.ts:485
Parameters
value
number
Returns
void
chromaLocation
Get Signature
get chromaLocation():
AVChromaLocation
Defined in: src/lib/codec-context.ts:453
Chroma sample location.
Position of chroma samples.
Direct mapping to AVCodecContext->chroma_sample_location.
Returns
Set Signature
set chromaLocation(
value
):void
Defined in: src/lib/codec-context.ts:457
Parameters
value
Returns
void
codecId
Get Signature
get codecId():
AVCodecID
Defined in: src/lib/codec-context.ts:99
Codec identifier.
Direct mapping to AVCodecContext->codec_id.
Returns
Set Signature
set codecId(
value
):void
Defined in: src/lib/codec-context.ts:103
Parameters
value
Returns
void
codecType
Get Signature
get codecType():
AVMediaType
Defined in: src/lib/codec-context.ts:86
Type of codec (video/audio/subtitle).
Direct mapping to AVCodecContext->codec_type.
Returns
Set Signature
set codecType(
value
):void
Defined in: src/lib/codec-context.ts:90
Parameters
value
Returns
void
colorPrimaries
Get Signature
get colorPrimaries():
AVColorPrimaries
Defined in: src/lib/codec-context.ts:408
Color primaries.
Chromaticity coordinates of source primaries.
Direct mapping to AVCodecContext->color_primaries.
Returns
Set Signature
set colorPrimaries(
value
):void
Defined in: src/lib/codec-context.ts:412
Parameters
value
Returns
void
colorRange
Get Signature
get colorRange():
AVColorRange
Defined in: src/lib/codec-context.ts:393
Color range.
MPEG (limited) or JPEG (full) range.
Direct mapping to AVCodecContext->color_range.
Returns
Set Signature
set colorRange(
value
):void
Defined in: src/lib/codec-context.ts:397
Parameters
value
Returns
void
colorSpace
Get Signature
get colorSpace():
AVColorSpace
Defined in: src/lib/codec-context.ts:438
YUV color space.
Color space for YUV content.
Direct mapping to AVCodecContext->colorspace.
Returns
Set Signature
set colorSpace(
value
):void
Defined in: src/lib/codec-context.ts:442
Parameters
value
Returns
void
colorTrc
Get Signature
get colorTrc():
AVColorTransferCharacteristic
Defined in: src/lib/codec-context.ts:423
Color transfer characteristic.
Transfer function (gamma).
Direct mapping to AVCodecContext->color_trc.
Returns
Set Signature
set colorTrc(
value
):void
Defined in: src/lib/codec-context.ts:427
Parameters
value
Returns
void
delay
Get Signature
get delay():
number
Defined in: src/lib/codec-context.ts:162
Codec delay.
Number of frames the decoder needs to output before first frame.
Direct mapping to AVCodecContext->delay.
Returns
number
extraData
Get Signature
get extraData():
null
|Buffer
<ArrayBufferLike
>
Defined in: src/lib/codec-context.ts:203
Extra binary data for codec.
Contains codec-specific initialization data.
Direct mapping to AVCodecContext->extradata.
Returns
null
| Buffer
<ArrayBufferLike
>
Set Signature
set extraData(
value
):void
Defined in: src/lib/codec-context.ts:207
Parameters
value
null
| Buffer
<ArrayBufferLike
>
Returns
void
flags
Get Signature
get flags():
AVCodecFlag
Defined in: src/lib/codec-context.ts:173
Codec flags.
Combination of AV_CODEC_FLAG_* values.
Direct mapping to AVCodecContext->flags.
Returns
Set Signature
set flags(
value
):void
Defined in: src/lib/codec-context.ts:177
Parameters
value
Returns
void
flags2
Get Signature
get flags2():
AVCodecFlag2
Defined in: src/lib/codec-context.ts:188
Additional codec flags.
Combination of AV_CODEC_FLAG2_* values.
Direct mapping to AVCodecContext->flags2.
Returns
Set Signature
set flags2(
value
):void
Defined in: src/lib/codec-context.ts:192
Parameters
value
Returns
void
frameNumber
Get Signature
get frameNumber():
number
Defined in: src/lib/codec-context.ts:524
Current frame number.
Frame counter for debugging.
Direct mapping to AVCodecContext->frame_number.
Returns
number
framerate
Get Signature
get framerate():
Rational
Defined in: src/lib/codec-context.ts:377
Frame rate.
Frames per second for encoding.
Direct mapping to AVCodecContext->framerate.
Returns
Set Signature
set framerate(
value
):void
Defined in: src/lib/codec-context.ts:382
Parameters
value
Returns
void
frameSize
Get Signature
get frameSize():
number
Defined in: src/lib/codec-context.ts:509
Number of samples per audio frame.
Direct mapping to AVCodecContext->frame_size.
Returns
number
Set Signature
set frameSize(
value
):void
Defined in: src/lib/codec-context.ts:513
Parameters
value
number
Returns
void
gopSize
Get Signature
get gopSize():
number
Defined in: src/lib/codec-context.ts:290
Group of pictures size.
Maximum number of frames between keyframes.
Direct mapping to AVCodecContext->gop_size.
Returns
number
Set Signature
set gopSize(
value
):void
Defined in: src/lib/codec-context.ts:294
Parameters
value
number
Returns
void
hasBFrames
Get Signature
get hasBFrames():
number
Defined in: src/lib/codec-context.ts:350
Number of frames delay in decoder.
For codecs with B-frames.
Direct mapping to AVCodecContext->has_b_frames.
Returns
number
height
Get Signature
get height():
number
Defined in: src/lib/codec-context.ts:275
Picture height in pixels.
Direct mapping to AVCodecContext->height.
Returns
number
Set Signature
set height(
value
):void
Defined in: src/lib/codec-context.ts:279
Parameters
value
number
Returns
void
hwDeviceCtx
Get Signature
get hwDeviceCtx():
null
|HardwareDeviceContext
Defined in: src/lib/codec-context.ts:625
Hardware device context.
Reference to hardware device for acceleration.
Direct mapping to AVCodecContext->hw_device_ctx.
Returns
null
| HardwareDeviceContext
Set Signature
set hwDeviceCtx(
value
):void
Defined in: src/lib/codec-context.ts:645
Parameters
value
null
| HardwareDeviceContext
Returns
void
hwFramesCtx
Get Signature
get hwFramesCtx():
null
|HardwareFramesContext
Defined in: src/lib/codec-context.ts:658
Hardware frames context.
Reference to hardware frames for GPU memory.
Direct mapping to AVCodecContext->hw_frames_ctx.
Returns
null
| HardwareFramesContext
Set Signature
set hwFramesCtx(
value
):void
Defined in: src/lib/codec-context.ts:678
Parameters
value
null
| HardwareFramesContext
Returns
void
isOpen
Get Signature
get isOpen():
boolean
Defined in: src/lib/codec-context.ts:689
Check if codec is open.
True if the codec has been opened.
Returns
boolean
level
Get Signature
get level():
number
Defined in: src/lib/codec-context.ts:233
Codec level.
Level within the specified profile.
Direct mapping to AVCodecContext->level.
Returns
number
Set Signature
set level(
value
):void
Defined in: src/lib/codec-context.ts:237
Parameters
value
number
Returns
void
maxBFrames
Get Signature
get maxBFrames():
number
Defined in: src/lib/codec-context.ts:320
Maximum number of B-frames.
B-frames between non-B-frames.
Direct mapping to AVCodecContext->max_b_frames.
Returns
number
Set Signature
set maxBFrames(
value
):void
Defined in: src/lib/codec-context.ts:324
Parameters
value
number
Returns
void
mbDecision
Get Signature
get mbDecision():
number
Defined in: src/lib/codec-context.ts:335
Macroblock decision mode.
Algorithm for macroblock decision.
Direct mapping to AVCodecContext->mb_decision.
Returns
number
Set Signature
set mbDecision(
value
):void
Defined in: src/lib/codec-context.ts:339
Parameters
value
number
Returns
void
pixelFormat
Get Signature
get pixelFormat():
AVPixelFormat
Defined in: src/lib/codec-context.ts:305
Pixel format.
Format of the video frames.
Direct mapping to AVCodecContext->pix_fmt.
Returns
Set Signature
set pixelFormat(
value
):void
Defined in: src/lib/codec-context.ts:309
Parameters
value
Returns
void
pktTimebase
Get Signature
get pktTimebase():
Rational
Defined in: src/lib/codec-context.ts:146
Packet time base.
Time base of the packets from/to the demuxer/muxer.
Direct mapping to AVCodecContext->pkt_timebase.
Returns
Set Signature
set pktTimebase(
value
):void
Defined in: src/lib/codec-context.ts:151
Parameters
value
Returns
void
profile
Get Signature
get profile():
AVProfile
Defined in: src/lib/codec-context.ts:218
Codec profile.
FF_PROFILE_* value indicating codec profile.
Direct mapping to AVCodecContext->profile.
Returns
Set Signature
set profile(
value
):void
Defined in: src/lib/codec-context.ts:222
Parameters
value
Returns
void
qMax
Get Signature
get qMax():
number
Defined in: src/lib/codec-context.ts:565
Maximum quantizer.
Maximum quantization parameter.
Direct mapping to AVCodecContext->qmax.
Returns
number
Set Signature
set qMax(
value
):void
Defined in: src/lib/codec-context.ts:569
Parameters
value
number
Returns
void
qMin
Get Signature
get qMin():
number
Defined in: src/lib/codec-context.ts:550
Minimum quantizer.
Minimum quantization parameter.
Direct mapping to AVCodecContext->qmin.
Returns
number
Set Signature
set qMin(
value
):void
Defined in: src/lib/codec-context.ts:554
Parameters
value
number
Returns
void
rcBufferSize
Get Signature
get rcBufferSize():
number
Defined in: src/lib/codec-context.ts:580
Rate control buffer size.
Decoder bitstream buffer size.
Direct mapping to AVCodecContext->rc_buffer_size.
Returns
number
Set Signature
set rcBufferSize(
value
):void
Defined in: src/lib/codec-context.ts:584
Parameters
value
number
Returns
void
rcMaxRate
Get Signature
get rcMaxRate():
bigint
Defined in: src/lib/codec-context.ts:595
Maximum bitrate.
Maximum bitrate in bits per second.
Direct mapping to AVCodecContext->rc_max_rate.
Returns
bigint
Set Signature
set rcMaxRate(
value
):void
Defined in: src/lib/codec-context.ts:599
Parameters
value
bigint
Returns
void
rcMinRate
Get Signature
get rcMinRate():
bigint
Defined in: src/lib/codec-context.ts:610
Minimum bitrate.
Minimum bitrate in bits per second.
Direct mapping to AVCodecContext->rc_min_rate.
Returns
bigint
Set Signature
set rcMinRate(
value
):void
Defined in: src/lib/codec-context.ts:614
Parameters
value
bigint
Returns
void
sampleAspectRatio
Get Signature
get sampleAspectRatio():
Rational
Defined in: src/lib/codec-context.ts:361
Sample aspect ratio.
Pixel width/height ratio.
Direct mapping to AVCodecContext->sample_aspect_ratio.
Returns
Set Signature
set sampleAspectRatio(
value
):void
Defined in: src/lib/codec-context.ts:366
Parameters
value
Returns
void
sampleFormat
Get Signature
get sampleFormat():
AVSampleFormat
Defined in: src/lib/codec-context.ts:496
Audio sample format.
Format of audio samples.
Direct mapping to AVCodecContext->sample_fmt.
Returns
Set Signature
set sampleFormat(
value
):void
Defined in: src/lib/codec-context.ts:500
Parameters
value
Returns
void
sampleRate
Get Signature
get sampleRate():
number
Defined in: src/lib/codec-context.ts:468
Audio sample rate.
Samples per second.
Direct mapping to AVCodecContext->sample_rate.
Returns
number
Set Signature
set sampleRate(
value
):void
Defined in: src/lib/codec-context.ts:472
Parameters
value
number
Returns
void
threadCount
Get Signature
get threadCount():
number
Defined in: src/lib/codec-context.ts:249
Thread count for codec.
Number of threads to use for decoding/encoding. 0 for automatic selection.
Direct mapping to AVCodecContext->thread_count.
Returns
number
Set Signature
set threadCount(
value
):void
Defined in: src/lib/codec-context.ts:253
Parameters
value
number
Returns
void
timeBase
Get Signature
get timeBase():
Rational
Defined in: src/lib/codec-context.ts:130
Time base for timestamps.
Fundamental unit of time in seconds for this context.
Direct mapping to AVCodecContext->time_base.
Returns
Set Signature
set timeBase(
value
):void
Defined in: src/lib/codec-context.ts:135
Parameters
value
Returns
void
width
Get Signature
get width():
number
Defined in: src/lib/codec-context.ts:262
Picture width in pixels.
Direct mapping to AVCodecContext->width.
Returns
number
Set Signature
set width(
value
):void
Defined in: src/lib/codec-context.ts:266
Parameters
value
number
Returns
void
Methods
[dispose]()
[dispose]():
void
Defined in: src/lib/codec-context.ts:1205
Dispose of the codec context.
Implements the Disposable interface for automatic cleanup. Equivalent to calling freeContext().
Returns
void
Example
{
using ctx = new CodecContext();
ctx.allocContext3(codec);
await ctx.open2();
// Use context...
} // Automatically freed when leaving scope
Implementation of
Disposable.[dispose]
allocContext3()
allocContext3(
codec
):void
Defined in: src/lib/codec-context.ts:714
Allocate codec context.
Allocates and initializes the context for the given codec.
Direct mapping to avcodec_alloc_context3().
Parameters
codec
Codec to use (null for default)
null
| Codec
Returns
void
Example
import { Codec } from 'node-av';
import { AV_CODEC_ID_H264 } from 'node-av/constants';
const codec = Codec.findDecoder(AV_CODEC_ID_H264);
ctx.allocContext3(codec);
See
- open2 To open the codec
- freeContext To free the context
flushBuffers()
flushBuffers():
void
Defined in: src/lib/codec-context.ts:871
Flush codec buffers.
Resets the internal codec state. Used when seeking or switching streams.
Direct mapping to avcodec_flush_buffers().
Returns
void
Example
// Flush when seeking
ctx.flushBuffers();
// Codec is now ready for new data
freeContext()
freeContext():
void
Defined in: src/lib/codec-context.ts:734
Free the codec context.
Releases all resources. The context becomes invalid.
Direct mapping to avcodec_free_context().
Returns
void
Example
ctx.freeContext();
// Context is now invalid
See
- Symbol.dispose For automatic cleanup
- allocContext3 To allocate a new context
getNative()
getNative():
NativeCodecContext
Defined in: src/lib/codec-context.ts:1185
Internal
Get the underlying native CodecContext object.
Returns
The native CodecContext binding object
Implementation of
getOption()
Get an option value from this object.
Uses the AVOption API to retrieve options.
Direct mapping to av_opt_get* functions.
Param
Option name
Param
Option type (defaults to AV_OPT_TYPE_STRING)
Param
Search flags (default: AV_OPT_SEARCH_CHILDREN)
Example
import { AV_OPT_TYPE_STRING, AV_OPT_TYPE_RATIONAL, AV_OPT_TYPE_PIXEL_FMT, AV_OPT_TYPE_INT64 } from 'node-av/constants';
// String options (default)
const preset = obj.getOption('preset');
const codec = obj.getOption('codec', AV_OPT_TYPE_STRING);
// Typed options
const framerate = obj.getOption('framerate', AV_OPT_TYPE_RATIONAL); // Returns {num, den}
const pixFmt = obj.getOption('pix_fmt', AV_OPT_TYPE_PIXEL_FMT); // Returns AVPixelFormat
const bitrate = obj.getOption('bitrate', AV_OPT_TYPE_INT64); // Returns bigint
Call Signature
getOption(
name
,type?
,searchFlags?
):null
|string
Defined in: src/lib/option.ts:1217
Parameters
name
string
type?
searchFlags?
Returns
null
| string
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|string
Defined in: src/lib/option.ts:1218
Parameters
name
string
type
searchFlags?
Returns
null
| string
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|number
Defined in: src/lib/option.ts:1221
Parameters
name
string
type
searchFlags?
Returns
null
| number
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|bigint
Defined in: src/lib/option.ts:1222
Parameters
name
string
type
searchFlags?
Returns
null
| bigint
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|number
Defined in: src/lib/option.ts:1223
Parameters
name
string
type
searchFlags?
Returns
null
| number
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|bigint
Defined in: src/lib/option.ts:1224
Parameters
name
string
type
searchFlags?
Returns
null
| bigint
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|number
Defined in: src/lib/option.ts:1225
Parameters
name
string
type
searchFlags?
Returns
null
| number
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|boolean
Defined in: src/lib/option.ts:1226
Parameters
name
string
type
searchFlags?
Returns
null
| boolean
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|number
Defined in: src/lib/option.ts:1227
Parameters
name
string
type
searchFlags?
Returns
null
| number
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|number
Defined in: src/lib/option.ts:1228
Parameters
name
string
type
searchFlags?
Returns
null
| number
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|number
Defined in: src/lib/option.ts:1231
Parameters
name
string
type
searchFlags?
Returns
null
| number
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|number
Defined in: src/lib/option.ts:1232
Parameters
name
string
type
searchFlags?
Returns
null
| number
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|IRational
Defined in: src/lib/option.ts:1235
Parameters
name
string
type
searchFlags?
Returns
null
| IRational
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|IRational
Defined in: src/lib/option.ts:1236
Parameters
name
string
type
searchFlags?
Returns
null
| IRational
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|AVPixelFormat
Defined in: src/lib/option.ts:1237
Parameters
name
string
type
searchFlags?
Returns
null
| AVPixelFormat
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|AVSampleFormat
Defined in: src/lib/option.ts:1238
Parameters
name
string
type
searchFlags?
Returns
null
| AVSampleFormat
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
| {height
:number
;width
:number
; }
Defined in: src/lib/option.ts:1239
Parameters
name
string
type
searchFlags?
Returns
null
| { height
: number
; width
: number
; }
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|ChannelLayout
Defined in: src/lib/option.ts:1240
Parameters
name
string
type
searchFlags?
Returns
null
| ChannelLayout
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|Dictionary
Defined in: src/lib/option.ts:1241
Parameters
name
string
type
searchFlags?
Returns
null
| Dictionary
Inherited from
Call Signature
getOption(
name
,type
,searchFlags?
):null
|string
Defined in: src/lib/option.ts:1242
Parameters
name
string
type
searchFlags?
Returns
null
| string
Inherited from
listOptions()
listOptions():
OptionInfo
[]
Defined in: src/lib/option.ts:1358
List all available options for this object.
Uses the AVOption API to enumerate all options. Useful for discovering available settings and their types.
Direct mapping to av_opt_next() iteration.
Returns
Array of option information objects
Example
const options = obj.listOptions();
for (const opt of options) {
console.log(`${opt.name}: ${opt.help}`);
console.log(` Type: ${opt.type}, Default: ${opt.defaultValue}`);
console.log(` Range: ${opt.min} - ${opt.max}`);
}
See
OptionInfo For option metadata structure
Inherited from
open2()
open2(
codec
,options
):Promise
<number
>
Defined in: src/lib/codec-context.ts:766
Open the codec.
Initializes the codec for encoding/decoding. Must be called before processing frames/packets.
Direct mapping to avcodec_open2().
Parameters
codec
Codec to open with (null to use already set)
null
| Codec
options
Codec-specific options
null
| Dictionary
Returns
Promise
<number
>
0 on success, negative AVERROR on error:
- AVERROR_EINVAL: Invalid parameters
- AVERROR_ENOMEM: Memory allocation failure
Example
import { FFmpegError } from 'node-av';
const ret = await ctx.open2(codec);
FFmpegError.throwIfError(ret, 'open2');
// Codec is now open and ready
See
- allocContext3 Must be called first
- isOpen To check if open
open2Sync()
open2Sync(
codec
,options
):number
Defined in: src/lib/codec-context.ts:798
Open the codec synchronously. Synchronous version of open2.
Initializes the codec for encoding/decoding. Must be called before processing frames/packets.
Direct mapping to avcodec_open2().
Parameters
codec
Codec to open with (null to use already set)
null
| Codec
options
Codec-specific options
null
| Dictionary
Returns
number
0 on success, negative AVERROR on error:
- AVERROR_EINVAL: Invalid parameters
- AVERROR_ENOMEM: Memory allocation failure
Example
import { FFmpegError } from 'node-av';
const ret = ctx.open2Sync(codec);
FFmpegError.throwIfError(ret, 'open2Sync');
// Codec is now open and ready
See
open2 For async version
parametersFromContext()
parametersFromContext(
params
):number
Defined in: src/lib/codec-context.ts:852
Fill parameters from codec context.
Copies codec parameters from context to stream. Used when setting up encoders.
Direct mapping to avcodec_parameters_from_context().
Parameters
params
Destination codec parameters
Returns
number
0 on success, negative AVERROR on error:
- AVERROR_EINVAL: Invalid parameters
Example
import { FFmpegError } from 'node-av';
const ret = ctx.parametersFromContext(stream.codecpar);
FFmpegError.throwIfError(ret, 'parametersFromContext');
See
parametersToContext For the reverse
parametersToContext()
parametersToContext(
params
):number
Defined in: src/lib/codec-context.ts:825
Fill codec context from parameters.
Copies codec parameters from stream to context. Used when setting up decoders.
Direct mapping to avcodec_parameters_to_context().
Parameters
params
Source codec parameters
Returns
number
0 on success, negative AVERROR on error:
- AVERROR_EINVAL: Invalid parameters
Example
import { FFmpegError } from 'node-av';
const ret = ctx.parametersToContext(stream.codecpar);
FFmpegError.throwIfError(ret, 'parametersToContext');
See
parametersFromContext For the reverse
receiveFrame()
receiveFrame(
frame
):Promise
<number
>
Defined in: src/lib/codec-context.ts:976
Receive decoded frame.
Gets a decoded frame from the decoder. Call after sendPacket().
Direct mapping to avcodec_receive_frame().
Parameters
frame
Frame to receive into
Returns
Promise
<number
>
0 on success, negative AVERROR on error:
- AVERROR_EAGAIN: Need more input
- AVERROR_EOF: All frames have been output
- AVERROR_EINVAL: Invalid decoder state
Example
import { FFmpegError } from 'node-av';
import { AVERROR_EAGAIN, AVERROR_EOF } from 'node-av';
const ret = await ctx.receiveFrame(frame);
if (ret === AVERROR_EAGAIN || ret === AVERROR_EOF) {
// No frame available
} else {
FFmpegError.throwIfError(ret, 'receiveFrame');
// Process decoded frame
}
See
sendPacket To send packets for decoding
receiveFrameSync()
receiveFrameSync(
frame
):number
Defined in: src/lib/codec-context.ts:1012
Receive decoded frame synchronously. Synchronous version of receiveFrame.
Gets a decoded frame from the decoder. Call after sendPacketSync().
Direct mapping to avcodec_receive_frame().
Parameters
frame
Frame to receive into
Returns
number
0 on success, negative AVERROR on error:
- AVERROR_EAGAIN: Need more input
- AVERROR_EOF: All frames have been output
- AVERROR_EINVAL: Invalid decoder state
Example
import { FFmpegError } from 'node-av';
import { AVERROR_EAGAIN, AVERROR_EOF } from 'node-av';
const ret = ctx.receiveFrameSync(frame);
if (ret === AVERROR_EAGAIN || ret === AVERROR_EOF) {
// No frame available
} else {
FFmpegError.throwIfError(ret, 'receiveFrameSync');
// Process frame
}
See
receiveFrame For async version
receivePacket()
receivePacket(
packet
):Promise
<number
>
Defined in: src/lib/codec-context.ts:1117
Receive encoded packet.
Gets an encoded packet from the encoder. Call after sendFrame().
Direct mapping to avcodec_receive_packet().
Parameters
packet
Packet to receive into
Returns
Promise
<number
>
0 on success, negative AVERROR on error:
- AVERROR_EAGAIN: Need more input
- AVERROR_EOF: All packets have been output
- AVERROR_EINVAL: Invalid encoder state
Example
import { FFmpegError } from 'node-av';
import { AVERROR_EAGAIN, AVERROR_EOF } from 'node-av';
const ret = await ctx.receivePacket(packet);
if (ret === AVERROR_EAGAIN || ret === AVERROR_EOF) {
// No packet available
} else {
FFmpegError.throwIfError(ret, 'receivePacket');
// Process encoded packet
}
See
sendFrame To send frames for encoding
receivePacketSync()
receivePacketSync(
packet
):number
Defined in: src/lib/codec-context.ts:1153
Receive encoded packet synchronously. Synchronous version of receivePacket.
Gets an encoded packet from the encoder. Call after sendFrameSync().
Direct mapping to avcodec_receive_packet().
Parameters
packet
Packet to receive into
Returns
number
0 on success, negative AVERROR on error:
- AVERROR_EAGAIN: Need more input
- AVERROR_EOF: All packets have been output
- AVERROR_EINVAL: Invalid encoder state
Example
import { FFmpegError } from 'node-av';
import { AVERROR_EAGAIN, AVERROR_EOF } from 'node-av';
const ret = ctx.receivePacketSync(packet);
if (ret === AVERROR_EAGAIN || ret === AVERROR_EOF) {
// No packet available
} else {
FFmpegError.throwIfError(ret, 'receivePacketSync');
// Process packet
}
See
receivePacket For async version
sendFrame()
sendFrame(
frame
):Promise
<number
>
Defined in: src/lib/codec-context.ts:1047
Send frame to encoder.
Submits raw frame for encoding. Call receivePacket() to get encoded packets.
Direct mapping to avcodec_send_frame().
Parameters
frame
Frame to encode (null to flush)
null
| Frame
Returns
Promise
<number
>
0 on success, negative AVERROR on error:
- AVERROR_EAGAIN: Must receive packets first
- AVERROR_EOF: Encoder has been flushed
- AVERROR_EINVAL: Invalid encoder state
- AVERROR_ENOMEM: Memory allocation failure
Example
import { FFmpegError } from 'node-av';
import { AVERROR_EAGAIN } from 'node-av';
const ret = await ctx.sendFrame(frame);
if (ret === AVERROR_EAGAIN) {
// Need to receive packets first
} else {
FFmpegError.throwIfError(ret, 'sendFrame');
}
See
receivePacket To get encoded packets
sendFrameSync()
sendFrameSync(
frame
):number
Defined in: src/lib/codec-context.ts:1082
Send frame to encoder synchronously. Synchronous version of sendFrame.
Submits raw frame for encoding. Call receivePacketSync() to get encoded packets.
Direct mapping to avcodec_send_frame().
Parameters
frame
Frame to encode (null to flush)
null
| Frame
Returns
number
0 on success, negative AVERROR on error:
- AVERROR_EAGAIN: Must receive packets first
- AVERROR_EOF: Encoder has been flushed
- AVERROR_EINVAL: Invalid encoder state
- AVERROR_ENOMEM: Memory allocation failure
Example
import { FFmpegError } from 'node-av';
import { AVERROR_EAGAIN } from 'node-av';
const ret = ctx.sendFrameSync(frame);
if (ret === AVERROR_EAGAIN) {
// Need to receive packets first
ctx.receivePacketSync(packet);
}
See
sendFrame For async version
sendPacket()
sendPacket(
packet
):Promise
<number
>
Defined in: src/lib/codec-context.ts:906
Send packet to decoder.
Submits encoded data for decoding. Call receiveFrame() to get decoded frames.
Direct mapping to avcodec_send_packet().
Parameters
packet
Packet to decode (null to flush)
null
| Packet
Returns
Promise
<number
>
0 on success, negative AVERROR on error:
- AVERROR_EAGAIN: Must receive frames first
- AVERROR_EOF: Decoder has been flushed
- AVERROR_EINVAL: Invalid decoder state
- AVERROR_ENOMEM: Memory allocation failure
Example
import { FFmpegError } from 'node-av';
import { AVERROR_EAGAIN } from 'node-av';
const ret = await ctx.sendPacket(packet);
if (ret === AVERROR_EAGAIN) {
// Need to receive frames first
} else {
FFmpegError.throwIfError(ret, 'sendPacket');
}
See
receiveFrame To get decoded frames
sendPacketSync()
sendPacketSync(
packet
):number
Defined in: src/lib/codec-context.ts:941
Send packet to decoder synchronously. Synchronous version of sendPacket.
Submits compressed data for decoding. Call receiveFrameSync() to get decoded frames.
Direct mapping to avcodec_send_packet().
Parameters
packet
Packet to decode (null to flush)
null
| Packet
Returns
number
0 on success, negative AVERROR on error:
- AVERROR_EAGAIN: Must receive frames first
- AVERROR_EOF: Decoder has been flushed
- AVERROR_EINVAL: Invalid decoder state
- AVERROR_ENOMEM: Memory allocation failure
Example
import { FFmpegError } from 'node-av';
import { AVERROR_EAGAIN } from 'node-av';
const ret = ctx.sendPacketSync(packet);
if (ret === AVERROR_EAGAIN) {
// Need to receive frames first
ctx.receiveFrameSync(frame);
}
See
sendPacket For async version
setHardwarePixelFormat()
setHardwarePixelFormat(
hwFormat
,swFormat?
):void
Defined in: src/lib/codec-context.ts:1174
Set hardware pixel format.
Configures hardware acceleration pixel formats. Used in get_format callback for hardware decoding.
Parameters
hwFormat
Hardware pixel format
swFormat?
Software pixel format (optional)
Returns
void
Example
import { AV_PIX_FMT_CUDA, AV_PIX_FMT_NV12 } from 'node-av/constants';
ctx.setHardwarePixelFormat(AV_PIX_FMT_CUDA, AV_PIX_FMT_NV12);
setOption()
Set an option on this object.
Uses the AVOption API to set options. Available options depend on the specific object type.
Direct mapping to av_opt_set* functions.
Param
Option name
Param
Option value
Param
Option type (defaults to AV_OPT_TYPE_STRING)
Param
Search flags (default: AV_OPT_SEARCH_CHILDREN)
Example
import { FFmpegError } from 'node-av';
import { AV_OPT_TYPE_STRING, AV_OPT_TYPE_INT64, AV_OPT_TYPE_RATIONAL, AV_OPT_TYPE_PIXEL_FMT } from 'node-av/constants';
// String options (default)
let ret = obj.setOption('preset', 'fast');
FFmpegError.throwIfError(ret, 'set preset');
ret = obj.setOption('codec', 'h264', AV_OPT_TYPE_STRING);
FFmpegError.throwIfError(ret, 'set codec');
// Integer options
ret = obj.setOption('bitrate', 2000000, AV_OPT_TYPE_INT64);
FFmpegError.throwIfError(ret, 'set bitrate');
ret = obj.setOption('threads', 4, AV_OPT_TYPE_INT);
FFmpegError.throwIfError(ret, 'set threads');
// Complex types with proper types
ret = obj.setOption('framerate', {num: 30, den: 1}, AV_OPT_TYPE_RATIONAL);
FFmpegError.throwIfError(ret, 'set framerate');
ret = obj.setOption('pix_fmt', AV_PIX_FMT_YUV420P, AV_OPT_TYPE_PIXEL_FMT);
FFmpegError.throwIfError(ret, 'set pixel format');
Call Signature
setOption(
name
,value
,type?
,searchFlags?
):number
Defined in: src/lib/option.ts:1006
Parameters
name
string
value
string
type?
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1007
Parameters
name
string
value
string
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1010
Parameters
name
string
value
number
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1011
Parameters
name
string
value
bigint
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1012
Parameters
name
string
value
number
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1013
Parameters
name
string
value
bigint
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1014
Parameters
name
string
value
number
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1015
Parameters
name
string
value
boolean
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1016
Parameters
name
string
value
number
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1017
Parameters
name
string
value
number
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1020
Parameters
name
string
value
number
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1021
Parameters
name
string
value
number
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1024
Parameters
name
string
value
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1025
Parameters
name
string
value
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1026
Parameters
name
string
value
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1027
Parameters
name
string
value
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1028
Parameters
name
string
value
height
number
width
number
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1029
Parameters
name
string
value
number
| bigint
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1030
Parameters
name
string
value
Buffer
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1031
Parameters
name
string
value
number
[]
type
searchFlags?
Returns
number
Inherited from
Call Signature
setOption(
name
,value
,type
,searchFlags?
):number
Defined in: src/lib/option.ts:1032
Parameters
name
string
value
type
searchFlags?
Returns
number