bare-ffmpeg 1.0.0-7 → 1.0.0-9

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/CMakeLists.txt CHANGED
@@ -6,9 +6,9 @@ find_package(cmake-ports REQUIRED PATHS node_modules/cmake-ports)
6
6
 
7
7
  project(bare_ffmpeg C CXX)
8
8
 
9
- fetch_package("github:holepunchto/libjstl#096669a")
9
+ fetch_package("github:holepunchto/libjstl#5b85a24")
10
10
 
11
- find_port(ffmpeg FEATURES dav1d svt-av1 x264)
11
+ find_port(ffmpeg FEATURES gpl dav1d svt-av1 x264 opus)
12
12
 
13
13
  add_bare_module(bare_ffmpeg)
14
14
 
package/README.md CHANGED
@@ -6,6 +6,815 @@ Low-level FFmpeg bindings for Bare.
6
6
  npm i bare-ffmpeg
7
7
  ```
8
8
 
9
+ ## API
10
+
11
+ ### `IOContext`
12
+
13
+ The `IOContext` API provides functionality to create input/output contexts for media files.
14
+
15
+ ```js
16
+ const io = new ffmpeg.IOContext(buffer)
17
+ ```
18
+
19
+ Parameters:
20
+
21
+ - `buffer` (`Buffer`): The media data buffer
22
+
23
+ **Returns**: A new `IOContext` instance
24
+
25
+ Example:
26
+
27
+ ```js
28
+ const image = require('./fixtures/image/sample.jpeg', {
29
+ with: { type: 'binary' }
30
+ })
31
+ const io = new ffmpeg.IOContext(image)
32
+ io.destroy()
33
+ ```
34
+
35
+ #### Methods
36
+
37
+ ##### `IOContext.destroy()`
38
+
39
+ Destroys the `IOContext` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
40
+
41
+ **Returns**: `void`
42
+
43
+ ### `FormatContext`
44
+
45
+ The `FormatContext` API provides the base functionality for reading and writing media files.
46
+
47
+ > This is the base class that `InputFormatContext` and `OutputFormatContext` extend.
48
+
49
+ #### Properties
50
+
51
+ ##### `FormatContext.io`
52
+
53
+ Gets the IO context associated with this format context.
54
+
55
+ **Returns**: `IOContext` instance or `null`
56
+
57
+ ##### `FormatContext.streams`
58
+
59
+ Gets the array of media streams.
60
+
61
+ **Returns**: Array of `Stream` instances
62
+
63
+ #### Methods
64
+
65
+ ##### `FormatContext.readFrame(packet)`
66
+
67
+ Reads the next frame from the media file into a packet.
68
+
69
+ Parameters:
70
+
71
+ - `packet` (`Packet`): The packet to store the frame data
72
+
73
+ **Returns**: `boolean` indicating if a frame was read
74
+
75
+ ##### `FormatContext.getBestStream(type)`
76
+
77
+ Gets the best stream of the specified media type.
78
+
79
+ Parameters:
80
+
81
+ - `type` (`number`): The media type from `ffmpeg.constants.mediaTypes`
82
+
83
+ **Returns**: `Stream` instance or `null` if not found
84
+
85
+ ##### `FormatContext.destroy()`
86
+
87
+ Destroys the `FormatContext` and frees all associated resources including streams. Automatically called when the object is managed by a `using` declaration.
88
+
89
+ **Returns**: `void`
90
+
91
+ ### `InputFormatContext`
92
+
93
+ The `InputFormatContext` API extends `FormatContext` to provide functionality for reading media files.
94
+
95
+ ```js
96
+ const format = new ffmpeg.InputFormatContext(io, options[, url])
97
+ ```
98
+
99
+ Parameters:
100
+
101
+ - `io` (`IOContext` | `InputFormat`): The IO context or input format. The ownership of `io` is transferred.
102
+ - `options` (`Dictionary`): Format options. Required when using `InputFormat`, ignored when using `IOContext`. The ownership of `options` is transferred.
103
+ - `url` (`string`, optional): Media source URL. Defaults to a platform-specific value
104
+
105
+ **Returns**: A new `InputFormatContext` instance
106
+
107
+ #### Methods
108
+
109
+ ##### `InputFormatContext.destroy()`
110
+
111
+ Destroys the `InputFormatContext` and closes the input format. Automatically called when the object is managed by a `using` declaration.
112
+
113
+ **Returns**: void
114
+
115
+ ### `OutputFormatContext`
116
+
117
+ The `OutputFormatContext` API extends `FormatContext` to provide functionality for writing media files.
118
+
119
+ ```js
120
+ const format = new ffmpeg.OutputFormatContext(formatName, io)
121
+ ```
122
+
123
+ Parameters:
124
+
125
+ - `formatName` (`string`): The output format name (e.g., `'mp4'`, `'avi'`)
126
+ - `io` (`IOContext`): The IO context for writing. The ownership of `io` is transferred.
127
+
128
+ **Returns**: A new `OutputFormatContext` instance
129
+
130
+ #### Methods
131
+
132
+ ##### `OutputFormatContext.createStream(codec)`
133
+
134
+ Creates a new stream in the output format.
135
+
136
+ Parameters:
137
+
138
+ - `codec` (`Codec`): The codec to use for the stream
139
+
140
+ **Returns**: A new `Stream` instance
141
+
142
+ ##### `OutputFormatContext.destroy()`
143
+
144
+ Destroys the `OutputFormatContext` and closes the output format. Automatically called when the object is managed by a `using` declaration.
145
+
146
+ **Returns**: `void`
147
+
148
+ ### `Codec`
149
+
150
+ The `Codec` API provides access to FFmpeg codecs for encoding and decoding.
151
+
152
+ #### Static properties
153
+
154
+ ##### `Codec.H264`
155
+
156
+ H.264 video codec.
157
+
158
+ **Returns**: `Codec` instance
159
+
160
+ ##### `Codec.MJPEG`
161
+
162
+ Motion JPEG video codec.
163
+
164
+ **Returns**: `Codec` instance
165
+
166
+ ##### `Codec.AAC`
167
+
168
+ AAC audio codec.
169
+
170
+ **Returns**: `Codec` instance
171
+
172
+ ##### `Codec.AV1`
173
+
174
+ AV1 video codec.
175
+
176
+ **Returns**: `Codec` instance
177
+
178
+ #### Properties
179
+
180
+ ##### `Codec.id`
181
+
182
+ Gets the codec ID.
183
+
184
+ **Returns**: `number`
185
+
186
+ ##### `Codec.encoder`
187
+
188
+ Gets the encoder for this codec.
189
+
190
+ **Returns**: `Encoder` instance
191
+
192
+ ##### `Codec.decoder`
193
+
194
+ Gets the decoder for this codec.
195
+
196
+ **Returns**: `Decoder` instance
197
+
198
+ #### Methods
199
+
200
+ ##### `Codec.for(id)`
201
+
202
+ Gets a codec by ID.
203
+
204
+ Parameters:
205
+
206
+ - `id` (`number`): The codec ID
207
+
208
+ **Returns**: `Codec` instance
209
+
210
+ ### `CodecContext`
211
+
212
+ The `CodecContext` API provides functionality to encode or decode media frames.
213
+
214
+ ```js
215
+ const codecCtx = new ffmpeg.CodecContext(codec)
216
+ ```
217
+
218
+ Parameters:
219
+
220
+ - `codec` (Codec): The codec to use (e.g., `ffmpeg.Codec.H264.encoder`)
221
+
222
+ **Returns**: A new `CodecContext` instance
223
+
224
+ #### Properties
225
+
226
+ ##### `CodecContext.timeBase`
227
+
228
+ Gets or sets the time base for the codec context.
229
+
230
+ **Returns**: `Rational` instance
231
+
232
+ ##### `CodecContext.pixelFormat`
233
+
234
+ Gets or sets the pixel format for video codecs.
235
+
236
+ **Returns**: `number` (pixel format constant)
237
+
238
+ ##### `CodecContext.width`
239
+
240
+ Gets or sets the frame width for video codecs.
241
+
242
+ **Returns**: `number`
243
+
244
+ ##### `CodecContext.height`
245
+
246
+ Gets or sets the frame height for video codecs.
247
+
248
+ **Returns**: `number`
249
+
250
+ #### Methods
251
+
252
+ ##### `CodecContext.open([options])`
253
+
254
+ Opens the codec context for encoding/decoding.
255
+
256
+ Parameters:
257
+
258
+ - `options` (`Dictionary`, optional): Codec-specific options
259
+
260
+ **Returns**: `CodecContext` instance (for chaining)
261
+
262
+ ##### `CodecContext.sendFrame(frame)`
263
+
264
+ Sends a frame to the encoder.
265
+
266
+ Parameters:
267
+
268
+ - `frame` (`Frame`): The frame to encode
269
+
270
+ **Returns**: `boolean` indicating if the frame was sent
271
+
272
+ ##### `CodecContext.receiveFrame(frame)`
273
+
274
+ Receives a decoded frame from the decoder.
275
+
276
+ Parameters:
277
+
278
+ - `frame` (`Frame`): The frame to store the decoded data
279
+
280
+ **Returns**: `boolean` indicating if a frame was received
281
+
282
+ ##### `CodecContext.sendPacket(packet)`
283
+
284
+ Sends a packet to the decoder.
285
+
286
+ Parameters:
287
+
288
+ - `packet` (`Packet`): The packet to decode
289
+
290
+ **Returns**: `boolean` indicating if the packet was sent
291
+
292
+ ##### `CodecContext.receivePacket(packet)`
293
+
294
+ Receives an encoded packet from the encoder.
295
+
296
+ Parameters:
297
+
298
+ - `packet` (`Packet`): The packet to store the encoded data
299
+
300
+ **Returns**: `boolean` indicating if a packet was received
301
+
302
+ ##### `CodecContext.destroy()`
303
+
304
+ Destroys the `CodecContext` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
305
+
306
+ **Returns**: `void`
307
+
308
+ ### `CodecParameters`
309
+
310
+ The `CodecParameters` API provides functionality to access codec parameters from streams.
311
+
312
+ ```js
313
+ const params = stream.codecParameters // Get from stream
314
+ ```
315
+
316
+ #### Properties
317
+
318
+ ##### `CodecParameters.bitRate`
319
+
320
+ Gets the bit rate.
321
+
322
+ **Returns**: `number`
323
+
324
+ ##### `CodecParameters.bitsPerCodedSample`
325
+
326
+ Gets the bits per coded sample.
327
+
328
+ **Returns**: `number`
329
+
330
+ ##### `CodecParameters.bitsPerRawSample`
331
+
332
+ Gets the bits per raw sample.
333
+
334
+ **Returns**: `number`
335
+
336
+ ##### `CodecParameters.sampleRate`
337
+
338
+ Gets the sample rate for audio codecs.
339
+
340
+ **Returns**: `number`
341
+
342
+ #### Methods
343
+
344
+ ##### `CodecParameters.fromContext(context)`
345
+
346
+ Copies parameters from a codec context.
347
+
348
+ Parameters:
349
+
350
+ - `context` (`CodecContext`): The codec context
351
+
352
+ **Returns**: `void`
353
+
354
+ ##### `CodecParameters.toContext(context)`
355
+
356
+ Copies parameters to a codec context.
357
+
358
+ Parameters:
359
+
360
+ - `context` (`CodecContext`): The codec context
361
+
362
+ **Returns**: `void`
363
+
364
+ ##### `CodecParameters.destroy()`
365
+
366
+ Destroys the `CodecParameters` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
367
+
368
+ **Returns**: `void`
369
+
370
+ ### `InputFormat`
371
+
372
+ The `InputFormat` API provides functionality to specify input format for media sources.
373
+
374
+ ```js
375
+ const format = new ffmpeg.InputFormat([name])
376
+ ```
377
+
378
+ Parameters:
379
+
380
+ - `name` (`string`, optional): The input format name. Defaults to a platform-specific value:
381
+ - `darwin`, `ios`: ``'avfoundation'`
382
+ - `linux`: `'v4l2'`
383
+ - `win32`: `'dshow'`
384
+
385
+ **Returns**: A new `InputFormat` instance
386
+
387
+ #### Methods
388
+
389
+ ##### `InputFormat.destroy()`
390
+
391
+ Destroys the `InputFormat` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
392
+
393
+ **Returns**: `void`
394
+
395
+ ### `OutputFormat`
396
+
397
+ The `OutputFormat` API provides functionality to specify output format for media files.
398
+
399
+ ```js
400
+ const format = new ffmpeg.OutputFormat(name)
401
+ ```
402
+
403
+ Parameters:
404
+
405
+ - `name` (`string`): The output format name (e.g., `'mp4'`, `'avi'`, `'mov'`)
406
+
407
+ **Returns**: A new `OutputFormat` instance
408
+
409
+ #### Methods
410
+
411
+ ##### `OutputFormat.destroy()`
412
+
413
+ Destroys the `OutputFormat` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
414
+
415
+ **Returns**: `void`
416
+
417
+ ### `Frame`
418
+
419
+ This structure describes decoded (raw) audio or video data.
420
+
421
+ ```js
422
+ const frame = new ffmpeg.Frame()
423
+ ```
424
+
425
+ **Returns**: A new `Frame` instance
426
+
427
+ #### Properties
428
+
429
+ ##### `Frame.width`
430
+
431
+ Gets or sets the frame width.
432
+
433
+ **Returns**: `number`
434
+
435
+ ##### `Frame.height`
436
+
437
+ Gets or sets the frame height.
438
+
439
+ **Returns**: `number`
440
+
441
+ ##### `Frame.pixelFormat`
442
+
443
+ Gets or sets the pixel format.
444
+
445
+ **Returns**: `number` (pixel format constant)
446
+
447
+ ##### `Frame.format`
448
+
449
+ Gets or sets the sample format for audio frames.
450
+
451
+ **Returns**: `number` (sample format constant)
452
+
453
+ ##### `Frame.channelLayout`
454
+
455
+ Gets or sets the channel layout for audio frames.
456
+
457
+ **Returns**: `number` (channel layout constant)
458
+
459
+ ##### `Frame.nbSamples`
460
+
461
+ Gets or sets the number of audio samples.
462
+
463
+ **Returns**: `number`
464
+
465
+ #### Methods
466
+
467
+ ##### `Frame.alloc()`
468
+
469
+ Allocates memory for the frame data.
470
+
471
+ **Returns**: `void`
472
+
473
+ ##### `Frame.destroy()`
474
+
475
+ Destroys the `Frame` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
476
+
477
+ **Returns**: `void`
478
+
479
+ ### `Packet`
480
+
481
+ 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.
482
+
483
+ ```js
484
+ const packet = new ffmpeg.Packet([buffer])
485
+ ```
486
+
487
+ Parameters:
488
+
489
+ - `buffer` (`Buffer`, optional): Initial packet data
490
+
491
+ **Returns**: A new `Packet` instance
492
+
493
+ #### Properties
494
+
495
+ ##### `Packet.data`
496
+
497
+ Gets the packet data buffer.
498
+
499
+ **Returns**: `Buffer`
500
+
501
+ ##### `Packet.streamIndex`
502
+
503
+ Gets the stream index this packet belongs to.
504
+
505
+ **Returns**: `number`
506
+
507
+ #### Methods
508
+
509
+ ##### `Packet.unref()`
510
+
511
+ Decrements the reference count and unreferences the packet.
512
+
513
+ **Returns**: `void`
514
+
515
+ ##### `Packet.destroy()`
516
+
517
+ Destroys the `Packet` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
518
+
519
+ **Returns**: `void`
520
+
521
+ ### `Image`
522
+
523
+ The `Image` API provides functionality to create and manage image buffers.
524
+
525
+ ```js
526
+ const image = new ffmpeg.Image(pixelFormat, width, height[, align])
527
+ ```
528
+
529
+ Parameters:
530
+
531
+ - `pixelFormat` (`number` | `string`): The pixel format
532
+ - `width` (`number`): The image width in pixels
533
+ - `height` (`number`): The image height in pixels
534
+ - `align` (`number`, optional): Memory alignment. Defaults to 1
535
+
536
+ **Returns**: A new `Image` instance
537
+
538
+ #### Properties
539
+
540
+ ##### `Image.pixelFormat`
541
+
542
+ Gets the pixel format.
543
+
544
+ **Returns**: `number`
545
+
546
+ ##### `Image.width`
547
+
548
+ Gets the image width.
549
+
550
+ **Returns**: `number`
551
+
552
+ ##### `Image.height`
553
+
554
+ Gets the image height.
555
+
556
+ **Returns**: `number`
557
+
558
+ ##### `Image.align`
559
+
560
+ Gets the memory alignment.
561
+
562
+ **Returns**: `number`
563
+
564
+ ##### `Image.data`
565
+
566
+ Gets the image data buffer.
567
+
568
+ **Returns**: `Buffer`
569
+
570
+ #### Methods
571
+
572
+ ##### `Image.fill(frame)`
573
+
574
+ Fills a frame with the image data.
575
+
576
+ Parameters:
577
+
578
+ - `frame` (`Frame`): The frame to fill
579
+
580
+ **Returns**: void
581
+
582
+ ##### `Image.lineSize([plane])`
583
+
584
+ Gets the line size for a specific plane.
585
+
586
+ Parameters:
587
+
588
+ - `plane` (`number`, optional): Plane index. Defaults to 0
589
+
590
+ **Returns**: `number`
591
+
592
+ #### Static methods
593
+
594
+ ##### `Image.lineSize(pixelFormat, width[, plane])`
595
+
596
+ Static method to get line size for a pixel format.
597
+
598
+ Parameters:
599
+
600
+ - `pixelFormat` (`number` | `string`): The pixel format
601
+ - `width` (`number`): The image width
602
+ - `plane` (`number`, optional): Plane index. Defaults to 0
603
+
604
+ **Returns**: `number`
605
+
606
+ ### `Rational`
607
+
608
+ The `Rational` API provides functionality to represent rational numbers (fractions).
609
+
610
+ ```js
611
+ const rational = new ffmpeg.Rational(numerator, denominator)
612
+ ```
613
+
614
+ Parameters:
615
+
616
+ - `numerator` (`number`): The numerator
617
+ - `denominator` (`number`): The denominator
618
+
619
+ **Returns**: A new `Rational` instance
620
+
621
+ #### Properties
622
+
623
+ ##### `Rational.numerator`
624
+
625
+ Gets the numerator.
626
+
627
+ **Returns**: `number`
628
+
629
+ ##### `Rational.denominator`
630
+
631
+ Gets the denominator.
632
+
633
+ **Returns**: `number`
634
+
635
+ ### `Stream`
636
+
637
+ The `Stream` API provides functionality to access media stream information and create decoders/encoders.
638
+
639
+ ```js
640
+ const stream = new ffmpeg.Stream(handle)
641
+ ```
642
+
643
+ Parameters:
644
+
645
+ - `handle` (`ArrayBuffer`): Internal stream handle
646
+
647
+ **Returns**: A new `Stream` instance
648
+
649
+ > Streams are typically obtained from format contexts: `const stream = format.streams[0]`
650
+
651
+ #### Properties
652
+
653
+ ##### `Stream.codecParameters`
654
+
655
+ Gets the codec parameters for this stream.
656
+
657
+ **Returns**: `CodecParameters` instance
658
+
659
+ ##### `Stream.codec`
660
+
661
+ Gets the codec for this stream
662
+
663
+ **Returns**: `Codec` instance
664
+
665
+ #### Methods
666
+
667
+ ##### `Stream.decoder()`
668
+
669
+ Creates and opens a decoder for this stream.
670
+
671
+ **Returns**: `CodecContext` instance
672
+
673
+ ##### `Stream.encoder()`
674
+
675
+ Creates and opens an encoder for this stream.
676
+
677
+ **Returns**: `CodecContext` instance
678
+
679
+ ##### `Stream.destroy()`
680
+
681
+ Destroys the `Stream` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
682
+
683
+ **Returns**: `void`
684
+
685
+ ### `Resampler`
686
+
687
+ The `Resampler` API provides functionality to convert audio between different sample rates, channel layouts, and sample formats.
688
+
689
+ ```js
690
+ const resampler = new ffmpeg.Resampler(
691
+ inputSampleRate,
692
+ inputChannelLayout,
693
+ inputSampleFormat,
694
+ outputSampleRate,
695
+ outputChannelLayout,
696
+ outputSampleFormat
697
+ )
698
+ ```
699
+
700
+ Parameters:
701
+
702
+ - `inputSampleRate` (`number`): Input sample rate in Hz
703
+ - `inputChannelLayout` (`number`): Input channel layout constant
704
+ - `inputSampleFormat` (`number`): Input sample format constant
705
+ - `outputSampleRate` (`number`): Output sample rate in Hz
706
+ - `outputChannelLayout` (`number`): Output channel layout constant
707
+ - `outputSampleFormat` (`number`): Output sample format constant
708
+
709
+ **Returns**: A new `Resampler` instance
710
+
711
+ #### Properties
712
+
713
+ ##### `Resampler.inputSampleRate`
714
+
715
+ Gets the input sample rate.
716
+
717
+ **Returns**: `number`
718
+
719
+ ##### `Resampler.outputSampleRate`
720
+
721
+ Gets the output sample rate.
722
+
723
+ **Returns**: `number`
724
+
725
+ ##### `Resampler.delay`
726
+
727
+ Gets the resampler delay in samples.
728
+
729
+ **Returns**: `number`
730
+
731
+ #### Methods
732
+
733
+ ##### `Resampler.convert(inputFrame, outputFrame)`
734
+
735
+ Converts audio data from input frame to output frame.
736
+
737
+ Parameters:
738
+
739
+ - `inputFrame` (`Frame`): The input audio frame
740
+ - `outputFrame` (`Frame`): The output audio frame
741
+
742
+ **Returns**: `number` of samples converted
743
+
744
+ ##### `Resampler.flush(outputFrame)`
745
+
746
+ Flushes any remaining samples in the resampler.
747
+
748
+ Parameters:
749
+
750
+ - `outputFrame` (`Frame`): The output audio frame
751
+
752
+ **Returns**: `number` of samples flushed
753
+
754
+ ##### `Resampler.destroy()`
755
+
756
+ Destroys the `Resampler` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
757
+
758
+ **Returns**: `void`
759
+
760
+ ### `Scaler`
761
+
762
+ The `Scaler` API provides functionality to scale and convert video frames between different pixel formats and resolutions.
763
+
764
+ ```js
765
+ const scaler = new ffmpeg.Scaler(
766
+ sourcePixelFormat,
767
+ sourceWidth,
768
+ sourceHeight,
769
+ targetPixelFormat,
770
+ targetWidth,
771
+ targetHeight
772
+ )
773
+ ```
774
+
775
+ Parameters:
776
+
777
+ - `sourcePixelFormat` (`number` | `string`): Source pixel format
778
+ - `sourceWidth` (`number`): Source width in pixels
779
+ - `sourceHeight` (`number`): Source height in pixels
780
+ - `targetPixelFormat` (`number` | `string`): Target pixel format
781
+ - `targetWidth` (`number`): Target width in pixels
782
+ - `targetHeight` (`number`): Target height in pixels
783
+
784
+ **Returns**: A new `Scaler` instance
785
+
786
+ #### Methods
787
+
788
+ ##### `Scaler.scale(source, target)`
789
+
790
+ Scales a source frame to a target frame.
791
+
792
+ Parameters:
793
+
794
+ - `source` (`Frame`): The source frame
795
+ - `target` (`Frame`): The target frame
796
+
797
+ **Returns**: `boolean` indicating success
798
+
799
+ ##### `Scaler.scale(source, y, height, target)`
800
+
801
+ Scales a portion of a source frame to a target frame.
802
+
803
+ Parameters:
804
+
805
+ - `source` (`Frame`): The source frame
806
+ - `y` (`number`): Starting Y coordinate
807
+ - `height` (`number`): Height to scale
808
+ - `target` (`Frame`): The target frame
809
+
810
+ **Returns**: `boolean` indicating success
811
+
812
+ ##### `Scaler.destroy()`
813
+
814
+ Destroys the `Scaler` and frees all associated resources. Automatically called when the object is managed by a `using` declaration.
815
+
816
+ **Returns**: `void`
817
+
9
818
  ## License
10
819
 
11
820
  Apache-2.0
@@ -1,17 +1,14 @@
1
1
  const binding = require('../binding')
2
- const ReferenceCounted = require('./reference-counted')
3
2
  const Rational = require('./rational')
4
3
 
5
- module.exports = class FFmpegCodecContext extends ReferenceCounted {
4
+ module.exports = class FFmpegCodecContext {
6
5
  constructor(codec) {
7
- super()
8
-
9
6
  this._codec = codec
10
7
  this._opened = false
11
8
  this._handle = binding.initCodecContext(codec._handle)
12
9
  }
13
10
 
14
- _destroy() {
11
+ destroy() {
15
12
  binding.destroyCodecContext(this._handle)
16
13
  this._handle = null
17
14
  }
@@ -65,14 +62,11 @@ module.exports = class FFmpegCodecContext extends ReferenceCounted {
65
62
  }
66
63
 
67
64
  sendPacket(packet) {
68
- binding.sendCodecContextPacket(this._handle, packet._handle)
69
- return this
65
+ return binding.sendCodecContextPacket(this._handle, packet._handle)
70
66
  }
71
67
 
72
68
  receivePacket(packet) {
73
- const res = binding.receiveCodecContextPacket(this._handle, packet._handle)
74
- if (res) packet._ref()
75
- return res
69
+ return binding.receiveCodecContextPacket(this._handle, packet._handle)
76
70
  }
77
71
 
78
72
  sendFrame(frame) {
@@ -82,4 +76,8 @@ module.exports = class FFmpegCodecContext extends ReferenceCounted {
82
76
  receiveFrame(frame) {
83
77
  return binding.receiveCodecContextFrame(this._handle, frame._handle)
84
78
  }
79
+
80
+ [Symbol.dispose]() {
81
+ this.destroy()
82
+ }
85
83
  }
@@ -1,14 +1,11 @@
1
1
  const binding = require('../binding')
2
- const ReferenceCounted = require('./reference-counted')
3
2
 
4
- module.exports = class FFmpegCodecParameters extends ReferenceCounted {
3
+ module.exports = class FFmpegCodecParameters {
5
4
  constructor(handle) {
6
- super()
7
-
8
5
  this._handle = handle
9
6
  }
10
7
 
11
- _destroy() {
8
+ destroy() {
12
9
  this._handle = null
13
10
  }
14
11
 
@@ -35,4 +32,8 @@ module.exports = class FFmpegCodecParameters extends ReferenceCounted {
35
32
  toContext(context) {
36
33
  binding.codecParametersToContext(context._handle, this._handle)
37
34
  }
35
+
36
+ [Symbol.dispose]() {
37
+ this.destroy()
38
+ }
38
39
  }
package/lib/codec.js CHANGED
@@ -37,5 +37,6 @@ module.exports = class FFmpegCodec {
37
37
  static MJPEG = this.for(constants.codecs.MJPEG)
38
38
  static H264 = this.for(constants.codecs.H264)
39
39
  static AAC = this.for(constants.codecs.AAC)
40
+ static OPUS = this.for(constants.codecs.OPUS)
40
41
  static AV1 = this.for(constants.codecs.AV1)
41
42
  }
package/lib/constants.js CHANGED
@@ -5,6 +5,7 @@ module.exports = exports = {
5
5
  MJPEG: binding.AV_CODEC_ID_MJPEG,
6
6
  H264: binding.AV_CODEC_ID_H264,
7
7
  AAC: binding.AV_CODEC_ID_AAC,
8
+ OPUS: binding.AV_CODEC_ID_OPUS,
8
9
  AV1: binding.AV_CODEC_ID_AV1
9
10
  },
10
11
  pixelFormats: {
package/lib/dictionary.js CHANGED
@@ -1,14 +1,11 @@
1
1
  const binding = require('../binding')
2
- const ReferenceCounted = require('./reference-counted')
3
2
 
4
- module.exports = class FFmpegDictionary extends ReferenceCounted {
3
+ module.exports = class FFmpegDictionary {
5
4
  constructor() {
6
- super()
7
-
8
5
  this._handle = binding.initDictionary()
9
6
  }
10
7
 
11
- _destroy() {
8
+ destroy() {
12
9
  binding.destroyDictionary(this._handle)
13
10
  this._handle = null
14
11
  }
@@ -22,4 +19,8 @@ module.exports = class FFmpegDictionary extends ReferenceCounted {
22
19
  set(key, value) {
23
20
  binding.setDictionaryEntry(this._handle, key, value)
24
21
  }
22
+
23
+ [Symbol.dispose]() {
24
+ this.destroy()
25
+ }
25
26
  }
@@ -1,22 +1,19 @@
1
1
  const binding = require('../binding')
2
2
  const Stream = require('./stream.js')
3
- const ReferenceCounted = require('./reference-counted')
4
3
  const OutputFormat = require('./output-format')
5
4
  const IOContext = require('./io-context')
6
5
  const InputFormat = require('./input-format')
7
6
  const Dictionary = require('./dictionary')
8
7
 
9
- class FFmpegFormatContext extends ReferenceCounted {
8
+ class FFmpegFormatContext {
10
9
  constructor(io) {
11
- super()
12
-
13
- this._io = io ? io._ref() : null
10
+ this._io = io
14
11
  this._streams = []
15
12
  }
16
13
 
17
- _destroy() {
14
+ destroy() {
18
15
  if (this._io) {
19
- this._io._unref()
16
+ this._io.destroy()
20
17
  this._io = null
21
18
  }
22
19
 
@@ -33,13 +30,19 @@ class FFmpegFormatContext extends ReferenceCounted {
33
30
  }
34
31
 
35
32
  readFrame(packet) {
36
- if (packet._refs !== 0) {
37
- throw new Error('Cannot read into packet with active references')
33
+ return binding.readFormatContextFrame(this._handle, packet._handle)
34
+ }
35
+
36
+ getBestStreamIndex(type) {
37
+ return binding.getFormatContextBestStreamIndex(this._handle, type)
38
+ }
39
+
40
+ getStream(index) {
41
+ if (index < 0 || index >= this._streams.length) {
42
+ return null
38
43
  }
39
44
 
40
- const result = binding.readFormatContextFrame(this._handle, packet._handle)
41
- if (result) packet._ref()
42
- return result
45
+ return this._streams[index]
43
46
  }
44
47
 
45
48
  getBestStream(type) {
@@ -58,16 +61,23 @@ class FFmpegFormatContext extends ReferenceCounted {
58
61
 
59
62
  return this._streams[bestIndex]
60
63
  }
64
+
65
+ [Symbol.dispose]() {
66
+ this.destroy()
67
+ }
61
68
  }
62
69
 
63
70
  let defaultURL = null
64
71
 
65
72
  switch (Bare.platform) {
66
73
  case 'darwin':
67
- defaultURL = '0'
74
+ case 'ios':
75
+ defaultURL = '0:0'
68
76
  break
69
77
  case 'linux':
70
78
  // TODO: test on real machine
79
+ // we might need separate contexts for linux
80
+ // video: V4L2 and audio: ALSA/PulseAudio
71
81
  defaultURL = '/dev/video0'
72
82
  break
73
83
  case 'win32':
@@ -102,8 +112,8 @@ exports.InputFormatContext = class FFmpegInputFormatContext extends (
102
112
  }
103
113
  }
104
114
 
105
- _destroy() {
106
- super._destroy()
115
+ destroy() {
116
+ super.destroy()
107
117
 
108
118
  binding.closeInputFormatContext(this._handle)
109
119
  this._handle = null
@@ -121,8 +131,8 @@ exports.OutputFormatContext = class FFmpegOutputFormatContext extends (
121
131
  this._handle = binding.openOutputFormatContext(format._handle, io._handle)
122
132
  }
123
133
 
124
- _destroy() {
125
- super._destroy()
134
+ destroy() {
135
+ super.destroy()
126
136
 
127
137
  binding.closeOutputFormatContext(this._handle)
128
138
  this._handle = null
package/lib/frame.js CHANGED
@@ -1,14 +1,11 @@
1
1
  const binding = require('../binding')
2
- const ReferenceCounted = require('./reference-counted')
3
2
 
4
- module.exports = class FFmpegFrame extends ReferenceCounted {
3
+ module.exports = class FFmpegFrame {
5
4
  constructor() {
6
- super()
7
-
8
5
  this._handle = binding.initFrame()
9
6
  }
10
7
 
11
- _destroy() {
8
+ destroy() {
12
9
  binding.destroyFrame(this._handle)
13
10
  this._handle = null
14
11
  }
@@ -68,4 +65,8 @@ module.exports = class FFmpegFrame extends ReferenceCounted {
68
65
  alloc() {
69
66
  binding.allocFrame(this._handle, 32)
70
67
  }
68
+
69
+ [Symbol.dispose]() {
70
+ this.destroy()
71
+ }
71
72
  }
@@ -4,6 +4,7 @@ let defaultName = null
4
4
 
5
5
  switch (Bare.platform) {
6
6
  case 'darwin':
7
+ case 'ios':
7
8
  defaultName = 'avfoundation'
8
9
  break
9
10
  case 'linux':
package/lib/io-context.js CHANGED
@@ -1,10 +1,7 @@
1
1
  const binding = require('../binding')
2
- const ReferenceCounted = require('./reference-counted')
3
2
 
4
- module.exports = class FFmpegIOContext extends ReferenceCounted {
3
+ module.exports = class FFmpegIOContext {
5
4
  constructor(buffer = Buffer.alloc(0)) {
6
- super()
7
-
8
5
  this._handle = binding.initIOContext(
9
6
  buffer.buffer,
10
7
  buffer.byteOffset,
@@ -12,8 +9,12 @@ module.exports = class FFmpegIOContext extends ReferenceCounted {
12
9
  )
13
10
  }
14
11
 
15
- _destroy() {
12
+ destroy() {
16
13
  binding.destroyIOContext(this._handle)
17
14
  this._handle = null
18
15
  }
16
+
17
+ [Symbol.dispose]() {
18
+ this.destroy()
19
+ }
19
20
  }
package/lib/packet.js CHANGED
@@ -1,10 +1,7 @@
1
1
  const binding = require('../binding')
2
- const ReferenceCounted = require('./reference-counted')
3
2
 
4
- module.exports = class FFmpegPacket extends ReferenceCounted {
3
+ module.exports = class FFmpegPacket {
5
4
  constructor(buffer) {
6
- super()
7
-
8
5
  if (buffer) {
9
6
  this._handle = binding.initPacketFromBuffer(
10
7
  buffer.buffer,
@@ -16,13 +13,12 @@ module.exports = class FFmpegPacket extends ReferenceCounted {
16
13
  }
17
14
  }
18
15
 
19
- _destroy() {
16
+ destroy() {
20
17
  binding.unrefPacket(this._handle)
21
18
  this._handle = null
22
19
  }
23
20
 
24
21
  unref() {
25
- this._unref()
26
22
  binding.unrefPacket(this._handle)
27
23
  }
28
24
 
@@ -33,4 +29,8 @@ module.exports = class FFmpegPacket extends ReferenceCounted {
33
29
  get data() {
34
30
  return Buffer.from(binding.getPacketData(this._handle))
35
31
  }
32
+
33
+ [Symbol.dispose]() {
34
+ this.destroy()
35
+ }
36
36
  }
package/lib/resampler.js CHANGED
@@ -44,6 +44,10 @@ class Resampler {
44
44
  this._handle = null
45
45
  }
46
46
  }
47
+
48
+ [Symbol.dispose]() {
49
+ this.destroy()
50
+ }
47
51
  }
48
52
 
49
53
  module.exports = Resampler
package/lib/scaler.js CHANGED
@@ -1,8 +1,7 @@
1
1
  const binding = require('../binding')
2
2
  const constants = require('./constants')
3
- const ReferenceCounted = require('./reference-counted')
4
3
 
5
- module.exports = class FFmpegScaler extends ReferenceCounted {
4
+ module.exports = class FFmpegScaler {
6
5
  constructor(
7
6
  sourcePixelFormat,
8
7
  sourceWidth,
@@ -11,8 +10,6 @@ module.exports = class FFmpegScaler extends ReferenceCounted {
11
10
  targetWidth,
12
11
  targetHeight
13
12
  ) {
14
- super()
15
-
16
13
  sourcePixelFormat = constants.toPixelFormat(sourcePixelFormat)
17
14
  targetPixelFormat = constants.toPixelFormat(targetPixelFormat)
18
15
 
@@ -33,7 +30,7 @@ module.exports = class FFmpegScaler extends ReferenceCounted {
33
30
  )
34
31
  }
35
32
 
36
- _destroy() {
33
+ destroy() {
37
34
  binding.destroyScaler(this._handle)
38
35
  this._handle = null
39
36
  }
@@ -56,4 +53,8 @@ module.exports = class FFmpegScaler extends ReferenceCounted {
56
53
  target._handle
57
54
  )
58
55
  }
56
+
57
+ [Symbol.dispose]() {
58
+ this.destroy()
59
+ }
59
60
  }
package/lib/stream.js CHANGED
@@ -2,12 +2,9 @@ const binding = require('../binding')
2
2
  const Codec = require('./codec')
3
3
  const CodecContext = require('./codec-context')
4
4
  const CodecParameters = require('./codec-parameters')
5
- const ReferenceCounted = require('./reference-counted')
6
5
 
7
- module.exports = class FFmpegStream extends ReferenceCounted {
6
+ module.exports = class FFmpegStream {
8
7
  constructor(handle) {
9
- super()
10
-
11
8
  this._handle = handle
12
9
 
13
10
  this._codec = Codec.for(binding.getStreamCodec(this._handle))
@@ -17,7 +14,7 @@ module.exports = class FFmpegStream extends ReferenceCounted {
17
14
  )
18
15
  }
19
16
 
20
- _destroy() {
17
+ destroy() {
21
18
  this._codecParameters.destroy()
22
19
  this._codecParameters = null
23
20
  }
@@ -41,4 +38,8 @@ module.exports = class FFmpegStream extends ReferenceCounted {
41
38
  this._codecParameters.toContext(context)
42
39
  return context.open()
43
40
  }
41
+
42
+ [Symbol.dispose]() {
43
+ this.destroy()
44
+ }
44
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-ffmpeg",
3
- "version": "1.0.0-7",
3
+ "version": "1.0.0-9",
4
4
  "description": "Low-level FFmpeg bindings for Bare",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -1,39 +0,0 @@
1
- module.exports = class FFmpegReferenceCounted {
2
- constructor() {
3
- this._refs = 0
4
- this._destroyed = false
5
- }
6
-
7
- _ref() {
8
- if (this._destroyed === false) this._refs++
9
- return this
10
- }
11
-
12
- _unref() {
13
- if (this._refs === 0) {
14
- throw new Error('Cannot unreference object with no active references')
15
- }
16
-
17
- if (--this._refs === 0 && this._destroyed === true) this._destroy()
18
- return this
19
- }
20
-
21
- _destroy() {}
22
-
23
- destroy() {
24
- if (this._refs !== 0) {
25
- throw new Error('Cannot destroy object with active references')
26
- }
27
-
28
- if (this._destroyed === true) {
29
- throw new Error('Object has already been destroyed')
30
- }
31
-
32
- this._destroyed = true
33
- this._destroy()
34
- }
35
-
36
- [Symbol.dispose]() {
37
- this.destroy()
38
- }
39
- }