0
# FLV Processing
1
2
Flash Video format support for legacy applications. The FLV module provides FLV transmuxing and inspection capabilities. Note that this module is in maintenance mode and will not receive further major development.
3
4
## Capabilities
5
6
### FLV Transmuxer
7
8
Transmuxer class for converting MPEG-2 transport streams to FLV format. Primarily used for legacy Flash-based video players.
9
10
```javascript { .api }
11
/**
12
* FLV Transmuxer for converting transport streams to FLV format
13
* Note: This module is in maintenance mode
14
*/
15
class Transmuxer {
16
constructor();
17
18
/** Add transport stream data for processing */
19
push(data: Uint8Array): void;
20
21
/** Flush any remaining data and finalize processing */
22
flush(): void;
23
24
/** Reset transmuxer state */
25
reset(): void;
26
27
/** Register event listeners for processed FLV segments */
28
on(event: 'data', callback: (flvData: FlvSegment) => void): void;
29
on(event: 'done', callback: () => void): void;
30
}
31
32
interface FlvSegment {
33
data: Uint8Array;
34
tags: FlvTag[];
35
}
36
```
37
38
**Usage Examples:**
39
40
```javascript
41
const muxjs = require("mux.js");
42
43
// Create FLV transmuxer
44
const flvTransmuxer = new muxjs.flv.Transmuxer();
45
46
// Handle processed FLV data
47
flvTransmuxer.on('data', (flvData) => {
48
console.log(`FLV segment: ${flvData.data.length} bytes`);
49
console.log(`FLV tags: ${flvData.tags.length}`);
50
51
// Process FLV tags
52
flvData.tags.forEach(tag => {
53
console.log(`Tag type: ${tag.tagType}, size: ${tag.dataSize}`);
54
});
55
56
// Use FLV data (e.g., send to Flash player)
57
processFlvData(flvData.data);
58
});
59
60
// Process transport stream data
61
flvTransmuxer.push(transportStreamData);
62
flvTransmuxer.flush();
63
```
64
65
### FLV Tag Processing
66
67
Class for representing and processing individual FLV tags within FLV files.
68
69
```javascript { .api }
70
/**
71
* FLV Tag representation and processing
72
* Handles audio, video, and script data tags
73
*/
74
class FlvTag {
75
constructor();
76
77
/** Tag type (audio, video, script) */
78
tagType: number;
79
80
/** Tag data size */
81
dataSize: number;
82
83
/** Timestamp for the tag */
84
timestamp: number;
85
86
/** Tag data payload */
87
data: Uint8Array;
88
89
/** Parse tag from binary data */
90
static parse(data: Uint8Array): FlvTag;
91
92
/** Serialize tag to binary data */
93
serialize(): Uint8Array;
94
}
95
96
// FLV tag type constants
97
const FLV_TAG_TYPES = {
98
AUDIO: 8,
99
VIDEO: 9,
100
SCRIPT_DATA: 18
101
};
102
```
103
104
**Usage Examples:**
105
106
```javascript
107
const muxjs = require("mux.js");
108
109
// Parse FLV tags from data
110
const flvData = new Uint8Array([/* FLV file data */]);
111
let offset = 13; // Skip FLV header
112
113
while (offset < flvData.length) {
114
const tag = muxjs.flv.tag.parse(flvData.subarray(offset));
115
console.log(`FLV Tag: type ${tag.tagType}, size ${tag.dataSize}`);
116
117
switch (tag.tagType) {
118
case 8: // Audio
119
console.log(`Audio tag at ${tag.timestamp}ms`);
120
break;
121
case 9: // Video
122
console.log(`Video tag at ${tag.timestamp}ms`);
123
break;
124
case 18: // Script data
125
console.log(`Script data at ${tag.timestamp}ms`);
126
break;
127
}
128
129
// Move to next tag
130
offset += tag.dataSize + 11; // Tag header (11 bytes) + data + previous tag size (4 bytes)
131
}
132
```
133
134
### FLV Header Generation
135
136
Function for generating FLV file headers with appropriate format specifications.
137
138
```javascript { .api }
139
/**
140
* Generate FLV file header
141
* @returns FLV header bytes including signature and format flags
142
*/
143
function getFlvHeader(): Uint8Array;
144
```
145
146
**Usage Examples:**
147
148
```javascript
149
const muxjs = require("mux.js");
150
151
// Generate FLV header
152
const flvHeader = muxjs.flv.getFlvHeader();
153
console.log(`FLV header: ${flvHeader.length} bytes`);
154
155
// Typical FLV header structure:
156
// - FLV signature (3 bytes): 'F', 'L', 'V'
157
// - Version (1 byte): 0x01
158
// - Flags (1 byte): audio/video presence flags
159
// - Header size (4 bytes): typically 9
160
// - Previous tag size (4 bytes): 0 for first tag
161
162
// Use header to start FLV file
163
const flvFile = new Uint8Array(flvHeader.length + flvData.length);
164
flvFile.set(flvHeader, 0);
165
flvFile.set(flvData, flvHeader.length);
166
```
167
168
### FLV Processing Pipeline
169
170
Complete pipeline for transport stream to FLV conversion.
171
172
```javascript
173
const muxjs = require("mux.js");
174
175
// Create FLV processing pipeline
176
const packetStream = new muxjs.mp2t.TransportPacketStream();
177
const parseStream = new muxjs.mp2t.TransportParseStream();
178
const elementaryStream = new muxjs.mp2t.ElementaryStream();
179
const flvTransmuxer = new muxjs.flv.Transmuxer();
180
181
// Connect transport stream processing
182
packetStream
183
.pipe(parseStream)
184
.pipe(elementaryStream);
185
186
// Route elementary stream data to FLV transmuxer
187
elementaryStream.on('data', (pesPacket) => {
188
if (pesPacket.type === 'video' || pesPacket.type === 'audio') {
189
flvTransmuxer.push(pesPacket.data);
190
}
191
});
192
193
// Handle FLV output
194
flvTransmuxer.on('data', (flvSegment) => {
195
// Create complete FLV file
196
const header = muxjs.flv.getFlvHeader();
197
const complete = new Uint8Array(header.length + flvSegment.data.length);
198
complete.set(header, 0);
199
complete.set(flvSegment.data, header.length);
200
201
// Use FLV data
202
processFlvFile(complete);
203
});
204
205
// Process original transport stream
206
packetStream.push(transportStreamData);
207
packetStream.flush();
208
flvTransmuxer.flush();
209
```
210
211
## FLV Inspection Tools
212
213
Debugging utilities for FLV file inspection and analysis.
214
215
```javascript { .api }
216
/**
217
* FLV inspection and debugging tools
218
*/
219
const tools: {
220
/** Inspect individual FLV tag structure */
221
inspectTag(data: Uint8Array): FlvTagInfo;
222
223
/** Inspect complete FLV file structure */
224
inspect(data: Uint8Array): FlvStructure;
225
226
/** Convert FLV structure to human-readable text */
227
textify(structure: FlvStructure): string;
228
};
229
230
interface FlvTagInfo {
231
tagType: number;
232
dataSize: number;
233
timestamp: number;
234
streamId: number;
235
data: Uint8Array;
236
}
237
238
interface FlvStructure {
239
header: FlvHeader;
240
tags: FlvTagInfo[];
241
totalSize: number;
242
}
243
244
interface FlvHeader {
245
signature: string;
246
version: number;
247
flags: {
248
audio: boolean;
249
video: boolean;
250
};
251
headerSize: number;
252
}
253
```
254
255
**Usage Examples:**
256
257
```javascript
258
const muxjs = require("mux.js");
259
260
// Inspect FLV file
261
const flvData = new Uint8Array([/* FLV file data */]);
262
const flvStructure = muxjs.flv.tools.inspect(flvData);
263
264
console.log('FLV Analysis:');
265
console.log(`Version: ${flvStructure.header.version}`);
266
console.log(`Has audio: ${flvStructure.header.flags.audio}`);
267
console.log(`Has video: ${flvStructure.header.flags.video}`);
268
console.log(`Total tags: ${flvStructure.tags.length}`);
269
270
// Analyze tags
271
let audioTags = 0;
272
let videoTags = 0;
273
let scriptTags = 0;
274
275
flvStructure.tags.forEach(tag => {
276
switch (tag.tagType) {
277
case 8: audioTags++; break;
278
case 9: videoTags++; break;
279
case 18: scriptTags++; break;
280
}
281
});
282
283
console.log(`Audio tags: ${audioTags}`);
284
console.log(`Video tags: ${videoTags}`);
285
console.log(`Script tags: ${scriptTags}`);
286
287
// Get human-readable representation
288
const textOutput = muxjs.flv.tools.textify(flvStructure);
289
console.log('FLV Structure:');
290
console.log(textOutput);
291
292
// Inspect individual tag
293
const firstTag = flvStructure.tags[0];
294
if (firstTag) {
295
const tagInfo = muxjs.flv.tools.inspectTag(firstTag.data);
296
console.log('First tag details:', tagInfo);
297
}
298
```
299
300
## Legacy Considerations
301
302
Since the FLV module is in maintenance mode, consider these points:
303
304
### Migration to MP4
305
306
For new applications, use MP4 transmuxing instead:
307
308
```javascript
309
// Instead of FLV transmuxing
310
const flvTransmuxer = new muxjs.flv.Transmuxer();
311
312
// Use MP4 transmuxing for better compatibility
313
const mp4Transmuxer = new muxjs.mp4.Transmuxer();
314
```
315
316
### Limited Feature Updates
317
318
The FLV module will not receive new features. For advanced video processing needs, use the MP4 module which provides:
319
320
- Better browser compatibility
321
- MSE integration
322
- Active development and updates
323
- Modern codec support
324
325
### Maintenance Mode Usage
326
327
The FLV module should only be used when:
328
329
- Supporting legacy Flash applications
330
- Working with existing FLV-based infrastructure
331
- Converting existing FLV processing to MP4
332
333
## Types
334
335
```javascript { .api }
336
interface FlvSegment {
337
data: Uint8Array;
338
tags: FlvTag[];
339
}
340
341
interface FlvTag {
342
tagType: number;
343
dataSize: number;
344
timestamp: number;
345
streamId: number;
346
data: Uint8Array;
347
}
348
349
interface FlvHeader {
350
signature: string;
351
version: number;
352
flags: {
353
audio: boolean;
354
video: boolean;
355
};
356
headerSize: number;
357
}
358
359
interface FlvStructure {
360
header: FlvHeader;
361
tags: FlvTagInfo[];
362
totalSize: number;
363
}
364
365
interface FlvTagInfo {
366
tagType: number;
367
dataSize: number;
368
timestamp: number;
369
streamId: number;
370
data: Uint8Array;
371
}
372
```