Class: CodecParser
Defined in: src/lib/codec-parser.ts:53
Parser for extracting codec frames from raw bitstream data.
Analyzes and splits raw bitstream data into individual codec frames. Essential for processing elementary streams, extracting NAL units, and handling frame boundaries in raw codec data. Commonly used for parsing H.264/H.265 streams, AAC ADTS streams, and other raw formats.
Direct mapping to FFmpeg's AVCodecParserContext.
Example
import { CodecParser, CodecContext, Packet, FFmpegError } from 'node-av';
import { AV_CODEC_ID_H264 } from 'node-av/constants';
// Create and initialize parser
const parser = new CodecParser();
parser.init(AV_CODEC_ID_H264);
// Parse raw data into packets
const packet = new Packet();
packet.alloc();
const rawData = Buffer.from([...]); // Raw H.264 stream
const ret = parser.parse2(
codecContext,
packet,
rawData,
0n, // pts
0n, // dts
0 // position
);
if (ret > 0) {
// Got complete packet, ret is consumed bytes
console.log(`Parsed ${ret} bytes into packet`);
}
// Cleanup
parser.close();See
- AVCodecParserContext - FFmpeg Doxygen
- CodecContext For decoding parsed packets
Implements
DisposableNativeWrapper<NativeCodecParser>
Constructors
Constructor
new CodecParser():
CodecParser
Defined in: src/lib/codec-parser.ts:56
Returns
CodecParser
Accessors
repeatPict
Get Signature
get repeatPict():
number
Defined in: src/lib/codec-parser.ts:171
Number of pictures to repeat for field-based interlaced content.
Used to calculate frame duration for interlaced video: frame_duration = (1 + repeat_pict) * time_base
Commonly used in H.264 to display telecined material. Value comes from the codec parser after parsing a frame.
Direct mapping to AVCodecParserContext.repeat_pict.
Example
const parser = stream.parser;
if (parser) {
const fields = 1 + parser.repeatPict;
console.log(`Frame uses ${fields} fields`);
}Returns
number
Repeat picture count (0 for progressive, >0 for interlaced)
Methods
[dispose]()
[dispose]():
void
Defined in: src/lib/codec-parser.ts:222
Dispose of the codec parser.
Implements the Disposable interface for automatic cleanup. Equivalent to calling close().
Returns
void
Example
{
using parser = new CodecParser();
parser.init(AV_CODEC_ID_H264);
// Use parser...
} // Automatically closed when leaving scopeImplementation of
Disposable.[dispose]
close()
close():
void
Defined in: src/lib/codec-parser.ts:192
Close the codec parser.
Releases all resources associated with the parser. The parser becomes invalid after calling this.
Direct mapping to av_parser_close().
Returns
void
Example
parser.close();
// Parser is now invalidSee
- init To initialize
- Symbol.dispose For automatic cleanup
getNative()
getNative():
NativeCodecParser
Defined in: src/lib/codec-parser.ts:203
Internal
Get the underlying native CodecParser object.
Returns
The native CodecParser binding object
Implementation of
init()
init(
codecId):void
Defined in: src/lib/codec-parser.ts:84
Initialize parser for specific codec.
Sets up the parser to handle a specific codec format. Must be called before parsing data.
Direct mapping to av_parser_init().
Parameters
codecId
Codec ID to parse
Returns
void
Throws
If codec parser not available
Example
import { AV_CODEC_ID_AAC } from 'node-av/constants';
const parser = new CodecParser();
parser.init(AV_CODEC_ID_AAC);
// Parser ready for AAC ADTS streamsSee
parse2()
parse2(
codecContext,packet,data,pts,dts,pos):number
Defined in: src/lib/codec-parser.ts:145
Parse bitstream data into packets.
Analyzes raw bitstream data and extracts complete codec frames. May require multiple calls to accumulate enough data for a complete frame. Returns the number of bytes consumed from the input buffer.
Direct mapping to av_parser_parse2().
Parameters
codecContext
Codec context for parser state
packet
Packet to receive parsed frame
data
Buffer
Raw bitstream data to parse
pts
bigint
Presentation timestamp for data
dts
bigint
Decoding timestamp for data
pos
number
Byte position in stream
Returns
number
Number of bytes consumed from data, negative on error:
- AVERROR_EINVAL: Invalid parameters
- AVERROR_ENOMEM: Memory allocation failure
Example
import { FFmpegError } from 'node-av';
let offset = 0;
while (offset < rawData.length) {
const remaining = rawData.subarray(offset);
const ret = parser.parse2(
codecContext,
packet,
remaining,
pts,
dts,
offset
);
if (ret < 0) {
FFmpegError.throwIfError(ret, 'parse2');
}
offset += ret;
if (packet.size > 0) {
// Got complete packet
await processPacket(packet);
packet.unref();
}
}See
init To initialize parser
