bare-ffmpeg 1.0.0-32 → 1.0.0-33

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 (34) hide show
  1. package/CMakeLists.txt +31 -9
  2. package/README.md +65 -1761
  3. package/binding.cc +734 -1
  4. package/cmake/ports/ffmpeg/port.cmake +8 -9
  5. package/cmake/ports/libdrm/port.cmake +32 -0
  6. package/cmake/ports/libva/port.cmake +53 -0
  7. package/cmake/ports/x264/port.cmake +7 -2
  8. package/index.js +6 -0
  9. package/lib/codec-context.js +9 -0
  10. package/lib/codec-parameters.js +32 -0
  11. package/lib/codec.js +1 -0
  12. package/lib/constants.js +118 -1
  13. package/lib/frame.js +19 -0
  14. package/lib/hw-device-context.js +31 -0
  15. package/lib/hw-frames-constraints.js +54 -0
  16. package/lib/hw-frames-context.js +92 -0
  17. package/package.json +1 -1
  18. package/prebuilds/android-arm/bare-ffmpeg.bare +0 -0
  19. package/prebuilds/android-arm64/bare-ffmpeg.bare +0 -0
  20. package/prebuilds/android-ia32/bare-ffmpeg.bare +0 -0
  21. package/prebuilds/android-x64/bare-ffmpeg.bare +0 -0
  22. package/prebuilds/darwin-arm64/bare-ffmpeg.bare +0 -0
  23. package/prebuilds/darwin-x64/bare-ffmpeg.bare +0 -0
  24. package/prebuilds/ios-arm64/bare-ffmpeg.bare +0 -0
  25. package/prebuilds/ios-arm64-simulator/bare-ffmpeg.bare +0 -0
  26. package/prebuilds/ios-x64-simulator/bare-ffmpeg.bare +0 -0
  27. package/prebuilds/linux-arm64/bare-ffmpeg/libva-drm.so.2 +0 -0
  28. package/prebuilds/linux-arm64/bare-ffmpeg/libva.so.2 +0 -0
  29. package/prebuilds/linux-arm64/bare-ffmpeg.bare +0 -0
  30. package/prebuilds/linux-x64/bare-ffmpeg/libva-drm.so.2 +0 -0
  31. package/prebuilds/linux-x64/bare-ffmpeg/libva.so.2 +0 -0
  32. package/prebuilds/linux-x64/bare-ffmpeg.bare +0 -0
  33. package/prebuilds/win32-arm64/bare-ffmpeg.bare +0 -0
  34. package/prebuilds/win32-x64/bare-ffmpeg.bare +0 -0
package/README.md CHANGED
@@ -2,1810 +2,114 @@
2
2
 
3
3
  Low-level FFmpeg bindings for Bare.
4
4
 
5
- ```
6
- npm i bare-ffmpeg
7
- ```
8
-
9
- ## API
10
-
11
- ### `IOContext`
12
-
13
- The `IOContext` API provides functionality to create input/output contexts for media files with support for streaming and custom I/O operations.
14
-
15
- ```js
16
- const io = new ffmpeg.IOContext(buffer[, options])
17
- ```
18
-
19
- Parameters:
20
-
21
- - `buffer` (`Buffer` | `number`): The media data buffer or buffer size for streaming
22
- - `options` (`object`, optional): Configuration options
23
- - `onread` (`function`): A function for refilling the buffer.
24
- - `onwrite` (`function`): A function for writing the buffer contents.
25
- - `onseek` (`function`): A function for seeking to specified byte position.
26
-
27
- **Returns**: A new `IOContext` instance
28
-
29
- #### Constructor Options
30
-
31
- ##### `onread(buffer, requestedLen)`
32
-
33
- Callback function called when FFmpeg needs to read data. For streaming scenarios where data is not available in a single buffer.
34
-
35
- Parameters:
36
-
37
- - `buffer` (`Buffer`): Buffer to fill with data
38
- - `requestedLen` (`number`): Number of bytes requested
39
-
40
- **Returns**: `number` - Number of bytes actually read, or 0 for EOF
41
-
42
- ##### `onwrite(buffer)`
43
-
44
- Callback function called when FFmpeg needs to write data. For streaming output scenarios.
45
-
46
- Parameters:
47
-
48
- - `buffer` (`Buffer`): Buffer containing data to write
49
-
50
- **Returns**: `number` - Number of bytes written
51
-
52
- ##### `onseek(offset, whence)`
53
-
54
- Callback function called when FFmpeg needs to seek within the data source.
55
-
56
- Parameters:
57
-
58
- - `offset` (`number`): Offset to seek to
59
- - `whence` (`number`): Seek mode (see `ffmpeg.constants.seek`)
60
-
61
- **Returns**: `number` - New position or file size for `AVSEEK_SIZE`
62
-
63
- #### Examples
64
-
65
- **Basic usage with buffer:**
66
-
67
- ```js
68
- const image = require('./fixtures/image/sample.jpeg', {
69
- with: { type: 'binary' }
70
- })
71
- const io = new ffmpeg.IOContext(image)
72
- io.destroy()
73
- ```
74
-
75
- **Streaming with custom read callback:**
76
-
77
- ```js
78
- const io = new ffmpeg.IOContext(4096, {
79
- onread: (buffer) => {
80
- const bytesToRead = Math.min(buffer.length, data.length - offset)
81
- if (bytesToRead === 0) return 0
82
-
83
- const chunk = data.subarray(offset, offset + bytesToRead)
84
- buffer.set(chunk)
85
- offset += bytesToRead
86
- return bytesToRead
87
- }
88
- })
89
- ```
90
-
91
- **Streaming with seek support:**
92
-
93
- ```js
94
- const io = new ffmpeg.IOContext(4096, {
95
- onread: (buffer) => {
96
- // ... read implementation
97
- },
98
- onseek: (offset, whence) => {
99
- switch (whence) {
100
- case ffmpeg.constants.seek.SIZE:
101
- return data.length
102
- case ffmpeg.constants.seek.SET:
103
- offset = offset
104
- return offset
105
- default:
106
- return -1
107
- }
108
- }
109
- })
110
- ```
111
-
112
- #### Methods
113
-
114
- ##### `IOContext.destroy()`
115
-
116
- Destroys the `IOContext` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
117
-
118
- **Returns**: `void`
119
-
120
- ### `Dictionary`
121
-
122
- The `Dictionary` API provides functionality to store and retrieve key-value pairs, commonly used for passing options to various FFmpeg components.
123
-
124
- ```js
125
- const dict = new ffmpeg.Dictionary()
126
- ```
127
-
128
- **Returns**: A new `Dictionary` instance
129
-
130
- Example:
131
-
132
- ```js
133
- const dict = new ffmpeg.Dictionary()
134
- dict.set('video_codec', 'h264')
135
- dict.set('audio_codec', 'aac')
136
- dict.set('bitrate', '1000k')
137
- ```
138
-
139
- #### Methods
140
-
141
- ##### `Dictionary.set(key, value)`
142
-
143
- Sets a key-value pair in the dictionary. Non-string values are automatically converted to strings.
144
-
145
- Parameters:
146
-
147
- - `key` (`string`): The dictionary key
148
- - `value` (`string` | `number`): The value to store
149
-
150
- **Returns**: `void`
151
-
152
- ##### `Dictionary.get(key)`
153
-
154
- Retrieves a value from the dictionary by key.
155
-
156
- Parameters:
157
-
158
- - `key` (`string`): The dictionary key
159
-
160
- **Returns**: `string` value or `null` if the key doesn't exist
161
-
162
- ##### `Dictionary.entries()`
163
-
164
- Retrieves all keys and values.
165
-
166
- **Returns**: `Array<[string, string]>` value
167
-
168
- ##### `Dictionary.destroy()`
169
-
170
- Destroys the `Dictionary` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
171
-
172
- **Returns**: `void`
173
-
174
- #### Iteration
175
-
176
- The `Dictionary` class implements the iterator protocol, allowing you to iterate over all key-value pairs:
177
-
178
- ```js
179
- const dict = new ffmpeg.Dictionary()
180
- dict.set('foo', 'bar')
181
- dict.set('baz', 'qux')
182
-
183
- for (const [key, value] of dict) {
184
- console.log(`${key}: ${value}`)
185
- }
186
- ```
187
-
188
- #### Static methods
189
-
190
- ##### `Dictionary.from(object)`
191
-
192
- A helper to create a `Dictionary` instance from an object.
193
-
194
- ```js
195
- const dict = ffmpeg.Dictionary.from({
196
- foo: 'bar',
197
- baz: 'qux'
198
- })
199
- ```
200
-
201
- **Returns**: A new `Dictionary` instance
202
-
203
- ### `FormatContext`
204
-
205
- The `FormatContext` API provides the base functionality for reading and writing media files.
206
-
207
- > This is the base class that `InputFormatContext` and `OutputFormatContext` extend.
208
-
209
- #### Properties
210
-
211
- ##### `FormatContext.io`
212
-
213
- Gets the IO context associated with this format context.
214
-
215
- **Returns**: `IOContext` instance or `null`
216
-
217
- ##### `FormatContext.streams`
218
-
219
- Gets the array of media streams.
220
-
221
- **Returns**: Array of `Stream` instances
222
-
223
- #### Methods
224
-
225
- ##### `FormatContext.readFrame(packet)`
226
-
227
- Reads the next frame from the media file into a packet.
228
-
229
- Parameters:
230
-
231
- - `packet` (`Packet`): The packet to store the frame data
232
-
233
- **Returns**: `boolean` indicating if a frame was read
234
-
235
- ##### `FormatContext.getBestStream(type)`
236
-
237
- Gets the best stream of the specified media type.
238
-
239
- Parameters:
240
-
241
- - `type` (`number`): The media type from `ffmpeg.constants.mediaTypes`
242
-
243
- **Returns**: `Stream` instance or `null` if not found
244
-
245
- ##### `FormatContext.destroy()`
246
-
247
- Destroys the `FormatContext` and frees all associated resources including streams. Automatically called when the object is managed by a `using` declaration.
248
-
249
- **Returns**: `void`
250
-
251
- ### `InputFormatContext`
252
-
253
- The `InputFormatContext` API extends `FormatContext` to provide functionality for reading media files.
254
-
255
- ```js
256
- const format = new ffmpeg.InputFormatContext(io, options[, url])
257
- ```
258
-
259
- Parameters:
260
-
261
- - `io` (`IOContext` | `InputFormat`): The IO context or input format. The ownership of `io` is transferred.
262
- - `options` (`Dictionary`): Format options. Required when using `InputFormat`, ignored when using `IOContext`. The ownership of `options` is transferred.
263
- - `url` (`string`, optional): Media source URL. Defaults to a platform-specific value
264
-
265
- **Returns**: A new `InputFormatContext` instance
266
-
267
- #### Methods
268
-
269
- ##### `InputFormatContext.inputFormat`
270
-
271
- Gets the input format associated with this context.
272
-
273
- **Returns**: `InputFormat` instance or `undefined` if not available
274
-
275
- ##### `InputFormatContext.destroy()`
276
-
277
- Destroys the `InputFormatContext` and closes the input format. Automatically called when the object is managed by a `using` declaration.
278
-
279
- **Returns**: void
280
-
281
- ### `OutputFormatContext`
282
-
283
- The `OutputFormatContext` API extends `FormatContext` to provide functionality for writing media files.
284
-
285
- ```js
286
- const format = new ffmpeg.OutputFormatContext(formatName, io)
287
- ```
288
-
289
- Parameters:
290
-
291
- - `formatName` (`string`): The output format name (e.g., `'mp4'`, `'avi'`)
292
- - `io` (`IOContext`): The IO context for writing. The ownership of `io` is transferred.
293
-
294
- **Returns**: A new `OutputFormatContext` instance
295
-
296
- #### Methods
297
-
298
- ##### `OutputFormatContext.createStream(codec)`
299
-
300
- Creates a new stream in the output format.
301
-
302
- Parameters:
303
-
304
- - `codec` (`Codec`): The codec to use for the stream
305
-
306
- **Returns**: A new `Stream` instance
307
-
308
- ##### `OutputFormatContext.outputFormat`
309
-
310
- Gets the output format associated with this context.
311
-
312
- **Returns**: `OutputFormat` instance or `undefined` if not available
313
-
314
- ##### `OutputFormatContext.destroy()`
315
-
316
- Destroys the `OutputFormatContext` and closes the output format. Automatically called when the object is managed by a `using` declaration.
317
-
318
- **Returns**: `void`
319
-
320
- ### `Codec`
321
-
322
- The `Codec` API provides access to FFmpeg codecs for encoding and decoding.
323
-
324
- #### Static properties
325
-
326
- ##### `Codec.H264`
327
-
328
- H.264 video codec.
329
-
330
- **Returns**: `Codec` instance
331
-
332
- ##### `Codec.MJPEG`
333
-
334
- Motion JPEG video codec.
335
-
336
- **Returns**: `Codec` instance
337
-
338
- ##### `Codec.AAC`
339
-
340
- AAC audio codec.
341
-
342
- **Returns**: `Codec` instance
343
-
344
- ##### `Codec.AV1`
345
-
346
- AV1 video codec.
347
-
348
- **Returns**: `Codec` instance
349
-
350
- #### Properties
351
-
352
- ##### `Codec.id`
353
-
354
- Gets the codec ID.
355
-
356
- **Returns**: `number`
357
-
358
- ##### `Codec.encoder`
359
-
360
- Gets the encoder for this codec.
361
-
362
- **Returns**: `Encoder` instance
363
-
364
- ##### `Codec.decoder`
365
-
366
- Gets the decoder for this codec.
367
-
368
- **Returns**: `Decoder` instance
369
-
370
- #### Methods
371
-
372
- ##### `Codec.for(id)`
373
-
374
- Gets a codec by ID.
375
-
376
- Parameters:
377
-
378
- - `id` (`number`): The codec ID
379
-
380
- **Returns**: `Codec` instance
381
-
382
- ### `CodecContext`
383
-
384
- The `CodecContext` API provides functionality to encode or decode media frames.
385
-
386
- ```js
387
- const codecCtx = new ffmpeg.CodecContext(codec)
388
- ```
389
-
390
- Parameters:
391
-
392
- - `codec` (Codec): The codec to use (e.g., `ffmpeg.Codec.H264.encoder`)
393
-
394
- **Returns**: A new `CodecContext` instance
395
-
396
- #### Properties
397
-
398
- ##### `CodecContext.timeBase`
399
-
400
- Gets or sets the time base for the codec context.
401
-
402
- **Returns**: `Rational` instance
403
-
404
- ##### `CodecContext.pixelFormat`
405
-
406
- Gets or sets the pixel format for video codecs.
407
-
408
- **Returns**: `number` (pixel format constant)
409
-
410
- ##### `CodecContext.width`
411
-
412
- Gets or sets the frame width for video codecs.
413
-
414
- **Returns**: `number`
415
-
416
- ##### `CodecContext.height`
417
-
418
- Gets or sets the frame height for video codecs.
419
-
420
- **Returns**: `number`
421
-
422
- ##### `CodecContext.frameSize`
423
-
424
- Gets the number of samples per channel in an audio frame.
425
-
426
- **Returns**: `number`
427
-
428
- ##### `CodecContext.frameNum`
429
-
430
- When encoding this gets the total number of frames passed to the encoder so far.
431
-
432
- When decoding this gets the total number of frames returned from the decoder so far.
433
-
434
- **Returns**: `number`
435
-
436
- ##### `CodecContext.requestSampleFormat`
437
-
438
- _Only when decoding_
439
-
440
- Set to before calling `context.open()` to hint decoder of preferred output format if supported.
441
-
442
- Always rely on `CodecContext.sampleFormat` for actual decoded format.
443
-
444
- **Returns**: `number`
445
-
446
- ##### `CodecContext.getFormat`
447
-
448
- _Only when decoding_
449
-
450
- Sets a callback function for pixel format negotiation during decoding. Called when FFmpeg needs to choose between multiple supported formats, typically for hardware acceleration.
451
-
452
- **Type**: `function` (setter only)
453
-
454
- **Callback Signature**: `(context: CodecContext, formats: number[]) => number`
455
-
456
- Parameters:
457
-
458
- - `context` (`CodecContext`): The codec context instance
459
- - `formats` (`number[]`): Array of available pixel format constants
460
-
461
- **Returns**: `number` - The chosen pixel format constant from the provided array
462
-
463
- Must be set before calling `context.open()`.
464
-
465
- #### Methods
466
-
467
- ##### `CodecContext.open([options])`
468
-
469
- Opens the codec context for encoding/decoding.
470
-
471
- Parameters:
472
-
473
- - `options` (`Dictionary`, optional): Codec-specific options
474
-
475
- **Returns**: `void`
476
-
477
- ##### `CodecContext.sendFrame(frame)`
478
-
479
- Sends a frame to the encoder.
480
-
481
- Parameters:
482
-
483
- - `frame` (`Frame`): The frame to encode
484
-
485
- **Returns**: `boolean` indicating if the frame was sent
486
-
487
- ##### `CodecContext.receiveFrame(frame)`
488
-
489
- Receives a decoded frame from the decoder.
490
-
491
- Parameters:
492
-
493
- - `frame` (`Frame`): The frame to store the decoded data
494
-
495
- **Returns**: `boolean` indicating if a frame was received
496
-
497
- ##### `CodecContext.sendPacket(packet)`
498
-
499
- Sends a packet to the decoder.
500
-
501
- Parameters:
502
-
503
- - `packet` (`Packet`): The packet to decode
504
-
505
- **Returns**: `boolean` indicating if the packet was sent
506
-
507
- ##### `CodecContext.receivePacket(packet)`
508
-
509
- Receives an encoded packet from the encoder.
510
-
511
- Parameters:
512
-
513
- - `packet` (`Packet`): The packet to store the encoded data
514
-
515
- **Returns**: `boolean` indicating if a packet was received
516
-
517
- ##### `CodecContext.getSupportedConfig(config)`
518
-
519
- Gets the supported values for a codec configuration option.
520
-
521
- Parameters:
522
-
523
- - `config` (`number`): The configuration type from `ffmpeg.constants.codecConfig`
524
-
525
- **Returns**:
526
-
527
- - For `PIX_FORMAT`, `SAMPLE_FORMAT`, `COLOR_RANGE`, `COLOR_SPACE`: `Int32Array` of supported values (all valid values are included if the codec has no restrictions)
528
- - For `SAMPLE_RATE`: `Int32Array` of supported sample rates or `null` if the codec accepts any valid sample rate
529
- - For `FRAME_RATE`: Array of `Rational` instances or `null` if the codec accepts any valid frame rate
530
- - For `CHANNEL_LAYOUT`: Array of `ChannelLayout` instances or `null` if the codec accepts any valid channel layout
531
-
532
- **Throws**: Error if the config type is not applicable to this codec (e.g., asking for pixel formats from an audio codec)
533
-
534
- Examples:
535
-
536
- ```js
537
- const codecCtx = new ffmpeg.CodecContext(ffmpeg.Codec.AV1.encoder)
538
-
539
- const pixelFormats = codecCtx.getSupportedConfig(ffmpeg.constants.codecConfig.PIX_FORMAT)
540
- if (pixelFormats) {
541
- console.log('Supported pixel formats:', pixelFormats)
542
- }
543
-
544
- const frameRates = codecCtx.getSupportedConfig(ffmpeg.constants.codecConfig.FRAME_RATE)
545
- if (frameRates) {
546
- frameRates.forEach((rate) => {
547
- console.log(`${rate.toNumber()} fps`)
548
- })
549
- }
550
-
551
- const layouts = codecCtx.getSupportedConfig(ffmpeg.constants.codecConfig.CHANNEL_LAYOUT)
552
- if (layouts) {
553
- console.log('Supported channel layouts:', layouts)
554
- }
555
- ```
556
-
557
- ##### `CodecContext.getOption(name[, flags])`
558
-
559
- Gets the value of a codec option.
560
-
561
- Parameters:
562
-
563
- - `name` (`string`): The option name (for example, `'threads'` or `'crf'`)
564
- - `flags` (`number`, optional): Option search flags (default `ffmpeg.constants.optionFlags.SEARCH_CHILDREN`)
565
-
566
- **Returns**: `string` value or `null` when the option is unset
567
-
568
- ##### `CodecContext.setOption(name, value[, flags])`
569
-
570
- Sets a codec option.
571
-
572
- Parameters:
573
-
574
- - `name` (`string`): The option name to set
575
- - `value` (`string`): The option value
576
- - `flags` (`number`, optional): Option search flags (default `ffmpeg.constants.optionFlags.SEARCH_CHILDREN`)
577
-
578
- **Returns**: `void`
579
-
580
- ##### `CodecContext.setOptionDictionary(dictionary[, flags])`
581
-
582
- Sets options from a `Dictionary`. Ownership of the dictionary is retained by the caller.
583
-
584
- Parameters:
585
-
586
- - `dictionary` (`Dictionary`): Dictionary of option key/value pairs
587
- - `flags` (`number`, optional): Option search flags (default `ffmpeg.constants.optionFlags.SEARCH_CHILDREN`)
588
-
589
- **Returns**: `void`
590
-
591
- ##### `CodecContext.setOptionDefaults()`
592
-
593
- Resets codec options to their defaults.
594
-
595
- **Returns**: `void`
596
-
597
- ##### `CodecContext.listOptionNames([flags])`
598
-
599
- Lists option names available on the codec context.
600
-
601
- Parameters:
602
-
603
- - `flags` (`number`, optional): Option search flags (default `ffmpeg.constants.optionFlags.SEARCH_CHILDREN`)
604
-
605
- **Returns**: `Array<string>` of option names
606
-
607
- ##### `CodecContext.getOptions([flags])`
608
-
609
- Collects option values into a plain object.
610
-
611
- Parameters:
612
-
613
- - `flags` (`number`, optional): Option search flags (default `ffmpeg.constants.optionFlags.SEARCH_CHILDREN`)
614
-
615
- **Returns**: `object` mapping option names to string values
616
-
617
- ##### `CodecContext.copyOptionsFrom(context)`
618
-
619
- Copies options from another codec context.
620
-
621
- Parameters:
622
-
623
- - `context` (`CodecContext`): Source context whose options should be copied
624
-
625
- **Returns**: `void`
626
-
627
- Example:
628
-
629
- ```js
630
- using source = new ffmpeg.CodecContext(ffmpeg.Codec.AV1.encoder)
631
- using target = new ffmpeg.CodecContext(ffmpeg.Codec.AV1.encoder)
632
-
633
- source.setOption('crf', '28')
634
- source.setOption('threads', '4')
635
- source.setOptionDictionary(ffmpeg.Dictionary.from({ preset: 'slow' }))
636
-
637
- target.setOptionDefaults()
638
- target.copyOptionsFrom(source)
639
-
640
- console.log(target.getOptions())
641
- ```
642
-
643
- ##### `CodecContext.destroy()`
644
-
645
- Destroys the `CodecContext` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
646
-
647
- **Returns**: `void`
648
-
649
- ### `CodecParameters`
650
-
651
- The `CodecParameters` API provides functionality to access codec parameters from streams.
652
-
653
- ```js
654
- const params = stream.codecParameters // Get from stream
655
- ```
656
-
657
- #### Properties
658
-
659
- ##### `CodecParameters.type`
660
-
661
- General type of the encoded data.
662
-
663
- **Returns**: `number`
664
-
665
- ##### `CodecParameters.id`
666
-
667
- Specific type of the encoded data (the codec used).
668
-
669
- **Returns**: `number`
670
-
671
- ##### `CodecParameters.tag`
672
-
673
- Additional information about the codec (corresponds to the AVI FOURCC).
674
-
675
- **Returns**: `number`
676
-
677
- ##### `CodecParameters.bitRate`
678
-
679
- Gets the bit rate.
680
-
681
- **Returns**: `number`
682
-
683
- ##### `CodecParameters.bitsPerCodedSample`
684
-
685
- Gets the bits per coded sample.
686
-
687
- **Returns**: `number`
688
-
689
- ##### `CodecParameters.bitsPerRawSample`
690
-
691
- Gets the bits per raw sample.
692
-
693
- **Returns**: `number`
694
-
695
- ##### `CodecParameters.sampleRate`
696
-
697
- Gets the sample rate for audio codecs.
698
-
699
- **Returns**: `number`
700
-
701
- ##### `CodecParameters.frameRate`
702
-
703
- Gets the frame rate for video codecs.
704
-
705
- **Returns**: `Rational`
706
-
707
- ##### `CodecParameters.extraData`
708
-
709
- Out-of-band global headers that may be used by some codecs.
710
-
711
- **Returns**: `Buffer`
712
-
713
- ##### `CodecParameters.profile`
714
-
715
- Codec-specific bitstream restrictions that the stream conforms to.
716
-
717
- **Returns**: `number`
718
-
719
- ##### `CodecParameters.level`
720
-
721
- Codec-specific bitstream restrictions that the stream conforms to.
722
-
723
- **Returns**: `number`
724
-
725
- ##### `CodecParameters.format`
726
-
727
- Video: the pixel format, the value corresponds to AVPixelFormat.
728
- Audio: the sample format, the value corresponds to AVSampleFormat.
729
-
730
- **Returns**: `number`
731
-
732
- ##### `CodecParameters.nbChannels`
733
-
734
- Number of channels in the layout.
735
-
736
- **Returns**: `number`
737
-
738
- ##### `CodecParameters.channelLayout`
739
-
740
- Gets or sets the channel layout, see `ffmpeg.constants.channelLayouts`
741
-
742
- **Returns**: `ChannelLayout`
743
-
744
- ##### `CodecParameters.blockAlign`
745
-
746
- Audio only. The number of bytes per coded audio frame, required by some
747
- formats.
748
-
749
- Corresponds to `nBlockAlign` in `WAVEFORMATEX`.
750
-
751
- **Returns**: `number`
752
-
753
- ##### `CodecParameters.initalPadding`
754
-
755
- Audio only. The amount of padding (in samples) inserted by the encoder at the beginning of the audio. I.e. this number of leading decoded samples must be discarded by the caller to get the original audio without leading padding.
756
-
757
- **Returns**: `number`
758
-
759
- ##### `CodecParameters.trailingPadding`
760
-
761
- Audio only. The amount of padding (in samples) appended by the encoder to the end of the audio. I.e. this number of decoded samples must be discarded by the caller from the end of the stream to get the original audio without any trailing padding.
762
-
763
- ##### `CodecParameters.seekPreroll`
764
-
765
- Audio only. Number of samples to skip after a discontinuity.
766
-
767
- **Returns**: `number`
768
-
769
- ##### `CodecParameters.sampleAspectRatio`
770
-
771
- Video only. The aspect ratio (width / height) which a single pixel should have when displayed.
772
-
773
- When the aspect ratio is unknown / undefined, the numerator should be set to 0 (the denominator may have any value).
774
-
775
- **Returns**: `number`
776
-
777
- ##### `CodecParameters.videoDelay`
778
-
779
- Video only. Number of delayed frames.
780
-
781
- **Returns**: `number`
782
-
783
- #### Methods
784
-
785
- ##### `CodecParameters.fromContext(context)`
786
-
787
- Copies parameters from a codec context.
788
-
789
- Parameters:
790
-
791
- - `context` (`CodecContext`): The codec context
792
-
793
- **Returns**: `void`
794
-
795
- ##### `CodecParameters.toContext(context)`
796
-
797
- Copies parameters to a codec context.
798
-
799
- Parameters:
800
-
801
- - `context` (`CodecContext`): The codec context
802
-
803
- **Returns**: `void`
804
-
805
- ### `InputFormat`
806
-
807
- The `InputFormat` API provides functionality to specify input format for media sources.
808
-
809
- ```js
810
- const format = new ffmpeg.InputFormat([name])
811
- ```
812
-
813
- Parameters:
814
-
815
- - `name` (`string`, optional): The input format name. Defaults to a platform-specific value:
816
- - `darwin`, `ios`: ``'avfoundation'`
817
- - `linux`: `'v4l2'`
818
- - `win32`: `'dshow'`
819
-
820
- **Returns**: A new `InputFormat` instance
821
-
822
- #### Properties
823
-
824
- ##### `InputFormat.extensions`
825
-
826
- Gets the file extensions associated with this input format.
827
-
828
- **Returns**: `string` - Comma-separated list of file extensions (e.g., `'mkv,mk3d,mka,mks,webm'`)
829
-
830
- ##### `InputFormat.mimeType`
831
-
832
- Gets the MIME type for this input format.
833
-
834
- **Returns**: `string` - The MIME type (e.g., `'audio/webm,audio/x-matroska,video/webm,video/x-matroska'`)
835
-
836
- ##### `InputFormat.name`
837
-
838
- Returns format name
839
-
840
- **Returns**: `string` - The short-name (e.g., `webm`)
841
-
842
- #### Example
843
-
844
- ```js
845
- const format = new ffmpeg.InputFormat('webm')
846
- console.log(format.extensions) // 'mkv,mk3d,mka,mks,webm'
847
- console.log(format.mimeType) // 'audio/webm,audio/x-matroska,video/webm,video/x-matroska'
848
- ```
849
-
850
- #### Methods
851
-
852
- ##### `InputFormat.destroy()`
853
-
854
- Destroys the `InputFormat` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
855
-
856
- **Returns**: `void`
857
-
858
- ### `OutputFormat`
859
-
860
- The `OutputFormat` API provides functionality to specify output format for media files.
861
-
862
- ```js
863
- const format = new ffmpeg.OutputFormat(name)
864
- ```
865
-
866
- Parameters:
867
-
868
- - `name` (`string`): The output format name (e.g., `'mp4'`, `'avi'`, `'mov'`)
869
-
870
- **Returns**: A new `OutputFormat` instance
871
-
872
- #### Properties
873
-
874
- ##### `OutputFormat.extensions`
875
-
876
- Gets the file extensions associated with this output format.
877
-
878
- **Returns**: `string` - Comma-separated list of file extensions (e.g., `'webm'`, `'mp4,m4a,m4v'`)
879
-
880
- ##### `OutputFormat.mimeType`
881
-
882
- Gets the MIME type for this output format.
883
-
884
- **Returns**: `string` - The MIME type (e.g., `'video/webm'`, `'video/mp4'`)
885
-
886
- ##### `OutputFormat.name`
887
-
888
- Returns format name
889
-
890
- **Returns**: `string` - The short-name (e.g., `webm`)
891
-
892
- #### Example
893
-
894
- ```js
895
- const format = new ffmpeg.OutputFormat('webm')
896
- console.log(format.extensions) // 'webm'
897
- console.log(format.mimeType) // 'video/webm'
898
- ```
899
-
900
- #### Methods
901
-
902
- ##### `OutputFormat.destroy()`
903
-
904
- Destroys the `OutputFormat` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
905
-
906
- **Returns**: `void`
907
-
908
- ### `Frame`
909
-
910
- This structure describes decoded (raw) audio or video data.
911
-
912
- ```js
913
- const frame = new ffmpeg.Frame()
914
- ```
915
-
916
- **Returns**: A new `Frame` instance
917
-
918
- #### Properties
919
-
920
- ##### `Frame.width`
921
-
922
- Gets or sets the frame width.
923
-
924
- **Returns**: `number`
925
-
926
- ##### `Frame.height`
927
-
928
- Gets or sets the frame height.
929
-
930
- **Returns**: `number`
931
-
932
- ##### `Frame.format`
933
-
934
- Gets or sets the format of the frame, `-1` if unknown or unset.
935
-
936
- **Returns**: `number` (sample format constant)
937
-
938
- ##### `Frame.channelLayout`
939
-
940
- Gets or sets the channel layout for audio frames.
941
-
942
- **Returns**: `number` (channel layout constant)
943
-
944
- ##### `Frame.nbSamples`
945
-
946
- Gets or sets the number of audio samples.
947
-
948
- **Returns**: `number`
949
-
950
- #### Methods
951
-
952
- ##### `Frame.alloc()`
953
-
954
- Allocates memory for the frame data.
955
-
956
- **Returns**: `void`
957
-
958
- ##### `Frame.destroy()`
959
-
960
- Destroys the `Frame` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
961
-
962
- **Returns**: `void`
963
-
964
- ##### `Frame.copyProperties(otherFrame)`
965
-
966
- Copies all metadata properties such as timestamps, timebase and width/height for videoframes and
967
- sampleRate channelLayout for audioFrames.
968
-
969
- see `av_frame_copy_props()` for details.
970
-
971
- ```js
972
- const src = new ffmpeg.Frame()
973
- const dst = new ffmpeg.Frame()
974
-
975
- decoder.receiveFrame(src)
976
- rescaler.convert(src, dst)
977
-
978
- dst.copyProperties(src) // transfer all meta-data
979
- ```
980
-
981
- **Returns**: `void`
982
-
983
- ### `Packet`
984
-
985
- This structure stores compressed data. It is typically exported by demuxers and then passed as input to decoders, or received as output from encoders and then passed to muxers.
986
-
987
- ```js
988
- const packet = new ffmpeg.Packet([buffer])
989
- ```
990
-
991
- Parameters:
992
-
993
- - `buffer` (`Buffer`, optional): Initial packet data
994
-
995
- **Returns**: A new `Packet` instance
996
-
997
- #### Properties
998
-
999
- ##### `Packet.data`
1000
-
1001
- Gets the packet data buffer.
1002
-
1003
- **Returns**: `Buffer`
1004
-
1005
- ##### `Packet.streamIndex`
1006
-
1007
- Gets the stream index this packet belongs to.
1008
-
1009
- **Returns**: `number`
1010
-
1011
- ##### `Packet.isKeyFrame`
1012
-
1013
- Get or set the key frame flag.
1014
-
1015
- **Returns**: `boolean`
1016
-
1017
- ##### `Packet.sideData`
1018
-
1019
- Gets or sets the side data associated with the packet.
1020
-
1021
- **Returns**: `Array<SideData>`
1022
-
1023
- #### Methods
1024
-
1025
- ##### `Packet.unref()`
1026
-
1027
- Decrements the reference count and unreferences the packet.
1028
-
1029
- **Returns**: `void`
1030
-
1031
- ##### `Packet.destroy()`
1032
-
1033
- Destroys the `Packet` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
1034
-
1035
- **Returns**: `void`
1036
-
1037
- ### `SideData`
1038
-
1039
- The `SideData` API provides functionality to handle side data associated with packets. Side data contains additional metadata that may be useful for specific codecs or processing.
1040
-
1041
- ```js
1042
- const sideData = new ffmpeg.Packet.SideData(handle, options)
1043
- ```
1044
-
1045
- Parameters:
1046
-
1047
- - `handle` (`ArrayBuffer`): Internal side data handle
1048
- - `options` (`object`, optional): Configuration options
1049
- - `type` (`number`): The side data type
1050
- - `data` (`Buffer`): The side data buffer
1051
-
1052
- **Returns**: A new `SideData` instance
1053
-
1054
- #### Static Methods
1055
-
1056
- ##### `SideData.fromData(data, type)`
1057
-
1058
- Creates a new `SideData` instance from data and type.
1059
-
1060
- Parameters:
1061
-
1062
- - `data` (`Buffer`): The side data buffer
1063
- - `type` (`number`): The side data type constant
1064
-
1065
- **Returns**: A new `SideData` instance
1066
-
1067
- #### Properties
1068
-
1069
- ##### `SideData.type`
1070
-
1071
- Gets the side data type.
1072
-
1073
- **Returns**: `number`
1074
-
1075
- ##### `SideData.name`
1076
-
1077
- Gets the human-readable name of the side data type.
1078
-
1079
- **Returns**: `string`
1080
-
1081
- ##### `SideData.data`
1082
-
1083
- Gets the side data buffer.
1084
-
1085
- **Returns**: `Buffer`
1086
-
1087
- #### Example
1088
-
1089
- ```js
1090
- const packet = new ffmpeg.Packet()
1091
- const sideData = ffmpeg.Packet.SideData.fromData(
1092
- Buffer.from('metadata'),
1093
- ffmpeg.constants.packetSideDataType.NEW_EXTRADATA
1094
- )
1095
- packet.sideData = [sideData]
1096
- ```
1097
-
1098
- ### `Image`
1099
-
1100
- The `Image` API provides functionality to create and manage image buffers.
1101
-
1102
- ```js
1103
- const image = new ffmpeg.Image(pixelFormat, width, height[, align])
1104
- ```
1105
-
1106
- Parameters:
1107
-
1108
- - `pixelFormat` (`number` | `string`): The pixel format
1109
- - `width` (`number`): The image width in pixels
1110
- - `height` (`number`): The image height in pixels
1111
- - `align` (`number`, optional): Memory alignment. Defaults to 1
1112
-
1113
- **Returns**: A new `Image` instance
1114
-
1115
- #### Properties
1116
-
1117
- ##### `Image.pixelFormat`
1118
-
1119
- Gets the pixel format.
1120
-
1121
- **Returns**: `number`
1122
-
1123
- ##### `Image.width`
1124
-
1125
- Gets the image width.
1126
-
1127
- **Returns**: `number`
1128
-
1129
- ##### `Image.height`
1130
-
1131
- Gets the image height.
1132
-
1133
- **Returns**: `number`
1134
-
1135
- ##### `Image.align`
1136
-
1137
- Gets the memory alignment.
1138
-
1139
- **Returns**: `number`
1140
-
1141
- ##### `Image.data`
1142
-
1143
- Gets the image data buffer.
5
+ ## Installation
1144
6
 
1145
- **Returns**: `Buffer`
1146
-
1147
- #### Methods
1148
-
1149
- ##### `Image.fill(frame)`
1150
-
1151
- Fills a frame with the image data.
1152
-
1153
- Parameters:
1154
-
1155
- - `frame` (`Frame`): The frame to fill
1156
-
1157
- **Returns**: void
1158
-
1159
- ##### `Image.read(frame)`
1160
-
1161
- Reads image data from a frame into the image buffer.
1162
-
1163
- Parameters:
1164
-
1165
- - `frame` (`Frame`): The frame to read from
1166
-
1167
- **Returns**: `void`
1168
-
1169
- ##### `Image.lineSize([plane])`
1170
-
1171
- Gets the line size for a specific plane.
1172
-
1173
- Parameters:
1174
-
1175
- - `plane` (`number`, optional): Plane index. Defaults to 0
1176
-
1177
- **Returns**: `number`
1178
-
1179
- #### Static methods
1180
-
1181
- ##### `Image.lineSize(pixelFormat, width[, plane])`
1182
-
1183
- Static method to get line size for a pixel format.
1184
-
1185
- Parameters:
1186
-
1187
- - `pixelFormat` (`number` | `string`): The pixel format
1188
- - `width` (`number`): The image width
1189
- - `plane` (`number`, optional): Plane index. Defaults to 0
1190
-
1191
- **Returns**: `number`
1192
-
1193
- ### `Rational`
1194
-
1195
- The `Rational` API provides functionality to represent rational numbers (fractions).
1196
-
1197
- ```js
1198
- const rational = new ffmpeg.Rational(numerator, denominator)
1199
- ```
1200
-
1201
- Parameters:
1202
-
1203
- - `numerator` (`number`): The numerator
1204
- - `denominator` (`number`): The denominator
1205
-
1206
- **Returns**: A new `Rational` instance
1207
-
1208
- #### Properties
1209
-
1210
- ##### `Rational.numerator`
1211
-
1212
- Gets the numerator.
1213
-
1214
- **Returns**: `number`
1215
-
1216
- ##### `Rational.denominator`
1217
-
1218
- Gets the denominator.
1219
-
1220
- **Returns**: `number`
1221
-
1222
- ##### `Rational.valid`
1223
-
1224
- Returns if true if rational describes a non-zero & non-negative quantity.
1225
-
1226
- **Returns**: `number`
1227
-
1228
- ##### `Rational.uninitialized`
1229
-
1230
- Returns if true when is not set.
1231
-
1232
- **Returns**: `number`
1233
-
1234
- ##### `Rational.toNumber()`
1235
-
1236
- see `av_q2d()`
1237
-
1238
- **Returns**: `number`
1239
-
1240
- ##### `static Rational.from(number)`
1241
-
1242
- see `av_d2q()`
1243
-
1244
- **Returns**: `Rational`
1245
-
1246
- ##### `Rational.rescaleQ(number, timebaseA, timebaseB)`
1247
-
1248
- see `av_rescale_q()`
1249
-
1250
- **Returns**: `number`
1251
-
1252
- ### `Stream`
1253
-
1254
- The `Stream` API provides functionality to access media stream information and create decoders/encoders.
1255
-
1256
- ```js
1257
- const stream = new ffmpeg.Stream(handle)
1258
- ```
1259
-
1260
- Parameters:
1261
-
1262
- - `handle` (`ArrayBuffer`): Internal stream handle
1263
-
1264
- **Returns**: A new `Stream` instance
1265
-
1266
- > Streams are typically obtained from format contexts: `const stream = format.streams[0]`
1267
-
1268
- #### Properties
1269
-
1270
- ##### `Stream.id`
1271
-
1272
- Gets or sets the stream ID.
1273
-
1274
- **Returns**: `number`
1275
-
1276
- ##### `Stream.index`
1277
-
1278
- Gets the stream index.
1279
-
1280
- **Returns**: `number`
1281
-
1282
- ##### `Stream.codec`
1283
-
1284
- Gets the codec for this stream.
1285
-
1286
- **Returns**: `Codec` instance
1287
-
1288
- ##### `Stream.codecParameters`
1289
-
1290
- Gets the codec parameters for this stream.
1291
-
1292
- **Returns**: `CodecParameters` instance
1293
-
1294
- ##### `Stream.timeBase`
1295
-
1296
- Gets or sets the time base for the stream.
1297
-
1298
- **Returns**: `Rational` instance
1299
-
1300
- ##### `Stream.avgFramerate`
1301
-
1302
- Gets or sets the average framerate for video streams.
1303
-
1304
- **Returns**: `Rational` instance
1305
-
1306
- Example:
1307
-
1308
- ```js
1309
- const fps = stream.avgFramerate.toNumber()
1310
- ```
1311
-
1312
- ##### `Stream.duration`
1313
-
1314
- Gets or sets the duration of the stream in time base units.
1315
-
1316
- **Returns**: `number` - Duration in time base units, or `0` if unknown
1317
-
1318
- #### Methods
1319
-
1320
- ##### `Stream.decoder()`
1321
-
1322
- Creates a decoder for this stream.
1323
-
1324
- **Returns**: `CodecContext` instance
1325
-
1326
- ##### `Stream.encoder()`
1327
-
1328
- Creates an encoder for this stream.
1329
-
1330
- **Returns**: `CodecContext` instance
1331
-
1332
- ### `Resampler`
1333
-
1334
- The `Resampler` API provides functionality to convert audio between different sample rates, channel layouts, and sample formats.
1335
-
1336
- ```js
1337
- const resampler = new ffmpeg.Resampler(
1338
- inputSampleRate,
1339
- inputChannelLayout,
1340
- inputSampleFormat,
1341
- outputSampleRate,
1342
- outputChannelLayout,
1343
- outputSampleFormat
1344
- )
1345
- ```
1346
-
1347
- Parameters:
1348
-
1349
- - `inputSampleRate` (`number`): Input sample rate in Hz
1350
- - `inputChannelLayout` (`number`): Input channel layout constant
1351
- - `inputSampleFormat` (`number`): Input sample format constant
1352
- - `outputSampleRate` (`number`): Output sample rate in Hz
1353
- - `outputChannelLayout` (`number`): Output channel layout constant
1354
- - `outputSampleFormat` (`number`): Output sample format constant
1355
-
1356
- **Returns**: A new `Resampler` instance
1357
-
1358
- #### Properties
1359
-
1360
- ##### `Resampler.inputSampleRate`
1361
-
1362
- Gets the input sample rate.
1363
-
1364
- **Returns**: `number`
1365
-
1366
- ##### `Resampler.outputSampleRate`
1367
-
1368
- Gets the output sample rate.
1369
-
1370
- **Returns**: `number`
1371
-
1372
- ##### `Resampler.delay`
1373
-
1374
- Gets the resampler delay in samples.
1375
-
1376
- **Returns**: `number`
1377
-
1378
- #### Methods
1379
-
1380
- ##### `Resampler.convert(inputFrame, outputFrame)`
1381
-
1382
- Converts audio data from input frame to output frame.
1383
-
1384
- Parameters:
1385
-
1386
- - `inputFrame` (`Frame`): The input audio frame
1387
- - `outputFrame` (`Frame`): The output audio frame
1388
-
1389
- **Returns**: `number` of samples converted
1390
-
1391
- ##### `Resampler.flush(outputFrame)`
1392
-
1393
- Flushes any remaining samples in the resampler.
1394
-
1395
- Parameters:
1396
-
1397
- - `outputFrame` (`Frame`): The output audio frame
1398
-
1399
- **Returns**: `number` of samples flushed
1400
-
1401
- ##### `Resampler.destroy()`
1402
-
1403
- Destroys the `Resampler` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
1404
-
1405
- **Returns**: `void`
1406
-
1407
- ### `Scaler`
1408
-
1409
- The `Scaler` API provides functionality to scale and convert video frames between different pixel formats and resolutions.
1410
-
1411
- ```js
1412
- const scaler = new ffmpeg.Scaler(
1413
- sourcePixelFormat,
1414
- sourceWidth,
1415
- sourceHeight,
1416
- targetPixelFormat,
1417
- targetWidth,
1418
- targetHeight
1419
- )
1420
- ```
1421
-
1422
- Parameters:
1423
-
1424
- - `sourcePixelFormat` (`number` | `string`): Source pixel format
1425
- - `sourceWidth` (`number`): Source width in pixels
1426
- - `sourceHeight` (`number`): Source height in pixels
1427
- - `targetPixelFormat` (`number` | `string`): Target pixel format
1428
- - `targetWidth` (`number`): Target width in pixels
1429
- - `targetHeight` (`number`): Target height in pixels
1430
-
1431
- **Returns**: A new `Scaler` instance
1432
-
1433
- #### Methods
1434
-
1435
- ##### `Scaler.scale(source, target)`
1436
-
1437
- Scales a source frame to a target frame.
1438
-
1439
- Parameters:
1440
-
1441
- - `source` (`Frame`): The source frame
1442
- - `target` (`Frame`): The target frame
1443
-
1444
- **Returns**: `boolean` indicating success
1445
-
1446
- ##### `Scaler.scale(source, y, height, target)`
1447
-
1448
- Scales a portion of a source frame to a target frame.
1449
-
1450
- Parameters:
1451
-
1452
- - `source` (`Frame`): The source frame
1453
- - `y` (`number`): Starting Y coordinate
1454
- - `height` (`number`): Height to scale
1455
- - `target` (`Frame`): The target frame
1456
-
1457
- **Returns**: `boolean` indicating success
1458
-
1459
- ##### `Scaler.destroy()`
1460
-
1461
- Destroys the `Scaler` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
1462
-
1463
- **Returns**: `void`
1464
-
1465
- ### `Filter`
1466
-
1467
- The `Filter` API provides access to FFmpeg filters by name.
1468
-
1469
- ```js
1470
- const filter = new ffmpeg.Filter(name)
1471
- ```
1472
-
1473
- Parameters:
1474
-
1475
- - `name` (`string`): The filter name (e.g., `'scale'`, `'buffer'`, `'overlay'`)
1476
-
1477
- **Returns**: A new `Filter` instance
1478
-
1479
- #### Example
1480
-
1481
- ```js
1482
- const filter = new ffmpeg.Filter('buffer')
1483
- ```
1484
-
1485
- ### `FilterGraph`
1486
-
1487
- The `FilterGraph` API provides functionality to create and manage complex filter chains for audio and video processing.
1488
-
1489
- ```js
1490
- const graph = new ffmpeg.FilterGraph()
1491
- ```
1492
-
1493
- **Returns**: A new `FilterGraph` instance
1494
-
1495
- #### Methods
1496
-
1497
- ##### `FilterGraph.createFilter(context, filter, name, args)`
1498
-
1499
- Creates a filter within the filter graph with the specified parameters.
1500
-
1501
- Parameters:
1502
-
1503
- - `context` (`FilterContext`): The filter context to associate with this filter
1504
- - `filter` (`Filter`): The filter to create (e.g., `new ffmpeg.Filter('buffer')`)
1505
- - `name` (`string`): A unique name for this filter instance
1506
- - `args` (`object` | `undefined`): Filter-specific arguments
1507
- - `width` (`number`): Video width in pixels
1508
- - `height` (`number`): Video height in pixels
1509
- - `pixelFormat` (`number`): Pixel format constant
1510
- - `timeBase` (`Rational`): Time base for the filter
1511
- - `aspectRatio` (`Rational`): Pixel aspect ratio
1512
-
1513
- **Returns**: `void`
1514
-
1515
- ```js
1516
- using graph = new ffmpeg.FilterGraph()
1517
- const context = new ffmpeg.FilterContext()
1518
- const filter = new ffmpeg.Filter('buffer')
1519
-
1520
- graph.createFilter(context, filter, 'in', {
1521
- width: 1920,
1522
- height: 1080,
1523
- pixelFormat: ffmpeg.constants.pixelFormats.RGB24,
1524
- timeBase: new ffmpeg.Rational(1, 30),
1525
- aspectRatio: new ffmpeg.Rational(1, 1)
1526
- })
1527
- ```
1528
-
1529
- ##### `FilterGraph.parse(filterDescription, inputs, outputs)`
1530
-
1531
- Parses a filter description string and applies it to the filter graph.
1532
-
1533
- Parameters:
1534
-
1535
- - `filterDescription` (`string`): The filter description (e.g., `'negate'`, `'scale=640:480'`)
1536
- - `inputs` (`FilterInOut`): Input filter endpoints
1537
- - `outputs` (`FilterInOut`): Output filter endpoints
1538
-
1539
- **Returns**: `void`
1540
-
1541
- ##### `FilterGraph.configure()`
1542
-
1543
- Configures the filter graph and validates all connections.
1544
-
1545
- **Returns**: `void`
1546
-
1547
- ##### `FilterGraph.destroy()`
1548
-
1549
- Destroys the `FilterGraph` and frees all associated resources including any created filters. Automatically called when the object is managed by a `using` declaration.
1550
-
1551
- **Returns**: `void`
1552
-
1553
- ### `AudioFIFO`
1554
-
1555
- The `AudioFIFO` API provides a first in first out buffer for audio samples. This is useful for buffering audio data between different processing stages.
1556
-
1557
- ```js
1558
- const fifo = new ffmpeg.AudioFIFO(sampleFormat, channels, nbSamples)
1559
- ```
1560
-
1561
- Parameters:
1562
-
1563
- - `sampleFormat` (`number` | `string`): The audio sample format
1564
- - `channels` (`number`): Number of audio channels
1565
- - `nbSamples` (`number`): Initial buffer size in samples
1566
-
1567
- **Returns**: A new `AudioFIFO` instance
1568
-
1569
- Example:
1570
-
1571
- ```js
1572
- const fifo = new ffmpeg.AudioFIFO(ffmpeg.constants.sampleFormats.S16, 2, 1024)
1573
7
  ```
1574
-
1575
- #### Properties
1576
-
1577
- ##### `AudioFIFO.size`
1578
-
1579
- Gets the number of samples currently in the FIFO.
1580
-
1581
- **Returns**: `number`
1582
-
1583
- ##### `AudioFIFO.space`
1584
-
1585
- Gets the number of samples that can be written to the FIFO.
1586
-
1587
- **Returns**: `number`
1588
-
1589
- #### Methods
1590
-
1591
- ##### `AudioFIFO.write(frame)`
1592
-
1593
- Writes samples from a frame to the FIFO. The FIFO will automatically grow if needed.
1594
-
1595
- Parameters:
1596
-
1597
- - `frame` (`Frame`): The audio frame containing samples to write
1598
-
1599
- **Returns**: `number` of samples written
1600
-
1601
- ##### `AudioFIFO.read(frame, nbSamples)`
1602
-
1603
- Reads samples from the FIFO into a frame.
1604
-
1605
- Parameters:
1606
-
1607
- - `frame` (`Frame`): The frame to read samples into
1608
- - `nbSamples` (`number`): Number of samples to read
1609
-
1610
- **Returns**: `number` of samples actually read
1611
-
1612
- ##### `AudioFIFO.peek(frame, nbSamples)`
1613
-
1614
- Reads samples from the FIFO without removing them.
1615
-
1616
- Parameters:
1617
-
1618
- - `frame` (`Frame`): The frame to read samples into
1619
- - `nbSamples` (`number`): Number of samples to peek
1620
-
1621
- **Returns**: `number` of samples peeked
1622
-
1623
- ##### `AudioFIFO.drain(nbSamples)`
1624
-
1625
- Removes samples from the FIFO without reading them.
1626
-
1627
- Parameters:
1628
-
1629
- - `nbSamples` (`number`): Number of samples to drain
1630
-
1631
- **Returns**: `void`
1632
-
1633
- ##### `AudioFIFO.reset()`
1634
-
1635
- Resets the FIFO to empty state.
1636
-
1637
- **Returns**: `void`
1638
-
1639
- ##### `AudioFIFO.destroy()`
1640
-
1641
- Destroys the `AudioFIFO` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
1642
-
1643
- **Returns**: `void`
1644
-
1645
- ### `FilterInOut`
1646
-
1647
- The `FilterInOut` API provides functionality to represent input and output pads for FFmpeg filter graphs.
1648
-
1649
- ```js
1650
- const filterInOut = new ffmpeg.FilterInOut()
8
+ npm i bare-ffmpeg
1651
9
  ```
1652
10
 
1653
- **Returns**: A new `FilterInOut` instance
1654
-
1655
- > **Note**: FilterInOut objects form a linked list structure in FFmpeg. When destroying a FilterInOut that has a `next` reference, the entire chain is automatically freed. Only destroy the head of the chain to avoid double-free errors.
11
+ ## API Documentation
1656
12
 
1657
- #### Properties
13
+ Complete API documentation for all components is available in the `/docs` directory:
1658
14
 
1659
- ##### `FilterInOut.name`
15
+ ### Core Components
1660
16
 
1661
- Gets or sets the name identifier for this input/output pad.
17
+ - [IOContext](docs/io-context.md) - Input/output context for media files with streaming support
18
+ - [Dictionary](docs/dictionary.md) - Key-value pairs for FFmpeg options
1662
19
 
1663
- **Returns**: `string` or `undefined` if not set
20
+ ### Codecs & Streams
1664
21
 
1665
- ```js
1666
- const filterInOut = new ffmpeg.FilterInOut()
1667
- filterInOut.name = 'input'
1668
- console.log(filterInOut.name) // 'input'
1669
- ```
22
+ - [Codec](docs/codec.md) - Access to FFmpeg codecs
23
+ - [CodecContext](docs/codec-context.md) - Encoding/decoding functionality
24
+ - [CodecParameters](docs/codec-parameters.md) - Codec parameter configuration
25
+ - [Stream](docs/stream.md) - Media stream information and operations
1670
26
 
1671
- ##### `FilterInOut.padIdx`
27
+ ### Formats
1672
28
 
1673
- Gets or sets the pad index within the filter context.
29
+ - [InputFormat](docs/input-format.md) - Input format specification
30
+ - [OutputFormat](docs/output-format.md) - Output format specification
31
+ - [FormatContext](docs/format-context.md) - Base class for media file handling
32
+ - [InputFormatContext](docs/input-format-context.md) - Reading media files
33
+ - [OutputFormatContext](docs/output-format-context.md) - Writing media files
1674
34
 
1675
- **Returns**: `number`
35
+ ### Data Structures
1676
36
 
1677
- ```js
1678
- filterInOut.padIdx = 0
1679
- console.log(filterInOut.padIdx) // 0
1680
- ```
37
+ - [Frame](docs/frame.md) - Decoded audio/video data
38
+ - [Packet](docs/packet.md) - Encoded audio/video data
39
+ - [SideData](docs/side-data.md) - Packet side data and metadata
40
+ - [Image](docs/image.md) - Raw pixel data management
41
+ - [Rational](docs/rational.md) - Rational number (fraction) representation
1681
42
 
1682
- ##### `FilterInOut.filterContext`
43
+ ### Processing
1683
44
 
1684
- Gets or sets the filter context associated with this input/output pad.
45
+ - [Scaler](docs/scaler.md) - Video scaling and pixel format conversion
46
+ - [Resampler](docs/resampler.md) - Audio resampling and format conversion
47
+ - [Filter](docs/filter.md) - FFmpeg filter access
48
+ - [FilterGraph](docs/filter-graph.md) - Filter chain management
49
+ - [FilterContext](docs/filter-context.md) - Filter instance representation
50
+ - [FilterInOut](docs/filter-in-out.md) - Filter input/output pads
51
+ - [AudioFIFO](docs/audio-fifo.md) - Audio sample buffering
1685
52
 
1686
- **Returns**: `FilterContext` instance or `null` if not set
53
+ ### Hardware Acceleration
1687
54
 
1688
- > **Note**: FilterContext must be created through FilterGraph operations before it can be used effectively.
55
+ - [HWDeviceContext](docs/hw-device-context.md) - Hardware device context for acceleration
56
+ - [HWFramesContext](docs/hw-frames-context.md) - Hardware frame pool management
57
+ - [HWFramesConstraints](docs/hw-frames-constraints.md) - Hardware capability information
1689
58
 
1690
- ##### `FilterInOut.next`
59
+ ### Utilities
1691
60
 
1692
- Gets or sets the next FilterInOut in the linked list chain.
61
+ - [Constants](docs/constants.md) - FFmpeg constants and utility functions
1693
62
 
1694
- **Returns**: `FilterInOut` instance or `null` if this is the last in the chain
63
+ ## Building
1695
64
 
1696
- ```js
1697
- const input = new ffmpeg.FilterInOut()
1698
- const output = new ffmpeg.FilterInOut()
1699
- output.name = 'output'
65
+ <https://github.com/holepunchto/bare-make> is used for compiling the native bindings in [`binding.cc`](binding.cc). Start by installing the tool globally:
1700
66
 
1701
- input.next = output
1702
- console.log(input.next.name) // 'output'
67
+ ```console
68
+ npm i -g bare-make
1703
69
  ```
1704
70
 
1705
- #### Methods
1706
-
1707
- ##### `FilterInOut.destroy()`
1708
-
1709
- Destroys the `FilterInOut` and frees all associated resources. **Important**: This automatically frees the entire linked list chain via FFmpeg's `avfilter_inout_free()`.
1710
-
1711
- **Returns**: `void`
1712
-
1713
- ```js
1714
- const head = new ffmpeg.FilterInOut()
1715
- const next = new ffmpeg.FilterInOut()
1716
- head.next = next
71
+ Next, generate the build system for compiling the bindings, optionally setting the `--debug` flag to enable debug symbols and assertions:
1717
72
 
1718
- // Only destroy the head - 'next' is automatically freed
1719
- head.destroy()
1720
- // DO NOT call next.destroy() - causes double-free error
73
+ ```console
74
+ bare-make generate [--debug]
1721
75
  ```
1722
76
 
1723
- ### Constants and Utilities
1724
-
1725
- The `constants` module provides utility functions for working with FFmpeg format constants and conversions.
1726
-
1727
- #### Methods
1728
-
1729
- ##### `ffmpeg.constants.toPixelFormat(format)`
1730
-
1731
- Converts a pixel format string or number to its corresponding constant value.
1732
-
1733
- Parameters:
1734
-
1735
- - `format` (`string` | `number`): The pixel format name (e.g., `'RGB24'`, `'YUV420P'`) or constant value
1736
-
1737
- **Returns**: `number` - The pixel format constant
1738
-
1739
- **Throws**: Error if the format is unknown or invalid type
77
+ This only has to be run once per repository checkout. When updating `bare-make` or your compiler toolchain it might also be necessary to regenerate the build system. To do so, run the command again with the `--no-cache` flag set to disregard the existing build system cache:
1740
78
 
1741
- ```js
1742
- const format = ffmpeg.constants.toPixelFormat('RGB24')
1743
- console.log(format) // Outputs the RGB24 constant value
79
+ ```console
80
+ bare-make generate [--debug] --no-cache
1744
81
  ```
1745
82
 
1746
- ##### `ffmpeg.constants.toSampleFormat(format)`
83
+ With a build system generated, the bindings can be compiled:
1747
84
 
1748
- Converts a sample format string or number to its corresponding constant value.
1749
-
1750
- Parameters:
1751
-
1752
- - `format` (`string` | `number`): The sample format name (e.g., `'S16'`, `'FLTP'`) or constant value
1753
-
1754
- **Returns**: `number` - The sample format constant
1755
-
1756
- **Throws**: Error if the format is unknown or invalid type
1757
-
1758
- ```js
1759
- const format = ffmpeg.constants.toSampleFormat('S16')
1760
- console.log(format) // Outputs the S16 constant value
85
+ ```console
86
+ bare-make build
1761
87
  ```
1762
88
 
1763
- ##### `ffmpeg.constants.toChannelLayout(layout)`
1764
-
1765
- Converts a channel layout string or number to its corresponding constant value.
1766
-
1767
- Parameters:
1768
-
1769
- - `layout` (`string` | `number`): The channel layout name (e.g., `'STEREO'`, `'5.1'`) or constant value
89
+ This will compile the bindings and output the resulting shared library module to the `build/` directory. To install it into the `prebuilds/` directory where the Bare addon resolution algorithm expects to find it, do:
1770
90
 
1771
- **Returns**: `number` - The channel layout constant
1772
-
1773
- **Throws**: Error if the layout is unknown or invalid type
1774
-
1775
- ```js
1776
- const layout = ffmpeg.constants.toChannelLayout('STEREO')
1777
- console.log(layout) // Outputs the STEREO constant value
91
+ ```console
92
+ bare-make install
1778
93
  ```
1779
94
 
1780
- ##### `ffmpeg.constants.getSampleFormatName(sampleFormat)`
1781
-
1782
- Gets the human-readable name of a sample format from its constant value.
1783
-
1784
- Parameters:
95
+ To make iteration faster during development, the shared library module can also be linked into the `prebuilds/` directory rather than copied. To do so, set the `--link` flag:
1785
96
 
1786
- - `sampleFormat` (`number`): The sample format constant
1787
-
1788
- **Returns**: `string` - The sample format name
1789
-
1790
- ```js
1791
- const name = ffmpeg.constants.getSampleFormatName(ffmpeg.constants.sampleFormats.S16)
1792
- console.log(name) // 's16'
97
+ ```console
98
+ bare-make install --link
1793
99
  ```
1794
100
 
1795
- ##### `ffmpeg.constants.getPixelFormatName(pixelFormat)`
101
+ Prior to publishing the module, make sure that no links exist within the `prebuilds/` directory as these will not be included in the resulting package archive.
1796
102
 
1797
- Gets the human-readable name of a pixel format from its constant value.
103
+ ### Options
1798
104
 
1799
- Parameters:
105
+ A few compile options can be configured to customize the addon. Compile options may be set by passing the `--define option=value` flag to the `bare-make generate` command when generating the build system.
1800
106
 
1801
- - `pixelFormat` (`number`): The pixel format constant
107
+ > [!WARNING]
108
+ > The compile options are not covered by semantic versioning and are subject to change without warning.
1802
109
 
1803
- **Returns**: `string` - The pixel format name
1804
-
1805
- ```js
1806
- const name = ffmpeg.constants.getPixelFormatName(ffmpeg.constants.pixelFormats.RGB24)
1807
- console.log(name) // 'rgb24'
1808
- ```
110
+ | Option | Default | Description |
111
+ | :----------------------- | :------ | :--------------------------------------- |
112
+ | `BARE_FFMPEG_ENABLE_GPL` | `OFF` | Enable GPL-licensed features (e.g, x264) |
1809
113
 
1810
114
  ## License
1811
115