node-av 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -38
- package/dist/api/bitstream-filter.d.ts +180 -123
- package/dist/api/bitstream-filter.js +180 -125
- package/dist/api/bitstream-filter.js.map +1 -1
- package/dist/api/decoder.d.ts +279 -132
- package/dist/api/decoder.js +285 -142
- package/dist/api/decoder.js.map +1 -1
- package/dist/api/encoder.d.ts +246 -162
- package/dist/api/encoder.js +272 -208
- package/dist/api/encoder.js.map +1 -1
- package/dist/api/filter-presets.d.ts +690 -94
- package/dist/api/filter-presets.js +686 -102
- package/dist/api/filter-presets.js.map +1 -1
- package/dist/api/filter.d.ts +249 -213
- package/dist/api/filter.js +252 -242
- package/dist/api/filter.js.map +1 -1
- package/dist/api/hardware.d.ts +224 -117
- package/dist/api/hardware.js +380 -214
- package/dist/api/hardware.js.map +1 -1
- package/dist/api/index.d.ts +3 -3
- package/dist/api/index.js +1 -1
- package/dist/api/index.js.map +1 -1
- package/dist/api/io-stream.d.ts +65 -61
- package/dist/api/io-stream.js +43 -46
- package/dist/api/io-stream.js.map +1 -1
- package/dist/api/media-input.d.ts +242 -140
- package/dist/api/media-input.js +205 -103
- package/dist/api/media-input.js.map +1 -1
- package/dist/api/media-output.d.ts +206 -128
- package/dist/api/media-output.js +210 -128
- package/dist/api/media-output.js.map +1 -1
- package/dist/api/pipeline.d.ts +168 -38
- package/dist/api/pipeline.js +238 -14
- package/dist/api/pipeline.js.map +1 -1
- package/dist/api/types.d.ts +21 -187
- package/dist/api/utils.d.ts +1 -2
- package/dist/api/utils.js +9 -0
- package/dist/api/utils.js.map +1 -1
- package/dist/lib/audio-fifo.d.ts +127 -170
- package/dist/lib/audio-fifo.js +130 -173
- package/dist/lib/audio-fifo.js.map +1 -1
- package/dist/lib/binding.js +5 -0
- package/dist/lib/binding.js.map +1 -1
- package/dist/lib/bitstream-filter-context.d.ts +139 -184
- package/dist/lib/bitstream-filter-context.js +139 -188
- package/dist/lib/bitstream-filter-context.js.map +1 -1
- package/dist/lib/bitstream-filter.d.ts +68 -54
- package/dist/lib/bitstream-filter.js +68 -54
- package/dist/lib/bitstream-filter.js.map +1 -1
- package/dist/lib/codec-context.d.ts +316 -380
- package/dist/lib/codec-context.js +316 -381
- package/dist/lib/codec-context.js.map +1 -1
- package/dist/lib/codec-parameters.d.ts +160 -170
- package/dist/lib/codec-parameters.js +162 -172
- package/dist/lib/codec-parameters.js.map +1 -1
- package/dist/lib/codec-parser.d.ts +91 -104
- package/dist/lib/codec-parser.js +92 -103
- package/dist/lib/codec-parser.js.map +1 -1
- package/dist/lib/codec.d.ts +264 -281
- package/dist/lib/codec.js +268 -285
- package/dist/lib/codec.js.map +1 -1
- package/dist/lib/dictionary.d.ts +149 -203
- package/dist/lib/dictionary.js +158 -212
- package/dist/lib/dictionary.js.map +1 -1
- package/dist/lib/error.d.ts +96 -130
- package/dist/lib/error.js +98 -128
- package/dist/lib/error.js.map +1 -1
- package/dist/lib/filter-context.d.ts +284 -218
- package/dist/lib/filter-context.js +290 -227
- package/dist/lib/filter-context.js.map +1 -1
- package/dist/lib/filter-graph.d.ts +251 -292
- package/dist/lib/filter-graph.js +253 -294
- package/dist/lib/filter-graph.js.map +1 -1
- package/dist/lib/filter-inout.d.ts +87 -95
- package/dist/lib/filter-inout.js +87 -95
- package/dist/lib/filter-inout.js.map +1 -1
- package/dist/lib/filter.d.ts +93 -111
- package/dist/lib/filter.js +93 -111
- package/dist/lib/filter.js.map +1 -1
- package/dist/lib/format-context.d.ts +320 -428
- package/dist/lib/format-context.js +313 -385
- package/dist/lib/format-context.js.map +1 -1
- package/dist/lib/frame.d.ts +262 -405
- package/dist/lib/frame.js +263 -408
- package/dist/lib/frame.js.map +1 -1
- package/dist/lib/hardware-device-context.d.ts +149 -203
- package/dist/lib/hardware-device-context.js +149 -203
- package/dist/lib/hardware-device-context.js.map +1 -1
- package/dist/lib/hardware-frames-context.d.ts +170 -180
- package/dist/lib/hardware-frames-context.js +171 -181
- package/dist/lib/hardware-frames-context.js.map +1 -1
- package/dist/lib/index.d.ts +2 -1
- package/dist/lib/index.js +2 -2
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/input-format.d.ts +89 -117
- package/dist/lib/input-format.js +89 -117
- package/dist/lib/input-format.js.map +1 -1
- package/dist/lib/io-context.d.ts +209 -241
- package/dist/lib/io-context.js +220 -252
- package/dist/lib/io-context.js.map +1 -1
- package/dist/lib/log.d.ts +85 -119
- package/dist/lib/log.js +85 -122
- package/dist/lib/log.js.map +1 -1
- package/dist/lib/native-types.d.ts +117 -106
- package/dist/lib/native-types.js +0 -7
- package/dist/lib/native-types.js.map +1 -1
- package/dist/lib/option.d.ts +284 -241
- package/dist/lib/option.js +309 -249
- package/dist/lib/option.js.map +1 -1
- package/dist/lib/output-format.d.ts +77 -101
- package/dist/lib/output-format.js +77 -101
- package/dist/lib/output-format.js.map +1 -1
- package/dist/lib/packet.d.ts +172 -240
- package/dist/lib/packet.js +172 -241
- package/dist/lib/packet.js.map +1 -1
- package/dist/lib/rational.d.ts +0 -2
- package/dist/lib/rational.js +0 -2
- package/dist/lib/rational.js.map +1 -1
- package/dist/lib/software-resample-context.d.ts +241 -325
- package/dist/lib/software-resample-context.js +242 -326
- package/dist/lib/software-resample-context.js.map +1 -1
- package/dist/lib/software-scale-context.d.ts +129 -173
- package/dist/lib/software-scale-context.js +131 -175
- package/dist/lib/software-scale-context.js.map +1 -1
- package/dist/lib/stream.d.ts +87 -197
- package/dist/lib/stream.js +87 -197
- package/dist/lib/stream.js.map +1 -1
- package/dist/lib/utilities.d.ts +372 -181
- package/dist/lib/utilities.js +373 -182
- package/dist/lib/utilities.js.map +1 -1
- package/install/check.js +0 -1
- package/package.json +21 -12
- package/release_notes.md +43 -59
- package/CHANGELOG.md +0 -8
package/dist/lib/io-context.js
CHANGED
|
@@ -4,92 +4,83 @@ import { OptionMember } from './option.js';
|
|
|
4
4
|
/**
|
|
5
5
|
* I/O context for custom input/output operations.
|
|
6
6
|
*
|
|
7
|
-
* Provides
|
|
8
|
-
* from
|
|
9
|
-
*
|
|
7
|
+
* Provides an abstraction layer for all I/O operations in FFmpeg.
|
|
8
|
+
* Enables reading from and writing to various sources including files,
|
|
9
|
+
* network streams, memory buffers, and custom callbacks. Essential for
|
|
10
|
+
* implementing custom protocols or handling non-standard I/O scenarios.
|
|
10
11
|
*
|
|
11
12
|
* Direct mapping to FFmpeg's AVIOContext.
|
|
12
13
|
*
|
|
13
14
|
* @example
|
|
14
15
|
* ```typescript
|
|
15
16
|
* import { IOContext, FFmpegError } from 'node-av';
|
|
16
|
-
* import { AVIO_FLAG_READ, AVSEEK_SET } from 'node-av/constants';
|
|
17
|
+
* import { AVIO_FLAG_READ, AVIO_FLAG_WRITE, AVSEEK_SET } from 'node-av/constants';
|
|
17
18
|
*
|
|
18
|
-
* // Open
|
|
19
|
+
* // Open file for reading
|
|
19
20
|
* const io = new IOContext();
|
|
20
21
|
* const ret = await io.open2('input.mp4', AVIO_FLAG_READ);
|
|
21
22
|
* FFmpegError.throwIfError(ret, 'open2');
|
|
22
23
|
*
|
|
23
24
|
* // Read data
|
|
24
|
-
* const
|
|
25
|
+
* const data = await io.read(4096);
|
|
26
|
+
* if (data instanceof Buffer) {
|
|
27
|
+
* console.log(`Read ${data.length} bytes`);
|
|
28
|
+
* }
|
|
25
29
|
*
|
|
26
30
|
* // Seek to position
|
|
27
|
-
* const
|
|
28
|
-
*
|
|
31
|
+
* const pos = await io.seek(1024n, AVSEEK_SET);
|
|
32
|
+
* console.log(`Seeked to position ${pos}`);
|
|
29
33
|
*
|
|
30
34
|
* // Get file size
|
|
31
|
-
* const
|
|
35
|
+
* const fileSize = await io.size();
|
|
36
|
+
* console.log(`File size: ${fileSize}`);
|
|
32
37
|
*
|
|
33
|
-
* //
|
|
38
|
+
* // Close when done
|
|
34
39
|
* await io.closep();
|
|
35
|
-
* ```
|
|
36
40
|
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
41
|
* // Custom I/O with callbacks
|
|
40
|
-
* const
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
42
|
+
* const customIO = new IOContext();
|
|
43
|
+
* let position = 0n;
|
|
44
|
+
* const buffer = Buffer.from('Hello World');
|
|
45
|
+
*
|
|
46
|
+
* customIO.allocContextWithCallbacks(
|
|
47
|
+
* 4096, // Buffer size
|
|
48
|
+
* 0, // Read mode
|
|
49
|
+
* (size) => {
|
|
50
|
+
* // Read callback
|
|
51
|
+
* const end = Number(position) + size;
|
|
52
|
+
* const chunk = buffer.subarray(Number(position), end);
|
|
53
|
+
* position = BigInt(end);
|
|
54
|
+
* return chunk;
|
|
46
55
|
* },
|
|
47
|
-
* null,
|
|
48
|
-
* (offset, whence) => {
|
|
49
|
-
*
|
|
56
|
+
* null, // No write callback for read mode
|
|
57
|
+
* (offset, whence) => {
|
|
58
|
+
* // Seek callback
|
|
59
|
+
* if (whence === AVSEEK_SET) position = offset;
|
|
60
|
+
* else if (whence === AVSEEK_CUR) position += offset;
|
|
61
|
+
* else if (whence === AVSEEK_END) position = BigInt(buffer.length) + offset;
|
|
62
|
+
* return position;
|
|
50
63
|
* }
|
|
51
64
|
* );
|
|
52
|
-
*
|
|
53
|
-
* // Use with FormatContext
|
|
54
|
-
* const ctx = new FormatContext();
|
|
55
|
-
* ctx.allocOutputContext2(null, 'mp4', null);
|
|
56
|
-
* ctx.pb = io;
|
|
57
|
-
* const openRet = await ctx.openInput(null, null, null);
|
|
58
|
-
* FFmpegError.throwIfError(openRet, 'openInput');
|
|
59
65
|
* ```
|
|
60
66
|
*
|
|
61
|
-
* @see
|
|
67
|
+
* @see [AVIOContext](https://ffmpeg.org/doxygen/trunk/structAVIOContext.html) - FFmpeg Doxygen
|
|
68
|
+
* @see {@link FormatContext} For using with demuxing/muxing
|
|
62
69
|
*/
|
|
63
70
|
export class IOContext extends OptionMember {
|
|
64
|
-
/**
|
|
65
|
-
* Create a new I/O context instance.
|
|
66
|
-
*
|
|
67
|
-
* The context is uninitialized - you must call allocContext() or open2() before use.
|
|
68
|
-
* No FFmpeg resources are allocated until initialization.
|
|
69
|
-
*
|
|
70
|
-
* Direct wrapper around AVIOContext allocation.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```typescript
|
|
74
|
-
* import { IOContext, FFmpegError, AVIO_FLAG_READ } from 'node-av';
|
|
75
|
-
*
|
|
76
|
-
* const io = new IOContext();
|
|
77
|
-
* const ret = await io.open2('file.mp4', AVIO_FLAG_READ);
|
|
78
|
-
* FFmpegError.throwIfError(ret, 'open2');
|
|
79
|
-
* // I/O context is now ready for use
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
71
|
constructor() {
|
|
83
72
|
super(new bindings.IOContext());
|
|
84
73
|
}
|
|
85
74
|
/**
|
|
86
|
-
*
|
|
75
|
+
* Find input format by short name.
|
|
87
76
|
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
77
|
+
* Creates an IOContext instance from a native binding object.
|
|
78
|
+
* Used internally for wrapping native I/O contexts.
|
|
90
79
|
*
|
|
91
|
-
* @param native - Native IOContext
|
|
80
|
+
* @param native - Native IOContext binding object
|
|
92
81
|
* @returns Wrapped IOContext instance
|
|
82
|
+
*
|
|
83
|
+
* @internal
|
|
93
84
|
*/
|
|
94
85
|
static fromNative(native) {
|
|
95
86
|
const io = Object.create(IOContext.prototype);
|
|
@@ -97,32 +88,32 @@ export class IOContext extends OptionMember {
|
|
|
97
88
|
return io;
|
|
98
89
|
}
|
|
99
90
|
/**
|
|
100
|
-
*
|
|
91
|
+
* End of file indicator.
|
|
101
92
|
*
|
|
102
|
-
*
|
|
93
|
+
* True if end of file has been reached during reading.
|
|
103
94
|
*
|
|
104
|
-
*
|
|
95
|
+
* Direct mapping to AVIOContext->eof_reached.
|
|
105
96
|
*/
|
|
106
97
|
get eof() {
|
|
107
98
|
return this.native.eof;
|
|
108
99
|
}
|
|
109
100
|
/**
|
|
110
|
-
* Error code
|
|
101
|
+
* Error code.
|
|
111
102
|
*
|
|
112
|
-
*
|
|
103
|
+
* Contains the last error that occurred, or 0 if no error.
|
|
113
104
|
*
|
|
114
|
-
*
|
|
105
|
+
* Direct mapping to AVIOContext->error.
|
|
115
106
|
*/
|
|
116
107
|
get error() {
|
|
117
108
|
return this.native.error;
|
|
118
109
|
}
|
|
119
110
|
/**
|
|
120
|
-
*
|
|
111
|
+
* Seekability indicator.
|
|
121
112
|
*
|
|
122
|
-
*
|
|
113
|
+
* Non-zero if the underlying resource supports seeking.
|
|
114
|
+
* Some protocols like pipes or network streams may not be seekable.
|
|
123
115
|
*
|
|
124
|
-
*
|
|
125
|
-
* @readonly
|
|
116
|
+
* Direct mapping to AVIOContext->seekable.
|
|
126
117
|
*/
|
|
127
118
|
get seekable() {
|
|
128
119
|
return this.native.seekable;
|
|
@@ -130,9 +121,10 @@ export class IOContext extends OptionMember {
|
|
|
130
121
|
/**
|
|
131
122
|
* Maximum packet size.
|
|
132
123
|
*
|
|
133
|
-
*
|
|
124
|
+
* Used to limit packet sizes in network protocols.
|
|
125
|
+
* 0 means no limit.
|
|
134
126
|
*
|
|
135
|
-
*
|
|
127
|
+
* Direct mapping to AVIOContext->max_packet_size.
|
|
136
128
|
*/
|
|
137
129
|
get maxPacketSize() {
|
|
138
130
|
return this.native.maxPacketSize;
|
|
@@ -141,13 +133,11 @@ export class IOContext extends OptionMember {
|
|
|
141
133
|
this.native.maxPacketSize = value;
|
|
142
134
|
}
|
|
143
135
|
/**
|
|
144
|
-
*
|
|
136
|
+
* Direct mode flag.
|
|
145
137
|
*
|
|
146
|
-
*
|
|
138
|
+
* If set, the I/O context will attempt to avoid buffering.
|
|
147
139
|
*
|
|
148
|
-
*
|
|
149
|
-
* instead of going through a buffer, and avio_seek will always call
|
|
150
|
-
* the underlying seek function directly.
|
|
140
|
+
* Direct mapping to AVIOContext->direct.
|
|
151
141
|
*/
|
|
152
142
|
get direct() {
|
|
153
143
|
return this.native.direct;
|
|
@@ -156,390 +146,368 @@ export class IOContext extends OptionMember {
|
|
|
156
146
|
this.native.direct = value;
|
|
157
147
|
}
|
|
158
148
|
/**
|
|
159
|
-
*
|
|
149
|
+
* Current position.
|
|
160
150
|
*
|
|
161
|
-
*
|
|
151
|
+
* Current byte position in the stream.
|
|
162
152
|
*
|
|
163
|
-
*
|
|
153
|
+
* Direct mapping to AVIOContext->pos.
|
|
164
154
|
*/
|
|
165
155
|
get pos() {
|
|
166
156
|
return this.native.pos;
|
|
167
157
|
}
|
|
168
158
|
/**
|
|
169
|
-
*
|
|
159
|
+
* Internal buffer size.
|
|
170
160
|
*
|
|
171
|
-
*
|
|
161
|
+
* Size of the internal buffer used for I/O operations.
|
|
172
162
|
*
|
|
173
|
-
*
|
|
163
|
+
* Direct mapping to AVIOContext->buffer_size.
|
|
174
164
|
*/
|
|
175
165
|
get bufferSize() {
|
|
176
166
|
return this.native.bufferSize;
|
|
177
167
|
}
|
|
178
168
|
/**
|
|
179
|
-
*
|
|
169
|
+
* Write flag.
|
|
180
170
|
*
|
|
181
|
-
*
|
|
171
|
+
* True if opened for writing, false for reading.
|
|
182
172
|
*
|
|
183
|
-
*
|
|
173
|
+
* Direct mapping to AVIOContext->write_flag.
|
|
184
174
|
*/
|
|
185
175
|
get writeFlag() {
|
|
186
176
|
return this.native.writeFlag;
|
|
187
177
|
}
|
|
188
178
|
/**
|
|
189
|
-
*
|
|
179
|
+
* Allocate I/O context with buffer.
|
|
190
180
|
*
|
|
191
|
-
* Allocates
|
|
192
|
-
* For
|
|
181
|
+
* Allocates a basic I/O context with an internal buffer.
|
|
182
|
+
* For custom I/O, use allocContextWithCallbacks instead.
|
|
193
183
|
*
|
|
194
|
-
* Direct mapping to avio_alloc_context()
|
|
184
|
+
* Direct mapping to avio_alloc_context() without callbacks.
|
|
195
185
|
*
|
|
196
|
-
* @param bufferSize - Size of
|
|
197
|
-
* @param writeFlag - 1 for
|
|
186
|
+
* @param bufferSize - Size of internal buffer
|
|
187
|
+
* @param writeFlag - 1 for write, 0 for read
|
|
198
188
|
*
|
|
199
189
|
* @example
|
|
200
190
|
* ```typescript
|
|
201
|
-
* import { IOContext } from 'node-av';
|
|
202
|
-
*
|
|
203
191
|
* const io = new IOContext();
|
|
204
192
|
* io.allocContext(4096, 0); // 4KB buffer for reading
|
|
205
|
-
*
|
|
206
|
-
* // For writing
|
|
207
|
-
* const writeIO = new IOContext();
|
|
208
|
-
* writeIO.allocContext(8192, 1); // 8KB buffer for writing
|
|
209
193
|
* ```
|
|
210
194
|
*
|
|
211
|
-
* @see {@link allocContextWithCallbacks} For custom I/O
|
|
212
|
-
* @see {@link open2} For file I/O
|
|
195
|
+
* @see {@link allocContextWithCallbacks} For custom I/O
|
|
213
196
|
*/
|
|
214
197
|
allocContext(bufferSize, writeFlag) {
|
|
215
|
-
|
|
198
|
+
this.native.allocContext(bufferSize, writeFlag);
|
|
216
199
|
}
|
|
217
200
|
/**
|
|
218
|
-
* Allocate
|
|
201
|
+
* Allocate I/O context with custom callbacks.
|
|
219
202
|
*
|
|
220
|
-
* Creates
|
|
203
|
+
* Creates an I/O context with custom read, write, and seek callbacks.
|
|
204
|
+
* Enables implementing custom protocols or data sources.
|
|
221
205
|
*
|
|
222
|
-
*
|
|
223
|
-
* return immediately. If you need async operations, buffer the data in JavaScript first.
|
|
206
|
+
* Direct mapping to avio_alloc_context() with callbacks.
|
|
224
207
|
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
* @param
|
|
228
|
-
* @param
|
|
229
|
-
* @param
|
|
230
|
-
* @param writeCallback - Synchronous callback for writing data. Returns bytes written or negative error code
|
|
231
|
-
* @param seekCallback - Synchronous callback for seeking. Returns new position or negative error code
|
|
208
|
+
* @param bufferSize - Size of internal buffer
|
|
209
|
+
* @param writeFlag - 1 for write mode, 0 for read mode
|
|
210
|
+
* @param readCallback - Function to read data (null for write-only)
|
|
211
|
+
* @param writeCallback - Function to write data (null for read-only)
|
|
212
|
+
* @param seekCallback - Function to seek in stream (optional)
|
|
232
213
|
*
|
|
233
214
|
* @example
|
|
234
215
|
* ```typescript
|
|
235
|
-
* import {
|
|
216
|
+
* import { AVSEEK_SET, AVSEEK_CUR, AVSEEK_END, AVSEEK_SIZE } from 'node-av/constants';
|
|
236
217
|
*
|
|
237
|
-
* const
|
|
218
|
+
* const data = Buffer.from('Custom data source');
|
|
238
219
|
* let position = 0;
|
|
239
|
-
* const buffer = Buffer.from('example data');
|
|
240
220
|
*
|
|
241
221
|
* io.allocContextWithCallbacks(
|
|
242
222
|
* 4096,
|
|
243
|
-
* 0,
|
|
223
|
+
* 0, // Read mode
|
|
244
224
|
* (size) => {
|
|
245
|
-
* // Read
|
|
246
|
-
*
|
|
225
|
+
* // Read callback
|
|
226
|
+
* if (position >= data.length) return -541; // EOF
|
|
227
|
+
* const chunk = data.subarray(position, position + size);
|
|
228
|
+
* position += chunk.length;
|
|
229
|
+
* return chunk;
|
|
247
230
|
* },
|
|
248
231
|
* null,
|
|
249
232
|
* (offset, whence) => {
|
|
250
|
-
* // Seek
|
|
233
|
+
* // Seek callback
|
|
234
|
+
* if (whence === AVSEEK_SIZE) return BigInt(data.length);
|
|
251
235
|
* if (whence === AVSEEK_SET) position = Number(offset);
|
|
252
236
|
* else if (whence === AVSEEK_CUR) position += Number(offset);
|
|
253
|
-
* else if (whence === AVSEEK_END) position =
|
|
237
|
+
* else if (whence === AVSEEK_END) position = data.length + Number(offset);
|
|
254
238
|
* return BigInt(position);
|
|
255
239
|
* }
|
|
256
240
|
* );
|
|
257
241
|
* ```
|
|
258
242
|
*
|
|
259
|
-
* @see {@link allocContext} For simple
|
|
243
|
+
* @see {@link allocContext} For simple allocation
|
|
260
244
|
*/
|
|
261
245
|
allocContextWithCallbacks(bufferSize, writeFlag, readCallback, writeCallback, seekCallback) {
|
|
262
|
-
|
|
246
|
+
this.native.allocContextWithCallbacks(bufferSize, writeFlag, readCallback ?? undefined, writeCallback ?? undefined, seekCallback ?? undefined);
|
|
263
247
|
}
|
|
264
248
|
/**
|
|
265
|
-
* Free
|
|
249
|
+
* Free I/O context.
|
|
266
250
|
*
|
|
267
|
-
*
|
|
251
|
+
* Releases the I/O context and its resources.
|
|
252
|
+
* The context becomes invalid after calling this.
|
|
253
|
+
*
|
|
254
|
+
* Direct mapping to avio_context_free().
|
|
268
255
|
*
|
|
269
256
|
* @example
|
|
270
257
|
* ```typescript
|
|
271
|
-
* import { IOContext } from 'node-av';
|
|
272
|
-
*
|
|
273
258
|
* io.freeContext();
|
|
274
|
-
* //
|
|
259
|
+
* // Context is now invalid
|
|
275
260
|
* ```
|
|
276
261
|
*/
|
|
277
262
|
freeContext() {
|
|
278
|
-
|
|
263
|
+
this.native.freeContext();
|
|
279
264
|
}
|
|
280
265
|
/**
|
|
281
|
-
* Open
|
|
266
|
+
* Open resource for I/O.
|
|
282
267
|
*
|
|
283
|
-
* Opens a URL
|
|
284
|
-
* Automatically
|
|
268
|
+
* Opens a URL or file for reading or writing.
|
|
269
|
+
* Automatically selects the appropriate protocol handler.
|
|
285
270
|
*
|
|
286
|
-
* Direct mapping to avio_open2()
|
|
287
|
-
*
|
|
288
|
-
* @param url - URL to open (file://, http://, https://, etc.)
|
|
289
|
-
* @param flags - I/O flags (AVIO_FLAG_READ, AVIO_FLAG_WRITE, etc.)
|
|
271
|
+
* Direct mapping to avio_open2().
|
|
290
272
|
*
|
|
273
|
+
* @param url - URL or file path to open
|
|
274
|
+
* @param flags - Open flags (AVIO_FLAG_READ, AVIO_FLAG_WRITE, etc.)
|
|
291
275
|
* @returns 0 on success, negative AVERROR on error:
|
|
292
|
-
* -
|
|
293
|
-
* -
|
|
294
|
-
* -
|
|
295
|
-
* - AVERROR(EIO): I/O error
|
|
296
|
-
* - AVERROR(ENOMEM): Memory allocation failure
|
|
297
|
-
* - <0: Other protocol-specific errors
|
|
276
|
+
* - AVERROR_ENOENT: File not found
|
|
277
|
+
* - AVERROR_EACCES: Permission denied
|
|
278
|
+
* - AVERROR_ENOMEM: Memory allocation failure
|
|
298
279
|
*
|
|
299
280
|
* @example
|
|
300
281
|
* ```typescript
|
|
301
|
-
* import {
|
|
282
|
+
* import { FFmpegError } from 'node-av';
|
|
302
283
|
* import { AVIO_FLAG_READ, AVIO_FLAG_WRITE } from 'node-av/constants';
|
|
303
284
|
*
|
|
304
|
-
* // Open
|
|
305
|
-
* const io = new IOContext();
|
|
285
|
+
* // Open for reading
|
|
306
286
|
* const ret = await io.open2('input.mp4', AVIO_FLAG_READ);
|
|
307
287
|
* FFmpegError.throwIfError(ret, 'open2');
|
|
308
288
|
*
|
|
309
|
-
* // Open
|
|
310
|
-
* const
|
|
311
|
-
*
|
|
312
|
-
* FFmpegError.throwIfError(writeRet, 'open2');
|
|
313
|
-
*
|
|
314
|
-
* // Open network stream
|
|
315
|
-
* const streamIO = new IOContext();
|
|
316
|
-
* const streamRet = await streamIO.open2('http://example.com/stream.m3u8', AVIO_FLAG_READ);
|
|
317
|
-
* FFmpegError.throwIfError(streamRet, 'open2');
|
|
289
|
+
* // Open for writing
|
|
290
|
+
* const ret2 = await io.open2('output.mp4', AVIO_FLAG_WRITE);
|
|
291
|
+
* FFmpegError.throwIfError(ret2, 'open2');
|
|
318
292
|
* ```
|
|
319
293
|
*
|
|
320
|
-
* @see {@link closep} To close
|
|
294
|
+
* @see {@link closep} To close after use
|
|
321
295
|
*/
|
|
322
296
|
async open2(url, flags = AVIO_FLAG_READ) {
|
|
323
|
-
return this.native.open2(url, flags);
|
|
297
|
+
return await this.native.open2(url, flags);
|
|
324
298
|
}
|
|
325
299
|
/**
|
|
326
|
-
* Close
|
|
300
|
+
* Close I/O context.
|
|
327
301
|
*
|
|
328
|
-
*
|
|
302
|
+
* Closes the I/O context and releases associated resources.
|
|
303
|
+
* Flushes any buffered data before closing.
|
|
329
304
|
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
* - <0: Other errors
|
|
305
|
+
* Direct mapping to avio_closep().
|
|
306
|
+
*
|
|
307
|
+
* @returns 0 on success, negative AVERROR on error
|
|
334
308
|
*
|
|
335
309
|
* @example
|
|
336
310
|
* ```typescript
|
|
337
|
-
* import { FFmpegError } from 'node-av';
|
|
338
|
-
*
|
|
339
311
|
* const ret = await io.closep();
|
|
340
|
-
*
|
|
341
|
-
*
|
|
312
|
+
* if (ret < 0) {
|
|
313
|
+
* console.error('Error closing I/O context');
|
|
314
|
+
* }
|
|
342
315
|
* ```
|
|
316
|
+
*
|
|
317
|
+
* @see {@link open2} To open resources
|
|
343
318
|
*/
|
|
344
319
|
async closep() {
|
|
345
|
-
return this.native.closep();
|
|
320
|
+
return await this.native.closep();
|
|
346
321
|
}
|
|
347
322
|
/**
|
|
348
|
-
* Read
|
|
323
|
+
* Read data from I/O context.
|
|
349
324
|
*
|
|
350
|
-
*
|
|
325
|
+
* Reads up to the specified number of bytes from the stream.
|
|
351
326
|
*
|
|
352
|
-
*
|
|
327
|
+
* Direct mapping to avio_read().
|
|
353
328
|
*
|
|
354
|
-
* @
|
|
355
|
-
*
|
|
329
|
+
* @param size - Maximum number of bytes to read
|
|
330
|
+
* @returns Buffer with data, or error code if negative:
|
|
356
331
|
* - AVERROR_EOF: End of file reached
|
|
357
|
-
* -
|
|
358
|
-
* - <0: Other errors
|
|
332
|
+
* - AVERROR_EIO: I/O error
|
|
359
333
|
*
|
|
360
334
|
* @example
|
|
361
335
|
* ```typescript
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
* const data = await io.read(1024);
|
|
365
|
-
* if (typeof data === 'number' && data < 0) {
|
|
366
|
-
* if (data === AVERROR_EOF) {
|
|
367
|
-
* console.log('End of file');
|
|
368
|
-
* } else {
|
|
369
|
-
* FFmpegError.throwIfError(data, 'read');
|
|
370
|
-
* }
|
|
371
|
-
* } else {
|
|
372
|
-
* // Process buffer
|
|
336
|
+
* const data = await io.read(4096);
|
|
337
|
+
* if (data instanceof Buffer) {
|
|
373
338
|
* console.log(`Read ${data.length} bytes`);
|
|
339
|
+
* } else {
|
|
340
|
+
* console.error(`Read error: ${data}`);
|
|
374
341
|
* }
|
|
375
342
|
* ```
|
|
343
|
+
*
|
|
344
|
+
* @see {@link write} For writing data
|
|
376
345
|
*/
|
|
377
346
|
async read(size) {
|
|
378
|
-
return this.native.read(size);
|
|
347
|
+
return await this.native.read(size);
|
|
379
348
|
}
|
|
380
349
|
/**
|
|
381
|
-
* Write
|
|
350
|
+
* Write data to I/O context.
|
|
351
|
+
*
|
|
352
|
+
* Writes buffer data to the stream.
|
|
382
353
|
*
|
|
383
|
-
* Direct mapping to avio_write()
|
|
354
|
+
* Direct mapping to avio_write().
|
|
384
355
|
*
|
|
385
|
-
* @param buffer -
|
|
356
|
+
* @param buffer - Data to write
|
|
386
357
|
*
|
|
387
358
|
* @example
|
|
388
359
|
* ```typescript
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
* const data = Buffer.from('Hello, World!');
|
|
360
|
+
* const data = Buffer.from('Hello World');
|
|
392
361
|
* await io.write(data);
|
|
393
|
-
* // Data written successfully
|
|
394
362
|
* ```
|
|
395
363
|
*
|
|
396
|
-
* @
|
|
364
|
+
* @see {@link read} For reading data
|
|
365
|
+
* @see {@link flush} To flush buffers
|
|
397
366
|
*/
|
|
398
367
|
async write(buffer) {
|
|
399
|
-
|
|
368
|
+
await this.native.write(buffer);
|
|
400
369
|
}
|
|
401
370
|
/**
|
|
402
|
-
* Seek to
|
|
371
|
+
* Seek to position in stream.
|
|
403
372
|
*
|
|
404
|
-
*
|
|
373
|
+
* Changes the current position in the stream.
|
|
374
|
+
* Not all streams support seeking.
|
|
405
375
|
*
|
|
406
|
-
*
|
|
407
|
-
* @param whence - AVSEEK_SET (0), AVSEEK_CUR (1), AVSEEK_END (2), or AVSEEK_SIZE (0x10000)
|
|
376
|
+
* Direct mapping to avio_seek().
|
|
408
377
|
*
|
|
409
|
-
* @
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
* -
|
|
413
|
-
* -
|
|
378
|
+
* @param offset - Byte offset to seek to
|
|
379
|
+
* @param whence - Seek origin (AVSEEK_SET, AVSEEK_CUR, AVSEEK_END)
|
|
380
|
+
* @returns New position, or negative AVERROR on error:
|
|
381
|
+
* - AVERROR_EINVAL: Invalid arguments
|
|
382
|
+
* - AVERROR_ENOSYS: Seeking not supported
|
|
414
383
|
*
|
|
415
384
|
* @example
|
|
416
385
|
* ```typescript
|
|
417
|
-
* import {
|
|
386
|
+
* import { AVSEEK_SET, AVSEEK_CUR, AVSEEK_END } from 'node-av/constants';
|
|
418
387
|
*
|
|
419
|
-
* // Seek to
|
|
420
|
-
* const
|
|
421
|
-
* FFmpegError.throwIfError(pos < 0n ? Number(pos) : 0, 'seek');
|
|
388
|
+
* // Seek to absolute position
|
|
389
|
+
* const pos1 = await io.seek(1024n, AVSEEK_SET);
|
|
422
390
|
*
|
|
423
|
-
* // Seek to
|
|
424
|
-
* const
|
|
425
|
-
* FFmpegError.throwIfError(endPos < 0n ? Number(endPos) : 0, 'seek');
|
|
391
|
+
* // Seek relative to current position
|
|
392
|
+
* const pos2 = await io.seek(512n, AVSEEK_CUR);
|
|
426
393
|
*
|
|
427
|
-
* //
|
|
428
|
-
* const
|
|
429
|
-
* console.log(`File size: ${size} bytes`);
|
|
394
|
+
* // Seek relative to end
|
|
395
|
+
* const pos3 = await io.seek(-1024n, AVSEEK_END);
|
|
430
396
|
* ```
|
|
397
|
+
*
|
|
398
|
+
* @see {@link tell} To get current position
|
|
399
|
+
* @see {@link skip} For relative seeking
|
|
431
400
|
*/
|
|
432
401
|
async seek(offset, whence) {
|
|
433
|
-
return this.native.seek(offset, whence);
|
|
402
|
+
return await this.native.seek(offset, whence);
|
|
434
403
|
}
|
|
435
404
|
/**
|
|
436
|
-
* Get
|
|
405
|
+
* Get stream size.
|
|
437
406
|
*
|
|
438
|
-
*
|
|
407
|
+
* Returns the total size of the stream in bytes.
|
|
408
|
+
* Not all streams have a known size.
|
|
439
409
|
*
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
* -
|
|
410
|
+
* Direct mapping to avio_size().
|
|
411
|
+
*
|
|
412
|
+
* @returns Size in bytes, or negative AVERROR if unknown:
|
|
413
|
+
* - AVERROR_ENOSYS: Size not available
|
|
444
414
|
*
|
|
445
415
|
* @example
|
|
446
416
|
* ```typescript
|
|
447
|
-
* import { FFmpegError } from 'node-av';
|
|
448
|
-
*
|
|
449
417
|
* const size = await io.size();
|
|
450
|
-
* if (size
|
|
451
|
-
*
|
|
452
|
-
* console.error('Cannot determine file size');
|
|
418
|
+
* if (size >= 0n) {
|
|
419
|
+
* console.log(`Stream size: ${size} bytes`);
|
|
453
420
|
* } else {
|
|
454
|
-
* console.log(
|
|
421
|
+
* console.log('Stream size unknown');
|
|
455
422
|
* }
|
|
456
423
|
* ```
|
|
457
424
|
*/
|
|
458
425
|
async size() {
|
|
459
|
-
return this.native.size();
|
|
426
|
+
return await this.native.size();
|
|
460
427
|
}
|
|
461
428
|
/**
|
|
462
|
-
*
|
|
429
|
+
* Flush buffered data.
|
|
430
|
+
*
|
|
431
|
+
* Forces any buffered data to be written to the underlying resource.
|
|
463
432
|
*
|
|
464
|
-
* Direct mapping to avio_flush()
|
|
433
|
+
* Direct mapping to avio_flush().
|
|
465
434
|
*
|
|
466
435
|
* @example
|
|
467
436
|
* ```typescript
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
* await io.flush();
|
|
471
|
-
* // All buffered data has been written
|
|
437
|
+
* await io.write(data);
|
|
438
|
+
* await io.flush(); // Ensure data is written
|
|
472
439
|
* ```
|
|
440
|
+
*
|
|
441
|
+
* @see {@link write} For writing data
|
|
473
442
|
*/
|
|
474
443
|
async flush() {
|
|
475
|
-
|
|
444
|
+
await this.native.flush();
|
|
476
445
|
}
|
|
477
446
|
/**
|
|
478
|
-
* Skip
|
|
447
|
+
* Skip bytes in stream.
|
|
479
448
|
*
|
|
480
|
-
*
|
|
449
|
+
* Advances the position by the specified offset.
|
|
450
|
+
* More efficient than reading and discarding data.
|
|
481
451
|
*
|
|
482
|
-
*
|
|
452
|
+
* Direct mapping to avio_skip().
|
|
483
453
|
*
|
|
484
|
-
* @
|
|
485
|
-
*
|
|
486
|
-
* - AVERROR_EOF: End of file reached
|
|
487
|
-
* - <0: Other errors
|
|
454
|
+
* @param offset - Number of bytes to skip
|
|
455
|
+
* @returns New position after skipping
|
|
488
456
|
*
|
|
489
457
|
* @example
|
|
490
458
|
* ```typescript
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
* // Skip 1024 bytes
|
|
459
|
+
* // Skip 1024 bytes forward
|
|
494
460
|
* const newPos = await io.skip(1024n);
|
|
495
|
-
* FFmpegError.throwIfError(newPos < 0n ? Number(newPos) : 0, 'skip');
|
|
496
461
|
* console.log(`New position: ${newPos}`);
|
|
497
462
|
* ```
|
|
463
|
+
*
|
|
464
|
+
* @see {@link seek} For absolute positioning
|
|
498
465
|
*/
|
|
499
466
|
async skip(offset) {
|
|
500
|
-
return this.native.skip(offset);
|
|
467
|
+
return await this.native.skip(offset);
|
|
501
468
|
}
|
|
502
469
|
/**
|
|
503
|
-
* Get
|
|
470
|
+
* Get current position.
|
|
471
|
+
*
|
|
472
|
+
* Returns the current byte position in the stream.
|
|
504
473
|
*
|
|
505
|
-
* Direct mapping to avio_tell()
|
|
474
|
+
* Direct mapping to avio_tell().
|
|
506
475
|
*
|
|
507
476
|
* @returns Current position in bytes
|
|
508
477
|
*
|
|
509
478
|
* @example
|
|
510
479
|
* ```typescript
|
|
511
|
-
* import { IOContext } from 'node-av';
|
|
512
|
-
*
|
|
513
480
|
* const position = io.tell();
|
|
514
|
-
* console.log(`Current position: ${position}
|
|
481
|
+
* console.log(`Current position: ${position}`);
|
|
515
482
|
* ```
|
|
483
|
+
*
|
|
484
|
+
* @see {@link seek} To change position
|
|
516
485
|
*/
|
|
517
486
|
tell() {
|
|
518
487
|
return this.native.tell();
|
|
519
488
|
}
|
|
520
489
|
/**
|
|
521
|
-
* Get the native
|
|
490
|
+
* Get the underlying native IOContext object.
|
|
522
491
|
*
|
|
523
|
-
* @
|
|
524
|
-
*
|
|
492
|
+
* @returns The native IOContext binding object
|
|
493
|
+
*
|
|
494
|
+
* @internal
|
|
525
495
|
*/
|
|
526
496
|
getNative() {
|
|
527
497
|
return this.native;
|
|
528
498
|
}
|
|
529
499
|
/**
|
|
530
|
-
* Dispose of the I/O context.
|
|
500
|
+
* Dispose of the I/O context asynchronously.
|
|
531
501
|
*
|
|
532
502
|
* Implements the AsyncDisposable interface for automatic cleanup.
|
|
533
|
-
*
|
|
503
|
+
* Closes the context and releases resources.
|
|
534
504
|
*
|
|
535
505
|
* @example
|
|
536
506
|
* ```typescript
|
|
537
|
-
* import { IOContext, AVIO_FLAG_READ } from 'node-av';
|
|
538
|
-
*
|
|
539
507
|
* {
|
|
540
508
|
* await using io = new IOContext();
|
|
541
|
-
* await io.open2('
|
|
542
|
-
* // ...
|
|
509
|
+
* await io.open2('input.mp4');
|
|
510
|
+
* // Use io...
|
|
543
511
|
* } // Automatically closed when leaving scope
|
|
544
512
|
* ```
|
|
545
513
|
*/
|