node-av 0.0.1
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/CHANGELOG.md +8 -0
- package/LICENSE.md +22 -0
- package/README.md +377 -0
- package/binding.gyp +78 -0
- package/dist/api/bitstream-filter.d.ts +246 -0
- package/dist/api/bitstream-filter.js +369 -0
- package/dist/api/bitstream-filter.js.map +1 -0
- package/dist/api/decoder.d.ts +257 -0
- package/dist/api/decoder.js +424 -0
- package/dist/api/decoder.js.map +1 -0
- package/dist/api/encoder.d.ts +298 -0
- package/dist/api/encoder.js +574 -0
- package/dist/api/encoder.js.map +1 -0
- package/dist/api/filter.d.ts +457 -0
- package/dist/api/filter.js +876 -0
- package/dist/api/filter.js.map +1 -0
- package/dist/api/hardware.d.ts +318 -0
- package/dist/api/hardware.js +558 -0
- package/dist/api/hardware.js.map +1 -0
- package/dist/api/index.d.ts +12 -0
- package/dist/api/index.js +20 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/io-stream.d.ts +109 -0
- package/dist/api/io-stream.js +124 -0
- package/dist/api/io-stream.js.map +1 -0
- package/dist/api/media-input.d.ts +295 -0
- package/dist/api/media-input.js +456 -0
- package/dist/api/media-input.js.map +1 -0
- package/dist/api/media-output.d.ts +274 -0
- package/dist/api/media-output.js +486 -0
- package/dist/api/media-output.js.map +1 -0
- package/dist/api/pipeline.d.ts +117 -0
- package/dist/api/pipeline.js +836 -0
- package/dist/api/pipeline.js.map +1 -0
- package/dist/api/types.d.ts +440 -0
- package/dist/api/types.js +2 -0
- package/dist/api/types.js.map +1 -0
- package/dist/api/utilities/audio-sample.d.ts +115 -0
- package/dist/api/utilities/audio-sample.js +110 -0
- package/dist/api/utilities/audio-sample.js.map +1 -0
- package/dist/api/utilities/channel-layout.d.ts +83 -0
- package/dist/api/utilities/channel-layout.js +87 -0
- package/dist/api/utilities/channel-layout.js.map +1 -0
- package/dist/api/utilities/image.d.ts +177 -0
- package/dist/api/utilities/image.js +183 -0
- package/dist/api/utilities/image.js.map +1 -0
- package/dist/api/utilities/index.d.ts +8 -0
- package/dist/api/utilities/index.js +17 -0
- package/dist/api/utilities/index.js.map +1 -0
- package/dist/api/utilities/media-type.d.ts +56 -0
- package/dist/api/utilities/media-type.js +60 -0
- package/dist/api/utilities/media-type.js.map +1 -0
- package/dist/api/utilities/pixel-format.d.ts +94 -0
- package/dist/api/utilities/pixel-format.js +102 -0
- package/dist/api/utilities/pixel-format.js.map +1 -0
- package/dist/api/utilities/sample-format.d.ts +132 -0
- package/dist/api/utilities/sample-format.js +144 -0
- package/dist/api/utilities/sample-format.js.map +1 -0
- package/dist/api/utilities/streaming.d.ts +104 -0
- package/dist/api/utilities/streaming.js +137 -0
- package/dist/api/utilities/streaming.js.map +1 -0
- package/dist/api/utilities/timestamp.d.ts +187 -0
- package/dist/api/utilities/timestamp.js +200 -0
- package/dist/api/utilities/timestamp.js.map +1 -0
- package/dist/api/utils.d.ts +61 -0
- package/dist/api/utils.js +330 -0
- package/dist/api/utils.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/audio-fifo.d.ts +339 -0
- package/dist/lib/audio-fifo.js +365 -0
- package/dist/lib/audio-fifo.js.map +1 -0
- package/dist/lib/binding.d.ts +192 -0
- package/dist/lib/binding.js +70 -0
- package/dist/lib/binding.js.map +1 -0
- package/dist/lib/bitstream-filter-context.d.ts +345 -0
- package/dist/lib/bitstream-filter-context.js +407 -0
- package/dist/lib/bitstream-filter-context.js.map +1 -0
- package/dist/lib/bitstream-filter.d.ts +124 -0
- package/dist/lib/bitstream-filter.js +138 -0
- package/dist/lib/bitstream-filter.js.map +1 -0
- package/dist/lib/channel-layouts.d.ts +51 -0
- package/dist/lib/channel-layouts.js +55 -0
- package/dist/lib/channel-layouts.js.map +1 -0
- package/dist/lib/codec-context.d.ts +763 -0
- package/dist/lib/codec-context.js +974 -0
- package/dist/lib/codec-context.js.map +1 -0
- package/dist/lib/codec-parameters.d.ts +362 -0
- package/dist/lib/codec-parameters.js +460 -0
- package/dist/lib/codec-parameters.js.map +1 -0
- package/dist/lib/codec-parser.d.ts +185 -0
- package/dist/lib/codec-parser.js +193 -0
- package/dist/lib/codec-parser.js.map +1 -0
- package/dist/lib/codec.d.ts +432 -0
- package/dist/lib/codec.js +492 -0
- package/dist/lib/codec.js.map +1 -0
- package/dist/lib/constants.d.ts +2037 -0
- package/dist/lib/constants.js +1659 -0
- package/dist/lib/constants.js.map +1 -0
- package/dist/lib/dictionary.d.ts +371 -0
- package/dist/lib/dictionary.js +406 -0
- package/dist/lib/dictionary.js.map +1 -0
- package/dist/lib/error.d.ts +216 -0
- package/dist/lib/error.js +254 -0
- package/dist/lib/error.js.map +1 -0
- package/dist/lib/filter-context.d.ts +445 -0
- package/dist/lib/filter-context.js +505 -0
- package/dist/lib/filter-context.js.map +1 -0
- package/dist/lib/filter-graph.d.ts +556 -0
- package/dist/lib/filter-graph.js +608 -0
- package/dist/lib/filter-graph.js.map +1 -0
- package/dist/lib/filter-inout.d.ts +205 -0
- package/dist/lib/filter-inout.js +264 -0
- package/dist/lib/filter-inout.js.map +1 -0
- package/dist/lib/filter.d.ts +231 -0
- package/dist/lib/filter.js +260 -0
- package/dist/lib/filter.js.map +1 -0
- package/dist/lib/format-context.d.ts +798 -0
- package/dist/lib/format-context.js +845 -0
- package/dist/lib/format-context.js.map +1 -0
- package/dist/lib/frame.d.ts +784 -0
- package/dist/lib/frame.js +933 -0
- package/dist/lib/frame.js.map +1 -0
- package/dist/lib/hardware-device-context.d.ts +407 -0
- package/dist/lib/hardware-device-context.js +429 -0
- package/dist/lib/hardware-device-context.js.map +1 -0
- package/dist/lib/hardware-frames-context.d.ts +374 -0
- package/dist/lib/hardware-frames-context.js +430 -0
- package/dist/lib/hardware-frames-context.js.map +1 -0
- package/dist/lib/index.d.ts +31 -0
- package/dist/lib/index.js +54 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/input-format.d.ts +216 -0
- package/dist/lib/input-format.js +246 -0
- package/dist/lib/input-format.js.map +1 -0
- package/dist/lib/io-context.d.ts +495 -0
- package/dist/lib/io-context.js +550 -0
- package/dist/lib/io-context.js.map +1 -0
- package/dist/lib/log.d.ts +201 -0
- package/dist/lib/log.js +219 -0
- package/dist/lib/log.js.map +1 -0
- package/dist/lib/native-types.d.ts +719 -0
- package/dist/lib/native-types.js +2 -0
- package/dist/lib/native-types.js.map +1 -0
- package/dist/lib/option.d.ts +589 -0
- package/dist/lib/option.js +853 -0
- package/dist/lib/option.js.map +1 -0
- package/dist/lib/output-format.d.ts +179 -0
- package/dist/lib/output-format.js +205 -0
- package/dist/lib/output-format.js.map +1 -0
- package/dist/lib/packet.d.ts +487 -0
- package/dist/lib/packet.js +558 -0
- package/dist/lib/packet.js.map +1 -0
- package/dist/lib/rational.d.ts +210 -0
- package/dist/lib/rational.js +233 -0
- package/dist/lib/rational.js.map +1 -0
- package/dist/lib/software-resample-context.d.ts +572 -0
- package/dist/lib/software-resample-context.js +610 -0
- package/dist/lib/software-resample-context.js.map +1 -0
- package/dist/lib/software-scale-context.d.ts +290 -0
- package/dist/lib/software-scale-context.js +308 -0
- package/dist/lib/software-scale-context.js.map +1 -0
- package/dist/lib/stream.d.ts +322 -0
- package/dist/lib/stream.js +408 -0
- package/dist/lib/stream.js.map +1 -0
- package/dist/lib/types.d.ts +59 -0
- package/dist/lib/types.js +8 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/lib/utilities.d.ts +346 -0
- package/dist/lib/utilities.js +424 -0
- package/dist/lib/utilities.js.map +1 -0
- package/install/check.js +113 -0
- package/install/ffmpeg.js +163 -0
- package/package.json +107 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encoder - High-level wrapper for media encoding
|
|
3
|
+
*
|
|
4
|
+
* Simplifies FFmpeg's encoding API with automatic codec selection,
|
|
5
|
+
* parameter configuration, and packet management.
|
|
6
|
+
*
|
|
7
|
+
* Handles codec initialization, frame encoding, and packet output.
|
|
8
|
+
* Supports hardware acceleration and zero-copy transcoding.
|
|
9
|
+
*
|
|
10
|
+
* @module api/encoder
|
|
11
|
+
*/
|
|
12
|
+
import { CodecContext, Packet } from '../lib/index.js';
|
|
13
|
+
import { Stream } from '../lib/stream.js';
|
|
14
|
+
import type { AVPixelFormat, Frame } from '../lib/index.js';
|
|
15
|
+
import type { EncoderOptions, StreamInfo } from './types.js';
|
|
16
|
+
/**
|
|
17
|
+
* High-level encoder for media streams.
|
|
18
|
+
*
|
|
19
|
+
* Handles codec initialization, frame encoding, and packet output.
|
|
20
|
+
* Supports various codecs with flexible configuration options.
|
|
21
|
+
*
|
|
22
|
+
* Manages codec context lifecycle and provides automatic cleanup.
|
|
23
|
+
* Supports hardware acceleration with shared frames context for zero-copy.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // Create H.264 encoder
|
|
28
|
+
* const encoder = await Encoder.create('libx264', {
|
|
29
|
+
* width: 1920,
|
|
30
|
+
* height: 1080,
|
|
31
|
+
* pixelFormat: 'yuv420p',
|
|
32
|
+
* bitrate: '5M',
|
|
33
|
+
* gopSize: 60,
|
|
34
|
+
* options: {
|
|
35
|
+
* preset: 'fast',
|
|
36
|
+
* crf: 23
|
|
37
|
+
* }
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* // Encode frames
|
|
41
|
+
* const packet = await encoder.encode(frame);
|
|
42
|
+
* if (packet) {
|
|
43
|
+
* // Write packet to output
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* // Flush encoder
|
|
47
|
+
* let packet;
|
|
48
|
+
* while ((packet = await encoder.flush()) !== null) {
|
|
49
|
+
* // Process final packets
|
|
50
|
+
* }
|
|
51
|
+
* encoder.close();
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // With hardware acceleration
|
|
57
|
+
* const hw = await HardwareContext.auto();
|
|
58
|
+
* const encoder = await Encoder.create('h264_videotoolbox', {
|
|
59
|
+
* width: 1920,
|
|
60
|
+
* height: 1080,
|
|
61
|
+
* pixelFormat: 'nv12',
|
|
62
|
+
* bitrate: '5M',
|
|
63
|
+
* hardware: hw
|
|
64
|
+
* });
|
|
65
|
+
* // ... use encoder
|
|
66
|
+
* encoder.close(); // Also disposes hardware
|
|
67
|
+
* hw?.dispose(); // Safe to call again (no-op)
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare class Encoder implements Disposable {
|
|
71
|
+
private codecContext;
|
|
72
|
+
private packet;
|
|
73
|
+
private codecName;
|
|
74
|
+
private isOpen;
|
|
75
|
+
private supportedFormats;
|
|
76
|
+
private preferredFormat?;
|
|
77
|
+
private hardware?;
|
|
78
|
+
private isHardwareEncoder;
|
|
79
|
+
/**
|
|
80
|
+
* Private constructor - use Encoder.create() instead.
|
|
81
|
+
*
|
|
82
|
+
* Initializes the encoder with a codec context and allocates a packet buffer.
|
|
83
|
+
*
|
|
84
|
+
* @param codecContext - Initialized codec context
|
|
85
|
+
* @param codecName - Name of the codec
|
|
86
|
+
* @param hardware - Optional hardware context for late framesContext binding
|
|
87
|
+
*/
|
|
88
|
+
private constructor();
|
|
89
|
+
/**
|
|
90
|
+
* Create an encoder with specified codec and options.
|
|
91
|
+
*
|
|
92
|
+
* Factory method that handles codec discovery, context setup,
|
|
93
|
+
* and initialization.
|
|
94
|
+
*
|
|
95
|
+
* Uses avcodec_find_encoder_by_name() to locate the codec,
|
|
96
|
+
* configures the context with provided options, and opens it.
|
|
97
|
+
* Handles hardware setup including shared frames context for zero-copy.
|
|
98
|
+
*
|
|
99
|
+
* @param codecName - Name of codec (e.g., 'libx264', 'aac', 'libopus')
|
|
100
|
+
* @param input - Stream or StreamInfo to copy parameters from
|
|
101
|
+
* @param options - Encoder configuration options
|
|
102
|
+
*
|
|
103
|
+
* @returns Promise resolving to configured Encoder
|
|
104
|
+
*
|
|
105
|
+
* @throws {Error} If codec not found or configuration fails
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* // Video encoder from stream
|
|
110
|
+
* const videoStream = media.video();
|
|
111
|
+
* const videoEncoder = await Encoder.create('libx264', videoStream, {
|
|
112
|
+
* bitrate: '5M',
|
|
113
|
+
* gopSize: 60
|
|
114
|
+
* });
|
|
115
|
+
*
|
|
116
|
+
* // Audio encoder from stream
|
|
117
|
+
* const audioStream = media.audio();
|
|
118
|
+
* const audioEncoder = await Encoder.create('aac', audioStream, {
|
|
119
|
+
* bitrate: '192k'
|
|
120
|
+
* });
|
|
121
|
+
*
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
static create(codecName: string, input: Stream | StreamInfo, options?: EncoderOptions): Promise<Encoder>;
|
|
125
|
+
/**
|
|
126
|
+
* Check if encoder is open.
|
|
127
|
+
*/
|
|
128
|
+
get isEncoderOpen(): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Encode a frame and return a packet if available.
|
|
131
|
+
*
|
|
132
|
+
* Sends frame to encoder and attempts to receive a packet.
|
|
133
|
+
* May return null if encoder needs more data.
|
|
134
|
+
*
|
|
135
|
+
* Uses avcodec_send_frame() and avcodec_receive_packet() internally.
|
|
136
|
+
* The encoder may buffer frames before producing packets.
|
|
137
|
+
*
|
|
138
|
+
* @param frame - Frame to encode (or null to flush)
|
|
139
|
+
*
|
|
140
|
+
* @returns Promise resolving to Packet or null
|
|
141
|
+
*
|
|
142
|
+
* @throws {Error} If encoder is closed or encode fails
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const packet = await encoder.encode(frame);
|
|
147
|
+
* if (packet) {
|
|
148
|
+
* // Write packet to output
|
|
149
|
+
* await output.writePacket(packet);
|
|
150
|
+
* }
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
encode(frame: Frame | null): Promise<Packet | null>;
|
|
154
|
+
/**
|
|
155
|
+
* Async iterator that encodes frames and yields packets.
|
|
156
|
+
*
|
|
157
|
+
* Encodes all provided frames and yields resulting packets.
|
|
158
|
+
* Automatically handles encoder flushing at the end.
|
|
159
|
+
* Input frames are automatically freed after encoding.
|
|
160
|
+
*
|
|
161
|
+
* Processes frames in sequence, encoding each and yielding packets.
|
|
162
|
+
* After all frames are processed, flushes the encoder for remaining packets.
|
|
163
|
+
*
|
|
164
|
+
* IMPORTANT: The yielded packets MUST be freed by the caller!
|
|
165
|
+
* Input frames are automatically freed after processing.
|
|
166
|
+
*
|
|
167
|
+
* @param frames - Async iterable of frames to encode (will be freed automatically)
|
|
168
|
+
*
|
|
169
|
+
* @yields Encoded packets (ownership transferred to caller)
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* // Transcode video
|
|
174
|
+
* for await (const packet of encoder.packets(decoder.frames(media.packets()))) {
|
|
175
|
+
* await output.writePacket(packet);
|
|
176
|
+
* packet.free(); // Must free output packet
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
packets(frames: AsyncIterable<Frame>): AsyncGenerator<Packet>;
|
|
181
|
+
/**
|
|
182
|
+
* Flush encoder and get remaining packets.
|
|
183
|
+
*
|
|
184
|
+
* Sends null frame to trigger flush mode.
|
|
185
|
+
* Call repeatedly until it returns null.
|
|
186
|
+
*
|
|
187
|
+
* Uses avcodec_send_frame(NULL) to signal end of stream.
|
|
188
|
+
* Retrieves buffered packets from the encoder.
|
|
189
|
+
*
|
|
190
|
+
* @returns Promise resolving to Packet or null
|
|
191
|
+
*
|
|
192
|
+
* @throws {Error} If encoder is closed
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```typescript
|
|
196
|
+
* // Flush all remaining packets
|
|
197
|
+
* let packet;
|
|
198
|
+
* while ((packet = await encoder.flush()) !== null) {
|
|
199
|
+
* // Write final packets
|
|
200
|
+
* await output.writePacket(packet);
|
|
201
|
+
* }
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
flush(): Promise<Packet | null>;
|
|
205
|
+
/**
|
|
206
|
+
* Flush encoder and yield all remaining packets as a generator.
|
|
207
|
+
*
|
|
208
|
+
* More convenient than calling flush() in a loop.
|
|
209
|
+
* Automatically sends flush signal and yields all buffered packets.
|
|
210
|
+
*
|
|
211
|
+
* @returns Async generator of remaining packets
|
|
212
|
+
*
|
|
213
|
+
* @throws {Error} If encoder is closed
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* // Process all remaining packets with generator
|
|
218
|
+
* for await (const packet of encoder.flushPackets()) {
|
|
219
|
+
* await output.writePacket(packet, streamIdx);
|
|
220
|
+
* using _ = packet; // Auto cleanup
|
|
221
|
+
* }
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
flushPackets(): AsyncGenerator<Packet>;
|
|
225
|
+
/**
|
|
226
|
+
* Close encoder and free resources.
|
|
227
|
+
*
|
|
228
|
+
* After closing, the encoder cannot be used again.
|
|
229
|
+
*
|
|
230
|
+
* Frees the packet buffer and codec context.
|
|
231
|
+
* Note: Does NOT dispose the HardwareContext - caller is responsible for that.
|
|
232
|
+
*/
|
|
233
|
+
close(): void;
|
|
234
|
+
/**
|
|
235
|
+
* Get the codec name.
|
|
236
|
+
*/
|
|
237
|
+
getCodecName(): string;
|
|
238
|
+
/**
|
|
239
|
+
* Get codec context for advanced configuration.
|
|
240
|
+
*
|
|
241
|
+
* Use with caution - direct manipulation may cause issues.
|
|
242
|
+
*
|
|
243
|
+
* Provides access to the underlying AVCodecContext for advanced operations.
|
|
244
|
+
*
|
|
245
|
+
* @returns CodecContext or null if closed
|
|
246
|
+
*
|
|
247
|
+
* @internal
|
|
248
|
+
*/
|
|
249
|
+
getCodecContext(): CodecContext | null;
|
|
250
|
+
/**
|
|
251
|
+
* Get the preferred pixel format for this encoder.
|
|
252
|
+
*
|
|
253
|
+
* Returns the first supported format, which is usually the most efficient.
|
|
254
|
+
*
|
|
255
|
+
* @returns Preferred pixel format or null if not available
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```typescript
|
|
259
|
+
* const format = encoder.getPreferredPixelFormat();
|
|
260
|
+
* if (format) {
|
|
261
|
+
* console.log(`Encoder prefers format: ${format}`);
|
|
262
|
+
* }
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
getPreferredPixelFormat(): AVPixelFormat | null;
|
|
266
|
+
/**
|
|
267
|
+
* Get all supported pixel formats for this encoder.
|
|
268
|
+
*
|
|
269
|
+
* Returns a list of pixel formats that this encoder can accept.
|
|
270
|
+
*
|
|
271
|
+
* @returns Array of supported pixel formats
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* ```typescript
|
|
275
|
+
* const formats = encoder.getSupportedPixelFormats();
|
|
276
|
+
* console.log(`Encoder supports: ${formats.join(', ')}`);
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
getSupportedPixelFormats(): AVPixelFormat[];
|
|
280
|
+
/**
|
|
281
|
+
* Receive a packet from the encoder.
|
|
282
|
+
*
|
|
283
|
+
* Internal method to receive encoded packets.
|
|
284
|
+
*
|
|
285
|
+
* Uses avcodec_receive_packet() to get encoded packets from the codec.
|
|
286
|
+
* Clones the packet for the user to prevent internal buffer corruption.
|
|
287
|
+
*
|
|
288
|
+
* @returns Packet or null if no packet available
|
|
289
|
+
*/
|
|
290
|
+
private receivePacket;
|
|
291
|
+
/**
|
|
292
|
+
* Symbol.dispose for automatic cleanup.
|
|
293
|
+
*
|
|
294
|
+
* Implements the Disposable interface for automatic resource management.
|
|
295
|
+
* Calls close() to free all resources.
|
|
296
|
+
*/
|
|
297
|
+
[Symbol.dispose](): void;
|
|
298
|
+
}
|