node-av 1.0.3 → 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.
Files changed (162) hide show
  1. package/README.md +56 -41
  2. package/dist/api/bitstream-filter.d.ts +180 -123
  3. package/dist/api/bitstream-filter.js +182 -126
  4. package/dist/api/bitstream-filter.js.map +1 -1
  5. package/dist/api/decoder.d.ts +286 -130
  6. package/dist/api/decoder.js +321 -159
  7. package/dist/api/decoder.js.map +1 -1
  8. package/dist/api/encoder.d.ts +254 -158
  9. package/dist/api/encoder.js +326 -298
  10. package/dist/api/encoder.js.map +1 -1
  11. package/dist/api/filter-presets.d.ts +912 -0
  12. package/dist/api/filter-presets.js +1407 -0
  13. package/dist/api/filter-presets.js.map +1 -0
  14. package/dist/api/filter.d.ts +280 -284
  15. package/dist/api/filter.js +435 -509
  16. package/dist/api/filter.js.map +1 -1
  17. package/dist/api/hardware.d.ts +226 -159
  18. package/dist/api/hardware.js +405 -287
  19. package/dist/api/hardware.js.map +1 -1
  20. package/dist/api/index.d.ts +3 -2
  21. package/dist/api/index.js +1 -0
  22. package/dist/api/index.js.map +1 -1
  23. package/dist/api/io-stream.d.ts +65 -61
  24. package/dist/api/io-stream.js +45 -47
  25. package/dist/api/io-stream.js.map +1 -1
  26. package/dist/api/media-input.d.ts +244 -141
  27. package/dist/api/media-input.js +207 -104
  28. package/dist/api/media-input.js.map +1 -1
  29. package/dist/api/media-output.d.ts +206 -128
  30. package/dist/api/media-output.js +212 -129
  31. package/dist/api/media-output.js.map +1 -1
  32. package/dist/api/pipeline.d.ts +168 -38
  33. package/dist/api/pipeline.js +238 -14
  34. package/dist/api/pipeline.js.map +1 -1
  35. package/dist/api/types.d.ts +22 -182
  36. package/dist/api/utilities/audio-sample.d.ts +1 -1
  37. package/dist/api/utilities/image.d.ts +1 -1
  38. package/dist/api/utilities/media-type.d.ts +1 -1
  39. package/dist/api/utilities/pixel-format.d.ts +1 -1
  40. package/dist/api/utilities/sample-format.d.ts +1 -1
  41. package/dist/api/utilities/timestamp.d.ts +1 -1
  42. package/dist/api/utils.d.ts +1 -2
  43. package/dist/api/utils.js +9 -0
  44. package/dist/api/utils.js.map +1 -1
  45. package/dist/{lib → constants}/channel-layouts.d.ts +1 -1
  46. package/dist/constants/channel-layouts.js.map +1 -0
  47. package/dist/{lib → constants}/constants.d.ts +19 -4
  48. package/dist/{lib → constants}/constants.js +15 -1
  49. package/dist/constants/constants.js.map +1 -0
  50. package/dist/constants/decoders.d.ts +609 -0
  51. package/dist/constants/decoders.js +617 -0
  52. package/dist/constants/decoders.js.map +1 -0
  53. package/dist/constants/encoders.d.ts +285 -0
  54. package/dist/constants/encoders.js +298 -0
  55. package/dist/constants/encoders.js.map +1 -0
  56. package/dist/constants/index.d.ts +4 -0
  57. package/dist/constants/index.js +5 -0
  58. package/dist/constants/index.js.map +1 -0
  59. package/dist/index.d.ts +1 -0
  60. package/dist/index.js +2 -0
  61. package/dist/index.js.map +1 -1
  62. package/dist/lib/audio-fifo.d.ts +128 -171
  63. package/dist/lib/audio-fifo.js +130 -173
  64. package/dist/lib/audio-fifo.js.map +1 -1
  65. package/dist/lib/binding.d.ts +7 -5
  66. package/dist/lib/binding.js +5 -0
  67. package/dist/lib/binding.js.map +1 -1
  68. package/dist/lib/bitstream-filter-context.d.ts +139 -184
  69. package/dist/lib/bitstream-filter-context.js +139 -188
  70. package/dist/lib/bitstream-filter-context.js.map +1 -1
  71. package/dist/lib/bitstream-filter.d.ts +69 -55
  72. package/dist/lib/bitstream-filter.js +68 -54
  73. package/dist/lib/bitstream-filter.js.map +1 -1
  74. package/dist/lib/codec-context.d.ts +317 -381
  75. package/dist/lib/codec-context.js +316 -381
  76. package/dist/lib/codec-context.js.map +1 -1
  77. package/dist/lib/codec-parameters.d.ts +161 -171
  78. package/dist/lib/codec-parameters.js +162 -172
  79. package/dist/lib/codec-parameters.js.map +1 -1
  80. package/dist/lib/codec-parser.d.ts +92 -105
  81. package/dist/lib/codec-parser.js +92 -103
  82. package/dist/lib/codec-parser.js.map +1 -1
  83. package/dist/lib/codec.d.ts +328 -217
  84. package/dist/lib/codec.js +392 -218
  85. package/dist/lib/codec.js.map +1 -1
  86. package/dist/lib/dictionary.d.ts +150 -204
  87. package/dist/lib/dictionary.js +159 -213
  88. package/dist/lib/dictionary.js.map +1 -1
  89. package/dist/lib/error.d.ts +97 -131
  90. package/dist/lib/error.js +98 -128
  91. package/dist/lib/error.js.map +1 -1
  92. package/dist/lib/filter-context.d.ts +317 -194
  93. package/dist/lib/filter-context.js +335 -200
  94. package/dist/lib/filter-context.js.map +1 -1
  95. package/dist/lib/filter-graph.d.ts +252 -293
  96. package/dist/lib/filter-graph.js +253 -294
  97. package/dist/lib/filter-graph.js.map +1 -1
  98. package/dist/lib/filter-inout.d.ts +87 -95
  99. package/dist/lib/filter-inout.js +87 -95
  100. package/dist/lib/filter-inout.js.map +1 -1
  101. package/dist/lib/filter.d.ts +93 -111
  102. package/dist/lib/filter.js +94 -112
  103. package/dist/lib/filter.js.map +1 -1
  104. package/dist/lib/format-context.d.ts +321 -429
  105. package/dist/lib/format-context.js +314 -386
  106. package/dist/lib/format-context.js.map +1 -1
  107. package/dist/lib/frame.d.ts +263 -406
  108. package/dist/lib/frame.js +263 -408
  109. package/dist/lib/frame.js.map +1 -1
  110. package/dist/lib/hardware-device-context.d.ts +150 -204
  111. package/dist/lib/hardware-device-context.js +149 -203
  112. package/dist/lib/hardware-device-context.js.map +1 -1
  113. package/dist/lib/hardware-frames-context.d.ts +171 -181
  114. package/dist/lib/hardware-frames-context.js +171 -181
  115. package/dist/lib/hardware-frames-context.js.map +1 -1
  116. package/dist/lib/index.d.ts +2 -3
  117. package/dist/lib/index.js +2 -5
  118. package/dist/lib/index.js.map +1 -1
  119. package/dist/lib/input-format.d.ts +90 -118
  120. package/dist/lib/input-format.js +89 -117
  121. package/dist/lib/input-format.js.map +1 -1
  122. package/dist/lib/io-context.d.ts +210 -242
  123. package/dist/lib/io-context.js +221 -253
  124. package/dist/lib/io-context.js.map +1 -1
  125. package/dist/lib/log.d.ts +86 -120
  126. package/dist/lib/log.js +85 -122
  127. package/dist/lib/log.js.map +1 -1
  128. package/dist/lib/native-types.d.ts +127 -112
  129. package/dist/lib/native-types.js +9 -0
  130. package/dist/lib/native-types.js.map +1 -1
  131. package/dist/lib/option.d.ts +285 -242
  132. package/dist/lib/option.js +310 -250
  133. package/dist/lib/option.js.map +1 -1
  134. package/dist/lib/output-format.d.ts +78 -102
  135. package/dist/lib/output-format.js +77 -101
  136. package/dist/lib/output-format.js.map +1 -1
  137. package/dist/lib/packet.d.ts +173 -241
  138. package/dist/lib/packet.js +172 -241
  139. package/dist/lib/packet.js.map +1 -1
  140. package/dist/lib/rational.d.ts +0 -2
  141. package/dist/lib/rational.js +0 -2
  142. package/dist/lib/rational.js.map +1 -1
  143. package/dist/lib/software-resample-context.d.ts +242 -326
  144. package/dist/lib/software-resample-context.js +242 -326
  145. package/dist/lib/software-resample-context.js.map +1 -1
  146. package/dist/lib/software-scale-context.d.ts +130 -174
  147. package/dist/lib/software-scale-context.js +132 -176
  148. package/dist/lib/software-scale-context.js.map +1 -1
  149. package/dist/lib/stream.d.ts +88 -198
  150. package/dist/lib/stream.js +87 -197
  151. package/dist/lib/stream.js.map +1 -1
  152. package/dist/lib/types.d.ts +1 -1
  153. package/dist/lib/utilities.d.ts +372 -181
  154. package/dist/lib/utilities.js +373 -182
  155. package/dist/lib/utilities.js.map +1 -1
  156. package/install/check.js +0 -1
  157. package/package.json +32 -24
  158. package/release_notes.md +43 -13
  159. package/CHANGELOG.md +0 -8
  160. package/dist/lib/channel-layouts.js.map +0 -1
  161. package/dist/lib/constants.js.map +0 -1
  162. /package/dist/{lib → constants}/channel-layouts.js +0 -0
@@ -4,821 +4,713 @@ import { InputFormat } from './input-format.js';
4
4
  import { OptionMember } from './option.js';
5
5
  import { OutputFormat } from './output-format.js';
6
6
  import { Stream } from './stream.js';
7
- import type { AVFormatFlag, AVMediaType, AVSeekFlag } from './constants.js';
7
+ import type { AVFormatFlag, AVMediaType, AVSeekFlag } from '../constants/constants.js';
8
8
  import type { IOContext } from './io-context.js';
9
9
  import type { NativeFormatContext, NativeWrapper } from './native-types.js';
10
10
  import type { Packet } from './packet.js';
11
11
  /**
12
- * FFmpeg format context for demuxing and muxing.
12
+ * Container format context for reading/writing multimedia files.
13
13
  *
14
- * Central structure for media file handling, managing both input (demuxing) and output (muxing).
15
- * Provides full control over allocation, configuration and lifecycle with no hidden operations.
16
- * Handles container formats, streams, metadata, and packet I/O operations.
14
+ * Central structure for demuxing (reading) and muxing (writing) media files.
15
+ * Manages streams, packets, metadata, and format-specific operations.
16
+ * Supports both file-based and custom I/O through IOContext.
17
+ * Essential for all file-based media operations.
17
18
  *
18
19
  * Direct mapping to FFmpeg's AVFormatContext.
19
20
  *
20
21
  * @example
21
22
  * ```typescript
22
- * import { FormatContext, Packet, FFmpegError } from 'node-av';
23
+ * import { FormatContext, FFmpegError } from 'node-av';
24
+ * import { AVMEDIA_TYPE_VIDEO } from 'node-av/constants';
23
25
  *
24
- * // Demuxing - full control over every step
26
+ * // Open input file
25
27
  * const ctx = new FormatContext();
26
- * ctx.allocContext();
27
- *
28
- * const ret = await ctx.openInput('video.mp4', null, null);
28
+ * let ret = await ctx.openInput('input.mp4');
29
29
  * FFmpegError.throwIfError(ret, 'openInput');
30
30
  *
31
- * const infoRet = await ctx.findStreamInfo(null);
32
- * FFmpegError.throwIfError(infoRet, 'findStreamInfo');
31
+ * ret = await ctx.findStreamInfo();
32
+ * FFmpegError.throwIfError(ret, 'findStreamInfo');
33
+ *
34
+ * // Find video stream
35
+ * const videoIndex = ctx.findBestStream(AVMEDIA_TYPE_VIDEO);
36
+ * if (videoIndex < 0) {
37
+ * throw new Error('No video stream found');
38
+ * }
33
39
  *
40
+ * // Read packets
34
41
  * const packet = new Packet();
35
42
  * packet.alloc();
36
- *
37
- * while (true) {
38
- * const readRet = await ctx.readFrame(packet);
39
- * if (readRet < 0) break; // EOF or error
40
- * // Process packet
43
+ * while ((ret = await ctx.readFrame(packet)) >= 0) {
44
+ * if (packet.streamIndex === videoIndex) {
45
+ * // Process video packet
46
+ * }
41
47
  * packet.unref();
42
48
  * }
43
49
  *
50
+ * // Cleanup
44
51
  * await ctx.closeInput();
45
- * ctx.freeContext();
46
- * ```
47
- *
48
- * @example
49
- * ```typescript
50
- * import { FormatContext, FFmpegError } from 'node-av';
51
- *
52
- * // Muxing - explicit control
53
- * const ctx = new FormatContext();
54
- * const ret = ctx.allocOutputContext2(null, 'mp4', 'output.mp4');
55
- * FFmpegError.throwIfError(ret, 'allocOutputContext2');
56
- *
57
- * // Add streams
58
- * const stream = ctx.newStream(null);
59
- *
60
- * // Write header
61
- * const headerRet = await ctx.writeHeader(null);
62
- * FFmpegError.throwIfError(headerRet, 'writeHeader');
63
- *
64
- * // Write packets
65
- * const writeRet = await ctx.interleavedWriteFrame(packet);
66
- * FFmpegError.throwIfError(writeRet, 'interleavedWriteFrame');
67
- *
68
- * // Finalize
69
- * const trailerRet = await ctx.writeTrailer();
70
- * FFmpegError.throwIfError(trailerRet, 'writeTrailer');
71
- * ctx.freeContext();
72
52
  * ```
73
53
  *
74
- * @see {@link InputFormat} For input format handling
75
- * @see {@link OutputFormat} For output format handling
54
+ * @see [AVFormatContext](https://ffmpeg.org/doxygen/trunk/structAVFormatContext.html) - FFmpeg Doxygen
55
+ * @see {@link InputFormat} For supported input formats
56
+ * @see {@link OutputFormat} For supported output formats
76
57
  * @see {@link Stream} For stream management
77
- * @see {@link Packet} For packet handling
78
58
  */
79
59
  export declare class FormatContext extends OptionMember<NativeFormatContext> implements AsyncDisposable, NativeWrapper<NativeFormatContext> {
80
60
  private _ioContext;
81
- /**
82
- * Create a new format context.
83
- *
84
- * The context is uninitialized - you must call allocContext() or allocOutputContext2() before use.
85
- * No FFmpeg resources are allocated until initialization.
86
- *
87
- * Direct wrapper around AVFormatContext.
88
- *
89
- * @example
90
- * ```typescript
91
- * import { FormatContext, FFmpegError } from 'node-av';
92
- *
93
- * const ctx = new FormatContext();
94
- * ctx.allocContext(); // For demuxing
95
- * // OR
96
- * const ret = ctx.allocOutputContext2(null, 'mp4', 'output.mp4'); // For muxing
97
- * FFmpegError.throwIfError(ret, 'allocOutputContext2');
98
- * ```
99
- */
100
61
  constructor();
101
62
  /**
102
- * Input or output URL/filename.
63
+ * URL or filename of the media.
103
64
  *
104
- * Direct mapping to AVFormatContext->url
65
+ * For input: the opened file path.
66
+ * For output: the target file path.
105
67
  *
106
- * - muxing: Set by user
107
- * - demuxing: Set by avformat_open_input
68
+ * Direct mapping to AVFormatContext->url.
108
69
  */
109
70
  get url(): string | null;
110
71
  set url(value: string | null);
111
72
  /**
112
- * Position of the first frame of the component, in AV_TIME_BASE units.
73
+ * Start time of the stream.
113
74
  *
114
- * Direct mapping to AVFormatContext->start_time
75
+ * Position of the first frame in microseconds.
76
+ * AV_NOPTS_VALUE if unknown.
115
77
  *
116
- * NEVER set this value directly: it is deduced from the AVStream values.
117
- * - demuxing: Set by libavformat
78
+ * Direct mapping to AVFormatContext->start_time.
118
79
  */
119
80
  get startTime(): bigint;
120
81
  /**
121
- * Duration of the stream, in AV_TIME_BASE units.
82
+ * Duration of the stream.
122
83
  *
123
- * Direct mapping to AVFormatContext->duration
84
+ * Total stream duration in microseconds.
85
+ * AV_NOPTS_VALUE if unknown.
124
86
  *
125
- * Only set this value if you know none of the individual stream durations.
126
- * - demuxing: Set by libavformat
127
- * - muxing: May be set by user
87
+ * Direct mapping to AVFormatContext->duration.
128
88
  */
129
89
  get duration(): bigint;
130
90
  /**
131
- * Total stream bitrate in bit/s, 0 if not available.
91
+ * Total stream bitrate.
132
92
  *
133
- * Direct mapping to AVFormatContext->bit_rate
93
+ * Bitrate in bits per second.
94
+ * 0 if unknown.
134
95
  *
135
- * Never set directly if the file_size and duration are known.
96
+ * Direct mapping to AVFormatContext->bit_rate.
136
97
  */
137
98
  get bitRate(): bigint;
138
99
  /**
139
- * Flags modifying demuxer/muxer behavior.
100
+ * Format-specific flags.
140
101
  *
141
- * Direct mapping to AVFormatContext->flags
102
+ * Combination of AVFMT_FLAG_* values controlling
103
+ * format behavior (e.g., AVFMT_FLAG_GENPTS).
142
104
  *
143
- * AVFMT_FLAG_GENPTS, AVFMT_FLAG_IGNIDX, etc.
144
- * - encoding: Set by user
145
- * - decoding: Set by user
105
+ * Direct mapping to AVFormatContext->flags.
146
106
  */
147
107
  get flags(): AVFormatFlag;
148
108
  set flags(value: AVFormatFlag);
149
109
  /**
150
- * Maximum size of the data read from input for determining format.
110
+ * Maximum bytes to probe for format detection.
151
111
  *
152
- * Direct mapping to AVFormatContext->probesize
112
+ * Larger values improve format detection accuracy
113
+ * but increase startup time.
153
114
  *
154
- * - encoding: unused
155
- * - decoding: Set by user
115
+ * Direct mapping to AVFormatContext->probesize.
156
116
  */
157
117
  get probesize(): bigint;
158
118
  set probesize(value: bigint);
159
119
  /**
160
- * Maximum duration (in AV_TIME_BASE units) of the data read from input.
120
+ * Maximum duration to analyze streams.
161
121
  *
162
- * Direct mapping to AVFormatContext->max_analyze_duration
122
+ * Time in microseconds to spend analyzing streams.
123
+ * Larger values improve stream detection accuracy.
163
124
  *
164
- * - encoding: unused
165
- * - decoding: Set by user
125
+ * Direct mapping to AVFormatContext->max_analyze_duration.
166
126
  */
167
127
  get maxAnalyzeDuration(): bigint;
168
128
  set maxAnalyzeDuration(value: bigint);
169
129
  /**
170
- * Metadata that applies to the whole file.
130
+ * Container metadata.
171
131
  *
172
- * Direct mapping to AVFormatContext->metadata
132
+ * Key-value pairs of metadata (title, author, etc.).
173
133
  *
174
- * - demuxing: Set by libavformat
175
- * - muxing: May be set by user
134
+ * Direct mapping to AVFormatContext->metadata.
176
135
  */
177
136
  get metadata(): Dictionary | null;
178
137
  set metadata(value: Dictionary | null);
179
138
  /**
180
- * Input format (demuxing only).
139
+ * Input format descriptor.
181
140
  *
182
- * Direct mapping to AVFormatContext->iformat
141
+ * Format used for demuxing. Null for output contexts.
183
142
  *
184
- * The detected or specified input format.
143
+ * Direct mapping to AVFormatContext->iformat.
185
144
  */
186
145
  get iformat(): InputFormat | null;
187
146
  /**
188
- * Output format (muxing only).
147
+ * Output format descriptor.
189
148
  *
190
- * Direct mapping to AVFormatContext->oformat
149
+ * Format used for muxing. Null for input contexts.
191
150
  *
192
- * The specified output format.
151
+ * Direct mapping to AVFormatContext->oformat.
193
152
  */
194
153
  get oformat(): OutputFormat | null;
195
154
  set oformat(value: OutputFormat | null);
196
155
  /**
197
- * I/O context.
156
+ * Custom I/O context.
198
157
  *
199
- * Direct mapping to AVFormatContext->pb
158
+ * For custom I/O operations instead of file I/O.
200
159
  *
201
- * - demuxing: Either set by user or internal (for protocols like file:)
202
- * - muxing: Set by user for custom I/O. Otherwise internal
203
- *
204
- * The FormatContext keeps track of the IOContext set by the user.
205
- * This prevents ownership confusion.
160
+ * Direct mapping to AVFormatContext->pb.
206
161
  */
207
162
  get pb(): IOContext | null;
208
163
  set pb(value: IOContext | null);
209
164
  /**
210
- * Number of streams in the file.
165
+ * Number of streams in the container.
211
166
  *
212
- * Direct mapping to AVFormatContext->nb_streams
167
+ * Direct mapping to AVFormatContext->nb_streams.
213
168
  */
214
169
  get nbStreams(): number;
215
170
  /**
216
- * Array of streams in the file.
171
+ * Array of streams in the container.
172
+ *
173
+ * All audio, video, subtitle, and data streams.
217
174
  *
218
- * Direct mapping to AVFormatContext->streams
175
+ * Direct mapping to AVFormatContext->streams.
219
176
  */
220
177
  get streams(): Stream[] | null;
221
178
  /**
222
- * Allow non-standard and experimental extension.
179
+ * Strictness level for standards compliance.
223
180
  *
224
- * Direct mapping to AVFormatContext->strict_std_compliance
181
+ * FF_COMPLIANCE_* value controlling how strictly
182
+ * to follow specifications.
225
183
  *
226
- * @see AVCodecContext.strict_std_compliance
184
+ * Direct mapping to AVFormatContext->strict_std_compliance.
227
185
  */
228
186
  get strictStdCompliance(): number;
229
187
  set strictStdCompliance(value: number);
230
188
  /**
231
189
  * Maximum number of streams.
232
190
  *
233
- * Direct mapping to AVFormatContext->max_streams
191
+ * Limit on stream count for security/resource reasons.
234
192
  *
235
- * - encoding: unused
236
- * - decoding: Set by user
193
+ * Direct mapping to AVFormatContext->max_streams.
237
194
  */
238
195
  get maxStreams(): number;
239
196
  set maxStreams(value: number);
240
197
  /**
241
- * Number of programs in the stream.
198
+ * Number of programs.
242
199
  *
243
- * Direct mapping to AVFormatContext->nb_programs
200
+ * For containers with multiple programs (e.g., MPEG-TS).
244
201
  *
245
- * - demuxing: Set by libavformat
202
+ * Direct mapping to AVFormatContext->nb_programs.
246
203
  */
247
204
  get nbPrograms(): number;
248
205
  /**
249
- * Current position in bytes (avio_tell).
250
- *
251
- * Direct mapping to avio_tell(AVFormatContext->pb)
206
+ * Number of bytes read/written through I/O context.
252
207
  *
253
- * Returns the current byte position in the I/O context.
254
- * - demuxing: Current read position
255
- * - muxing: Current write position
208
+ * Direct mapping to avio_tell(AVFormatContext->pb).
256
209
  */
257
210
  get pbBytes(): bigint;
258
211
  /**
259
- * Format probing score (0-100).
212
+ * Format probe score.
260
213
  *
261
- * Direct mapping to AVFormatContext->probe_score
214
+ * Confidence score from format detection (0-100).
215
+ * Higher values indicate more confident detection.
262
216
  *
263
- * Indicates confidence in format detection (higher is better).
264
- * - demuxing: Set by format probing
217
+ * Direct mapping to AVFormatContext->probe_score.
265
218
  */
266
219
  get probeScore(): number;
267
220
  /**
268
- * Allocate an AVFormatContext for input.
221
+ * Allocate a format context.
269
222
  *
270
- * Direct mapping to avformat_alloc_context()
223
+ * Allocates the context structure. Usually not needed
224
+ * as openInput/allocOutputContext2 handle this.
225
+ *
226
+ * Direct mapping to avformat_alloc_context().
271
227
  *
272
228
  * @example
273
229
  * ```typescript
274
230
  * const ctx = new FormatContext();
275
231
  * ctx.allocContext();
276
- * // Context is now allocated for demuxing
232
+ * // Context is now allocated
277
233
  * ```
278
- *
279
- * @throws {Error} If allocation fails (ENOMEM)
280
234
  */
281
235
  allocContext(): void;
282
236
  /**
283
- * Allocate an AVFormatContext for output.
237
+ * Allocate an output format context.
284
238
  *
285
- * Direct mapping to avformat_alloc_output_context2()
239
+ * Allocates and configures context for writing.
240
+ * Format is determined by parameters in priority order.
286
241
  *
287
- * @param oformat - Output format. May be null to auto-detect from filename
288
- * @param formatName - Format name. May be null to auto-detect
289
- * @param filename - Filename for output. Used to guess format if not specified
242
+ * Direct mapping to avformat_alloc_output_context2().
290
243
  *
244
+ * @param oformat - Specific output format to use
245
+ * @param formatName - Format name (e.g., 'mp4', 'mkv')
246
+ * @param filename - Filename to guess format from extension
291
247
  * @returns 0 on success, negative AVERROR on error:
292
- * - 0: Success
293
- * - AVERROR(EINVAL): Invalid arguments
294
- * - AVERROR(ENOMEM): Memory allocation failure
248
+ * - AVERROR_ENOMEM: Memory allocation failure
249
+ * - AVERROR_EINVAL: Invalid parameters
295
250
  *
296
251
  * @example
297
252
  * ```typescript
253
+ * import { FFmpegError } from 'node-av';
254
+ *
298
255
  * const ctx = new FormatContext();
299
256
  * const ret = ctx.allocOutputContext2(null, 'mp4', 'output.mp4');
300
- * if (ret < 0) {
301
- * throw new FFmpegError(ret);
302
- * }
257
+ * FFmpegError.throwIfError(ret, 'allocOutputContext2');
303
258
  * ```
259
+ *
260
+ * @see {@link openOutput} To open output file
261
+ * @see {@link writeHeader} To write file header
304
262
  */
305
263
  allocOutputContext2(oformat: OutputFormat | null, formatName: string | null, filename: string | null): number;
306
264
  /**
307
- * Free an AVFormatContext and all its streams.
265
+ * Free the format context.
308
266
  *
309
- * Direct mapping to avformat_free_context()
267
+ * Releases all resources. The context becomes invalid.
268
+ *
269
+ * Direct mapping to avformat_free_context().
310
270
  *
311
271
  * @example
312
272
  * ```typescript
313
273
  * ctx.freeContext();
314
- * // ctx is now invalid and should not be used
274
+ * // Context is now invalid
315
275
  * ```
276
+ *
277
+ * @see {@link Symbol.asyncDispose} For automatic cleanup
316
278
  */
317
279
  freeContext(): void;
318
280
  /**
319
- * Close an opened input AVFormatContext.
281
+ * Close an input format context.
282
+ *
283
+ * Closes input file and releases resources.
320
284
  *
321
- * Direct mapping to avformat_close_input()
285
+ * Direct mapping to avformat_close_input().
286
+ *
287
+ * @returns Promise that resolves when closed
322
288
  *
323
289
  * @example
324
290
  * ```typescript
325
291
  * await ctx.closeInput();
326
- * // Input is closed and context is freed
292
+ * // Input closed and context freed
327
293
  * ```
294
+ *
295
+ * @see {@link openInput} To open input
328
296
  */
329
297
  closeInput(): Promise<void>;
330
298
  /**
331
- * Open the output file for writing.
299
+ * Open output file for writing.
332
300
  *
333
- * Must be called after allocOutputContext2() and before writeHeader()
334
- * for file-based formats (not NOFILE formats).
301
+ * Opens the output file specified in url.
302
+ * Must call allocOutputContext2 first.
335
303
  *
336
- * Direct mapping to avio_open()
304
+ * Direct mapping to avio_open2().
337
305
  *
338
- * @returns Promise resolving to 0 on success, negative AVERROR on failure:
339
- * - 0: Success
340
- * - AVERROR(ENOENT): File cannot be created
341
- * - AVERROR(EACCES): Permission denied
342
- * - AVERROR(EIO): I/O error
306
+ * @returns 0 on success, negative AVERROR on error:
307
+ * - AVERROR_ENOENT: File not found
308
+ * - AVERROR_EACCES: Permission denied
309
+ * - AVERROR_EIO: I/O error
343
310
  *
344
311
  * @example
345
312
  * ```typescript
346
- * import { FormatContext, FFmpegError } from 'node-av';
313
+ * import { FFmpegError } from 'node-av';
347
314
  *
348
- * ctx.allocOutputContext2(null, null, 'output.mp4');
349
- * const ret = await ctx.openOutput(); // Opens the file for writing
315
+ * const ret = await ctx.openOutput();
350
316
  * FFmpegError.throwIfError(ret, 'openOutput');
351
- * await ctx.writeHeader(null);
352
317
  * ```
318
+ *
319
+ * @see {@link allocOutputContext2} Must be called first
320
+ * @see {@link closeOutput} To close output
353
321
  */
354
322
  openOutput(): Promise<number>;
355
323
  /**
356
- * Close the output file.
324
+ * Close output file.
325
+ *
326
+ * Closes the output file and releases I/O resources.
357
327
  *
358
- * Direct mapping to avio_closep()
328
+ * Direct mapping to avio_closep().
359
329
  *
360
- * Should be called after writeTrailer() for file-based formats.
361
- * The file is automatically closed by freeContext() as well.
330
+ * @returns Promise that resolves when closed
362
331
  *
363
332
  * @example
364
333
  * ```typescript
365
- * await ctx.writeTrailer();
366
334
  * await ctx.closeOutput();
367
- * ctx.freeContext();
335
+ * // Output file closed
368
336
  * ```
337
+ *
338
+ * @see {@link openOutput} To open output
369
339
  */
370
340
  closeOutput(): Promise<void>;
371
341
  /**
372
- * Open an input stream and read the header.
373
- *
374
- * Opens the specified URL, reads the format header, and initializes the stream information.
375
- * Automatically detects the format if not specified.
342
+ * Open input file for reading.
376
343
  *
377
- * Direct mapping to avformat_open_input()
344
+ * Opens and probes the input file, detecting format automatically
345
+ * unless specified.
378
346
  *
379
- * @param url - URL of the stream to open
380
- * @param fmt - Input format. May be null to auto-detect
381
- * @param options - Dictionary of options. May be null
347
+ * Direct mapping to avformat_open_input().
382
348
  *
349
+ * @param url - URL or file path to open
350
+ * @param fmt - Force specific input format (null for auto-detect)
351
+ * @param options - Format-specific options
383
352
  * @returns 0 on success, negative AVERROR on error:
384
- * - 0: Success
385
- * - AVERROR(ENOENT): File not found
386
- * - AVERROR(EIO): I/O error
387
- * - AVERROR(EINVAL): Invalid data found
388
- * - AVERROR(ENOMEM): Memory allocation failure
389
- * - <0: Other format-specific errors
353
+ * - AVERROR_ENOENT: File not found
354
+ * - AVERROR_INVALIDDATA: Invalid file format
355
+ * - AVERROR_EIO: I/O error
390
356
  *
391
357
  * @example
392
358
  * ```typescript
393
- * import { FormatContext, FFmpegError } from 'node-av';
359
+ * import { FFmpegError } from 'node-av';
394
360
  *
395
- * const ret = await ctx.openInput('video.mp4', null, null);
361
+ * const ret = await ctx.openInput('input.mp4');
396
362
  * FFmpegError.throwIfError(ret, 'openInput');
397
- * // Context is now open and header is read
398
363
  * ```
364
+ *
365
+ * @see {@link findStreamInfo} To analyze streams after opening
366
+ * @see {@link closeInput} To close input
399
367
  */
400
368
  openInput(url: string, fmt?: InputFormat | null, options?: Dictionary | null): Promise<number>;
401
369
  /**
402
- * Read packets of a media file to get stream information.
370
+ * Analyze streams to get stream info.
403
371
  *
404
- * Analyzes the media file by reading packets to determine stream parameters
405
- * like codec, dimensions, sample rate, etc. Required after openInput().
372
+ * Reads packet headers to fill in stream information.
373
+ * Should be called after openInput for accurate stream data.
406
374
  *
407
- * Direct mapping to avformat_find_stream_info()
408
- *
409
- * @param options - Dictionary of options per stream. May be null
375
+ * Direct mapping to avformat_find_stream_info().
410
376
  *
377
+ * @param options - Per-stream options array
411
378
  * @returns >=0 on success, negative AVERROR on error:
412
- * - >=0: Success (number of stream info found)
413
- * - AVERROR(EIO): I/O error
414
- * - AVERROR(ENOMEM): Memory allocation failure
415
- * - <0: Other errors
379
+ * - AVERROR_EOF: End of file reached
380
+ * - AVERROR_ENOMEM: Memory allocation failure
416
381
  *
417
382
  * @example
418
383
  * ```typescript
419
- * import { FormatContext, FFmpegError } from 'node-av';
384
+ * import { FFmpegError } from 'node-av';
420
385
  *
421
- * const ret = await ctx.findStreamInfo(null);
386
+ * const ret = await ctx.findStreamInfo();
422
387
  * FFmpegError.throwIfError(ret, 'findStreamInfo');
423
388
  * console.log(`Found ${ctx.nbStreams} streams`);
424
389
  * ```
390
+ *
391
+ * @see {@link openInput} Must be called first
425
392
  */
426
393
  findStreamInfo(options?: Dictionary[] | null): Promise<number>;
427
394
  /**
428
- * Read the next packet in the file.
395
+ * Read next packet from the input.
429
396
  *
430
- * Reads and returns the next packet from the input file.
431
- * Packets must be unreferenced after use to free memory.
397
+ * Reads and returns the next packet in the stream.
398
+ * Packet must be unreferenced after use.
432
399
  *
433
- * Direct mapping to av_read_frame()
434
- *
435
- * @param pkt - Packet to fill with data
400
+ * Direct mapping to av_read_frame().
436
401
  *
402
+ * @param pkt - Packet to read into
437
403
  * @returns 0 on success, negative AVERROR on error:
438
- * - 0: Success
439
- * - AVERROR_EOF: End of file reached
440
- * - AVERROR(EAGAIN): Temporarily unavailable
441
- * - AVERROR(EIO): I/O error
442
- * - <0: Other errors
404
+ * - AVERROR_EOF: End of file
405
+ * - AVERROR_EAGAIN: Temporarily unavailable
443
406
  *
444
407
  * @example
445
408
  * ```typescript
446
- * import { Packet, FormatContext, FFmpegError, AVERROR_EOF } from 'node-av';
409
+ * import { FFmpegError } from 'node-av';
410
+ * import { AVERROR_EOF } from 'node-av';
447
411
  *
448
412
  * const packet = new Packet();
449
413
  * packet.alloc();
450
414
  *
451
- * while (true) {
452
- * const ret = await ctx.readFrame(packet);
453
- * if (ret === AVERROR_EOF) break;
454
- * FFmpegError.throwIfError(ret, 'readFrame');
455
- *
415
+ * let ret;
416
+ * while ((ret = await ctx.readFrame(packet)) >= 0) {
456
417
  * // Process packet
457
418
  * console.log(`Stream ${packet.streamIndex}, PTS: ${packet.pts}`);
458
419
  * packet.unref();
459
420
  * }
421
+ *
422
+ * if (ret !== AVERROR_EOF) {
423
+ * FFmpegError.throwIfError(ret, 'readFrame');
424
+ * }
460
425
  * ```
426
+ *
427
+ * @see {@link seekFrame} To seek before reading
461
428
  */
462
429
  readFrame(pkt: Packet): Promise<number>;
463
430
  /**
464
- * Seek to the keyframe at timestamp.
465
- *
466
- * Seeks to the keyframe at or before the specified timestamp.
467
- * The next packet read will be from the new position.
431
+ * Seek to timestamp in stream.
468
432
  *
469
- * Direct mapping to av_seek_frame()
433
+ * Seeks to the keyframe at or before the given timestamp.
470
434
  *
471
- * @param streamIndex - Stream index or -1 for default
472
- * @param timestamp - Timestamp in stream time_base units or AV_TIME_BASE if stream_index is -1
473
- * @param flags - AVSEEK_FLAG_* flags
435
+ * Direct mapping to av_seek_frame().
474
436
  *
437
+ * @param streamIndex - Stream to seek in (-1 for default)
438
+ * @param timestamp - Target timestamp in stream time base
439
+ * @param flags - Seek flags (AVSEEK_FLAG_*)
475
440
  * @returns >=0 on success, negative AVERROR on error:
476
- * - >=0: Success
477
- * - AVERROR(EINVAL): Invalid arguments
478
- * - AVERROR(EIO): I/O error
479
- * - <0: Other errors
441
+ * - AVERROR_EINVAL: Invalid parameters
442
+ * - AVERROR_EOF: Seek beyond file
480
443
  *
481
444
  * @example
482
445
  * ```typescript
483
- * import { FormatContext, FFmpegError, AV_TIME_BASE, AVSEEK_FLAG_BACKWARD } from 'node-av';
446
+ * import { FFmpegError } from 'node-av';
447
+ * import { AVSEEK_FLAG_BACKWARD } from 'node-av/constants';
484
448
  *
485
- * // Seek to 10 seconds
486
- * const timestamp = 10n * BigInt(AV_TIME_BASE);
487
- * const ret = await ctx.seekFrame(-1, timestamp, AVSEEK_FLAG_BACKWARD);
449
+ * // Seek to 10 seconds (assuming 1/1000 time base)
450
+ * const ret = await ctx.seekFrame(videoStreamIndex, 10000n, AVSEEK_FLAG_BACKWARD);
488
451
  * FFmpegError.throwIfError(ret, 'seekFrame');
489
- * // Next readFrame will return packet from new position
490
452
  * ```
453
+ *
454
+ * @see {@link seekFile} For more precise seeking
491
455
  */
492
456
  seekFrame(streamIndex: number, timestamp: bigint, flags?: AVSeekFlag): Promise<number>;
493
457
  /**
494
- * Seek to timestamp ts with more control.
458
+ * Seek to timestamp with bounds.
495
459
  *
496
- * Seeks to a timestamp within the specified min/max range.
497
- * More precise than seekFrame() with tolerance control.
460
+ * More precise seeking with min/max timestamp bounds.
498
461
  *
499
- * Direct mapping to avformat_seek_file()
462
+ * Direct mapping to avformat_seek_file().
500
463
  *
501
- * @param streamIndex - Stream index or -1 for default
502
- * @param minTs - Smallest acceptable timestamp
464
+ * @param streamIndex - Stream to seek in (-1 for default)
465
+ * @param minTs - Minimum acceptable timestamp
503
466
  * @param ts - Target timestamp
504
- * @param maxTs - Largest acceptable timestamp
505
- * @param flags - AVSEEK_FLAG_* flags
506
- *
507
- * @returns >=0 on success, negative AVERROR on error:
508
- * - >=0: Success
509
- * - AVERROR(EINVAL): Invalid arguments
510
- * - AVERROR(EIO): I/O error
511
- * - <0: Other errors
467
+ * @param maxTs - Maximum acceptable timestamp
468
+ * @param flags - Seek flags
469
+ * @returns >=0 on success, negative AVERROR on error
512
470
  *
513
471
  * @example
514
472
  * ```typescript
515
- * import { FormatContext, FFmpegError, AV_TIME_BASE } from 'node-av';
473
+ * import { FFmpegError } from 'node-av';
516
474
  *
517
- * // Seek to timestamp with tolerance
518
- * const target = 10n * BigInt(AV_TIME_BASE);
519
- * const tolerance = BigInt(AV_TIME_BASE);
475
+ * // Seek to 10s with 0.5s tolerance
476
+ * const target = 10000n;
520
477
  * const ret = await ctx.seekFile(
521
478
  * -1,
522
- * target - tolerance, // min: 9 seconds
523
- * target, // target: 10 seconds
524
- * target + tolerance, // max: 11 seconds
525
- * 0
479
+ * target - 500n,
480
+ * target,
481
+ * target + 500n
526
482
  * );
527
483
  * FFmpegError.throwIfError(ret, 'seekFile');
528
484
  * ```
485
+ *
486
+ * @see {@link seekFrame} For simpler seeking
529
487
  */
530
488
  seekFile(streamIndex: number, minTs: bigint, ts: bigint, maxTs: bigint, flags?: AVSeekFlag): Promise<number>;
531
489
  /**
532
- * Allocate the stream private data and write the stream header.
533
- *
534
- * Initializes the output file and writes the file header.
535
- * Must be called before writing any packets.
490
+ * Write file header.
536
491
  *
537
- * Direct mapping to avformat_write_header()
492
+ * Writes the file header and initializes output.
493
+ * Must be called before writing packets.
538
494
  *
539
- * @param options - Dictionary of options. May be null
495
+ * Direct mapping to avformat_write_header().
540
496
  *
497
+ * @param options - Muxer-specific options
541
498
  * @returns 0 on success, negative AVERROR on error:
542
- * - 0: Success
543
- * - AVERROR(EINVAL): Invalid parameters
544
- * - AVERROR(EIO): I/O error
545
- * - AVERROR(ENOMEM): Memory allocation failure
546
- * - <0: Other muxer-specific errors
499
+ * - AVERROR_EINVAL: Invalid parameters
500
+ * - AVERROR_EIO: I/O error
547
501
  *
548
502
  * @example
549
503
  * ```typescript
550
- * import { Dictionary, FormatContext, FFmpegError } from 'node-av';
551
- *
552
- * const options = new Dictionary();
553
- * options.set('movflags', 'faststart', 0);
504
+ * import { FFmpegError } from 'node-av';
554
505
  *
555
- * const ret = await ctx.writeHeader(options);
506
+ * const ret = await ctx.writeHeader();
556
507
  * FFmpegError.throwIfError(ret, 'writeHeader');
557
- * options.free();
558
- * // Header written, ready to write packets
508
+ * // Now ready to write packets
559
509
  * ```
510
+ *
511
+ * @see {@link writeTrailer} To finalize file
512
+ * @see {@link writeFrame} To write packets
560
513
  */
561
514
  writeHeader(options?: Dictionary | null): Promise<number>;
562
515
  /**
563
- * Write a packet to an output media file.
564
- *
565
- * Writes a packet directly without reordering.
566
- * Use interleavedWriteFrame() for automatic interleaving.
516
+ * Write packet to output.
567
517
  *
568
- * Direct mapping to av_write_frame()
518
+ * Writes a packet directly without interleaving.
519
+ * Caller must handle correct interleaving.
569
520
  *
570
- * @param pkt - Packet to write. May be null to flush
521
+ * Direct mapping to av_write_frame().
571
522
  *
523
+ * @param pkt - Packet to write (null to flush)
572
524
  * @returns 0 on success, negative AVERROR on error:
573
- * - 0: Success
574
- * - AVERROR(EINVAL): Invalid packet
575
- * - AVERROR(EIO): I/O error
576
- * - <0: Other muxer-specific errors
525
+ * - AVERROR_EINVAL: Invalid packet
526
+ * - AVERROR_EIO: I/O error
577
527
  *
578
528
  * @example
579
529
  * ```typescript
580
- * import { FormatContext, FFmpegError } from 'node-av';
530
+ * import { FFmpegError } from 'node-av';
581
531
  *
582
532
  * const ret = await ctx.writeFrame(packet);
583
533
  * FFmpegError.throwIfError(ret, 'writeFrame');
584
- *
585
- * // Flush at the end
586
- * const flushRet = await ctx.writeFrame(null);
587
- * FFmpegError.throwIfError(flushRet, 'writeFrame flush');
588
534
  * ```
589
535
  *
590
- * @note This function does NOT take ownership of the packet.
591
- *
592
536
  * @see {@link interleavedWriteFrame} For automatic interleaving
593
537
  */
594
538
  writeFrame(pkt: Packet | null): Promise<number>;
595
539
  /**
596
- * Write a packet to an output media file ensuring correct interleaving.
597
- *
598
- * Automatically interleaves packets from different streams based on DTS.
599
- * Preferred method for writing packets in most cases.
540
+ * Write packet with automatic interleaving.
600
541
  *
601
- * Direct mapping to av_interleaved_write_frame()
542
+ * Writes packet with proper interleaving for muxing.
543
+ * Preferred method for writing packets.
602
544
  *
603
- * @param pkt - Packet to write. May be null to flush
545
+ * Direct mapping to av_interleaved_write_frame().
604
546
  *
547
+ * @param pkt - Packet to write (null to flush)
605
548
  * @returns 0 on success, negative AVERROR on error:
606
- * - 0: Success
607
- * - AVERROR(EINVAL): Invalid packet
608
- * - AVERROR(EIO): I/O error
609
- * - <0: Other muxer-specific errors
549
+ * - AVERROR_EINVAL: Invalid packet
550
+ * - AVERROR_EIO: I/O error
610
551
  *
611
552
  * @example
612
553
  * ```typescript
613
- * import { FormatContext, FFmpegError } from 'node-av';
554
+ * import { FFmpegError } from 'node-av';
614
555
  *
615
- * // Write packets with automatic interleaving
556
+ * // Write with proper interleaving
616
557
  * const ret = await ctx.interleavedWriteFrame(packet);
617
558
  * FFmpegError.throwIfError(ret, 'interleavedWriteFrame');
618
- * // Packet is automatically freed after writing
619
- * ```
620
559
  *
621
- * @note This function TAKES OWNERSHIP of the packet and will free it.
560
+ * // Flush buffered packets
561
+ * await ctx.interleavedWriteFrame(null);
562
+ * ```
622
563
  *
623
- * @see {@link writeFrame} For manual interleaving control
564
+ * @see {@link writeFrame} For direct writing
624
565
  */
625
566
  interleavedWriteFrame(pkt: Packet | null): Promise<number>;
626
567
  /**
627
- * Write the stream trailer to an output media file.
568
+ * Write file trailer.
628
569
  *
629
- * Finalizes the output file by writing the trailer.
630
- * Must be called after all packets have been written.
570
+ * Finalizes the output file, writing index and metadata.
571
+ * Must be called to properly close output files.
631
572
  *
632
- * Direct mapping to av_write_trailer()
573
+ * Direct mapping to av_write_trailer().
633
574
  *
634
575
  * @returns 0 on success, negative AVERROR on error:
635
- * - 0: Success
636
- * - AVERROR(EIO): I/O error
637
- * - <0: Other muxer-specific errors
576
+ * - AVERROR_EIO: I/O error
638
577
  *
639
578
  * @example
640
579
  * ```typescript
641
- * import { FormatContext, FFmpegError } from 'node-av';
580
+ * import { FFmpegError } from 'node-av';
642
581
  *
643
- * // Finalize the output file
644
582
  * const ret = await ctx.writeTrailer();
645
583
  * FFmpegError.throwIfError(ret, 'writeTrailer');
646
- * // File is now complete and finalized
584
+ * // File is now finalized
647
585
  * ```
648
586
  *
649
- * @note Must be called after all packets have been written.
587
+ * @see {@link writeHeader} Must be called first
650
588
  */
651
589
  writeTrailer(): Promise<number>;
652
590
  /**
653
- * Flush any buffered data to the output.
591
+ * Flush buffered data.
592
+ *
593
+ * Flushes any buffered packets in muxers.
654
594
  *
655
- * Direct mapping to avio_flush()
595
+ * Direct mapping to avio_flush().
656
596
  *
657
597
  * @example
658
598
  * ```typescript
659
- * import { FormatContext } from 'node-av';
660
- *
661
- * // Flush buffered data
662
599
  * ctx.flush();
663
- * // All buffered data written to output
600
+ * // Buffered data written to output
664
601
  * ```
665
602
  */
666
603
  flush(): void;
667
604
  /**
668
- * Dump format information to stderr.
605
+ * Print format information.
669
606
  *
670
- * Prints detailed information about the format and streams to stderr.
671
- * Useful for debugging and understanding file structure.
607
+ * Dumps human-readable format info to stderr.
608
+ * Useful for debugging.
672
609
  *
673
- * Direct mapping to av_dump_format()
610
+ * Direct mapping to av_dump_format().
674
611
  *
675
- * @param index - Stream index or program index to dump
676
- * @param url - URL or filename to display
677
- * @param isOutput - true for output format, false for input format
612
+ * @param index - Stream index to highlight (-1 for none)
613
+ * @param url - URL to display
614
+ * @param isOutput - True for output format, false for input
678
615
  *
679
616
  * @example
680
617
  * ```typescript
681
- * import { FormatContext } from 'node-av';
682
- *
683
618
  * // Dump input format info
684
- * formatCtx.dumpFormat(0, 'input.mp4', false);
619
+ * ctx.dumpFormat(0, 'input.mp4', false);
685
620
  *
686
621
  * // Dump output format info
687
- * formatCtx.dumpFormat(0, 'output.mp4', true);
622
+ * ctx.dumpFormat(0, 'output.mp4', true);
688
623
  * ```
689
624
  */
690
625
  dumpFormat(index: number, url: string, isOutput: boolean): void;
691
626
  /**
692
- * Find the "best" stream in the file.
693
- *
694
- * The best stream is determined according to various heuristics as the most
695
- * likely to be what the user expects.
627
+ * Find the best stream of a given type.
696
628
  *
697
- * Direct mapping to av_find_best_stream()
629
+ * Selects the most suitable stream (e.g., default audio/video).
698
630
  *
699
- * @param type - Media type (AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, etc.)
700
- * @param wantedStreamNb - User-requested stream number, or -1 for automatic selection
701
- * @param relatedStream - Try to find a stream related to this one, or -1 if none
631
+ * Direct mapping to av_find_best_stream().
702
632
  *
703
- * @returns The stream index if found, or negative AVERROR on error:
704
- * - >=0: Stream index of the best stream
705
- * - AVERROR_STREAM_NOT_FOUND: No stream of the requested type found
706
- * - AVERROR_DECODER_NOT_FOUND: Streams were found but no decoder
707
- * - <0: Other errors
633
+ * @param type - Media type to find (AVMEDIA_TYPE_*)
634
+ * @param wantedStreamNb - Preferred stream index (-1 for auto)
635
+ * @param relatedStream - Related stream for audio/video sync (-1 for none)
636
+ * @returns Stream index, or negative AVERROR if not found
708
637
  *
709
638
  * @example
710
639
  * ```typescript
711
- * import { FormatContext, FFmpegError, AVMEDIA_TYPE_VIDEO } from 'node-av';
640
+ * import { AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO } from 'node-av/constants';
712
641
  *
713
- * // Find best video stream
714
- * const videoStreamIndex = ctx.findBestStream(
715
- * AVMEDIA_TYPE_VIDEO,
716
- * -1, // automatic selection
717
- * -1 // no related stream
718
- * );
642
+ * const videoIndex = ctx.findBestStream(AVMEDIA_TYPE_VIDEO);
643
+ * if (videoIndex >= 0) {
644
+ * console.log(`Best video stream: ${videoIndex}`);
645
+ * }
719
646
  *
720
- * if (videoStreamIndex < 0) {
721
- * FFmpegError.throwIfError(videoStreamIndex, 'findBestStream');
647
+ * const audioIndex = ctx.findBestStream(AVMEDIA_TYPE_AUDIO);
648
+ * if (audioIndex >= 0) {
649
+ * console.log(`Best audio stream: ${audioIndex}`);
722
650
  * }
723
- * const videoStream = ctx.streams[videoStreamIndex];
724
- * // Process video stream
725
651
  * ```
726
652
  */
727
653
  findBestStream(type: AVMediaType, wantedStreamNb?: number, relatedStream?: number): number;
728
654
  /**
729
- * Find the "best" stream in the file and optionally return the decoder.
730
- *
731
- * The best stream is determined according to various heuristics as the most
732
- * likely to be what the user expects.
733
- *
734
- * Direct mapping to av_find_best_stream()
735
- *
736
- * @param type - Media type (AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, etc.)
737
- * @param wantedStreamNb - User-requested stream number, or -1 for automatic selection
738
- * @param relatedStream - Try to find a stream related to this one, or -1 if none
739
- * @param wantDecoder - Whether to return the decoder for the stream
740
- * @param flags - Currently unused, pass 0
741
- *
742
- * @returns Object with stream index and decoder if found
743
- *
744
- * @example
745
- * ```typescript
746
- * import { FormatContext, FFmpegError, AVMEDIA_TYPE_VIDEO } from 'node-av';
747
- *
748
- * // Find best video stream with decoder
749
- * const result = ctx.findBestStream(
750
- * AVMEDIA_TYPE_VIDEO,
751
- * -1, // automatic selection
752
- * -1, // no related stream
753
- * true, // want decoder
754
- * 0
755
- * );
655
+ * Find the best stream and its decoder.
756
656
  *
757
- * if (result.streamIndex < 0) {
758
- * FFmpegError.throwIfError(result.streamIndex, 'findBestStream');
759
- * }
760
- * const videoStream = ctx.streams[result.streamIndex];
761
- * const decoder = result.decoder;
762
- * // Use decoder directly
763
- * ```
657
+ * @param type - Media type to find
658
+ * @param wantedStreamNb - Preferred stream index
659
+ * @param relatedStream - Related stream index
660
+ * @param wantDecoder - True to also find decoder
661
+ * @param flags - Reserved flags
662
+ * @returns Object with stream index and decoder
764
663
  */
765
664
  findBestStream(type: AVMediaType, wantedStreamNb: number, relatedStream: number, wantDecoder: true, flags?: number): {
766
665
  streamIndex: number;
767
666
  decoder: Codec | null;
768
667
  };
769
668
  /**
770
- * Add a new stream to the media file.
771
- *
772
- * Creates a new stream in the output format context.
773
- * The stream must be configured before calling writeHeader().
669
+ * Add a new stream to output context.
774
670
  *
775
- * Direct mapping to avformat_new_stream()
671
+ * Creates a new stream for writing.
776
672
  *
777
- * @param c - Codec to use for the stream. May be null
673
+ * Direct mapping to avformat_new_stream().
778
674
  *
779
- * @returns The newly created stream
780
- *
781
- * @throws {Error} If stream creation fails (ENOMEM)
675
+ * @param c - Codec for the stream (optional)
676
+ * @returns New stream instance
782
677
  *
783
678
  * @example
784
679
  * ```typescript
785
- * import { FormatContext, Codec, FFmpegError } from 'node-av';
786
- * import { AV_CODEC_ID_H264, AVMEDIA_TYPE_VIDEO } from 'node-av/constants';
787
- *
788
- * // Add a video stream
789
- * const videoCodec = Codec.findEncoder(AV_CODEC_ID_H264);
790
- * const videoStream = ctx.newStream(videoCodec);
791
- *
792
- * // Configure stream parameters
793
- * videoStream.codecpar.codecType = AVMEDIA_TYPE_VIDEO;
794
- * videoStream.codecpar.codecId = AV_CODEC_ID_H264;
795
- * videoStream.codecpar.width = 1920;
796
- * videoStream.codecpar.height = 1080;
680
+ * import { Codec } from 'node-av';
681
+ * import { AV_CODEC_ID_H264 } from 'node-av/constants';
682
+ *
683
+ * const codec = Codec.findEncoder(AV_CODEC_ID_H264);
684
+ * const stream = ctx.newStream(codec);
685
+ * stream.id = ctx.nbStreams - 1;
797
686
  * ```
687
+ *
688
+ * @see {@link Stream} For stream configuration
798
689
  */
799
690
  newStream(c?: Codec | null): Stream;
800
691
  /**
801
- * Get the native FFmpeg AVFormatContext pointer.
692
+ * Get the underlying native FormatContext object.
802
693
  *
803
- * @internal For use by other wrapper classes
804
- * @returns The underlying native format context object
694
+ * @returns The native FormatContext binding object
695
+ *
696
+ * @internal
805
697
  */
806
698
  getNative(): NativeFormatContext;
807
699
  /**
808
- * Dispose of the format context asynchronously.
700
+ * Dispose of the format context.
809
701
  *
810
702
  * Implements the AsyncDisposable interface for automatic cleanup.
811
- * Properly handles network connections and I/O operations.
703
+ * Closes input/output and frees resources.
704
+ *
705
+ * @returns Promise that resolves when disposed
812
706
  *
813
707
  * @example
814
708
  * ```typescript
815
- * import { FormatContext } from 'node-av';
816
- *
817
709
  * {
818
710
  * await using ctx = new FormatContext();
819
- * await ctx.openInput('rtsp://camera/stream');
820
- * // ... use context
821
- * } // Automatically closed when leaving scope
711
+ * await ctx.openInput('input.mp4');
712
+ * // Use context...
713
+ * } // Automatically closed and freed
822
714
  * ```
823
715
  */
824
716
  [Symbol.asyncDispose](): Promise<void>;