Skip to content

node-av / api / IOStream

Class: IOStream

Defined in: src/api/io-stream.ts:127

Factory for creating custom I/O contexts.

Provides simplified creation of I/O contexts from buffers or custom callbacks. Handles buffer management and seek operations for in-memory media. Bridges the gap between high-level media operations and custom I/O sources. Essential for processing media from non-file sources like network streams or memory.

Examples

typescript
import { IOStream, Demuxer } from 'node-av/api';

// From buffer
const buffer = await fs.readFile('video.mp4');
const ioContext = IOStream.create(buffer);
const input = await Demuxer.open(buffer);
typescript
// Custom I/O callbacks
const callbacks = {
  read: async (size: number) => {
    // Read from custom source
    return Buffer.alloc(size);
  },
  seek: async (offset: bigint, whence: AVSeekWhence) => {
    // Seek in custom source
    return offset;
  }
};

const ioContext = IOStream.create(callbacks, {
  bufferSize: 4096
});

See

Constructors

Constructor

new IOStream(): IOStream

Returns

IOStream

Methods

create()

Call Signature

static create(buffer, options?): IOContext

Defined in: src/api/io-stream.ts:148

Create I/O context from buffer.

Creates an I/O context from an in-memory buffer for reading media data. Automatically handles seek operations and position tracking.

Parameters
buffer

Buffer

Buffer containing media data

options?

DemuxerOptions

I/O configuration options

Returns

IOContext

Configured I/O context

Example
typescript
const buffer = await fs.readFile('video.mp4');
const ioContext = IOStream.create(buffer, {
  bufferSize: 8192
});

Call Signature

static create(callbacks, options?): IOContext

Defined in: src/api/io-stream.ts:175

Create I/O context from callbacks.

Creates an I/O context using custom read and seek callbacks. Useful for streaming from non-file sources like network or custom storage.

Parameters
callbacks

IOInputCallbacks

I/O callbacks for read and seek operations

options?

DemuxerOptions

I/O configuration options

Returns

IOContext

Configured I/O context

Throws

If callbacks missing required read function

Example
typescript
const ioContext = IOStream.create({
  read: async (size) => {
    return await customSource.read(size);
  },
  seek: async (offset, whence) => {
    return await customSource.seek(offset, whence);
  }
});

Call Signature

static create(stream, options?): IOContext

Defined in: src/api/io-stream.ts:194

Create I/O context from a Node.js Readable stream.

Creates an I/O context that reads from a Readable stream. Seeking is not supported for streams.

Parameters
stream

Readable

Node.js Readable stream

options?

DemuxerOptions

I/O configuration options

Returns

IOContext

Configured I/O context

Example
typescript
const readable = fs.createReadStream('video.mkv');
const ioContext = IOStream.create(readable, { format: 'matroska' });

createOutput()

Call Signature

static createOutput(stream, options?): IOContext

Defined in: src/api/io-stream.ts:235

Create I/O context for writing to a Node.js Writable stream.

Creates a write-mode I/O context from a Writable stream with backpressure support and automatic buffer copying. Seeking is not supported for streams.

Parameters
stream

Writable

Node.js Writable stream

options?

MuxerOptions

Output configuration options

Returns

IOContext

Configured I/O context

Example
typescript
const writable = fs.createWriteStream('output.mkv');
const ioContext = IOStream.createOutput(writable, { format: 'matroska' });

Call Signature

static createOutput(callbacks, options?): IOContext

Defined in: src/api/io-stream.ts:261

Create I/O context from custom output callbacks.

Creates a write-mode I/O context using custom write, optional seek, and optional read callbacks.

Parameters
callbacks

IOOutputCallbacks

I/O callbacks for write, seek, and read operations

options?

MuxerOptions

Output configuration options

Returns

IOContext

Configured I/O context

Throws

If callbacks missing required write function

Example
typescript
const chunks: Buffer[] = [];
const ioContext = IOStream.createOutput({
  write: (buffer) => {
    chunks.push(Buffer.from(buffer));
    return buffer.length;
  }
}, { format: 'matroska' });