@revizly/node-av 5.2.2-beta.1
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/BUILD_LINUX.md +61 -0
- package/LICENSE.md +22 -0
- package/README.md +662 -0
- package/build_mac_local.sh +69 -0
- package/dist/api/audio-frame-buffer.d.ts +205 -0
- package/dist/api/audio-frame-buffer.js +287 -0
- package/dist/api/audio-frame-buffer.js.map +1 -0
- package/dist/api/bitstream-filter.d.ts +820 -0
- package/dist/api/bitstream-filter.js +1242 -0
- package/dist/api/bitstream-filter.js.map +1 -0
- package/dist/api/constants.d.ts +44 -0
- package/dist/api/constants.js +45 -0
- package/dist/api/constants.js.map +1 -0
- package/dist/api/data/test_av1.ivf +0 -0
- package/dist/api/data/test_h264.h264 +0 -0
- package/dist/api/data/test_hevc.h265 +0 -0
- package/dist/api/data/test_mjpeg.mjpeg +0 -0
- package/dist/api/data/test_vp8.ivf +0 -0
- package/dist/api/data/test_vp9.ivf +0 -0
- package/dist/api/decoder.d.ts +1088 -0
- package/dist/api/decoder.js +1775 -0
- package/dist/api/decoder.js.map +1 -0
- package/dist/api/demuxer.d.ts +1219 -0
- package/dist/api/demuxer.js +2081 -0
- package/dist/api/demuxer.js.map +1 -0
- package/dist/api/device.d.ts +586 -0
- package/dist/api/device.js +961 -0
- package/dist/api/device.js.map +1 -0
- package/dist/api/encoder.d.ts +1132 -0
- package/dist/api/encoder.js +1988 -0
- package/dist/api/encoder.js.map +1 -0
- package/dist/api/filter-complex.d.ts +821 -0
- package/dist/api/filter-complex.js +1604 -0
- package/dist/api/filter-complex.js.map +1 -0
- package/dist/api/filter-presets.d.ts +1286 -0
- package/dist/api/filter-presets.js +2152 -0
- package/dist/api/filter-presets.js.map +1 -0
- package/dist/api/filter.d.ts +1234 -0
- package/dist/api/filter.js +1976 -0
- package/dist/api/filter.js.map +1 -0
- package/dist/api/fmp4-stream.d.ts +426 -0
- package/dist/api/fmp4-stream.js +739 -0
- package/dist/api/fmp4-stream.js.map +1 -0
- package/dist/api/hardware.d.ts +651 -0
- package/dist/api/hardware.js +1260 -0
- package/dist/api/hardware.js.map +1 -0
- package/dist/api/index.d.ts +17 -0
- package/dist/api/index.js +32 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/io-stream.d.ts +307 -0
- package/dist/api/io-stream.js +282 -0
- package/dist/api/io-stream.js.map +1 -0
- package/dist/api/muxer.d.ts +957 -0
- package/dist/api/muxer.js +2002 -0
- package/dist/api/muxer.js.map +1 -0
- package/dist/api/pipeline.d.ts +607 -0
- package/dist/api/pipeline.js +1145 -0
- package/dist/api/pipeline.js.map +1 -0
- package/dist/api/utilities/async-queue.d.ts +120 -0
- package/dist/api/utilities/async-queue.js +211 -0
- package/dist/api/utilities/async-queue.js.map +1 -0
- package/dist/api/utilities/audio-sample.d.ts +117 -0
- package/dist/api/utilities/audio-sample.js +112 -0
- package/dist/api/utilities/audio-sample.js.map +1 -0
- package/dist/api/utilities/channel-layout.d.ts +76 -0
- package/dist/api/utilities/channel-layout.js +80 -0
- package/dist/api/utilities/channel-layout.js.map +1 -0
- package/dist/api/utilities/electron-shared-texture.d.ts +328 -0
- package/dist/api/utilities/electron-shared-texture.js +503 -0
- package/dist/api/utilities/electron-shared-texture.js.map +1 -0
- package/dist/api/utilities/image.d.ts +207 -0
- package/dist/api/utilities/image.js +213 -0
- package/dist/api/utilities/image.js.map +1 -0
- package/dist/api/utilities/index.d.ts +12 -0
- package/dist/api/utilities/index.js +25 -0
- package/dist/api/utilities/index.js.map +1 -0
- package/dist/api/utilities/media-type.d.ts +49 -0
- package/dist/api/utilities/media-type.js +53 -0
- package/dist/api/utilities/media-type.js.map +1 -0
- package/dist/api/utilities/pixel-format.d.ts +89 -0
- package/dist/api/utilities/pixel-format.js +97 -0
- package/dist/api/utilities/pixel-format.js.map +1 -0
- package/dist/api/utilities/sample-format.d.ts +129 -0
- package/dist/api/utilities/sample-format.js +141 -0
- package/dist/api/utilities/sample-format.js.map +1 -0
- package/dist/api/utilities/scheduler.d.ts +138 -0
- package/dist/api/utilities/scheduler.js +98 -0
- package/dist/api/utilities/scheduler.js.map +1 -0
- package/dist/api/utilities/streaming.d.ts +186 -0
- package/dist/api/utilities/streaming.js +309 -0
- package/dist/api/utilities/streaming.js.map +1 -0
- package/dist/api/utilities/timestamp.d.ts +193 -0
- package/dist/api/utilities/timestamp.js +206 -0
- package/dist/api/utilities/timestamp.js.map +1 -0
- package/dist/api/utilities/whisper-model.d.ts +310 -0
- package/dist/api/utilities/whisper-model.js +528 -0
- package/dist/api/utilities/whisper-model.js.map +1 -0
- package/dist/api/utils.d.ts +19 -0
- package/dist/api/utils.js +39 -0
- package/dist/api/utils.js.map +1 -0
- package/dist/api/whisper.d.ts +324 -0
- package/dist/api/whisper.js +362 -0
- package/dist/api/whisper.js.map +1 -0
- package/dist/constants/channel-layouts.d.ts +53 -0
- package/dist/constants/channel-layouts.js +57 -0
- package/dist/constants/channel-layouts.js.map +1 -0
- package/dist/constants/constants.d.ts +2325 -0
- package/dist/constants/constants.js +1887 -0
- package/dist/constants/constants.js.map +1 -0
- package/dist/constants/decoders.d.ts +633 -0
- package/dist/constants/decoders.js +641 -0
- package/dist/constants/decoders.js.map +1 -0
- package/dist/constants/encoders.d.ts +295 -0
- package/dist/constants/encoders.js +308 -0
- package/dist/constants/encoders.js.map +1 -0
- package/dist/constants/hardware.d.ts +26 -0
- package/dist/constants/hardware.js +27 -0
- package/dist/constants/hardware.js.map +1 -0
- package/dist/constants/index.d.ts +5 -0
- package/dist/constants/index.js +6 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/ffmpeg/index.d.ts +99 -0
- package/dist/ffmpeg/index.js +115 -0
- package/dist/ffmpeg/index.js.map +1 -0
- package/dist/ffmpeg/utils.d.ts +31 -0
- package/dist/ffmpeg/utils.js +68 -0
- package/dist/ffmpeg/utils.js.map +1 -0
- package/dist/ffmpeg/version.d.ts +6 -0
- package/dist/ffmpeg/version.js +7 -0
- package/dist/ffmpeg/version.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/audio-fifo.d.ts +399 -0
- package/dist/lib/audio-fifo.js +431 -0
- package/dist/lib/audio-fifo.js.map +1 -0
- package/dist/lib/binding.d.ts +228 -0
- package/dist/lib/binding.js +60 -0
- package/dist/lib/binding.js.map +1 -0
- package/dist/lib/bitstream-filter-context.d.ts +379 -0
- package/dist/lib/bitstream-filter-context.js +441 -0
- package/dist/lib/bitstream-filter-context.js.map +1 -0
- package/dist/lib/bitstream-filter.d.ts +140 -0
- package/dist/lib/bitstream-filter.js +154 -0
- package/dist/lib/bitstream-filter.js.map +1 -0
- package/dist/lib/codec-context.d.ts +1071 -0
- package/dist/lib/codec-context.js +1354 -0
- package/dist/lib/codec-context.js.map +1 -0
- package/dist/lib/codec-parameters.d.ts +616 -0
- package/dist/lib/codec-parameters.js +761 -0
- package/dist/lib/codec-parameters.js.map +1 -0
- package/dist/lib/codec-parser.d.ts +201 -0
- package/dist/lib/codec-parser.js +213 -0
- package/dist/lib/codec-parser.js.map +1 -0
- package/dist/lib/codec.d.ts +586 -0
- package/dist/lib/codec.js +713 -0
- package/dist/lib/codec.js.map +1 -0
- package/dist/lib/device.d.ts +291 -0
- package/dist/lib/device.js +324 -0
- package/dist/lib/device.js.map +1 -0
- package/dist/lib/dictionary.d.ts +333 -0
- package/dist/lib/dictionary.js +372 -0
- package/dist/lib/dictionary.js.map +1 -0
- package/dist/lib/error.d.ts +242 -0
- package/dist/lib/error.js +303 -0
- package/dist/lib/error.js.map +1 -0
- package/dist/lib/fifo.d.ts +416 -0
- package/dist/lib/fifo.js +453 -0
- package/dist/lib/fifo.js.map +1 -0
- package/dist/lib/filter-context.d.ts +712 -0
- package/dist/lib/filter-context.js +789 -0
- package/dist/lib/filter-context.js.map +1 -0
- package/dist/lib/filter-graph-segment.d.ts +160 -0
- package/dist/lib/filter-graph-segment.js +171 -0
- package/dist/lib/filter-graph-segment.js.map +1 -0
- package/dist/lib/filter-graph.d.ts +641 -0
- package/dist/lib/filter-graph.js +704 -0
- package/dist/lib/filter-graph.js.map +1 -0
- package/dist/lib/filter-inout.d.ts +198 -0
- package/dist/lib/filter-inout.js +257 -0
- package/dist/lib/filter-inout.js.map +1 -0
- package/dist/lib/filter.d.ts +243 -0
- package/dist/lib/filter.js +272 -0
- package/dist/lib/filter.js.map +1 -0
- package/dist/lib/format-context.d.ts +1254 -0
- package/dist/lib/format-context.js +1379 -0
- package/dist/lib/format-context.js.map +1 -0
- package/dist/lib/frame-utils.d.ts +116 -0
- package/dist/lib/frame-utils.js +98 -0
- package/dist/lib/frame-utils.js.map +1 -0
- package/dist/lib/frame.d.ts +1222 -0
- package/dist/lib/frame.js +1435 -0
- package/dist/lib/frame.js.map +1 -0
- package/dist/lib/hardware-device-context.d.ts +362 -0
- package/dist/lib/hardware-device-context.js +383 -0
- package/dist/lib/hardware-device-context.js.map +1 -0
- package/dist/lib/hardware-frames-context.d.ts +419 -0
- package/dist/lib/hardware-frames-context.js +477 -0
- package/dist/lib/hardware-frames-context.js.map +1 -0
- package/dist/lib/index.d.ts +35 -0
- package/dist/lib/index.js +60 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/input-format.d.ts +249 -0
- package/dist/lib/input-format.js +306 -0
- package/dist/lib/input-format.js.map +1 -0
- package/dist/lib/io-context.d.ts +696 -0
- package/dist/lib/io-context.js +769 -0
- package/dist/lib/io-context.js.map +1 -0
- package/dist/lib/log.d.ts +174 -0
- package/dist/lib/log.js +184 -0
- package/dist/lib/log.js.map +1 -0
- package/dist/lib/native-types.d.ts +946 -0
- package/dist/lib/native-types.js +2 -0
- package/dist/lib/native-types.js.map +1 -0
- package/dist/lib/option.d.ts +927 -0
- package/dist/lib/option.js +1583 -0
- package/dist/lib/option.js.map +1 -0
- package/dist/lib/output-format.d.ts +180 -0
- package/dist/lib/output-format.js +213 -0
- package/dist/lib/output-format.js.map +1 -0
- package/dist/lib/packet.d.ts +501 -0
- package/dist/lib/packet.js +590 -0
- package/dist/lib/packet.js.map +1 -0
- package/dist/lib/rational.d.ts +251 -0
- package/dist/lib/rational.js +278 -0
- package/dist/lib/rational.js.map +1 -0
- package/dist/lib/software-resample-context.d.ts +552 -0
- package/dist/lib/software-resample-context.js +592 -0
- package/dist/lib/software-resample-context.js.map +1 -0
- package/dist/lib/software-scale-context.d.ts +344 -0
- package/dist/lib/software-scale-context.js +366 -0
- package/dist/lib/software-scale-context.js.map +1 -0
- package/dist/lib/stream.d.ts +379 -0
- package/dist/lib/stream.js +526 -0
- package/dist/lib/stream.js.map +1 -0
- package/dist/lib/sync-queue.d.ts +179 -0
- package/dist/lib/sync-queue.js +197 -0
- package/dist/lib/sync-queue.js.map +1 -0
- package/dist/lib/types.d.ts +34 -0
- package/dist/lib/types.js +2 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/lib/utilities.d.ts +1127 -0
- package/dist/lib/utilities.js +1225 -0
- package/dist/lib/utilities.js.map +1 -0
- package/dist/utils/electron.d.ts +49 -0
- package/dist/utils/electron.js +63 -0
- package/dist/utils/electron.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/index.js.map +1 -0
- package/install/check.js +121 -0
- package/install/ffmpeg.js +66 -0
- package/jellyfin-ffmpeg.patch +181 -0
- package/package.json +129 -0
|
@@ -0,0 +1,1132 @@
|
|
|
1
|
+
import { CodecContext } from '../lib/codec-context.js';
|
|
2
|
+
import { Codec } from '../lib/codec.js';
|
|
3
|
+
import { Frame } from '../lib/frame.js';
|
|
4
|
+
import { Packet } from '../lib/packet.js';
|
|
5
|
+
import { SchedulerControl } from './utilities/scheduler.js';
|
|
6
|
+
import type { AVCodecFlag, AVCodecID, AVThreadType, EOFSignal, FFEncoderCodec } from '../constants/index.js';
|
|
7
|
+
import type { Decoder } from './decoder.js';
|
|
8
|
+
import type { FilterComplexAPI } from './filter-complex.js';
|
|
9
|
+
import type { FilterAPI } from './filter.js';
|
|
10
|
+
import type { Muxer } from './muxer.js';
|
|
11
|
+
/**
|
|
12
|
+
* Options for encoder creation.
|
|
13
|
+
*/
|
|
14
|
+
export interface EncoderOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Target bitrate.
|
|
17
|
+
*
|
|
18
|
+
* Can be specified as number, bigint, or string with suffix (e.g., '5M', '128k').
|
|
19
|
+
* Used for rate control in video and audio encoding.
|
|
20
|
+
*
|
|
21
|
+
* @default 128k for audio, 1M for video
|
|
22
|
+
*/
|
|
23
|
+
bitrate?: number | bigint | string;
|
|
24
|
+
/**
|
|
25
|
+
* Minimum bitrate for rate control.
|
|
26
|
+
*
|
|
27
|
+
* Can be specified as number, bigint, or string with suffix (e.g., '5M', '128k').
|
|
28
|
+
* Used with variable bitrate encoding to enforce quality floor.
|
|
29
|
+
*/
|
|
30
|
+
minRate?: number | bigint | string;
|
|
31
|
+
/**
|
|
32
|
+
* Maximum bitrate for rate control.
|
|
33
|
+
*
|
|
34
|
+
* Can be specified as number, bigint, or string with suffix (e.g., '5M', '128k').
|
|
35
|
+
* Used with variable bitrate encoding to enforce bitrate ceiling.
|
|
36
|
+
*/
|
|
37
|
+
maxRate?: number | bigint | string;
|
|
38
|
+
/**
|
|
39
|
+
* Rate control buffer size.
|
|
40
|
+
*
|
|
41
|
+
* Can be specified as number, bigint, or string with suffix (e.g., '5M', '128k').
|
|
42
|
+
* Determines the decoder buffer model size for rate control.
|
|
43
|
+
*/
|
|
44
|
+
bufSize?: number | bigint | string;
|
|
45
|
+
/**
|
|
46
|
+
* Group of Pictures (GOP) size.
|
|
47
|
+
*
|
|
48
|
+
* Number of frames between keyframes.
|
|
49
|
+
* Larger GOP improves compression but reduces seekability.
|
|
50
|
+
*/
|
|
51
|
+
gopSize?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Maximum number of consecutive B-frames.
|
|
54
|
+
*
|
|
55
|
+
* B-frames improve compression but increase encoding complexity.
|
|
56
|
+
* Maximum B-frames allowed between I or P frames.
|
|
57
|
+
*/
|
|
58
|
+
maxBFrames?: number;
|
|
59
|
+
/**
|
|
60
|
+
* Optional decoder reference for metadata extraction.
|
|
61
|
+
*
|
|
62
|
+
* Used to extract bits_per_raw_sample and other decoder-specific properties.
|
|
63
|
+
* Helps maintain quality during transcoding.
|
|
64
|
+
*/
|
|
65
|
+
decoder?: Decoder;
|
|
66
|
+
/**
|
|
67
|
+
* Optional filter reference for metadata extraction.
|
|
68
|
+
*
|
|
69
|
+
* Used to extract filter output parameters.
|
|
70
|
+
* Ensures encoder matches filter output characteristics.
|
|
71
|
+
*/
|
|
72
|
+
filter?: FilterAPI | FilterComplexAPI;
|
|
73
|
+
/**
|
|
74
|
+
* Number of threads to use for encoding.
|
|
75
|
+
*
|
|
76
|
+
* Set to 0 to auto-detect based on CPU cores.
|
|
77
|
+
*
|
|
78
|
+
* @default 1
|
|
79
|
+
*/
|
|
80
|
+
threadCount?: number;
|
|
81
|
+
/**
|
|
82
|
+
* Type of threading to use for encoding.
|
|
83
|
+
*
|
|
84
|
+
* - `FF_THREAD_FRAME` (1): Encode multiple frames in parallel.
|
|
85
|
+
* Higher throughput but adds latency (1 frame delay per thread).
|
|
86
|
+
*
|
|
87
|
+
* - `FF_THREAD_SLICE` (2): Encode parts of a single frame in parallel.
|
|
88
|
+
* Lower latency, suitable for real-time encoding.
|
|
89
|
+
*
|
|
90
|
+
* @default FFmpeg default (both methods, codec chooses best)
|
|
91
|
+
*/
|
|
92
|
+
threadType?: AVThreadType;
|
|
93
|
+
/**
|
|
94
|
+
* Additional codec-specific options.
|
|
95
|
+
*
|
|
96
|
+
* Key-value pairs of FFmpeg AVCodecContext options.
|
|
97
|
+
* These are passed directly to the encoder.
|
|
98
|
+
*/
|
|
99
|
+
options?: Record<string, string | number | boolean | undefined | null>;
|
|
100
|
+
/**
|
|
101
|
+
* AbortSignal for cancellation.
|
|
102
|
+
*
|
|
103
|
+
* When aborted, async generators stop yielding and async methods throw AbortError.
|
|
104
|
+
*/
|
|
105
|
+
signal?: AbortSignal;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* High-level encoder for audio and video streams.
|
|
109
|
+
*
|
|
110
|
+
* Provides a simplified interface for encoding media frames to packets.
|
|
111
|
+
* Handles codec initialization, hardware acceleration setup, and packet management.
|
|
112
|
+
* Supports both synchronous frame-by-frame encoding and async iteration over packets.
|
|
113
|
+
* Essential component in media processing pipelines for converting raw frames to compressed data.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* import { Encoder } from 'node-av/api';
|
|
118
|
+
* import { AV_CODEC_ID_H264, FF_ENCODER_LIBX264 } from 'node-av/constants';
|
|
119
|
+
*
|
|
120
|
+
* // Create H.264 encoder
|
|
121
|
+
* const encoder = await Encoder.create(FF_ENCODER_LIBX264, {
|
|
122
|
+
* type: 'video',
|
|
123
|
+
* width: 1920,
|
|
124
|
+
* height: 1080,
|
|
125
|
+
* pixelFormat: AV_PIX_FMT_YUV420P,
|
|
126
|
+
* timeBase: { num: 1, den: 30 },
|
|
127
|
+
* frameRate: { num: 30, den: 1 }
|
|
128
|
+
* }, {
|
|
129
|
+
* bitrate: '5M',
|
|
130
|
+
* gopSize: 60
|
|
131
|
+
* });
|
|
132
|
+
*
|
|
133
|
+
* // Encode frames
|
|
134
|
+
* const packet = await encoder.encode(frame);
|
|
135
|
+
* if (packet) {
|
|
136
|
+
* await output.writePacket(packet);
|
|
137
|
+
* packet.free();
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* // Hardware-accelerated encoding with lazy initialization
|
|
144
|
+
* import { HardwareContext } from 'node-av/api';
|
|
145
|
+
* import { FF_ENCODER_H264_VIDEOTOOLBOX } from 'node-av/constants';
|
|
146
|
+
*
|
|
147
|
+
* const hw = HardwareContext.auto();
|
|
148
|
+
* const encoderCodec = hw?.getEncoderCodec('h264') ?? FF_ENCODER_H264_VIDEOTOOLBOX;
|
|
149
|
+
* const encoder = await Encoder.create(encoderCodec, {
|
|
150
|
+
* timeBase: video.timeBase,
|
|
151
|
+
* bitrate: '10M'
|
|
152
|
+
* });
|
|
153
|
+
*
|
|
154
|
+
* // Hardware context will be detected from first frame's hw_frames_ctx
|
|
155
|
+
* for await (const packet of encoder.packets(frames)) {
|
|
156
|
+
* await output.writePacket(packet);
|
|
157
|
+
* packet.free();
|
|
158
|
+
* }
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @see {@link Decoder} For decoding packets to frames
|
|
162
|
+
* @see {@link Muxer} For writing encoded packets
|
|
163
|
+
* @see {@link HardwareContext} For GPU acceleration
|
|
164
|
+
*/
|
|
165
|
+
export declare class Encoder implements Disposable {
|
|
166
|
+
private codecContext;
|
|
167
|
+
private packet;
|
|
168
|
+
private codec;
|
|
169
|
+
private initializePromise;
|
|
170
|
+
private initialized;
|
|
171
|
+
private isClosed;
|
|
172
|
+
private opts?;
|
|
173
|
+
private options;
|
|
174
|
+
private audioFrameBuffer?;
|
|
175
|
+
private inputQueue;
|
|
176
|
+
private outputQueue;
|
|
177
|
+
private workerPromise;
|
|
178
|
+
private pipeToPromise;
|
|
179
|
+
private signal?;
|
|
180
|
+
/**
|
|
181
|
+
* @param codecContext - Configured codec context
|
|
182
|
+
*
|
|
183
|
+
* @param codec - Encoder codec
|
|
184
|
+
*
|
|
185
|
+
* @param options - Encoder options
|
|
186
|
+
*
|
|
187
|
+
* @param opts - Encoder options as Dictionary
|
|
188
|
+
*
|
|
189
|
+
* @internal
|
|
190
|
+
*/
|
|
191
|
+
private constructor();
|
|
192
|
+
/**
|
|
193
|
+
* Create an encoder with specified codec and options.
|
|
194
|
+
*
|
|
195
|
+
* Initializes an encoder with the appropriate codec and configuration.
|
|
196
|
+
* Uses lazy initialization - encoder is opened when first frame is received.
|
|
197
|
+
* Hardware context will be automatically detected from first frame if not provided.
|
|
198
|
+
*
|
|
199
|
+
* Direct mapping to avcodec_find_encoder_by_name() or avcodec_find_encoder().
|
|
200
|
+
*
|
|
201
|
+
* @param encoderCodec - Codec name, ID, or instance to use for encoding
|
|
202
|
+
*
|
|
203
|
+
* @param options - Optional encoder configuration options including required timeBase
|
|
204
|
+
*
|
|
205
|
+
* @returns Configured encoder instance
|
|
206
|
+
*
|
|
207
|
+
* @throws {Error} If encoder not found
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```typescript
|
|
211
|
+
* // From decoder stream info
|
|
212
|
+
* const encoder = await Encoder.create(FF_ENCODER_LIBX264, {
|
|
213
|
+
* timeBase: video.timeBase,
|
|
214
|
+
* bitrate: '5M',
|
|
215
|
+
* gopSize: 60,
|
|
216
|
+
* options: {
|
|
217
|
+
* preset: 'fast',
|
|
218
|
+
* crf: '23'
|
|
219
|
+
* }
|
|
220
|
+
* });
|
|
221
|
+
* ```
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```typescript
|
|
225
|
+
* // With custom stream info
|
|
226
|
+
* const encoder = await Encoder.create(FF_ENCODER_AAC, {
|
|
227
|
+
* timeBase: audio.timeBase,
|
|
228
|
+
* bitrate: '192k'
|
|
229
|
+
* });
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```typescript
|
|
234
|
+
* // Hardware encoder
|
|
235
|
+
* const hw = HardwareContext.auto();
|
|
236
|
+
* const encoderCodec = hw?.getEncoderCodec('h264') ?? FF_ENCODER_H264_VIDEOTOOLBOX;
|
|
237
|
+
* const encoder = await Encoder.create(encoderCodec, {
|
|
238
|
+
* timeBase: video.timeBase,
|
|
239
|
+
* bitrate: '8M'
|
|
240
|
+
* });
|
|
241
|
+
* ```
|
|
242
|
+
*
|
|
243
|
+
* @see {@link EncoderOptions} For configuration options
|
|
244
|
+
* @see {@link createSync} For synchronous version
|
|
245
|
+
*/
|
|
246
|
+
static create(encoderCodec: FFEncoderCodec | AVCodecID | Codec, options?: EncoderOptions): Promise<Encoder>;
|
|
247
|
+
/**
|
|
248
|
+
* Create an encoder with specified codec and options synchronously.
|
|
249
|
+
* Synchronous version of create.
|
|
250
|
+
*
|
|
251
|
+
* Initializes an encoder with the appropriate codec and configuration.
|
|
252
|
+
* Uses lazy initialization - encoder is opened when first frame is received.
|
|
253
|
+
* Hardware context will be automatically detected from first frame if not provided.
|
|
254
|
+
*
|
|
255
|
+
* Direct mapping to avcodec_find_encoder_by_name() or avcodec_find_encoder().
|
|
256
|
+
*
|
|
257
|
+
* @param encoderCodec - Codec name, ID, or instance to use for encoding
|
|
258
|
+
*
|
|
259
|
+
* @param options - Optional encoder configuration options including required timeBase
|
|
260
|
+
*
|
|
261
|
+
* @returns Configured encoder instance
|
|
262
|
+
*
|
|
263
|
+
* @throws {Error} If encoder not found or timeBase not provided
|
|
264
|
+
*
|
|
265
|
+
* @throws {FFmpegError} If codec allocation fails
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* // From decoder stream info
|
|
270
|
+
* const encoder = await Encoder.create(FF_ENCODER_LIBX264, {
|
|
271
|
+
* timeBase: video.timeBase,
|
|
272
|
+
* bitrate: '5M',
|
|
273
|
+
* gopSize: 60,
|
|
274
|
+
* options: {
|
|
275
|
+
* preset: 'fast',
|
|
276
|
+
* crf: '23'
|
|
277
|
+
* }
|
|
278
|
+
* });
|
|
279
|
+
* ```
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* // With custom stream info
|
|
284
|
+
* const encoder = await Encoder.create(FF_ENCODER_AAC, {
|
|
285
|
+
* timeBase: audio.timeBase,
|
|
286
|
+
* bitrate: '192k'
|
|
287
|
+
* });
|
|
288
|
+
* ```
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* ```typescript
|
|
292
|
+
* // Hardware encoder
|
|
293
|
+
* const hw = HardwareContext.auto();
|
|
294
|
+
* const encoderCodec = hw?.getEncoderCodec('h264') ?? FF_ENCODER_H264_VIDEOTOOLBOX;
|
|
295
|
+
* const encoder = await Encoder.create(encoderCodec, {
|
|
296
|
+
* timeBase: video.timeBase,
|
|
297
|
+
* bitrate: '8M'
|
|
298
|
+
* });
|
|
299
|
+
* ```
|
|
300
|
+
*
|
|
301
|
+
* @see {@link EncoderOptions} For configuration options
|
|
302
|
+
* @see {@link create} For async version
|
|
303
|
+
*/
|
|
304
|
+
static createSync(encoderCodec: FFEncoderCodec | AVCodecID | Codec, options?: EncoderOptions): Encoder;
|
|
305
|
+
/**
|
|
306
|
+
* Check if encoder is open.
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* if (encoder.isEncoderOpen) {
|
|
311
|
+
* const packet = await encoder.encode(frame);
|
|
312
|
+
* }
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
get isEncoderOpen(): boolean;
|
|
316
|
+
/**
|
|
317
|
+
* Check if encoder has been initialized.
|
|
318
|
+
*
|
|
319
|
+
* Returns true after first frame has been processed and encoder opened.
|
|
320
|
+
* Useful for checking if encoder has received frame properties.
|
|
321
|
+
*
|
|
322
|
+
* @returns true if encoder has been initialized with frame data
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```typescript
|
|
326
|
+
* if (!encoder.isEncoderInitialized) {
|
|
327
|
+
* console.log('Encoder will initialize on first frame');
|
|
328
|
+
* }
|
|
329
|
+
* ```
|
|
330
|
+
*/
|
|
331
|
+
get isEncoderInitialized(): boolean;
|
|
332
|
+
/**
|
|
333
|
+
* Codec flags.
|
|
334
|
+
*
|
|
335
|
+
* @returns Current codec flags
|
|
336
|
+
*
|
|
337
|
+
* @throws {Error} If encoder is closed
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```typescript
|
|
341
|
+
* const flags = encoder.codecFlags;
|
|
342
|
+
* console.log('Current flags:', flags);
|
|
343
|
+
* ```
|
|
344
|
+
*
|
|
345
|
+
* @see {@link setCodecFlags} To set flags
|
|
346
|
+
* @see {@link clearCodecFlags} To clear flags
|
|
347
|
+
* @see {@link hasCodecFlags} To check flags
|
|
348
|
+
*/
|
|
349
|
+
get codecFlags(): AVCodecFlag;
|
|
350
|
+
/**
|
|
351
|
+
* Set codec flags.
|
|
352
|
+
*
|
|
353
|
+
* @param flags - One or more flag values to set
|
|
354
|
+
*
|
|
355
|
+
* @throws {Error} If encoder is already initialized or closed
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* ```typescript
|
|
359
|
+
* import { AV_CODEC_FLAG_GLOBAL_HEADER, AV_CODEC_FLAG_QSCALE } from 'node-av/constants';
|
|
360
|
+
*
|
|
361
|
+
* // Set multiple flags before initialization
|
|
362
|
+
* encoder.setCodecFlags(AV_CODEC_FLAG_GLOBAL_HEADER, AV_CODEC_FLAG_QSCALE);
|
|
363
|
+
* ```
|
|
364
|
+
*
|
|
365
|
+
* @see {@link clearCodecFlags} To clear flags
|
|
366
|
+
* @see {@link hasCodecFlags} To check flags
|
|
367
|
+
* @see {@link codecFlags} For direct flag access
|
|
368
|
+
*/
|
|
369
|
+
setCodecFlags(...flags: AVCodecFlag[]): void;
|
|
370
|
+
/**
|
|
371
|
+
* Clear codec flags.
|
|
372
|
+
*
|
|
373
|
+
* @param flags - One or more flag values to clear
|
|
374
|
+
*
|
|
375
|
+
* @throws {Error} If encoder is already initialized or closed
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* ```typescript
|
|
379
|
+
* import { AV_CODEC_FLAG_QSCALE } from 'node-av/constants';
|
|
380
|
+
*
|
|
381
|
+
* // Clear specific flag before initialization
|
|
382
|
+
* encoder.clearCodecFlags(AV_CODEC_FLAG_QSCALE);
|
|
383
|
+
* ```
|
|
384
|
+
*
|
|
385
|
+
* @see {@link setCodecFlags} To set flags
|
|
386
|
+
* @see {@link hasCodecFlags} To check flags
|
|
387
|
+
* @see {@link codecFlags} For direct flag access
|
|
388
|
+
*/
|
|
389
|
+
clearCodecFlags(...flags: AVCodecFlag[]): void;
|
|
390
|
+
/**
|
|
391
|
+
* Check if codec has specific flags.
|
|
392
|
+
*
|
|
393
|
+
* Tests whether all specified codec flags are set using bitwise AND.
|
|
394
|
+
*
|
|
395
|
+
* @param flags - One or more flag values to check
|
|
396
|
+
*
|
|
397
|
+
* @returns true if all specified flags are set, false otherwise
|
|
398
|
+
*
|
|
399
|
+
* @throws {Error} If encoder is closed
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* ```typescript
|
|
403
|
+
* import { AV_CODEC_FLAG_GLOBAL_HEADER } from 'node-av/constants';
|
|
404
|
+
*
|
|
405
|
+
* if (encoder.hasCodecFlags(AV_CODEC_FLAG_GLOBAL_HEADER)) {
|
|
406
|
+
* console.log('Global header flag is set');
|
|
407
|
+
* }
|
|
408
|
+
* ```
|
|
409
|
+
*
|
|
410
|
+
* @see {@link setCodecFlags} To set flags
|
|
411
|
+
* @see {@link clearCodecFlags} To clear flags
|
|
412
|
+
* @see {@link codecFlags} For direct flag access
|
|
413
|
+
*/
|
|
414
|
+
hasCodecFlags(...flags: AVCodecFlag[]): boolean;
|
|
415
|
+
/**
|
|
416
|
+
* Check if encoder uses hardware acceleration.
|
|
417
|
+
*
|
|
418
|
+
* @returns true if hardware-accelerated
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* ```typescript
|
|
422
|
+
* if (encoder.isHardware()) {
|
|
423
|
+
* console.log('Using GPU acceleration');
|
|
424
|
+
* }
|
|
425
|
+
* ```
|
|
426
|
+
*
|
|
427
|
+
* @see {@link HardwareContext} For hardware setup
|
|
428
|
+
*/
|
|
429
|
+
isHardware(): boolean;
|
|
430
|
+
/**
|
|
431
|
+
* Check if encoder is ready for processing.
|
|
432
|
+
*
|
|
433
|
+
* @returns true if initialized and ready
|
|
434
|
+
*
|
|
435
|
+
* @example
|
|
436
|
+
* ```typescript
|
|
437
|
+
* if (encoder.isReady()) {
|
|
438
|
+
* const packet = await encoder.encode(frame);
|
|
439
|
+
* }
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
isReady(): boolean;
|
|
443
|
+
/**
|
|
444
|
+
* Send a frame to the encoder.
|
|
445
|
+
*
|
|
446
|
+
* Sends a raw frame to the encoder for encoding.
|
|
447
|
+
* Does not return encoded packets - use {@link receive} to retrieve packets.
|
|
448
|
+
* On first frame, automatically initializes encoder with frame properties.
|
|
449
|
+
* A single frame can produce zero, one, or multiple packets depending on codec buffering.
|
|
450
|
+
*
|
|
451
|
+
* **Important**: This method only SENDS the frame to the encoder.
|
|
452
|
+
* You must call {@link receive} separately (potentially multiple times) to get encoded packets.
|
|
453
|
+
*
|
|
454
|
+
* Direct mapping to avcodec_send_frame().
|
|
455
|
+
*
|
|
456
|
+
* @param frame - Raw frame to send to encoder, or null to flush
|
|
457
|
+
*
|
|
458
|
+
* @throws {FFmpegError} If sending frame fails
|
|
459
|
+
*
|
|
460
|
+
* @example
|
|
461
|
+
* ```typescript
|
|
462
|
+
* // Send frame and receive packets
|
|
463
|
+
* await encoder.encode(frame);
|
|
464
|
+
*
|
|
465
|
+
* // Receive all available packets
|
|
466
|
+
* while (true) {
|
|
467
|
+
* const packet = await encoder.receive();
|
|
468
|
+
* if (!packet) break;
|
|
469
|
+
* console.log(`Encoded packet with PTS: ${packet.pts}`);
|
|
470
|
+
* await output.writePacket(packet);
|
|
471
|
+
* packet.free();
|
|
472
|
+
* }
|
|
473
|
+
* ```
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* ```typescript
|
|
477
|
+
* for await (const frame of decoder.frames(input.packets())) {
|
|
478
|
+
* // Send frame
|
|
479
|
+
* await encoder.encode(frame);
|
|
480
|
+
*
|
|
481
|
+
* // Receive available packets
|
|
482
|
+
* let packet;
|
|
483
|
+
* while ((packet = await encoder.receive())) {
|
|
484
|
+
* await output.writePacket(packet);
|
|
485
|
+
* packet.free();
|
|
486
|
+
* }
|
|
487
|
+
* frame.free();
|
|
488
|
+
* }
|
|
489
|
+
* ```
|
|
490
|
+
*
|
|
491
|
+
* @see {@link receive} For receiving encoded packets
|
|
492
|
+
* @see {@link encodeAll} For combined send+receive operation
|
|
493
|
+
* @see {@link packets} For automatic frame iteration
|
|
494
|
+
* @see {@link flush} For end-of-stream handling
|
|
495
|
+
* @see {@link encodeSync} For synchronous version
|
|
496
|
+
*/
|
|
497
|
+
encode(frame: Frame | null): Promise<void>;
|
|
498
|
+
/**
|
|
499
|
+
* Send a frame to the encoder synchronously.
|
|
500
|
+
* Synchronous version of encode.
|
|
501
|
+
*
|
|
502
|
+
* Sends a raw frame to the encoder for encoding.
|
|
503
|
+
* Does not return encoded packets - use {@link receiveSync} to retrieve packets.
|
|
504
|
+
* On first frame, automatically initializes encoder with frame properties.
|
|
505
|
+
* A single frame can produce zero, one, or multiple packets depending on codec buffering.
|
|
506
|
+
*
|
|
507
|
+
* **Important**: This method only SENDS the frame to the encoder.
|
|
508
|
+
* You must call {@link receiveSync} separately (potentially multiple times) to get encoded packets.
|
|
509
|
+
*
|
|
510
|
+
* Direct mapping to avcodec_send_frame().
|
|
511
|
+
*
|
|
512
|
+
* @param frame - Raw frame to send to encoder, or null to flush
|
|
513
|
+
*
|
|
514
|
+
* @throws {FFmpegError} If sending frame fails
|
|
515
|
+
*
|
|
516
|
+
* @example
|
|
517
|
+
* ```typescript
|
|
518
|
+
* // Send frame and receive packets
|
|
519
|
+
* encoder.encodeSync(frame);
|
|
520
|
+
*
|
|
521
|
+
* // Receive all available packets
|
|
522
|
+
* let packet;
|
|
523
|
+
* while ((packet = encoder.receiveSync())) {
|
|
524
|
+
* console.log(`Encoded packet with PTS: ${packet.pts}`);
|
|
525
|
+
* output.writePacketSync(packet);
|
|
526
|
+
* packet.free();
|
|
527
|
+
* }
|
|
528
|
+
* ```
|
|
529
|
+
*
|
|
530
|
+
* @see {@link receiveSync} For receiving encoded packets
|
|
531
|
+
* @see {@link encodeAllSync} For combined send+receive operation
|
|
532
|
+
* @see {@link packetsSync} For automatic frame iteration
|
|
533
|
+
* @see {@link flushSync} For end-of-stream handling
|
|
534
|
+
* @see {@link encode} For async version
|
|
535
|
+
*/
|
|
536
|
+
encodeSync(frame: Frame | null): void;
|
|
537
|
+
/**
|
|
538
|
+
* Encode a frame to packets.
|
|
539
|
+
*
|
|
540
|
+
* Sends a frame to the encoder and receives all available encoded packets.
|
|
541
|
+
* Returns array of packets - may be empty if encoder needs more data.
|
|
542
|
+
* On first frame, automatically initializes encoder with frame properties.
|
|
543
|
+
* One frame can produce zero, one, or multiple packets depending on codec.
|
|
544
|
+
*
|
|
545
|
+
* Direct mapping to avcodec_send_frame() and avcodec_receive_packet().
|
|
546
|
+
*
|
|
547
|
+
* @param frame - Raw frame to encode (or null to flush)
|
|
548
|
+
*
|
|
549
|
+
* @returns Array of encoded packets (empty if more data needed or encoder is closed)
|
|
550
|
+
*
|
|
551
|
+
* @throws {FFmpegError} If encoding fails
|
|
552
|
+
*
|
|
553
|
+
* @example
|
|
554
|
+
* ```typescript
|
|
555
|
+
* const packets = await encoder.encodeAll(frame);
|
|
556
|
+
* for (const packet of packets) {
|
|
557
|
+
* console.log(`Encoded packet with PTS: ${packet.pts}`);
|
|
558
|
+
* await output.writePacket(packet);
|
|
559
|
+
* packet.free();
|
|
560
|
+
* }
|
|
561
|
+
* ```
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* ```typescript
|
|
565
|
+
* // Encode loop
|
|
566
|
+
* for await (const frame of decoder.frames(input.packets())) {
|
|
567
|
+
* const packets = await encoder.encodeAll(frame);
|
|
568
|
+
* for (const packet of packets) {
|
|
569
|
+
* await output.writePacket(packet);
|
|
570
|
+
* packet.free();
|
|
571
|
+
* }
|
|
572
|
+
* frame.free();
|
|
573
|
+
* }
|
|
574
|
+
* ```
|
|
575
|
+
*
|
|
576
|
+
* @see {@link encode} For single packet encoding
|
|
577
|
+
* @see {@link packets} For automatic frame iteration
|
|
578
|
+
* @see {@link flush} For end-of-stream handling
|
|
579
|
+
* @see {@link encodeAllSync} For synchronous version
|
|
580
|
+
*/
|
|
581
|
+
encodeAll(frame: Frame | null): Promise<Packet[]>;
|
|
582
|
+
/**
|
|
583
|
+
* Encode a frame to packets synchronously.
|
|
584
|
+
* Synchronous version of encodeAll.
|
|
585
|
+
*
|
|
586
|
+
* Sends a frame to the encoder and receives all available encoded packets.
|
|
587
|
+
* Returns array of packets - may be empty if encoder needs more data.
|
|
588
|
+
* On first frame, automatically initializes encoder with frame properties.
|
|
589
|
+
* One frame can produce zero, one, or multiple packets depending on codec.
|
|
590
|
+
*
|
|
591
|
+
* Direct mapping to avcodec_send_frame() and avcodec_receive_packet().
|
|
592
|
+
*
|
|
593
|
+
* @param frame - Raw frame to encode (or null to flush)
|
|
594
|
+
*
|
|
595
|
+
* @returns Array of encoded packets (empty if more data needed or encoder is closed)
|
|
596
|
+
*
|
|
597
|
+
* @throws {FFmpegError} If encoding fails
|
|
598
|
+
*
|
|
599
|
+
* @example
|
|
600
|
+
* ```typescript
|
|
601
|
+
* const packets = encoder.encodeAllSync(frame);
|
|
602
|
+
* for (const packet of packets) {
|
|
603
|
+
* console.log(`Encoded packet with PTS: ${packet.pts}`);
|
|
604
|
+
* output.writePacketSync(packet);
|
|
605
|
+
* packet.free();
|
|
606
|
+
* }
|
|
607
|
+
* ```
|
|
608
|
+
*
|
|
609
|
+
* @example
|
|
610
|
+
* ```typescript
|
|
611
|
+
* // Encode loop
|
|
612
|
+
* for (const frame of decoder.framesSync(packets)) {
|
|
613
|
+
* const packets = encoder.encodeAllSync(frame);
|
|
614
|
+
* for (const packet of packets) {
|
|
615
|
+
* output.writePacketSync(packet);
|
|
616
|
+
* packet.free();
|
|
617
|
+
* }
|
|
618
|
+
* frame.free();
|
|
619
|
+
* }
|
|
620
|
+
* ```
|
|
621
|
+
*
|
|
622
|
+
* @see {@link encodeSync} For single packet encoding
|
|
623
|
+
* @see {@link packetsSync} For automatic frame iteration
|
|
624
|
+
* @see {@link flushSync} For end-of-stream handling
|
|
625
|
+
* @see {@link encodeAll} For async version
|
|
626
|
+
*/
|
|
627
|
+
encodeAllSync(frame: Frame | null): Packet[];
|
|
628
|
+
/**
|
|
629
|
+
* Encode frame stream to packet stream.
|
|
630
|
+
*
|
|
631
|
+
* High-level async generator for complete encoding pipeline.
|
|
632
|
+
* Encoder is only flushed when EOF (null) signal is explicitly received.
|
|
633
|
+
* Primary interface for stream-based encoding.
|
|
634
|
+
*
|
|
635
|
+
* **EOF Handling:**
|
|
636
|
+
* - Send null to flush encoder and get remaining buffered packets
|
|
637
|
+
* - Generator yields null after flushing when null is received
|
|
638
|
+
* - No automatic flushing - encoder stays open until EOF or close()
|
|
639
|
+
*
|
|
640
|
+
* @param frames - Async iterable of frames, single frame, or null to flush
|
|
641
|
+
*
|
|
642
|
+
* @yields {Packet | null} Encoded packets, followed by null when explicitly flushed
|
|
643
|
+
*
|
|
644
|
+
* @throws {FFmpegError} If encoding fails
|
|
645
|
+
*
|
|
646
|
+
* @example
|
|
647
|
+
* ```typescript
|
|
648
|
+
* // Stream of frames with automatic EOF propagation
|
|
649
|
+
* for await (const packet of encoder.packets(decoder.frames(input.packets()))) {
|
|
650
|
+
* if (packet === null) {
|
|
651
|
+
* console.log('Encoder flushed');
|
|
652
|
+
* break;
|
|
653
|
+
* }
|
|
654
|
+
* await output.writePacket(packet);
|
|
655
|
+
* packet.free(); // Must free output packets
|
|
656
|
+
* }
|
|
657
|
+
* ```
|
|
658
|
+
*
|
|
659
|
+
* @example
|
|
660
|
+
* ```typescript
|
|
661
|
+
* // Single frame - no automatic flush
|
|
662
|
+
* for await (const packet of encoder.packets(singleFrame)) {
|
|
663
|
+
* await output.writePacket(packet);
|
|
664
|
+
* packet.free();
|
|
665
|
+
* }
|
|
666
|
+
* // Encoder remains open, buffered packets not flushed
|
|
667
|
+
* ```
|
|
668
|
+
*
|
|
669
|
+
* @example
|
|
670
|
+
* ```typescript
|
|
671
|
+
* // Explicit flush with EOF
|
|
672
|
+
* for await (const packet of encoder.packets(null)) {
|
|
673
|
+
* if (packet === null) {
|
|
674
|
+
* console.log('All buffered packets flushed');
|
|
675
|
+
* break;
|
|
676
|
+
* }
|
|
677
|
+
* console.log('Buffered packet:', packet.pts);
|
|
678
|
+
* await output.writePacket(packet);
|
|
679
|
+
* packet.free();
|
|
680
|
+
* }
|
|
681
|
+
* ```
|
|
682
|
+
*
|
|
683
|
+
* @see {@link encode} For single frame encoding
|
|
684
|
+
* @see {@link Decoder.frames} For frame source
|
|
685
|
+
* @see {@link packetsSync} For sync version
|
|
686
|
+
*/
|
|
687
|
+
packets(frames: AsyncIterable<Frame | null> | Frame | null): AsyncGenerator<Packet | null>;
|
|
688
|
+
/**
|
|
689
|
+
* Encode frame stream to packet stream synchronously.
|
|
690
|
+
* Synchronous version of packets.
|
|
691
|
+
*
|
|
692
|
+
* High-level sync generator for complete encoding pipeline.
|
|
693
|
+
* Encoder is only flushed when EOF (null) signal is explicitly received.
|
|
694
|
+
* Primary interface for stream-based encoding.
|
|
695
|
+
*
|
|
696
|
+
* **EOF Handling:**
|
|
697
|
+
* - Send null to flush encoder and get remaining buffered packets
|
|
698
|
+
* - Generator yields null after flushing when null is received
|
|
699
|
+
* - No automatic flushing - encoder stays open until EOF or close()
|
|
700
|
+
*
|
|
701
|
+
* @param frames - Iterable of frames, single frame, or null to flush
|
|
702
|
+
*
|
|
703
|
+
* @yields {Packet | null} Encoded packets, followed by null when explicitly flushed
|
|
704
|
+
*
|
|
705
|
+
* @throws {FFmpegError} If encoding fails
|
|
706
|
+
*
|
|
707
|
+
* @example
|
|
708
|
+
* ```typescript
|
|
709
|
+
* // Stream of frames with automatic EOF propagation
|
|
710
|
+
* for (const packet of encoder.packetsSync(decoder.framesSync(packets))) {
|
|
711
|
+
* if (packet === null) {
|
|
712
|
+
* console.log('Encoder flushed');
|
|
713
|
+
* break;
|
|
714
|
+
* }
|
|
715
|
+
* output.writePacketSync(packet);
|
|
716
|
+
* packet.free(); // Must free output packets
|
|
717
|
+
* }
|
|
718
|
+
* ```
|
|
719
|
+
*
|
|
720
|
+
* @example
|
|
721
|
+
* ```typescript
|
|
722
|
+
* // Single frame - no automatic flush
|
|
723
|
+
* for (const packet of encoder.packetsSync(singleFrame)) {
|
|
724
|
+
* output.writePacketSync(packet);
|
|
725
|
+
* packet.free();
|
|
726
|
+
* }
|
|
727
|
+
* // Encoder remains open, buffered packets not flushed
|
|
728
|
+
* ```
|
|
729
|
+
*
|
|
730
|
+
* @example
|
|
731
|
+
* ```typescript
|
|
732
|
+
* // Explicit flush with EOF
|
|
733
|
+
* for (const packet of encoder.packetsSync(null)) {
|
|
734
|
+
* if (packet === null) {
|
|
735
|
+
* console.log('All buffered packets flushed');
|
|
736
|
+
* break;
|
|
737
|
+
* }
|
|
738
|
+
* console.log('Buffered packet:', packet.pts);
|
|
739
|
+
* output.writePacketSync(packet);
|
|
740
|
+
* packet.free();
|
|
741
|
+
* }
|
|
742
|
+
* ```
|
|
743
|
+
*
|
|
744
|
+
* @see {@link encodeSync} For single frame encoding
|
|
745
|
+
* @see {@link Decoder.framesSync} For frame source
|
|
746
|
+
* @see {@link packets} For async version
|
|
747
|
+
*/
|
|
748
|
+
packetsSync(frames: Iterable<Frame | null> | Frame | null): Generator<Packet | null>;
|
|
749
|
+
/**
|
|
750
|
+
* Flush encoder and signal end-of-stream.
|
|
751
|
+
*
|
|
752
|
+
* Sends null frame to encoder to signal end-of-stream.
|
|
753
|
+
* Does nothing if encoder was never initialized or is closed.
|
|
754
|
+
* Must call receive() to get remaining buffered packets.
|
|
755
|
+
*
|
|
756
|
+
* Direct mapping to avcodec_send_frame(NULL).
|
|
757
|
+
*
|
|
758
|
+
* @example
|
|
759
|
+
* ```typescript
|
|
760
|
+
* // Signal end of stream
|
|
761
|
+
* await encoder.flush();
|
|
762
|
+
*
|
|
763
|
+
* // Then get remaining packets
|
|
764
|
+
* let packet;
|
|
765
|
+
* while ((packet = await encoder.receive()) !== null) {
|
|
766
|
+
* console.log('Got buffered packet');
|
|
767
|
+
* await output.writePacket(packet);
|
|
768
|
+
* packet.free();
|
|
769
|
+
* }
|
|
770
|
+
* ```
|
|
771
|
+
*
|
|
772
|
+
* @see {@link flushPackets} For async iteration
|
|
773
|
+
* @see {@link receive} For getting buffered packets
|
|
774
|
+
* @see {@link flushSync} For synchronous version
|
|
775
|
+
*/
|
|
776
|
+
flush(): Promise<void>;
|
|
777
|
+
/**
|
|
778
|
+
* Flush encoder and signal end-of-stream synchronously.
|
|
779
|
+
* Synchronous version of flush.
|
|
780
|
+
*
|
|
781
|
+
* Sends null frame to encoder to signal end-of-stream.
|
|
782
|
+
* Does nothing if encoder was never initialized or is closed.
|
|
783
|
+
* Must call receiveSync() to get remaining buffered packets.
|
|
784
|
+
*
|
|
785
|
+
* Direct mapping to avcodec_send_frame(NULL).
|
|
786
|
+
*
|
|
787
|
+
* @example
|
|
788
|
+
* ```typescript
|
|
789
|
+
* // Signal end of stream
|
|
790
|
+
* encoder.flushSync();
|
|
791
|
+
*
|
|
792
|
+
* // Then get remaining packets
|
|
793
|
+
* let packet;
|
|
794
|
+
* while ((packet = encoder.receiveSync()) !== null) {
|
|
795
|
+
* console.log('Got buffered packet');
|
|
796
|
+
* output.writePacketSync(packet);
|
|
797
|
+
* packet.free();
|
|
798
|
+
* }
|
|
799
|
+
* ```
|
|
800
|
+
*
|
|
801
|
+
* @see {@link flushPacketsSync} For sync iteration
|
|
802
|
+
* @see {@link receiveSync} For getting buffered packets
|
|
803
|
+
* @see {@link flush} For async version
|
|
804
|
+
*/
|
|
805
|
+
flushSync(): void;
|
|
806
|
+
/**
|
|
807
|
+
* Flush all buffered packets as async generator.
|
|
808
|
+
*
|
|
809
|
+
* Convenient async iteration over remaining packets.
|
|
810
|
+
* Automatically handles flush and repeated receive calls.
|
|
811
|
+
* Returns immediately if encoder was never initialized or is closed.
|
|
812
|
+
*
|
|
813
|
+
* @yields {Packet} Buffered packets
|
|
814
|
+
*
|
|
815
|
+
* @example
|
|
816
|
+
* ```typescript
|
|
817
|
+
* // Flush at end of encoding
|
|
818
|
+
* for await (const packet of encoder.flushPackets()) {
|
|
819
|
+
* console.log('Processing buffered packet');
|
|
820
|
+
* await output.writePacket(packet);
|
|
821
|
+
* packet.free();
|
|
822
|
+
* }
|
|
823
|
+
* ```
|
|
824
|
+
*
|
|
825
|
+
* @see {@link encode} For sending frames and receiving packets
|
|
826
|
+
* @see {@link flush} For signaling end-of-stream
|
|
827
|
+
* @see {@link flushPacketsSync} For synchronous version
|
|
828
|
+
*/
|
|
829
|
+
flushPackets(): AsyncGenerator<Packet>;
|
|
830
|
+
/**
|
|
831
|
+
* Flush all buffered packets as generator synchronously.
|
|
832
|
+
* Synchronous version of flushPackets.
|
|
833
|
+
*
|
|
834
|
+
* Convenient sync iteration over remaining packets.
|
|
835
|
+
* Automatically handles flush and repeated receive calls.
|
|
836
|
+
* Returns immediately if encoder was never initialized or is closed.
|
|
837
|
+
*
|
|
838
|
+
* @yields {Packet} Buffered packets
|
|
839
|
+
*
|
|
840
|
+
* @example
|
|
841
|
+
* ```typescript
|
|
842
|
+
* // Flush at end of encoding
|
|
843
|
+
* for (const packet of encoder.flushPacketsSync()) {
|
|
844
|
+
* console.log('Processing buffered packet');
|
|
845
|
+
* output.writePacketSync(packet);
|
|
846
|
+
* packet.free();
|
|
847
|
+
* }
|
|
848
|
+
* ```
|
|
849
|
+
*
|
|
850
|
+
* @see {@link encodeSync} For sending frames and receiving packets
|
|
851
|
+
* @see {@link flushSync} For signaling end-of-stream
|
|
852
|
+
* @see {@link flushPackets} For async version
|
|
853
|
+
*/
|
|
854
|
+
flushPacketsSync(): Generator<Packet>;
|
|
855
|
+
/**
|
|
856
|
+
* Receive packet from encoder.
|
|
857
|
+
*
|
|
858
|
+
* Gets encoded packets from the codec's internal buffer.
|
|
859
|
+
* Handles packet cloning and error checking.
|
|
860
|
+
* Implements FFmpeg's send/receive pattern.
|
|
861
|
+
*
|
|
862
|
+
* **Return Values:**
|
|
863
|
+
* - `Packet` - Successfully encoded packet (AVERROR >= 0)
|
|
864
|
+
* - `null` - Need more input frames (AVERROR_EAGAIN), or encoder not initialized
|
|
865
|
+
* - `undefined` - End of stream reached (AVERROR_EOF), or encoder is closed
|
|
866
|
+
*
|
|
867
|
+
* Direct mapping to avcodec_receive_packet().
|
|
868
|
+
*
|
|
869
|
+
* @returns Cloned packet, null if need more data, or undefined if stream ended
|
|
870
|
+
*
|
|
871
|
+
* @throws {FFmpegError} If receive fails with error other than AVERROR_EAGAIN or AVERROR_EOF
|
|
872
|
+
*
|
|
873
|
+
* @throws {Error} If packet cloning fails (out of memory)
|
|
874
|
+
*
|
|
875
|
+
* @example
|
|
876
|
+
* ```typescript
|
|
877
|
+
* // Process all buffered packets
|
|
878
|
+
* while (true) {
|
|
879
|
+
* const packet = await encoder.receive();
|
|
880
|
+
* if (!packet) break; // Stop on EAGAIN or EOF
|
|
881
|
+
* console.log(`Got packet with PTS: ${packet.pts}`);
|
|
882
|
+
* await output.writePacket(packet);
|
|
883
|
+
* packet.free();
|
|
884
|
+
* }
|
|
885
|
+
* ```
|
|
886
|
+
*
|
|
887
|
+
* @example
|
|
888
|
+
* ```typescript
|
|
889
|
+
* // Handle each return value explicitly
|
|
890
|
+
* const packet = await encoder.receive();
|
|
891
|
+
* if (packet === EOF) {
|
|
892
|
+
* console.log('Encoder stream ended');
|
|
893
|
+
* } else if (packet === null) {
|
|
894
|
+
* console.log('Need more input frames');
|
|
895
|
+
* } else {
|
|
896
|
+
* console.log(`Got packet: pts=${packet.pts}`);
|
|
897
|
+
* await output.writePacket(packet);
|
|
898
|
+
* packet.free();
|
|
899
|
+
* }
|
|
900
|
+
* ```
|
|
901
|
+
*
|
|
902
|
+
* @see {@link encode} For sending frames and receiving packets
|
|
903
|
+
* @see {@link flush} For signaling end-of-stream
|
|
904
|
+
* @see {@link receiveSync} For synchronous version
|
|
905
|
+
* @see {@link EOF} For end-of-stream signal
|
|
906
|
+
*/
|
|
907
|
+
receive(): Promise<Packet | EOFSignal | null>;
|
|
908
|
+
/**
|
|
909
|
+
* Receive packet from encoder synchronously.
|
|
910
|
+
* Synchronous version of receive.
|
|
911
|
+
*
|
|
912
|
+
* Gets encoded packets from the codec's internal buffer.
|
|
913
|
+
* Handles packet cloning and error checking.
|
|
914
|
+
* Implements FFmpeg's send/receive pattern.
|
|
915
|
+
*
|
|
916
|
+
* **Return Values:**
|
|
917
|
+
* - `Packet` - Successfully encoded packet (AVERROR >= 0)
|
|
918
|
+
* - `null` - Need more input frames (AVERROR_EAGAIN), or encoder not initialized
|
|
919
|
+
* - `undefined` - End of stream reached (AVERROR_EOF), or encoder is closed
|
|
920
|
+
*
|
|
921
|
+
* Direct mapping to avcodec_receive_packet().
|
|
922
|
+
*
|
|
923
|
+
* @returns Cloned packet, null if need more data, or undefined if stream ended
|
|
924
|
+
*
|
|
925
|
+
* @throws {FFmpegError} If receive fails with error other than AVERROR_EAGAIN or AVERROR_EOF
|
|
926
|
+
*
|
|
927
|
+
* @throws {Error} If packet cloning fails (out of memory)
|
|
928
|
+
*
|
|
929
|
+
* @example
|
|
930
|
+
* ```typescript
|
|
931
|
+
* // Process all buffered packets
|
|
932
|
+
* while (true) {
|
|
933
|
+
* const packet = encoder.receiveSync();
|
|
934
|
+
* if (!packet) break; // Stop on EAGAIN or EOF
|
|
935
|
+
* console.log(`Got packet with PTS: ${packet.pts}`);
|
|
936
|
+
* output.writePacketSync(packet);
|
|
937
|
+
* packet.free();
|
|
938
|
+
* }
|
|
939
|
+
* ```
|
|
940
|
+
*
|
|
941
|
+
* @example
|
|
942
|
+
* ```typescript
|
|
943
|
+
* // Handle each return value explicitly
|
|
944
|
+
* const packet = encoder.receiveSync();
|
|
945
|
+
* if (packet === EOF) {
|
|
946
|
+
* console.log('Encoder stream ended');
|
|
947
|
+
* } else if (packet === null) {
|
|
948
|
+
* console.log('Need more input frames');
|
|
949
|
+
* } else {
|
|
950
|
+
* console.log(`Got packet: pts=${packet.pts}`);
|
|
951
|
+
* output.writePacketSync(packet);
|
|
952
|
+
* packet.free();
|
|
953
|
+
* }
|
|
954
|
+
* ```
|
|
955
|
+
*
|
|
956
|
+
* @see {@link encodeSync} For sending frames and receiving packets
|
|
957
|
+
* @see {@link flushSync} For signaling end-of-stream
|
|
958
|
+
* @see {@link receive} For async version
|
|
959
|
+
* @see {@link EOF} For end-of-stream signal
|
|
960
|
+
*/
|
|
961
|
+
receiveSync(): Packet | EOFSignal | null;
|
|
962
|
+
/**
|
|
963
|
+
* Pipe encoded packets to muxer.
|
|
964
|
+
*
|
|
965
|
+
* @param target - Media output component to write packets to
|
|
966
|
+
*
|
|
967
|
+
* @param streamIndex - Stream index to write packets to
|
|
968
|
+
*
|
|
969
|
+
* @returns Scheduler for continued chaining
|
|
970
|
+
*
|
|
971
|
+
* @example
|
|
972
|
+
* ```typescript
|
|
973
|
+
* decoder.pipeTo(filter).pipeTo(encoder)
|
|
974
|
+
* ```
|
|
975
|
+
*/
|
|
976
|
+
pipeTo(target: Muxer, streamIndex: number): SchedulerControl<Frame>;
|
|
977
|
+
/**
|
|
978
|
+
* Close encoder and free resources.
|
|
979
|
+
*
|
|
980
|
+
* Releases codec context and internal packet buffer.
|
|
981
|
+
* Safe to call multiple times.
|
|
982
|
+
* Automatically called by Symbol.dispose.
|
|
983
|
+
*
|
|
984
|
+
* @example
|
|
985
|
+
* ```typescript
|
|
986
|
+
* const encoder = await Encoder.create(FF_ENCODER_LIBX264, { ... });
|
|
987
|
+
* try {
|
|
988
|
+
* // Use encoder
|
|
989
|
+
* } finally {
|
|
990
|
+
* encoder.close();
|
|
991
|
+
* }
|
|
992
|
+
* ```
|
|
993
|
+
*
|
|
994
|
+
* @see {@link Symbol.dispose} For automatic cleanup
|
|
995
|
+
*/
|
|
996
|
+
close(): void;
|
|
997
|
+
/**
|
|
998
|
+
* Get encoder codec.
|
|
999
|
+
*
|
|
1000
|
+
* Returns the codec used by this encoder.
|
|
1001
|
+
* Useful for checking codec capabilities and properties.
|
|
1002
|
+
*
|
|
1003
|
+
* @returns Codec instance
|
|
1004
|
+
*
|
|
1005
|
+
* @internal
|
|
1006
|
+
*
|
|
1007
|
+
* @see {@link Codec} For codec details
|
|
1008
|
+
*/
|
|
1009
|
+
getCodec(): Codec;
|
|
1010
|
+
/**
|
|
1011
|
+
* Get underlying codec context.
|
|
1012
|
+
*
|
|
1013
|
+
* Returns the codec context for advanced operations.
|
|
1014
|
+
* Useful for accessing low-level codec properties and settings.
|
|
1015
|
+
* Returns null if encoder is closed or not initialized.
|
|
1016
|
+
*
|
|
1017
|
+
* @returns Codec context or null if closed/not initialized
|
|
1018
|
+
*
|
|
1019
|
+
* @internal
|
|
1020
|
+
*
|
|
1021
|
+
* @see {@link CodecContext} For context details
|
|
1022
|
+
*/
|
|
1023
|
+
getCodecContext(): CodecContext | null;
|
|
1024
|
+
/**
|
|
1025
|
+
* Worker loop for push-based processing.
|
|
1026
|
+
*
|
|
1027
|
+
* @internal
|
|
1028
|
+
*/
|
|
1029
|
+
private runWorker;
|
|
1030
|
+
/**
|
|
1031
|
+
* Send frame to input queue or flush the pipeline.
|
|
1032
|
+
*
|
|
1033
|
+
* When frame is provided, queues it for encoding.
|
|
1034
|
+
* When null is provided, triggers flush sequence:
|
|
1035
|
+
* - Closes input queue
|
|
1036
|
+
* - Waits for worker completion
|
|
1037
|
+
* - Flushes encoder and sends remaining packets to output queue
|
|
1038
|
+
* - Closes output queue
|
|
1039
|
+
* - Waits for pipeTo task completion (writes to muxer)
|
|
1040
|
+
*
|
|
1041
|
+
* Used by scheduler system for pipeline control.
|
|
1042
|
+
*
|
|
1043
|
+
* @param frame - Frame to send, or null to flush
|
|
1044
|
+
*
|
|
1045
|
+
* @internal
|
|
1046
|
+
*/
|
|
1047
|
+
private sendToQueue;
|
|
1048
|
+
/**
|
|
1049
|
+
* Receive packet from output queue.
|
|
1050
|
+
*
|
|
1051
|
+
* @returns Packet from output queue
|
|
1052
|
+
*
|
|
1053
|
+
* @internal
|
|
1054
|
+
*/
|
|
1055
|
+
private receiveFromQueue;
|
|
1056
|
+
/**
|
|
1057
|
+
* Initialize encoder from first frame.
|
|
1058
|
+
*
|
|
1059
|
+
* Sets codec context parameters from frame properties.
|
|
1060
|
+
* Configures hardware context if present in frame.
|
|
1061
|
+
* Opens encoder with accumulated options.
|
|
1062
|
+
*
|
|
1063
|
+
* @param frame - First frame to encode
|
|
1064
|
+
*
|
|
1065
|
+
* @throws {FFmpegError} If encoder open fails
|
|
1066
|
+
*
|
|
1067
|
+
* @internal
|
|
1068
|
+
*/
|
|
1069
|
+
private initialize;
|
|
1070
|
+
/**
|
|
1071
|
+
* Initialize encoder from first frame synchronously.
|
|
1072
|
+
* Synchronous version of initialize.
|
|
1073
|
+
*
|
|
1074
|
+
* Sets codec context parameters from frame properties.
|
|
1075
|
+
* Configures hardware context if present in frame.
|
|
1076
|
+
* Opens encoder with accumulated options.
|
|
1077
|
+
*
|
|
1078
|
+
* @param frame - First frame to encode
|
|
1079
|
+
*
|
|
1080
|
+
* @throws {FFmpegError} If encoder open fails
|
|
1081
|
+
*
|
|
1082
|
+
* @internal
|
|
1083
|
+
*
|
|
1084
|
+
* @see {@link initialize} For async version
|
|
1085
|
+
*/
|
|
1086
|
+
private initializeSync;
|
|
1087
|
+
/**
|
|
1088
|
+
* Setup hardware acceleration for encoder.
|
|
1089
|
+
*
|
|
1090
|
+
* Implements FFmpeg's hw_device_setup_for_encode logic.
|
|
1091
|
+
* Validates hardware frames context format and codec support.
|
|
1092
|
+
* Falls back to device context if frames context is incompatible.
|
|
1093
|
+
*
|
|
1094
|
+
* @param frame - Frame to get hardware context from
|
|
1095
|
+
*
|
|
1096
|
+
* @internal
|
|
1097
|
+
*/
|
|
1098
|
+
private setupHardwareAcceleration;
|
|
1099
|
+
/**
|
|
1100
|
+
* Prepare frame for encoding.
|
|
1101
|
+
*
|
|
1102
|
+
* Implements FFmpeg's frame_encode() pre-encoding logic:
|
|
1103
|
+
* 1. Video: Sets frame.quality from encoder's globalQuality (like -qscale)
|
|
1104
|
+
* 2. Audio: Validates channel count consistency for encoders without PARAM_CHANGE capability
|
|
1105
|
+
*
|
|
1106
|
+
* This matches FFmpeg CLI behavior where these properties are automatically managed.
|
|
1107
|
+
*
|
|
1108
|
+
* @param frame - Frame to prepare for encoding
|
|
1109
|
+
*
|
|
1110
|
+
* @throws {Error} If audio channel count changed and encoder doesn't support parameter changes
|
|
1111
|
+
*
|
|
1112
|
+
* @internal
|
|
1113
|
+
*/
|
|
1114
|
+
private prepareFrameForEncoding;
|
|
1115
|
+
/**
|
|
1116
|
+
* Dispose of encoder.
|
|
1117
|
+
*
|
|
1118
|
+
* Implements Disposable interface for automatic cleanup.
|
|
1119
|
+
* Equivalent to calling close().
|
|
1120
|
+
*
|
|
1121
|
+
* @example
|
|
1122
|
+
* ```typescript
|
|
1123
|
+
* {
|
|
1124
|
+
* using encoder = await Encoder.create(FF_ENCODER_LIBX264, { ... });
|
|
1125
|
+
* // Encode frames...
|
|
1126
|
+
* } // Automatically closed
|
|
1127
|
+
* ```
|
|
1128
|
+
*
|
|
1129
|
+
* @see {@link close} For manual cleanup
|
|
1130
|
+
*/
|
|
1131
|
+
[Symbol.dispose](): void;
|
|
1132
|
+
}
|