node-av 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -38
- package/dist/api/bitstream-filter.d.ts +180 -123
- package/dist/api/bitstream-filter.js +180 -125
- package/dist/api/bitstream-filter.js.map +1 -1
- package/dist/api/decoder.d.ts +279 -132
- package/dist/api/decoder.js +285 -142
- package/dist/api/decoder.js.map +1 -1
- package/dist/api/encoder.d.ts +246 -162
- package/dist/api/encoder.js +272 -208
- package/dist/api/encoder.js.map +1 -1
- package/dist/api/filter-presets.d.ts +690 -94
- package/dist/api/filter-presets.js +686 -102
- package/dist/api/filter-presets.js.map +1 -1
- package/dist/api/filter.d.ts +249 -213
- package/dist/api/filter.js +252 -242
- package/dist/api/filter.js.map +1 -1
- package/dist/api/hardware.d.ts +224 -117
- package/dist/api/hardware.js +380 -214
- package/dist/api/hardware.js.map +1 -1
- package/dist/api/index.d.ts +3 -3
- package/dist/api/index.js +1 -1
- package/dist/api/index.js.map +1 -1
- package/dist/api/io-stream.d.ts +65 -61
- package/dist/api/io-stream.js +43 -46
- package/dist/api/io-stream.js.map +1 -1
- package/dist/api/media-input.d.ts +242 -140
- package/dist/api/media-input.js +205 -103
- package/dist/api/media-input.js.map +1 -1
- package/dist/api/media-output.d.ts +206 -128
- package/dist/api/media-output.js +210 -128
- package/dist/api/media-output.js.map +1 -1
- package/dist/api/pipeline.d.ts +168 -38
- package/dist/api/pipeline.js +238 -14
- package/dist/api/pipeline.js.map +1 -1
- package/dist/api/types.d.ts +21 -187
- package/dist/api/utils.d.ts +1 -2
- package/dist/api/utils.js +9 -0
- package/dist/api/utils.js.map +1 -1
- package/dist/lib/audio-fifo.d.ts +127 -170
- package/dist/lib/audio-fifo.js +130 -173
- package/dist/lib/audio-fifo.js.map +1 -1
- package/dist/lib/binding.js +5 -0
- package/dist/lib/binding.js.map +1 -1
- package/dist/lib/bitstream-filter-context.d.ts +139 -184
- package/dist/lib/bitstream-filter-context.js +139 -188
- package/dist/lib/bitstream-filter-context.js.map +1 -1
- package/dist/lib/bitstream-filter.d.ts +68 -54
- package/dist/lib/bitstream-filter.js +68 -54
- package/dist/lib/bitstream-filter.js.map +1 -1
- package/dist/lib/codec-context.d.ts +316 -380
- package/dist/lib/codec-context.js +316 -381
- package/dist/lib/codec-context.js.map +1 -1
- package/dist/lib/codec-parameters.d.ts +160 -170
- package/dist/lib/codec-parameters.js +162 -172
- package/dist/lib/codec-parameters.js.map +1 -1
- package/dist/lib/codec-parser.d.ts +91 -104
- package/dist/lib/codec-parser.js +92 -103
- package/dist/lib/codec-parser.js.map +1 -1
- package/dist/lib/codec.d.ts +264 -281
- package/dist/lib/codec.js +268 -285
- package/dist/lib/codec.js.map +1 -1
- package/dist/lib/dictionary.d.ts +149 -203
- package/dist/lib/dictionary.js +158 -212
- package/dist/lib/dictionary.js.map +1 -1
- package/dist/lib/error.d.ts +96 -130
- package/dist/lib/error.js +98 -128
- package/dist/lib/error.js.map +1 -1
- package/dist/lib/filter-context.d.ts +284 -218
- package/dist/lib/filter-context.js +290 -227
- package/dist/lib/filter-context.js.map +1 -1
- package/dist/lib/filter-graph.d.ts +251 -292
- package/dist/lib/filter-graph.js +253 -294
- package/dist/lib/filter-graph.js.map +1 -1
- package/dist/lib/filter-inout.d.ts +87 -95
- package/dist/lib/filter-inout.js +87 -95
- package/dist/lib/filter-inout.js.map +1 -1
- package/dist/lib/filter.d.ts +93 -111
- package/dist/lib/filter.js +93 -111
- package/dist/lib/filter.js.map +1 -1
- package/dist/lib/format-context.d.ts +320 -428
- package/dist/lib/format-context.js +313 -385
- package/dist/lib/format-context.js.map +1 -1
- package/dist/lib/frame.d.ts +262 -405
- package/dist/lib/frame.js +263 -408
- package/dist/lib/frame.js.map +1 -1
- package/dist/lib/hardware-device-context.d.ts +149 -203
- package/dist/lib/hardware-device-context.js +149 -203
- package/dist/lib/hardware-device-context.js.map +1 -1
- package/dist/lib/hardware-frames-context.d.ts +170 -180
- package/dist/lib/hardware-frames-context.js +171 -181
- package/dist/lib/hardware-frames-context.js.map +1 -1
- package/dist/lib/index.d.ts +2 -1
- package/dist/lib/index.js +2 -2
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/input-format.d.ts +89 -117
- package/dist/lib/input-format.js +89 -117
- package/dist/lib/input-format.js.map +1 -1
- package/dist/lib/io-context.d.ts +209 -241
- package/dist/lib/io-context.js +220 -252
- package/dist/lib/io-context.js.map +1 -1
- package/dist/lib/log.d.ts +85 -119
- package/dist/lib/log.js +85 -122
- package/dist/lib/log.js.map +1 -1
- package/dist/lib/native-types.d.ts +117 -106
- package/dist/lib/native-types.js +0 -7
- package/dist/lib/native-types.js.map +1 -1
- package/dist/lib/option.d.ts +284 -241
- package/dist/lib/option.js +309 -249
- package/dist/lib/option.js.map +1 -1
- package/dist/lib/output-format.d.ts +77 -101
- package/dist/lib/output-format.js +77 -101
- package/dist/lib/output-format.js.map +1 -1
- package/dist/lib/packet.d.ts +172 -240
- package/dist/lib/packet.js +172 -241
- package/dist/lib/packet.js.map +1 -1
- package/dist/lib/rational.d.ts +0 -2
- package/dist/lib/rational.js +0 -2
- package/dist/lib/rational.js.map +1 -1
- package/dist/lib/software-resample-context.d.ts +241 -325
- package/dist/lib/software-resample-context.js +242 -326
- package/dist/lib/software-resample-context.js.map +1 -1
- package/dist/lib/software-scale-context.d.ts +129 -173
- package/dist/lib/software-scale-context.js +131 -175
- package/dist/lib/software-scale-context.js.map +1 -1
- package/dist/lib/stream.d.ts +87 -197
- package/dist/lib/stream.js +87 -197
- package/dist/lib/stream.js.map +1 -1
- package/dist/lib/utilities.d.ts +372 -181
- package/dist/lib/utilities.js +373 -182
- package/dist/lib/utilities.js.map +1 -1
- package/install/check.js +0 -1
- package/package.json +21 -12
- package/release_notes.md +43 -59
- package/CHANGELOG.md +0 -8
package/dist/lib/packet.js
CHANGED
|
@@ -1,72 +1,57 @@
|
|
|
1
1
|
import { bindings } from './binding.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Container for compressed audio/video data.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Stores encoded data from demuxers or to be sent to muxers. Each packet contains
|
|
6
|
+
* a portion of compressed stream data, typically one video frame or several audio frames.
|
|
7
|
+
* Includes timing information (PTS/DTS), stream index, and flags. Essential for
|
|
8
|
+
* demuxing, muxing, and codec operations.
|
|
8
9
|
*
|
|
9
10
|
* Direct mapping to FFmpeg's AVPacket.
|
|
10
11
|
*
|
|
11
12
|
* @example
|
|
12
13
|
* ```typescript
|
|
13
|
-
* import { Packet,
|
|
14
|
+
* import { Packet, FFmpegError } from 'node-av';
|
|
15
|
+
* import { AV_PKT_FLAG_KEY } from 'node-av/constants';
|
|
14
16
|
*
|
|
15
|
-
* // Create and allocate packet
|
|
17
|
+
* // Create and allocate packet
|
|
16
18
|
* const packet = new Packet();
|
|
17
19
|
* packet.alloc();
|
|
18
20
|
*
|
|
19
|
-
* // Read from
|
|
21
|
+
* // Read packet from format context
|
|
20
22
|
* const ret = await formatContext.readFrame(packet);
|
|
21
23
|
* FFmpegError.throwIfError(ret, 'readFrame');
|
|
22
24
|
*
|
|
23
|
-
* //
|
|
24
|
-
* console.log(`Stream: ${packet.streamIndex}
|
|
25
|
+
* // Check packet properties
|
|
26
|
+
* console.log(`Stream: ${packet.streamIndex}`);
|
|
27
|
+
* console.log(`PTS: ${packet.pts}`);
|
|
28
|
+
* console.log(`Size: ${packet.size} bytes`);
|
|
29
|
+
* console.log(`Keyframe: ${packet.isKeyframe}`);
|
|
25
30
|
*
|
|
26
31
|
* // Send to decoder
|
|
27
|
-
* const
|
|
28
|
-
* FFmpegError.throwIfError(
|
|
32
|
+
* const ret2 = await codecContext.sendPacket(packet);
|
|
33
|
+
* FFmpegError.throwIfError(ret2, 'sendPacket');
|
|
29
34
|
*
|
|
30
35
|
* // Cleanup
|
|
31
|
-
* packet.unref();
|
|
32
|
-
* packet.free(); // Free packet completely
|
|
36
|
+
* packet.unref();
|
|
33
37
|
* ```
|
|
34
38
|
*
|
|
35
|
-
* @see
|
|
39
|
+
* @see [AVPacket](https://ffmpeg.org/doxygen/trunk/structAVPacket.html) - FFmpeg Doxygen
|
|
36
40
|
* @see {@link FormatContext} For reading/writing packets
|
|
41
|
+
* @see {@link CodecContext} For encoding/decoding packets
|
|
37
42
|
*/
|
|
38
43
|
export class Packet {
|
|
39
44
|
native;
|
|
40
|
-
/**
|
|
41
|
-
* Create a new packet instance.
|
|
42
|
-
*
|
|
43
|
-
* The packet is uninitialized - you must call alloc() before use.
|
|
44
|
-
* No FFmpeg resources are allocated until alloc() is called.
|
|
45
|
-
*
|
|
46
|
-
* Direct wrapper around AVPacket allocation.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```typescript
|
|
50
|
-
* import { Packet } from 'node-av';
|
|
51
|
-
*
|
|
52
|
-
* const packet = new Packet();
|
|
53
|
-
* packet.alloc();
|
|
54
|
-
* // Packet is now ready for use
|
|
55
|
-
*
|
|
56
|
-
* // Always free when done
|
|
57
|
-
* packet.free();
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
45
|
constructor() {
|
|
61
46
|
this.native = new bindings.Packet();
|
|
62
47
|
}
|
|
63
48
|
/**
|
|
64
49
|
* Stream index this packet belongs to.
|
|
65
50
|
*
|
|
66
|
-
*
|
|
51
|
+
* Identifies which stream in a format context this packet is from/for.
|
|
52
|
+
* Set automatically when reading, must be set manually when writing.
|
|
67
53
|
*
|
|
68
|
-
*
|
|
69
|
-
* Must be set by user for muxing.
|
|
54
|
+
* Direct mapping to AVPacket->stream_index.
|
|
70
55
|
*/
|
|
71
56
|
get streamIndex() {
|
|
72
57
|
return this.native.streamIndex;
|
|
@@ -75,16 +60,12 @@ export class Packet {
|
|
|
75
60
|
this.native.streamIndex = value;
|
|
76
61
|
}
|
|
77
62
|
/**
|
|
78
|
-
* Presentation timestamp
|
|
63
|
+
* Presentation timestamp.
|
|
79
64
|
*
|
|
80
|
-
*
|
|
65
|
+
* Time when the decompressed packet should be presented to the user.
|
|
66
|
+
* In stream time base units. AV_NOPTS_VALUE if unknown.
|
|
81
67
|
*
|
|
82
|
-
*
|
|
83
|
-
* Can be AV_NOPTS_VALUE if it is not stored in the file.
|
|
84
|
-
* pts MUST be larger or equal to dts as presentation cannot happen before
|
|
85
|
-
* decompression, unless one wants to view hex dumps. Some formats misuse
|
|
86
|
-
* the terms dts and pts/cts to mean something different. Such timestamps
|
|
87
|
-
* must be converted to true pts/dts before they are stored in AVPacket.
|
|
68
|
+
* Direct mapping to AVPacket->pts.
|
|
88
69
|
*/
|
|
89
70
|
get pts() {
|
|
90
71
|
return this.native.pts;
|
|
@@ -93,12 +74,12 @@ export class Packet {
|
|
|
93
74
|
this.native.pts = value;
|
|
94
75
|
}
|
|
95
76
|
/**
|
|
96
|
-
* Decompression timestamp
|
|
77
|
+
* Decompression timestamp.
|
|
97
78
|
*
|
|
98
|
-
*
|
|
79
|
+
* Time when the packet should be decompressed.
|
|
80
|
+
* In stream time base units. AV_NOPTS_VALUE if unknown.
|
|
99
81
|
*
|
|
100
|
-
*
|
|
101
|
-
* Can be AV_NOPTS_VALUE if it is not stored in the file.
|
|
82
|
+
* Direct mapping to AVPacket->dts.
|
|
102
83
|
*/
|
|
103
84
|
get dts() {
|
|
104
85
|
return this.native.dts;
|
|
@@ -107,11 +88,12 @@ export class Packet {
|
|
|
107
88
|
this.native.dts = value;
|
|
108
89
|
}
|
|
109
90
|
/**
|
|
110
|
-
* Duration of this packet
|
|
91
|
+
* Duration of this packet.
|
|
111
92
|
*
|
|
112
|
-
*
|
|
93
|
+
* Duration in stream time base units, 0 if unknown.
|
|
94
|
+
* Typically equal to next_pts - this_pts.
|
|
113
95
|
*
|
|
114
|
-
*
|
|
96
|
+
* Direct mapping to AVPacket->duration.
|
|
115
97
|
*/
|
|
116
98
|
get duration() {
|
|
117
99
|
return this.native.duration;
|
|
@@ -120,11 +102,12 @@ export class Packet {
|
|
|
120
102
|
this.native.duration = value;
|
|
121
103
|
}
|
|
122
104
|
/**
|
|
123
|
-
* Byte position in stream
|
|
105
|
+
* Byte position in stream.
|
|
124
106
|
*
|
|
125
|
-
*
|
|
107
|
+
* Byte position of packet data in the input file.
|
|
108
|
+
* -1 if unknown.
|
|
126
109
|
*
|
|
127
|
-
*
|
|
110
|
+
* Direct mapping to AVPacket->pos.
|
|
128
111
|
*/
|
|
129
112
|
get pos() {
|
|
130
113
|
return this.native.pos;
|
|
@@ -135,23 +118,18 @@ export class Packet {
|
|
|
135
118
|
/**
|
|
136
119
|
* Size of packet data in bytes.
|
|
137
120
|
*
|
|
138
|
-
* Direct mapping to AVPacket->size
|
|
139
|
-
*
|
|
140
|
-
* @readonly
|
|
121
|
+
* Direct mapping to AVPacket->size.
|
|
141
122
|
*/
|
|
142
123
|
get size() {
|
|
143
124
|
return this.native.size;
|
|
144
125
|
}
|
|
145
126
|
/**
|
|
146
|
-
*
|
|
127
|
+
* Packet flags.
|
|
147
128
|
*
|
|
148
|
-
*
|
|
129
|
+
* Combination of AV_PKT_FLAG values indicating packet properties
|
|
130
|
+
* (e.g., AV_PKT_FLAG_KEY for keyframes).
|
|
149
131
|
*
|
|
150
|
-
*
|
|
151
|
-
* - AV_PKT_FLAG_CORRUPT: The packet content is corrupted
|
|
152
|
-
* - AV_PKT_FLAG_DISCARD: Flag for packets that should be dropped
|
|
153
|
-
* - AV_PKT_FLAG_TRUSTED: The packet comes from a trusted source
|
|
154
|
-
* - AV_PKT_FLAG_DISPOSABLE: Flag for packets that can be dropped if needed
|
|
132
|
+
* Direct mapping to AVPacket->flags.
|
|
155
133
|
*/
|
|
156
134
|
get flags() {
|
|
157
135
|
return this.native.flags;
|
|
@@ -162,11 +140,10 @@ export class Packet {
|
|
|
162
140
|
/**
|
|
163
141
|
* Packet data buffer.
|
|
164
142
|
*
|
|
165
|
-
*
|
|
143
|
+
* Contains the compressed audio/video data.
|
|
144
|
+
* May be null for packets signaling special conditions.
|
|
166
145
|
*
|
|
167
|
-
*
|
|
168
|
-
* Setting null will unref the packet data.
|
|
169
|
-
* When setting a new buffer, av_packet_from_data() is called internally.
|
|
146
|
+
* Direct mapping to AVPacket->data.
|
|
170
147
|
*/
|
|
171
148
|
get data() {
|
|
172
149
|
return this.native.data;
|
|
@@ -177,7 +154,8 @@ export class Packet {
|
|
|
177
154
|
/**
|
|
178
155
|
* Check if packet contains a keyframe.
|
|
179
156
|
*
|
|
180
|
-
*
|
|
157
|
+
* Convenience property that checks AV_PKT_FLAG_KEY flag.
|
|
158
|
+
* Keyframes can be decoded independently without reference frames.
|
|
181
159
|
*/
|
|
182
160
|
get isKeyframe() {
|
|
183
161
|
return this.native.isKeyframe;
|
|
@@ -186,28 +164,23 @@ export class Packet {
|
|
|
186
164
|
this.native.isKeyframe = value;
|
|
187
165
|
}
|
|
188
166
|
/**
|
|
189
|
-
* Allocate
|
|
167
|
+
* Allocate a new packet.
|
|
190
168
|
*
|
|
191
|
-
* Allocates the
|
|
192
|
-
*
|
|
169
|
+
* Allocates the packet structure. Must be called before using the packet
|
|
170
|
+
* unless it was created by another function (e.g., clone()).
|
|
193
171
|
*
|
|
194
|
-
* Direct mapping to av_packet_alloc()
|
|
172
|
+
* Direct mapping to av_packet_alloc().
|
|
195
173
|
*
|
|
196
|
-
* @throws {Error}
|
|
174
|
+
* @throws {Error} If allocation fails (ENOMEM)
|
|
197
175
|
*
|
|
198
176
|
* @example
|
|
199
177
|
* ```typescript
|
|
200
|
-
* import { Packet } from 'node-av';
|
|
201
|
-
*
|
|
202
178
|
* const packet = new Packet();
|
|
203
179
|
* packet.alloc();
|
|
204
|
-
* // Packet is now
|
|
205
|
-
* // pts = AV_NOPTS_VALUE, dts = AV_NOPTS_VALUE
|
|
206
|
-
* // size = 0, data = null
|
|
180
|
+
* // Packet is now ready for use
|
|
207
181
|
* ```
|
|
208
182
|
*
|
|
209
|
-
* @see {@link free}
|
|
210
|
-
* @see {@link unref} To clear data but keep allocation
|
|
183
|
+
* @see {@link free} To deallocate the packet
|
|
211
184
|
*/
|
|
212
185
|
alloc() {
|
|
213
186
|
this.native.alloc();
|
|
@@ -215,109 +188,91 @@ export class Packet {
|
|
|
215
188
|
/**
|
|
216
189
|
* Free the packet.
|
|
217
190
|
*
|
|
218
|
-
*
|
|
191
|
+
* Deallocates the packet and its data. The packet becomes invalid after this.
|
|
219
192
|
*
|
|
220
|
-
*
|
|
221
|
-
* After calling this, the packet is no longer usable until alloc() is called again.
|
|
222
|
-
*
|
|
223
|
-
* @note
|
|
224
|
-
* Calling this multiple times is safe, because we check the freed state
|
|
193
|
+
* Direct mapping to av_packet_free().
|
|
225
194
|
*
|
|
226
195
|
* @example
|
|
227
196
|
* ```typescript
|
|
228
197
|
* packet.free();
|
|
229
|
-
* //
|
|
198
|
+
* // Packet is now invalid
|
|
230
199
|
* ```
|
|
200
|
+
*
|
|
201
|
+
* @see {@link unref} To only free data, keeping structure
|
|
231
202
|
*/
|
|
232
203
|
free() {
|
|
233
204
|
this.native.free();
|
|
234
205
|
}
|
|
235
206
|
/**
|
|
236
|
-
*
|
|
207
|
+
* Create a reference to another packet.
|
|
237
208
|
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
* All the other fields are copied from src.
|
|
209
|
+
* Sets up this packet as a reference to the source packet's data.
|
|
210
|
+
* Both packets will share the same data buffer.
|
|
241
211
|
*
|
|
242
|
-
* Direct mapping to av_packet_ref()
|
|
212
|
+
* Direct mapping to av_packet_ref().
|
|
243
213
|
*
|
|
244
214
|
* @param src - Source packet to reference
|
|
245
|
-
*
|
|
246
215
|
* @returns 0 on success, negative AVERROR on error:
|
|
247
|
-
* -
|
|
248
|
-
* -
|
|
249
|
-
* - AVERROR(EINVAL): Invalid parameters (null packet)
|
|
216
|
+
* - AVERROR_ENOMEM: Memory allocation failure
|
|
217
|
+
* - AVERROR_EINVAL: Invalid parameters
|
|
250
218
|
*
|
|
251
219
|
* @example
|
|
252
220
|
* ```typescript
|
|
253
|
-
* import {
|
|
221
|
+
* import { FFmpegError } from 'node-av';
|
|
254
222
|
*
|
|
255
|
-
* const
|
|
256
|
-
*
|
|
257
|
-
* const
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
* const ret = dst.ref(src);
|
|
261
|
-
* FFmpegError.throwIfError(ret, 'packet.ref');
|
|
262
|
-
* // dst now references the same data as src
|
|
223
|
+
* const packet2 = new Packet();
|
|
224
|
+
* packet2.alloc();
|
|
225
|
+
* const ret = packet2.ref(packet1);
|
|
226
|
+
* FFmpegError.throwIfError(ret, 'ref');
|
|
227
|
+
* // packet2 now references packet1's data
|
|
263
228
|
* ```
|
|
264
229
|
*
|
|
265
230
|
* @see {@link unref} To remove reference
|
|
266
|
-
* @see {@link clone}
|
|
231
|
+
* @see {@link clone} To create independent copy
|
|
267
232
|
*/
|
|
268
233
|
ref(src) {
|
|
269
234
|
return this.native.ref(src.getNative());
|
|
270
235
|
}
|
|
271
236
|
/**
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
* Direct mapping to av_packet_unref()
|
|
237
|
+
* Unreference the packet.
|
|
275
238
|
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
239
|
+
* Frees the packet data if this was the last reference.
|
|
240
|
+
* The packet structure remains allocated and can be reused.
|
|
278
241
|
*
|
|
279
|
-
*
|
|
242
|
+
* Direct mapping to av_packet_unref().
|
|
280
243
|
*
|
|
281
244
|
* @example
|
|
282
245
|
* ```typescript
|
|
283
246
|
* packet.unref();
|
|
284
|
-
* // Packet data is
|
|
285
|
-
* // Can reuse the packet for another read
|
|
247
|
+
* // Packet data is freed, structure can be reused
|
|
286
248
|
* ```
|
|
287
249
|
*
|
|
288
|
-
* @
|
|
250
|
+
* @see {@link ref} To create reference
|
|
251
|
+
* @see {@link free} To free everything
|
|
289
252
|
*/
|
|
290
253
|
unref() {
|
|
291
254
|
this.native.unref();
|
|
292
255
|
}
|
|
293
256
|
/**
|
|
294
|
-
*
|
|
257
|
+
* Clone the packet.
|
|
295
258
|
*
|
|
296
|
-
*
|
|
297
|
-
* The new packet
|
|
259
|
+
* Creates an independent copy of the packet with its own data buffer.
|
|
260
|
+
* The new packet has the same content but can be modified independently.
|
|
298
261
|
*
|
|
299
|
-
* Direct mapping to av_packet_clone()
|
|
262
|
+
* Direct mapping to av_packet_clone().
|
|
300
263
|
*
|
|
301
|
-
* @returns New packet
|
|
264
|
+
* @returns New packet instance, or null on allocation failure
|
|
302
265
|
*
|
|
303
266
|
* @example
|
|
304
267
|
* ```typescript
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
* const cloned = original.clone();
|
|
311
|
-
* if (!cloned) {
|
|
312
|
-
* throw new Error('Failed to clone packet: Out of memory');
|
|
268
|
+
* const copy = packet.clone();
|
|
269
|
+
* if (copy) {
|
|
270
|
+
* // Modify copy without affecting original
|
|
271
|
+
* copy.pts = packet.pts + 1000n;
|
|
313
272
|
* }
|
|
314
|
-
* // cloned references the same data as original
|
|
315
|
-
* // Both packets must be freed independently
|
|
316
|
-
* cloned.free();
|
|
317
|
-
* original.free();
|
|
318
273
|
* ```
|
|
319
274
|
*
|
|
320
|
-
* @see {@link ref}
|
|
275
|
+
* @see {@link ref} To create reference instead of copy
|
|
321
276
|
*/
|
|
322
277
|
clone() {
|
|
323
278
|
const cloned = this.native.clone();
|
|
@@ -326,200 +281,188 @@ export class Packet {
|
|
|
326
281
|
}
|
|
327
282
|
// Wrap the native cloned packet
|
|
328
283
|
const packet = Object.create(Packet.prototype);
|
|
329
|
-
// Need to set private property - this is safe since we control the implementation
|
|
330
284
|
packet.native = cloned;
|
|
331
285
|
return packet;
|
|
332
286
|
}
|
|
333
287
|
/**
|
|
334
|
-
*
|
|
288
|
+
* Rescale packet timestamps.
|
|
335
289
|
*
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
* different contexts, for example when remuxing or when the decoder and encoder
|
|
339
|
-
* use different timebases.
|
|
290
|
+
* Converts PTS, DTS, and duration from one time base to another.
|
|
291
|
+
* Essential when moving packets between streams with different time bases.
|
|
340
292
|
*
|
|
341
|
-
* Direct mapping to av_packet_rescale_ts()
|
|
293
|
+
* Direct mapping to av_packet_rescale_ts().
|
|
342
294
|
*
|
|
343
|
-
* @param srcTimebase - Source
|
|
344
|
-
* @param dstTimebase - Destination
|
|
295
|
+
* @param srcTimebase - Source time base
|
|
296
|
+
* @param dstTimebase - Destination time base
|
|
345
297
|
*
|
|
346
298
|
* @example
|
|
347
299
|
* ```typescript
|
|
348
|
-
* import {
|
|
349
|
-
*
|
|
350
|
-
* // Convert from stream timebase to codec timebase
|
|
351
|
-
* const streamTimebase = new Rational(1, 90000); // 90kHz
|
|
352
|
-
* const codecTimebase = new Rational(1, 25); // 25 fps
|
|
300
|
+
* import { Rational } from 'node-av';
|
|
353
301
|
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
* );
|
|
358
|
-
* // PTS and DTS are now in codec timebase
|
|
302
|
+
* // Convert from 1/25 fps to 1/1000 (milliseconds)
|
|
303
|
+
* const src = new Rational(1, 25);
|
|
304
|
+
* const dst = new Rational(1, 1000);
|
|
305
|
+
* packet.rescaleTs(src, dst);
|
|
359
306
|
* ```
|
|
360
|
-
*
|
|
361
|
-
* @see {@link Rational} For timebase representation
|
|
362
307
|
*/
|
|
363
308
|
rescaleTs(srcTimebase, dstTimebase) {
|
|
364
309
|
this.native.rescaleTs({ num: srcTimebase.num, den: srcTimebase.den }, { num: dstTimebase.num, den: dstTimebase.den });
|
|
365
310
|
}
|
|
366
311
|
/**
|
|
367
|
-
* Ensure
|
|
312
|
+
* Ensure packet data is reference counted.
|
|
368
313
|
*
|
|
369
|
-
* Makes the packet data
|
|
370
|
-
*
|
|
314
|
+
* Makes sure the packet data is stored in a reference-counted buffer.
|
|
315
|
+
* If not already reference-counted, allocates a new buffer and copies data.
|
|
371
316
|
*
|
|
372
|
-
* Direct mapping to av_packet_make_refcounted()
|
|
317
|
+
* Direct mapping to av_packet_make_refcounted().
|
|
373
318
|
*
|
|
374
319
|
* @returns 0 on success, negative AVERROR on error:
|
|
375
|
-
* -
|
|
376
|
-
* -
|
|
320
|
+
* - AVERROR_ENOMEM: Memory allocation failure
|
|
321
|
+
* - AVERROR_EINVAL: Invalid packet
|
|
377
322
|
*
|
|
378
323
|
* @example
|
|
379
324
|
* ```typescript
|
|
380
|
-
* import {
|
|
325
|
+
* import { FFmpegError } from 'node-av';
|
|
381
326
|
*
|
|
382
327
|
* const ret = packet.makeRefcounted();
|
|
383
328
|
* FFmpegError.throwIfError(ret, 'makeRefcounted');
|
|
384
|
-
* // Packet data is now reference counted
|
|
385
|
-
* // Safe to share with multiple owners
|
|
386
329
|
* ```
|
|
387
|
-
*
|
|
388
|
-
* Note: This function does not ensure that the reference will be writable.
|
|
389
|
-
* Use makeWritable() for that purpose.
|
|
390
|
-
*
|
|
391
|
-
* @see {@link makeWritable} To ensure data is writable
|
|
392
330
|
*/
|
|
393
331
|
makeRefcounted() {
|
|
394
332
|
return this.native.makeRefcounted();
|
|
395
333
|
}
|
|
396
334
|
/**
|
|
397
|
-
*
|
|
335
|
+
* Ensure packet data is writable.
|
|
398
336
|
*
|
|
399
|
-
*
|
|
400
|
-
*
|
|
401
|
-
* is unreferenced. This avoids data copy if possible.
|
|
337
|
+
* Creates a private copy of the data if it's shared with other packets.
|
|
338
|
+
* Call before modifying packet data to avoid affecting other references.
|
|
402
339
|
*
|
|
403
|
-
* Direct mapping to av_packet_make_writable()
|
|
340
|
+
* Direct mapping to av_packet_make_writable().
|
|
404
341
|
*
|
|
405
342
|
* @returns 0 on success, negative AVERROR on error:
|
|
406
|
-
* -
|
|
407
|
-
* -
|
|
343
|
+
* - AVERROR_ENOMEM: Memory allocation failure
|
|
344
|
+
* - AVERROR_EINVAL: Invalid packet
|
|
408
345
|
*
|
|
409
346
|
* @example
|
|
410
347
|
* ```typescript
|
|
411
|
-
* import {
|
|
348
|
+
* import { FFmpegError } from 'node-av';
|
|
412
349
|
*
|
|
413
|
-
* //
|
|
350
|
+
* // Ensure we can safely modify data
|
|
414
351
|
* const ret = packet.makeWritable();
|
|
415
352
|
* FFmpegError.throwIfError(ret, 'makeWritable');
|
|
416
|
-
*
|
|
417
|
-
* // Now safe to modify packet data
|
|
418
|
-
* const data = packet.data;
|
|
419
|
-
* if (data) {
|
|
420
|
-
* // Modify data buffer directly
|
|
421
|
-
* data[0] = 0xFF;
|
|
422
|
-
* }
|
|
353
|
+
* // Now safe to modify packet.data
|
|
423
354
|
* ```
|
|
424
|
-
*
|
|
425
|
-
* @see {@link makeRefcounted} To make data reference counted
|
|
426
355
|
*/
|
|
427
356
|
makeWritable() {
|
|
428
357
|
return this.native.makeWritable();
|
|
429
358
|
}
|
|
430
359
|
/**
|
|
431
|
-
* Get side data
|
|
360
|
+
* Get packet side data.
|
|
361
|
+
*
|
|
362
|
+
* Retrieves additional data associated with the packet
|
|
363
|
+
* (e.g., palette data, quality stats, encryption info).
|
|
432
364
|
*
|
|
433
|
-
* Direct mapping to av_packet_get_side_data()
|
|
365
|
+
* Direct mapping to av_packet_get_side_data().
|
|
434
366
|
*
|
|
435
|
-
* @param type -
|
|
436
|
-
* @returns
|
|
367
|
+
* @param type - Type of side data to retrieve
|
|
368
|
+
* @returns Side data buffer, or null if not present
|
|
437
369
|
*
|
|
438
370
|
* @example
|
|
439
371
|
* ```typescript
|
|
440
|
-
* import { Packet } from 'node-av';
|
|
441
372
|
* import { AV_PKT_DATA_PALETTE } from 'node-av/constants';
|
|
442
373
|
*
|
|
443
|
-
* const
|
|
444
|
-
* if (
|
|
445
|
-
* console.log(
|
|
374
|
+
* const palette = packet.getSideData(AV_PKT_DATA_PALETTE);
|
|
375
|
+
* if (palette) {
|
|
376
|
+
* console.log(`Palette size: ${palette.length} bytes`);
|
|
446
377
|
* }
|
|
447
378
|
* ```
|
|
379
|
+
*
|
|
380
|
+
* @see {@link addSideData} To add side data
|
|
381
|
+
* @see {@link newSideData} To allocate new side data
|
|
448
382
|
*/
|
|
449
383
|
getSideData(type) {
|
|
450
384
|
return this.native.getSideData(type);
|
|
451
385
|
}
|
|
452
386
|
/**
|
|
453
|
-
* Add side data to
|
|
387
|
+
* Add side data to packet.
|
|
454
388
|
*
|
|
455
|
-
*
|
|
456
|
-
* Direct mapping to av_packet_add_side_data()
|
|
389
|
+
* Attaches additional data to the packet. The data is copied.
|
|
457
390
|
*
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
* @
|
|
391
|
+
* Direct mapping to av_packet_add_side_data().
|
|
392
|
+
*
|
|
393
|
+
* @param type - Type of side data
|
|
394
|
+
* @param data - Side data buffer
|
|
395
|
+
* @returns 0 on success, negative AVERROR on error:
|
|
396
|
+
* - AVERROR_ENOMEM: Memory allocation failure
|
|
397
|
+
* - AVERROR_EINVAL: Invalid parameters
|
|
461
398
|
*
|
|
462
399
|
* @example
|
|
463
400
|
* ```typescript
|
|
464
|
-
* import {
|
|
401
|
+
* import { FFmpegError } from 'node-av';
|
|
465
402
|
* import { AV_PKT_DATA_NEW_EXTRADATA } from 'node-av/constants';
|
|
466
403
|
*
|
|
467
|
-
* const
|
|
468
|
-
* const ret = packet.addSideData(AV_PKT_DATA_NEW_EXTRADATA,
|
|
469
|
-
*
|
|
470
|
-
* console.error('Failed to add side data');
|
|
471
|
-
* }
|
|
404
|
+
* const extradata = Buffer.from([...]);
|
|
405
|
+
* const ret = packet.addSideData(AV_PKT_DATA_NEW_EXTRADATA, extradata);
|
|
406
|
+
* FFmpegError.throwIfError(ret, 'addSideData');
|
|
472
407
|
* ```
|
|
408
|
+
*
|
|
409
|
+
* @see {@link getSideData} To retrieve side data
|
|
410
|
+
* @see {@link newSideData} To allocate in-place
|
|
473
411
|
*/
|
|
474
412
|
addSideData(type, data) {
|
|
475
413
|
return this.native.addSideData(type, data);
|
|
476
414
|
}
|
|
477
415
|
/**
|
|
478
|
-
* Allocate new side data
|
|
416
|
+
* Allocate new side data.
|
|
417
|
+
*
|
|
418
|
+
* Allocates side data buffer attached to the packet.
|
|
419
|
+
* Returns buffer that can be written to directly.
|
|
420
|
+
*
|
|
421
|
+
* Direct mapping to av_packet_new_side_data().
|
|
479
422
|
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
*
|
|
423
|
+
* @param type - Type of side data
|
|
424
|
+
* @param size - Size in bytes to allocate
|
|
425
|
+
* @returns Allocated buffer for writing
|
|
483
426
|
*
|
|
484
|
-
* @param type - The type of side data to allocate
|
|
485
|
-
* @param size - Size of the side data buffer to allocate
|
|
486
|
-
* @returns Buffer referencing the allocated side data
|
|
487
427
|
* @throws {Error} If allocation fails
|
|
488
428
|
*
|
|
489
429
|
* @example
|
|
490
430
|
* ```typescript
|
|
491
|
-
* import {
|
|
492
|
-
* import { AV_PKT_DATA_STRINGS_METADATA } from 'node-av/constants';
|
|
431
|
+
* import { AV_PKT_DATA_NEW_EXTRADATA } from 'node-av/constants';
|
|
493
432
|
*
|
|
494
|
-
* // Allocate
|
|
495
|
-
* const
|
|
496
|
-
*
|
|
497
|
-
* metadataBuffer.write('key=value', 0, 'utf8');
|
|
433
|
+
* // Allocate and write side data directly
|
|
434
|
+
* const sideData = packet.newSideData(AV_PKT_DATA_NEW_EXTRADATA, 16);
|
|
435
|
+
* sideData.writeUInt32LE(0x12345678, 0);
|
|
498
436
|
* ```
|
|
437
|
+
*
|
|
438
|
+
* @see {@link getSideData} To retrieve side data
|
|
439
|
+
* @see {@link addSideData} To add existing buffer
|
|
499
440
|
*/
|
|
500
441
|
newSideData(type, size) {
|
|
501
442
|
return this.native.newSideData(type, size);
|
|
502
443
|
}
|
|
503
444
|
/**
|
|
504
|
-
* Free all side data
|
|
445
|
+
* Free all side data.
|
|
446
|
+
*
|
|
447
|
+
* Removes all side data attached to the packet.
|
|
505
448
|
*
|
|
506
|
-
*
|
|
507
|
-
* Direct mapping to av_packet_free_side_data()
|
|
449
|
+
* Direct mapping to av_packet_free_side_data().
|
|
508
450
|
*
|
|
509
451
|
* @example
|
|
510
452
|
* ```typescript
|
|
511
|
-
* // Remove all side data from the packet
|
|
512
453
|
* packet.freeSideData();
|
|
454
|
+
* // All side data removed
|
|
513
455
|
* ```
|
|
514
456
|
*/
|
|
515
457
|
freeSideData() {
|
|
516
458
|
this.native.freeSideData();
|
|
517
459
|
}
|
|
518
460
|
/**
|
|
519
|
-
* Get the native
|
|
461
|
+
* Get the underlying native Packet object.
|
|
520
462
|
*
|
|
521
|
-
* @
|
|
522
|
-
*
|
|
463
|
+
* @returns The native Packet binding object
|
|
464
|
+
*
|
|
465
|
+
* @internal
|
|
523
466
|
*/
|
|
524
467
|
getNative() {
|
|
525
468
|
return this.native;
|
|
@@ -532,23 +475,11 @@ export class Packet {
|
|
|
532
475
|
*
|
|
533
476
|
* @example
|
|
534
477
|
* ```typescript
|
|
535
|
-
* import { Packet } from 'node-av';
|
|
536
|
-
*
|
|
537
|
-
* // Using 'using' declaration for automatic cleanup
|
|
538
478
|
* {
|
|
539
479
|
* using packet = new Packet();
|
|
540
480
|
* packet.alloc();
|
|
541
|
-
* //
|
|
481
|
+
* // Use packet...
|
|
542
482
|
* } // Automatically freed when leaving scope
|
|
543
|
-
*
|
|
544
|
-
* // Or with try-finally
|
|
545
|
-
* const packet = new Packet();
|
|
546
|
-
* try {
|
|
547
|
-
* packet.alloc();
|
|
548
|
-
* // ... use packet
|
|
549
|
-
* } finally {
|
|
550
|
-
* packet[Symbol.dispose]();
|
|
551
|
-
* }
|
|
552
483
|
* ```
|
|
553
484
|
*/
|
|
554
485
|
[Symbol.dispose]() {
|