node-av 5.2.4 → 6.0.0-beta.11

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.
Files changed (111) hide show
  1. package/README.md +15 -1
  2. package/dist/api/bitstream-filter.d.ts +110 -87
  3. package/dist/api/bitstream-filter.js +161 -103
  4. package/dist/api/bitstream-filter.js.map +1 -1
  5. package/dist/api/decoder.d.ts +177 -15
  6. package/dist/api/decoder.js +335 -28
  7. package/dist/api/decoder.js.map +1 -1
  8. package/dist/api/demuxer.d.ts +30 -24
  9. package/dist/api/demuxer.js +4 -5
  10. package/dist/api/demuxer.js.map +1 -1
  11. package/dist/api/device.js.map +1 -1
  12. package/dist/api/encoder-pool.d.ts +220 -0
  13. package/dist/api/encoder-pool.js +285 -0
  14. package/dist/api/encoder-pool.js.map +1 -0
  15. package/dist/api/encoder.d.ts +194 -7
  16. package/dist/api/encoder.js +431 -71
  17. package/dist/api/encoder.js.map +1 -1
  18. package/dist/api/filter-complex.d.ts +2 -1
  19. package/dist/api/filter-complex.js +11 -7
  20. package/dist/api/filter-complex.js.map +1 -1
  21. package/dist/api/filter-presets.d.ts +130 -654
  22. package/dist/api/filter-presets.js +180 -858
  23. package/dist/api/filter-presets.js.map +1 -1
  24. package/dist/api/filter.js +12 -9
  25. package/dist/api/filter.js.map +1 -1
  26. package/dist/api/fmp4-stream.js +1 -1
  27. package/dist/api/fmp4-stream.js.map +1 -1
  28. package/dist/api/index.d.ts +6 -6
  29. package/dist/api/index.js +8 -8
  30. package/dist/api/index.js.map +1 -1
  31. package/dist/api/muxer.d.ts +43 -15
  32. package/dist/api/muxer.js +79 -27
  33. package/dist/api/muxer.js.map +1 -1
  34. package/dist/api/pipeline.d.ts +50 -0
  35. package/dist/api/pipeline.js +138 -22
  36. package/dist/api/pipeline.js.map +1 -1
  37. package/dist/api/probe.d.ts +128 -0
  38. package/dist/api/probe.js +227 -0
  39. package/dist/api/probe.js.map +1 -0
  40. package/dist/api/rtp-stream.d.ts +14 -11
  41. package/dist/api/rtp-stream.js +23 -48
  42. package/dist/api/rtp-stream.js.map +1 -1
  43. package/dist/api/scaler.d.ts +431 -0
  44. package/dist/api/scaler.js +620 -0
  45. package/dist/api/scaler.js.map +1 -0
  46. package/dist/api/utilities/async-queue.d.ts +27 -1
  47. package/dist/api/utilities/async-queue.js +38 -3
  48. package/dist/api/utilities/async-queue.js.map +1 -1
  49. package/dist/api/utilities/codec-format.d.ts +87 -0
  50. package/dist/api/utilities/codec-format.js +117 -0
  51. package/dist/api/utilities/codec-format.js.map +1 -0
  52. package/dist/api/utilities/electron-shared-texture.d.ts +41 -1
  53. package/dist/api/utilities/electron-shared-texture.js +41 -4
  54. package/dist/api/utilities/electron-shared-texture.js.map +1 -1
  55. package/dist/api/utilities/index.d.ts +2 -1
  56. package/dist/api/utilities/index.js +2 -0
  57. package/dist/api/utilities/index.js.map +1 -1
  58. package/dist/api/webrtc-stream.d.ts +0 -1
  59. package/dist/api/webrtc-stream.js +0 -1
  60. package/dist/api/webrtc-stream.js.map +1 -1
  61. package/dist/constants/bsf-options.d.ts +333 -0
  62. package/dist/constants/bsf-options.js +7 -0
  63. package/dist/constants/bsf-options.js.map +1 -0
  64. package/dist/constants/constants.d.ts +109 -0
  65. package/dist/constants/constants.js +110 -0
  66. package/dist/constants/constants.js.map +1 -1
  67. package/dist/constants/decoders.d.ts +636 -618
  68. package/dist/constants/decoders.js +1 -3
  69. package/dist/constants/decoders.js.map +1 -1
  70. package/dist/constants/encoders.d.ts +300 -282
  71. package/dist/constants/encoders.js +0 -2
  72. package/dist/constants/encoders.js.map +1 -1
  73. package/dist/constants/filter-options.d.ts +10915 -0
  74. package/dist/constants/filter-options.js +7 -0
  75. package/dist/constants/filter-options.js.map +1 -0
  76. package/dist/constants/format-options.d.ts +3056 -0
  77. package/dist/constants/format-options.js +7 -0
  78. package/dist/constants/format-options.js.map +1 -0
  79. package/dist/constants/formats.d.ts +18 -0
  80. package/dist/constants/formats.js +7 -0
  81. package/dist/constants/formats.js.map +1 -0
  82. package/dist/constants/index.d.ts +5 -0
  83. package/dist/constants/options.d.ts +4073 -0
  84. package/dist/constants/options.js +7 -0
  85. package/dist/constants/options.js.map +1 -0
  86. package/dist/lib/binding.d.ts +5 -1
  87. package/dist/lib/binding.js.map +1 -1
  88. package/dist/lib/codec.d.ts +36 -5
  89. package/dist/lib/codec.js +37 -4
  90. package/dist/lib/codec.js.map +1 -1
  91. package/dist/lib/dictionary.d.ts +1 -1
  92. package/dist/lib/dictionary.js.map +1 -1
  93. package/dist/lib/error.d.ts +69 -0
  94. package/dist/lib/error.js +92 -0
  95. package/dist/lib/error.js.map +1 -1
  96. package/dist/lib/frame.d.ts +55 -3
  97. package/dist/lib/frame.js +59 -3
  98. package/dist/lib/frame.js.map +1 -1
  99. package/dist/lib/index.d.ts +1 -1
  100. package/dist/lib/index.js.map +1 -1
  101. package/dist/lib/native-types.d.ts +68 -0
  102. package/dist/lib/packet.d.ts +22 -3
  103. package/dist/lib/packet.js +24 -3
  104. package/dist/lib/packet.js.map +1 -1
  105. package/dist/lib/utilities.d.ts +45 -0
  106. package/dist/lib/utilities.js +49 -0
  107. package/dist/lib/utilities.js.map +1 -1
  108. package/dist/webrtc/index.d.ts +3 -0
  109. package/dist/webrtc/index.js +7 -0
  110. package/dist/webrtc/index.js.map +1 -0
  111. package/package.json +34 -23
@@ -3,7 +3,7 @@ import { Codec } from '../lib/codec.js';
3
3
  import { Frame } from '../lib/frame.js';
4
4
  import { Packet } from '../lib/packet.js';
5
5
  import { SchedulerControl } from './utilities/scheduler.js';
6
- import type { AVCodecFlag, AVCodecID, AVThreadType, EOFSignal, FFEncoderCodec } from '../constants/index.js';
6
+ import type { AVCodecFlag, AVCodecID, AVThreadType, EncoderOptionsFor, EOFSignal, FFEncoderCodec } from '../constants/index.js';
7
7
  import type { Decoder } from './decoder.js';
8
8
  import type { FilterComplexAPI } from './filter-complex.js';
9
9
  import type { FilterAPI } from './filter.js';
@@ -11,7 +11,7 @@ import type { Muxer } from './muxer.js';
11
11
  /**
12
12
  * Options for encoder creation.
13
13
  */
14
- export interface EncoderOptions {
14
+ export interface EncoderOptions<C = unknown> {
15
15
  /**
16
16
  * Target bitrate.
17
17
  *
@@ -90,13 +90,47 @@ export interface EncoderOptions {
90
90
  * @default FFmpeg default (both methods, codec chooses best)
91
91
  */
92
92
  threadType?: AVThreadType;
93
+ /**
94
+ * Automatically resample incoming audio to a format the codec supports.
95
+ *
96
+ * Audio encoders only accept specific sample rates, sample formats, and channel
97
+ * layouts (e.g. libmp3lame rejects 96 kHz; AAC needs planar `fltp`). When `true`,
98
+ * the encoder transparently converts each frame to the nearest supported
99
+ * sample rate / sample format / channel layout (like `ffmpeg`'s automatic
100
+ * `aresample`). When `false` (default), an unsupported input raises a descriptive
101
+ * error instead — keeping behaviour explicit and 1:1 with the codec.
102
+ *
103
+ * Has no effect on video.
104
+ *
105
+ * @default false
106
+ */
107
+ autoResample?: boolean;
108
+ /**
109
+ * Automatically convert incoming video to a pixel format the codec supports.
110
+ *
111
+ * Video encoders only accept specific pixel formats (e.g. libx264 wants planar
112
+ * YUV like `yuv420p` and rejects `rgb24`). When `true`, the encoder transparently
113
+ * converts each frame to the least-loss supported pixel format via swscale (like
114
+ * `ffmpeg`'s automatic `format` filter), keeping the same resolution. When `false`
115
+ * (default), an unsupported input raises a descriptive error instead — keeping
116
+ * behaviour explicit and 1:1 with the codec.
117
+ *
118
+ * Resolution is never changed (the encoder already adopts the frame's dimensions),
119
+ * and hardware frames are left untouched (their format is negotiated via the
120
+ * hardware frames context). Has no effect on audio.
121
+ *
122
+ * @default false
123
+ */
124
+ autoFormat?: boolean;
93
125
  /**
94
126
  * Additional codec-specific options.
95
127
  *
96
- * Key-value pairs of FFmpeg AVCodecContext options.
97
- * These are passed directly to the encoder.
128
+ * Key-value pairs of FFmpeg private codec options, passed directly to the encoder.
129
+ * When the codec is created from a branded constant (e.g. `FF_ENCODER_LIBX264`),
130
+ * these are strongly typed to that codec's known options (autocomplete + validation);
131
+ * otherwise any string/number/boolean values are accepted.
98
132
  */
99
- options?: Record<string, string | number | boolean | undefined | null>;
133
+ options?: EncoderOptionsFor<C>;
100
134
  /**
101
135
  * AbortSignal for cancellation.
102
136
  *
@@ -172,6 +206,14 @@ export declare class Encoder implements Disposable {
172
206
  private opts?;
173
207
  private options;
174
208
  private audioFrameBuffer?;
209
+ private autoResample;
210
+ private audioResampler?;
211
+ private resampledFrame?;
212
+ private audioInputLayout?;
213
+ private autoFormat;
214
+ private videoScaler?;
215
+ private scaledFrame?;
216
+ private videoTargetFormat?;
175
217
  private inputQueue;
176
218
  private outputQueue;
177
219
  private workerPromise;
@@ -243,7 +285,7 @@ export declare class Encoder implements Disposable {
243
285
  * @see {@link EncoderOptions} For configuration options
244
286
  * @see {@link createSync} For synchronous version
245
287
  */
246
- static create(encoderCodec: FFEncoderCodec | AVCodecID | Codec, options?: EncoderOptions): Promise<Encoder>;
288
+ static create<const C extends FFEncoderCodec | AVCodecID | Codec>(encoderCodec: C, options?: EncoderOptions<C>): Promise<Encoder>;
247
289
  /**
248
290
  * Create an encoder with specified codec and options synchronously.
249
291
  * Synchronous version of create.
@@ -301,7 +343,65 @@ export declare class Encoder implements Disposable {
301
343
  * @see {@link EncoderOptions} For configuration options
302
344
  * @see {@link create} For async version
303
345
  */
304
- static createSync(encoderCodec: FFEncoderCodec | AVCodecID | Codec, options?: EncoderOptions): Encoder;
346
+ static createSync<const C extends FFEncoderCodec | AVCodecID | Codec>(encoderCodec: C, options?: EncoderOptions<C>): Encoder;
347
+ /**
348
+ * Encode a single frame into a self-contained image buffer.
349
+ *
350
+ * One-shot, stateless helper for intra-only image codecs (MJPEG, PNG, WebP, ...).
351
+ * Creates a fresh encoder, encodes the frame, flushes and frees everything in one call.
352
+ * The encoder adopts dimensions, pixel format and hardware context from the frame,
353
+ * so any frame size works without reconfiguration.
354
+ *
355
+ * @param encoderCodec - Encoder codec (name, ID, branded constant, or Codec)
356
+ *
357
+ * @param frame - Frame to encode
358
+ *
359
+ * @param options - Optional encoder configuration (e.g. `{ options: { q: 3 } }` for MJPEG quality)
360
+ *
361
+ * @returns Encoded image bytes
362
+ *
363
+ * @throws {FFmpegError} If the encoder is not found or encoding fails
364
+ *
365
+ * @throws {Error} If the encoder produced no output
366
+ *
367
+ * @example
368
+ * ```typescript
369
+ * const jpeg = await Encoder.encodeOne(FF_ENCODER_MJPEG, frame, { options: { q: 3 } });
370
+ * ```
371
+ *
372
+ * @see {@link EncoderPool} For reusing encoders across recurring resolutions
373
+ */
374
+ static encodeOne<const C extends FFEncoderCodec | AVCodecID | Codec>(encoderCodec: C, frame: Frame, options?: EncoderOptions<C>): Promise<Buffer>;
375
+ /**
376
+ * Encode a single frame into a self-contained image buffer synchronously.
377
+ * Synchronous version of encodeOne.
378
+ *
379
+ * One-shot, stateless helper for intra-only image codecs (MJPEG, PNG, WebP, ...).
380
+ * Creates a fresh encoder, encodes the frame, flushes and frees everything in one call.
381
+ * The encoder adopts dimensions, pixel format and hardware context from the frame,
382
+ * so any frame size works without reconfiguration.
383
+ *
384
+ * @param encoderCodec - Encoder codec (name, ID, branded constant, or Codec)
385
+ *
386
+ * @param frame - Frame to encode
387
+ *
388
+ * @param options - Optional encoder configuration (e.g. `{ options: { q: 3 } }` for MJPEG quality)
389
+ *
390
+ * @returns Encoded image bytes
391
+ *
392
+ * @throws {FFmpegError} If the encoder is not found or encoding fails
393
+ *
394
+ * @throws {Error} If the encoder produced no output
395
+ *
396
+ * @example
397
+ * ```typescript
398
+ * const jpeg = Encoder.encodeOneSync(FF_ENCODER_MJPEG, frame, { options: { q: 3 } });
399
+ * ```
400
+ *
401
+ * @see {@link encodeOne} For async version
402
+ * @see {@link EncoderPool} For reusing encoders across recurring resolutions
403
+ */
404
+ static encodeOneSync<const C extends FFEncoderCodec | AVCodecID | Codec>(encoderCodec: C, frame: Frame, options?: EncoderOptions<C>): Buffer;
305
405
  /**
306
406
  * Check if encoder is open.
307
407
  *
@@ -1096,6 +1196,93 @@ export declare class Encoder implements Disposable {
1096
1196
  * @internal
1097
1197
  */
1098
1198
  private setupHardwareAcceleration;
1199
+ /**
1200
+ * Configure the codec context's audio parameters from the first frame.
1201
+ *
1202
+ * Audio encoders only accept specific sample rates / sample formats / channel
1203
+ * layouts. This picks codec-supported targets; if they differ from the input it
1204
+ * either sets up a resampler (when `autoResample`) or throws a descriptive error.
1205
+ *
1206
+ * @param frame - First audio frame
1207
+ *
1208
+ * @throws {Error} If the input is unsupported and `autoResample` is disabled
1209
+ *
1210
+ * @throws {FFmpegError} If the resampler fails to configure
1211
+ *
1212
+ * @internal
1213
+ */
1214
+ private setupAudioParams;
1215
+ /**
1216
+ * Lazily allocate the reused resampler output frame.
1217
+ *
1218
+ * @returns The allocated output frame
1219
+ *
1220
+ * @internal
1221
+ */
1222
+ private getResampleFrame;
1223
+ /**
1224
+ * Resample an incoming audio frame to the codec's target format.
1225
+ *
1226
+ * Reuses a single output frame; `swr_convert_frame` allocates/sizes its buffer.
1227
+ * The (fixed-frame-size) audio FIFO copies the samples and re-stamps PTS, so the
1228
+ * reused frame and its carried timing are only relevant on the non-FIFO path.
1229
+ *
1230
+ * @param frame - Source audio frame
1231
+ *
1232
+ * @returns The resampled frame (owned by the encoder, reused across calls)
1233
+ *
1234
+ * @internal
1235
+ */
1236
+ private resampleAudio;
1237
+ /**
1238
+ * Drain samples buffered inside the resampler (rate-conversion delay) into the
1239
+ * encoder path. Returns the drained frame if any, else null.
1240
+ *
1241
+ * @returns The drained frame (reused), or null when the resampler is empty
1242
+ *
1243
+ * @internal
1244
+ */
1245
+ private drainResampler;
1246
+ /**
1247
+ * Configure the codec context's pixel format from the first video frame.
1248
+ *
1249
+ * Video encoders only accept specific pixel formats. This keeps the input format
1250
+ * when the codec accepts it; otherwise it either sets up a swscale converter to
1251
+ * the least-loss supported format (when `autoFormat`) or throws a descriptive
1252
+ * error. Hardware frames are left untouched - their format is negotiated through
1253
+ * the hardware frames context, not swscale.
1254
+ *
1255
+ * @param frame - First video frame
1256
+ *
1257
+ * @throws {Error} If the input is unsupported and `autoFormat` is disabled
1258
+ *
1259
+ * @throws {FFmpegError} If the converter fails to configure
1260
+ *
1261
+ * @internal
1262
+ */
1263
+ private setupVideoFormat;
1264
+ /**
1265
+ * Lazily allocate the reused scaler output frame.
1266
+ *
1267
+ * @returns The allocated output frame
1268
+ *
1269
+ * @internal
1270
+ */
1271
+ private getScaledFrame;
1272
+ /**
1273
+ * Convert an incoming video frame to the codec's target pixel format.
1274
+ *
1275
+ * Reuses a single output frame; `sws_scale_frame` allocates/sizes its buffer.
1276
+ * Resolution is unchanged - only the pixel format differs. Timing is carried over
1277
+ * explicitly so the encoder's PTS rescale stays correct.
1278
+ *
1279
+ * @param frame - Source video frame
1280
+ *
1281
+ * @returns The converted frame (owned by the encoder, reused across calls)
1282
+ *
1283
+ * @internal
1284
+ */
1285
+ private scaleVideo;
1099
1286
  /**
1100
1287
  * Prepare frame for encoding.
1101
1288
  *