0
# FLV Processing
1
2
FLV (Flash Video) container manipulation including tag processing, header generation, and transmuxing capabilities for legacy Flash video support and conversion workflows.
3
4
## Capabilities
5
6
### FLV Transmuxer
7
8
Main transmuxer class for processing and converting FLV format data.
9
10
```javascript { .api }
11
/**
12
* FLV format transmuxer
13
*/
14
class Transmuxer extends Stream {
15
constructor();
16
17
/** Process FLV data */
18
push(data: Uint8Array): void;
19
20
/** Complete processing and output final data */
21
flush(): void;
22
23
/** Reset transmuxer state */
24
reset(): void;
25
}
26
```
27
28
**Usage Example:**
29
30
```javascript
31
const muxjs = require("mux.js");
32
33
const flvTransmuxer = new muxjs.flv.Transmuxer();
34
35
flvTransmuxer.on('data', function(output) {
36
// Process converted FLV data
37
console.log('FLV output:', output);
38
});
39
40
// Process FLV input data
41
flvTransmuxer.push(flvBytes);
42
flvTransmuxer.flush();
43
```
44
45
### FLV Header Generation
46
47
Utility for creating FLV file headers with proper metadata and stream information.
48
49
```javascript { .api }
50
/**
51
* Generate FLV file header
52
* @param duration - Video duration in seconds (optional)
53
* @param audio - Whether file contains audio streams
54
* @param video - Whether file contains video streams
55
* @returns FLV header as Uint8Array
56
*/
57
function getFlvHeader(
58
duration?: number,
59
audio?: boolean,
60
video?: boolean
61
): Uint8Array;
62
```
63
64
**Usage Example:**
65
66
```javascript
67
const muxjs = require("mux.js");
68
69
// Create FLV header for audio+video file
70
const header = muxjs.flv.getFlvHeader(120.5, true, true);
71
console.log('FLV header size:', header.byteLength);
72
73
// Create header for audio-only file
74
const audioHeader = muxjs.flv.getFlvHeader(undefined, true, false);
75
76
// Create header with known duration
77
const headerWithDuration = muxjs.flv.getFlvHeader(300, true, true);
78
```
79
80
### FLV Tag Processing
81
82
Low-level FLV tag manipulation for reading, writing, and modifying FLV tag structures.
83
84
```javascript { .api }
85
/**
86
* FLV tag manipulation class
87
*/
88
class FlvTag {
89
constructor();
90
91
/** Read tag data from buffer */
92
read(buffer: Uint8Array, offset: number): TagData;
93
94
/** Write tag data to buffer */
95
write(tagData: TagData): Uint8Array;
96
97
/** Parse tag header information */
98
parseHeader(buffer: Uint8Array, offset: number): TagHeader;
99
100
/** Create metadata tag */
101
createMetadataTag(metadata: MetadataObject): Uint8Array;
102
103
/** Create audio tag */
104
createAudioTag(audioData: AudioTagData): Uint8Array;
105
106
/** Create video tag */
107
createVideoTag(videoData: VideoTagData): Uint8Array;
108
}
109
110
interface TagData {
111
type: 'audio' | 'video' | 'script';
112
timestamp: number;
113
streamId: number;
114
data: Uint8Array;
115
}
116
117
interface TagHeader {
118
type: number;
119
dataSize: number;
120
timestamp: number;
121
timestampExtended: number;
122
streamId: number;
123
}
124
125
interface MetadataObject {
126
[key: string]: any;
127
duration?: number;
128
width?: number;
129
height?: number;
130
videodatarate?: number;
131
audiodatarate?: number;
132
framerate?: number;
133
}
134
135
interface AudioTagData {
136
soundFormat: number;
137
soundRate: number;
138
soundSize: number;
139
soundType: number;
140
data: Uint8Array;
141
timestamp: number;
142
}
143
144
interface VideoTagData {
145
frameType: number;
146
codecId: number;
147
data: Uint8Array;
148
timestamp: number;
149
compositionTime?: number;
150
}
151
```
152
153
## Advanced Usage
154
155
### FLV File Construction
156
157
```javascript
158
const muxjs = require("mux.js");
159
160
// Create complete FLV file
161
const flvTag = new muxjs.flv.tag();
162
163
// Create FLV header
164
const header = muxjs.flv.getFlvHeader(120, true, true);
165
166
// Create metadata tag
167
const metadata = {
168
duration: 120,
169
width: 1920,
170
height: 1080,
171
framerate: 30,
172
videodatarate: 2000,
173
audiodatarate: 128
174
};
175
const metadataTag = flvTag.createMetadataTag(metadata);
176
177
// Create audio tag
178
const audioTag = flvTag.createAudioTag({
179
soundFormat: 10, // AAC
180
soundRate: 3, // 44.1kHz
181
soundSize: 1, // 16-bit
182
soundType: 1, // Stereo
183
data: audioData,
184
timestamp: 0
185
});
186
187
// Create video tag
188
const videoTag = flvTag.createVideoTag({
189
frameType: 1, // Keyframe
190
codecId: 7, // AVC/H.264
191
data: videoData,
192
timestamp: 0,
193
compositionTime: 0
194
});
195
196
console.log('FLV file constructed with header and tags');
197
```
198
199
### FLV Tag Stream Processing
200
201
```javascript
202
const muxjs = require("mux.js");
203
204
const flvTransmuxer = new muxjs.flv.Transmuxer();
205
const flvTag = new muxjs.flv.tag();
206
207
// Handle different tag types during processing
208
flvTransmuxer.on('data', function(output) {
209
if (output.tags) {
210
output.tags.forEach(tag => {
211
const tagData = flvTag.read(tag.data, 0);
212
213
switch (tagData.type) {
214
case 'audio':
215
console.log('Audio tag - timestamp:', tagData.timestamp);
216
break;
217
case 'video':
218
console.log('Video tag - timestamp:', tagData.timestamp);
219
break;
220
case 'script':
221
console.log('Script/metadata tag');
222
break;
223
}
224
});
225
}
226
});
227
228
// Process FLV stream
229
flvTransmuxer.push(flvStreamData);
230
flvTransmuxer.flush();
231
```
232
233
### Metadata Handling
234
235
```javascript
236
const muxjs = require("mux.js");
237
const flvTag = new muxjs.flv.tag();
238
239
// Extract metadata from existing FLV
240
function extractMetadata(flvData) {
241
let offset = 13; // Skip FLV header
242
243
while (offset < flvData.length) {
244
const header = flvTag.parseHeader(flvData, offset);
245
246
if (header.type === 18) { // Script data tag
247
const tagData = flvTag.read(flvData, offset);
248
console.log('Found metadata:', tagData);
249
return tagData;
250
}
251
252
offset += header.dataSize + 15; // Tag size + header size
253
}
254
255
return null;
256
}
257
258
// Create custom metadata
259
function createCustomMetadata(videoInfo) {
260
const metadata = {
261
duration: videoInfo.duration,
262
width: videoInfo.width,
263
height: videoInfo.height,
264
framerate: videoInfo.fps,
265
videodatarate: videoInfo.bitrate / 1000,
266
createdby: 'mux.js',
267
date: new Date().toISOString()
268
};
269
270
return flvTag.createMetadataTag(metadata);
271
}
272
```
273
274
## FLV Format Constants
275
276
### Audio Format Codes
277
278
```javascript { .api }
279
const AUDIO_FORMATS = {
280
LINEAR_PCM_PLATFORM_ENDIAN: 0,
281
ADPCM: 1,
282
MP3: 2,
283
LINEAR_PCM_LITTLE_ENDIAN: 3,
284
NELLYMOSER_16KHZ_MONO: 4,
285
NELLYMOSER_8KHZ_MONO: 5,
286
NELLYMOSER: 6,
287
G711_A_LAW_PCM: 7,
288
G711_MU_LAW_PCM: 8,
289
AAC: 10,
290
SPEEX: 11,
291
MP3_8KHZ: 14,
292
DEVICE_SPECIFIC: 15
293
};
294
```
295
296
### Video Codec IDs
297
298
```javascript { .api }
299
const VIDEO_CODECS = {
300
SORENSON_H263: 2,
301
SCREEN_VIDEO: 3,
302
ON2_VP6: 4,
303
ON2_VP6_ALPHA: 5,
304
SCREEN_VIDEO_V2: 6,
305
AVC: 7 // H.264
306
};
307
```
308
309
### Frame Types
310
311
```javascript { .api }
312
const FRAME_TYPES = {
313
KEYFRAME: 1,
314
INTER_FRAME: 2,
315
DISPOSABLE_INTER_FRAME: 3,
316
GENERATED_KEYFRAME: 4,
317
VIDEO_INFO_FRAME: 5
318
};
319
```
320
321
## Error Handling
322
323
FLV processing streams emit standard events for error handling:
324
325
```javascript
326
const flvTransmuxer = new muxjs.flv.Transmuxer();
327
328
flvTransmuxer.on('error', function(error) {
329
console.error('FLV processing error:', error.message);
330
// Common errors: invalid FLV format, unsupported codecs, corrupted tags
331
});
332
333
flvTransmuxer.on('warning', function(warning) {
334
console.warn('FLV processing warning:', warning.message);
335
// Common warnings: missing metadata, timestamp discontinuities
336
});
337
```