Skip to content

node-av / lib / Option

Class: Option

Defined in: src/lib/option.ts:241

FFmpeg option management utilities.

Provides static methods for getting, setting, and querying options on FFmpeg objects that support the AVOption API. Handles type conversion and validation for various option types including strings, numbers, rationals, pixel formats, and more.

Direct mapping to FFmpeg's AVOption API.

Example

typescript
import { Option, FFmpegError } from 'node-av';
import { AV_OPT_SEARCH_CHILDREN, AV_PIX_FMT_YUV420P } from 'node-av/constants';

// Set various option types
let ret = Option.set(obj, 'preset', 'fast');
FFmpegError.throwIfError(ret, 'set preset');

ret = Option.setInt(obj, 'bitrate', 2000000);
FFmpegError.throwIfError(ret, 'set bitrate');

ret = Option.setRational(obj, 'framerate', { num: 30, den: 1 });
FFmpegError.throwIfError(ret, 'set framerate');

// Get option values
const preset = Option.get(obj, 'preset');
const bitrate = Option.getInt(obj, 'bitrate');
const framerate = Option.getRational(obj, 'framerate');

// List all options
let opt = null;
while ((opt = Option.next(obj, opt))) {
  console.log(`${opt.name}: ${opt.help}`);
}

See

Constructors

Constructor

new Option(): Option

Returns

Option

Methods

copy()

static copy(dest, src): number

Defined in: src/lib/option.ts:849

Copy options between objects.

Copies option values from source to destination.

Direct mapping to av_opt_copy().

Parameters

dest

OptionCapableObject

Destination object

src

OptionCapableObject

Source object

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Copy options from one codec context to another
const ret = Option.copy(destCodecContext, srcCodecContext);
FFmpegError.throwIfError(ret, 'Failed to copy options');

find()

static find(obj, name, searchFlags): null | OptionInfo

Defined in: src/lib/option.ts:291

Find option by name.

Searches for an option with the specified name.

Direct mapping to av_opt_find().

Parameters

obj

OptionCapableObject

Object to search

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | OptionInfo

Option info if found, null otherwise

Example

typescript
const opt = Option.find(obj, 'bitrate');
if (opt) {
  console.log(`Found: ${opt.name}, Type: ${opt.type}`);
}

find2()

static find2(obj, name, searchFlags): null | { isDifferentTarget: boolean; option: null | OptionInfo; }

Defined in: src/lib/option.ts:319

Find option with target info.

Like find() but also indicates if option was found on different target.

Direct mapping to av_opt_find2().

Parameters

obj

OptionCapableObject

Object to search

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | { isDifferentTarget: boolean; option: null | OptionInfo; }

Object with option and target info

Example

typescript
const result = Option.find2(obj, 'bitrate', AV_OPT_SEARCH_CHILDREN);
if (result?.option) {
  console.log(`Found on ${result.isDifferentTarget ? 'child' : 'object'}`);
}

free()

static free(obj): void

Defined in: src/lib/option.ts:918

Free option resources.

Direct mapping to av_opt_free().

Parameters

obj

OptionCapableObject

Object to free options from

Returns

void

Example

typescript
// Free codec context options
Option.free(codecContext);

get()

static get(obj, name, searchFlags): null | string

Defined in: src/lib/option.ts:348

Get string option value.

Direct mapping to av_opt_get().

Parameters

obj

OptionCapableObject

Object to query

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | string

Option value as string, or null

Example

typescript
// Get codec preset option
const preset = Option.get(codecContext, 'preset', AV_OPT_SEARCH_CHILDREN);
console.log('Codec preset:', preset); // 'medium', 'fast', etc.

getChannelLayout()

static getChannelLayout(obj, name, searchFlags): null | ChannelLayout

Defined in: src/lib/option.ts:516

Get channel layout option value.

Direct mapping to av_opt_get_chlayout().

Parameters

obj

OptionCapableObject

Object to query

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | ChannelLayout

Channel layout, or null

Example

typescript
// Get audio channel layout
const layout = Option.getChannelLayout(codecContext, 'channel_layout', AV_OPT_SEARCH_CHILDREN);
console.log('Channel layout:', layout); // stereo, 5.1, etc.

getDict()

static getDict(obj, name, searchFlags): null | Dictionary

Defined in: src/lib/option.ts:540

Get dictionary option value.

Direct mapping to av_opt_get_dict_val().

Parameters

obj

OptionCapableObject

Object to query

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | Dictionary

Dictionary value, or null

Example

typescript
// Get metadata dictionary
const metadata = Option.getDict(formatContext, 'metadata', AV_OPT_SEARCH_CHILDREN);
console.log('Metadata:', metadata?.get('title'));

getDouble()

static getDouble(obj, name, searchFlags): null | number

Defined in: src/lib/option.ts:396

Get double option value.

Direct mapping to av_opt_get_double().

Parameters

obj

OptionCapableObject

Object to query

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | number

Option value as double, or null

Example

typescript
// Get codec quality scale
const crf = Option.getDouble(codecContext, 'crf', AV_OPT_SEARCH_CHILDREN);
console.log('CRF value:', crf); // 23.0, 18.0, etc.

getImageSize()

static getImageSize(obj, name, searchFlags): null | { height: number; width: number; }

Defined in: src/lib/option.ts:492

Get image size option value.

Direct mapping to av_opt_get_image_size().

Parameters

obj

OptionCapableObject

Object to query

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | { height: number; width: number; }

Width and height, or null

Example

typescript
// Get filter output size
const size = Option.getImageSize(filterContext, 'size', AV_OPT_SEARCH_CHILDREN);
console.log('Output size:', size); // { width: 1920, height: 1080 }

getInt()

static getInt(obj, name, searchFlags): null | number

Defined in: src/lib/option.ts:372

Get integer option value.

Direct mapping to av_opt_get_int().

Parameters

obj

OptionCapableObject

Object to query

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | number

Option value as integer, or null

Example

typescript
// Get codec GOP size
const gopSize = Option.getInt(codecContext, 'g', AV_OPT_SEARCH_CHILDREN);
console.log('GOP size:', gopSize); // 60, 120, etc.

getPixelFormat()

static getPixelFormat(obj, name, searchFlags): null | AVPixelFormat

Defined in: src/lib/option.ts:444

Get pixel format option value.

Direct mapping to av_opt_get_pixel_fmt().

Parameters

obj

OptionCapableObject

Object to query

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | AVPixelFormat

Pixel format value, or null

Example

typescript
// Get filter pixel format
const pixFmt = Option.getPixelFormat(filterContext, 'pix_fmt', AV_OPT_SEARCH_CHILDREN);
console.log('Pixel format:', pixFmt); // AV_PIX_FMT_YUV420P, etc.

getRational()

static getRational(obj, name, searchFlags): null | IRational

Defined in: src/lib/option.ts:420

Get rational option value.

Direct mapping to av_opt_get_q().

Parameters

obj

OptionCapableObject

Object to query

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | IRational

Option value as rational, or null

Example

typescript
// Get codec time base
const timeBase = Option.getRational(codecContext, 'time_base', AV_OPT_SEARCH_CHILDREN);
console.log('Time base:', timeBase); // { num: 1, den: 30 }

getSampleFormat()

static getSampleFormat(obj, name, searchFlags): null | AVSampleFormat

Defined in: src/lib/option.ts:468

Get sample format option value.

Direct mapping to av_opt_get_sample_fmt().

Parameters

obj

OptionCapableObject

Object to query

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | AVSampleFormat

Sample format value, or null

Example

typescript
// Get audio codec sample format
const sampleFmt = Option.getSampleFormat(codecContext, 'sample_fmt', AV_OPT_SEARCH_CHILDREN);
console.log('Sample format:', sampleFmt); // AV_SAMPLE_FMT_FLTP, etc.

isSetToDefault()

static isSetToDefault(obj, name, searchFlags): null | boolean

Defined in: src/lib/option.ts:873

Check if option is set to default.

Direct mapping to av_opt_is_set_to_default().

Parameters

obj

OptionCapableObject

Object to check

name

string

Option name

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

null | boolean

True if default, false if modified, null if not found

Example

typescript
// Check if bitrate is at default value
const isDefault = Option.isSetToDefault(codecContext, 'b', AV_OPT_SEARCH_CHILDREN);
console.log('Bitrate is default:', isDefault);

next()

static next(obj, prev): null | OptionInfo

Defined in: src/lib/option.ts:263

Iterate to next option.

Iterates through available options on an object.

Direct mapping to av_opt_next().

Parameters

obj

OptionCapableObject

Object with options

prev

Previous option (null to get first)

null | OptionInfo

Returns

null | OptionInfo

Next option, or null if no more

Example

typescript
let opt = null;
while ((opt = Option.next(obj, opt))) {
  console.log(`Option: ${opt.name}`);
}

serialize()

static serialize(obj, optFlags, flags, keyValSep, pairsSep): null | string

Defined in: src/lib/option.ts:901

Serialize options to string.

Direct mapping to av_opt_serialize().

Parameters

obj

OptionCapableObject

Object to serialize

optFlags

number = 0

Option flags filter

flags

number = 0

Serialization flags

keyValSep

string = '='

Key-value separator

pairsSep

string = ','

Pairs separator

Returns

null | string

Serialized string, or null on error

Example

typescript
// Serialize codec options to string
const serialized = Option.serialize(codecContext, 0, 0, '=', ':');
console.log('Options:', serialized); // 'bitrate=2000000:preset=fast'

set()

static set(obj, name, value, searchFlags): number

Defined in: src/lib/option.ts:567

Set string option value.

Direct mapping to av_opt_set().

Parameters

obj

OptionCapableObject

Object to modify

name

string

Option name

value

string

String value

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Set codec preset
const ret = Option.set(codecContext, 'preset', 'fast', AV_OPT_SEARCH_CHILDREN);
FFmpegError.throwIfError(ret, 'Failed to set preset');

setBin()

static setBin(obj, name, value, searchFlags): number

Defined in: src/lib/option.ts:806

Set binary option value.

Direct mapping to av_opt_set_bin().

Parameters

obj

OptionCapableObject

Object to modify

name

string

Option name

value

Buffer

Binary data

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Set binary extradata
const extradata = Buffer.from([0x00, 0x01, 0x02, 0x03]);
const ret = Option.setBin(codecContext, 'extradata', extradata, AV_OPT_SEARCH_CHILDREN);
FFmpegError.throwIfError(ret, 'Failed to set extradata');

setChannelLayout()

static setChannelLayout(obj, name, value, searchFlags): number

Defined in: src/lib/option.ts:751

Set channel layout option value.

Direct mapping to av_opt_set_chlayout().

Parameters

obj

OptionCapableObject

Object to modify

name

string

Option name

value

number

Channel layout

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Set audio channel layout to stereo
const ret = Option.setChannelLayout(codecContext, 'channel_layout', AV_CHANNEL_LAYOUT_STEREO, AV_OPT_SEARCH_CHILDREN);
FFmpegError.throwIfError(ret, 'Failed to set channel layout');

setDefaults()

static setDefaults(obj): void

Defined in: src/lib/option.ts:825

Set defaults on object.

Sets all options to their default values.

Direct mapping to av_opt_set_defaults().

Parameters

obj

OptionCapableObject

Object to reset

Returns

void

Example

typescript
// Reset all codec options to defaults
Option.setDefaults(codecContext);

setDict()

static setDict(obj, name, value, searchFlags): number

Defined in: src/lib/option.ts:779

Set dictionary option value.

Direct mapping to av_opt_set_dict_val().

Parameters

obj

OptionCapableObject

Object to modify

name

string

Option name

value

Dictionary

Dictionary value

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Set metadata dictionary
const dict = new Dictionary();
dict.set('title', 'My Video');
const ret = Option.setDict(formatContext, 'metadata', dict, AV_OPT_SEARCH_CHILDREN);
FFmpegError.throwIfError(ret, 'Failed to set metadata');

setDouble()

static setDouble(obj, name, value, searchFlags): number

Defined in: src/lib/option.ts:619

Set double option value.

Direct mapping to av_opt_set_double().

Parameters

obj

OptionCapableObject

Object to modify

name

string

Option name

value

number

Double value

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Set codec CRF value
const ret = Option.setDouble(codecContext, 'crf', 23.0, AV_OPT_SEARCH_CHILDREN);
FFmpegError.throwIfError(ret, 'Failed to set CRF');

setImageSize()

static setImageSize(obj, name, width, height, searchFlags): number

Defined in: src/lib/option.ts:725

Set image size option value.

Direct mapping to av_opt_set_image_size().

Parameters

obj

OptionCapableObject

Object to modify

name

string

Option name

width

number

Image width

height

number

Image height

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Set filter output size
const ret = Option.setImageSize(filterContext, 'size', 1920, 1080, AV_OPT_SEARCH_CHILDREN);
FFmpegError.throwIfError(ret, 'Failed to set image size');

setInt()

static setInt(obj, name, value, searchFlags): number

Defined in: src/lib/option.ts:593

Set integer option value.

Direct mapping to av_opt_set_int().

Parameters

obj

OptionCapableObject

Object to modify

name

string

Option name

value

Integer value

number | bigint

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Set codec bitrate
const ret = Option.setInt(codecContext, 'b', 2000000, AV_OPT_SEARCH_CHILDREN);
FFmpegError.throwIfError(ret, 'Failed to set bitrate');

setPixelFormat()

static setPixelFormat(obj, name, value, searchFlags): number

Defined in: src/lib/option.ts:671

Set pixel format option value.

Direct mapping to av_opt_set_pixel_fmt().

Parameters

obj

OptionCapableObject

Object to modify

name

string

Option name

value

AVPixelFormat

Pixel format

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Set filter pixel format
const ret = Option.setPixelFormat(filterContext, 'pix_fmt', AV_PIX_FMT_YUV420P, AV_OPT_SEARCH_CHILDREN);
FFmpegError.throwIfError(ret, 'Failed to set pixel format');

setRational()

static setRational(obj, name, value, searchFlags): number

Defined in: src/lib/option.ts:645

Set rational option value.

Direct mapping to av_opt_set_q().

Parameters

obj

OptionCapableObject

Object to modify

name

string

Option name

value

IRational

Rational value

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Set codec frame rate
const ret = Option.setRational(codecContext, 'framerate', { num: 30, den: 1 }, AV_OPT_SEARCH_CHILDREN);
FFmpegError.throwIfError(ret, 'Failed to set framerate');

setSampleFormat()

static setSampleFormat(obj, name, value, searchFlags): number

Defined in: src/lib/option.ts:697

Set sample format option value.

Direct mapping to av_opt_set_sample_fmt().

Parameters

obj

OptionCapableObject

Object to modify

name

string

Option name

value

AVSampleFormat

Sample format

searchFlags

AVOptionSearchFlags = AVFLAG_NONE

Search flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Set audio codec sample format
const ret = Option.setSampleFormat(codecContext, 'sample_fmt', AV_SAMPLE_FMT_FLTP, AV_OPT_SEARCH_CHILDREN);
FFmpegError.throwIfError(ret, 'Failed to set sample format');

show()

static show(obj, reqFlags, rejFlags): number

Defined in: src/lib/option.ts:942

Show options for debugging.

Direct mapping to av_opt_show2().

Parameters

obj

OptionCapableObject

Object to show options for

reqFlags

number = 0

Required flags

rejFlags

number = 0

Rejected flags

Returns

number

0 on success, negative AVERROR on error

Example

typescript
// Show all codec options for debugging
const ret = Option.show(codecContext, 0, 0);
FFmpegError.throwIfError(ret, 'Failed to show options');